Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Sunday, 13 November 2016

How to print in Python without newline or space?

code:

row = int(raw_input("Enter a number :"))
for i in range (row):
    print i,


Know how to run python code 


Output: 

$ python print_pattern1.py 
Enter a number :5

0 1 2 3 4

Find an input number is odd or even using Python. Simple program

This program will find an input number is odd or even using Python.

code:

x = int(raw_input("Enter a number: "))
if (x%2 == 0):
        print "%d is Even" % x
else:
        print "%d is Odd" % x


Know how to run python code 

Output: 

$ python odd_even.py
Enter a number: 5
5 is Odd

$ python odd_even.py
Enter a number: 54
54 is Odd


How to run python code?

For running a python code you must have python installed in your system.

After that use these following:

1. open a file using "vi" if you are working in linux terminal or "start" in windows followed by filename (eg. hello_world.py).

2. Write your code in that file. NOTE: do not forget the tabs/space for indentation, it's important for a script.

3. close that file.

4. python hello_world.py


That's it. Done.

Sunday, 26 June 2016

Input and output in different data types in Python

There are mainly three data types, namely:
int
float
string


Let's see an example of all of them in this small program:

a = raw_input("Enter a: ")                   # Taking input for first num in string form
b = raw_input("Enter b: ")                   # Taking input for second num string form
print "a + b as strings: %s" % a  + b   # + will concate and will show in string
a = int(a)                                              # Converting in to integer
b = int(b)                                              # Converting in to integer
c = a + b                                              # Adding two integer
print "a + b as integers: %d" % c         # Printing  two integer
a = float(a)                                           # Converting in to float
b = float(b)                                           # Converting in to float
d = a/b                                                  # deriding
print "a / b as float : %f" % d               # printing as float







Output:
Enter a: 100
Enter b: 35
a + b as strings: 10035
a + b as integers: 135
a / b as float : 2.857143

Take interger as input in Python / Arithmatic operation in Python

#!/usr/bin/python

num1 = int(raw_input('Enter a num:'))
num2 = int(raw_input('Enter second num:'))

add = num1 + num2
sub = num1 - num2
mul = num1 * num2
div = num1 / num2

print(add)
print(sub)
print(mul)
print(div)


Output:
$ python ./input.py
Enter a num:10
Enter second num:2
12
8
20
5

$ python ./input.py
Enter a num:2  
Enter second num:10
12
-8
20
0