Python Programs
PYTHON PROGRAM
Aim:
To write each and every given python program and execute it.
Program 1:
#!/usr/bin/python
#program to select odd number from the list
a=[11,12,13,14,15,16,17,18,19,20,21,31,44,45,10];
print("List is:",a);
n=len(a);
print("length:",n);
i=0;
print("Odd number");
for i in range(len(a)):
if(a[i]%2==1):
print(a[i]);
Output:
[root@fosslab html]# python odd.py
('List is:', [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 31, 44, 45, 10])
('length:', 15)
Odd number
11 13 15 17 19 21 31 45
Program 2: Program to display the celsius value\
#!/usr/bin/python
a=input("enter the celsius value:")
f=(a*1.8)
b=f+32;
print b
Output:
[fosslab@fosslab ~]$ python celsius.py
enter the celsius value:23
73.4
Program 3: Program to display the fibonacciseries values
#!/usr/bin/python
a, b = 0, 1
while b < 200:
print b,
a, b = b, a+b
Output:
[fosslab@fosslab ~]$ python fibanoo.py
1 1 2 3 5 8 13 21 34 55 89 144
Program 4: Program to display odd or even numbers.
#!/usr/bin/python
a = int(raw_input("Please enter an integer: "));
if (a % 2 == 1):
print 'a' + ' is odd.'
else:
print 'a' + ' is even.'
Output:
[fosslab@fosslab ~]$ python odd.py
Please enter an integer: 23
a is odd.
Program 5: String Concatenation in python programming
#String concatenation
worda='computer';
wordb='science';
print("worda is ",worda);
print("wordb is",wordb);
wordc=worda+" " +wordb;
print("wordc is",wordc);
wordd=worda*3;
print("wordd is ",wordd);
str = 'HelloWorld!'
length=len(str);
print ("str :",str);
print("length:",length);
print ("first character is",str[0]);
print ("print character from 2rd to 6th :", str[2:7] );
print ("Prints string starting from 3rd character:",str[2:]);
print ("Prints string two times",str * 2);
print ("Prints concatenated string :",str + "TEST" );
print(str[-1]); #print last character
print(str[-6]);#print character from last 6th position
print(str[:-2]);# Everything except the last two characters
OUTPUT:
(‘word a is’,’computer’)
(‘word b is’,’science’)
(‘word c is’,’computer science’)
(‘word d is’,’computer computer computer’)
(‘str:’,’HelloWord!’)
(‘length:’,11)
(‘first character is’,’H’)
(‘Print character from 2rd to 6th :’,’llowo’)
(‘Prints string starting from 3rd character:’,’lloWord!’)
(‘Prints string two times’,’HelloWorld! HelloWorld!’)
(‘Prints concatenated string :’,’HelloWorld! TEST’ )
!
W
HelloWord
>>>
Program 6: Write a python program to perform function in Lists
#Python Lists
#!/usr/bin/python
print("\t \t \t Python Lists");
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']
print("Prints complete list:",list);
print("Prints first element of the list : ",list[0]);
print("Prints elements starting from 2nd to 4th:",list[1:3]);
print("Prints elements starting from 3rd element:",list[2:]);
print("Prints list two times:",tinylist * 2);
print("Prints concatenated lists: ", list + tinylist );
#modify the 4th elements in the list
print("Before modifying the 4th element in list :",list[4]);
list[4]='efgh';
print("4th element in list :",list[4]);
print(" complete list:",list);
#Appending new elements
list.append('ijkl');
print("After appending list:",list);
#deleting an element in list
del list[2];
print("List :",list);
OUTPUT:
(‘Prints complete list:’,[‘abcd’,786,2.23,’john’,70.2])
(‘Prints first element of the list : ‘,’abcd’)
(‘Prints elements starting from 2nd to 4th:’,[786,2.23])
(‘Prints elements starting from 3rd element:’,[2.23,’john’,70.2])
(‘Prints list two times:’,[123,’john’,123,’john’])
(‘Prints concatenated lists: ‘,[‘abcd’,786,2.23,’john’,70.2,123,’john’])
(‘Before modifying the 4th element in list:’,70.2)
(‘4th element is list:’,’efgh’)
(‘complete list:’,[‘abcd’,786.2.23,’john’,’efgh’])
(‘After appending list:’[‘abcd’,786,2.23,’john’,’efgh’,’ijkl’])
(‘List:’[‘abcd’,786,2.23,’john’,’efgh’,’ijkl’])
Program 7: Write a python Program to select odd number from the lists
#!/usr/bin/python
#program to select odd number from the list
a=[11,12,13,14,15,16,17,18,19,20,21,31,44,45,10];
print("List is:",a);
n=len(a);
print("length:",n);
i=0;
print("Odd number");
for i in range(len(a)):
if(a[i]%2==1):
print(a[i]);
OUTPUT:
11
13
15
17
19
21
31
45
Program 8: For Statement in Python
>>> # Measure some strings:
... a = ['cat', 'window', 'defenestrate']
>>> for x in a:
... print x, len(x)
cat 3
window 6
OUTPUT
cat
window
defenestrate
Program 9: The Range() and Len() in Python
>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for i in range(len(a)):
... print i, a[i]
...
OUTPUT:
0 Mary
1 had
2 a
3 little
4 lamb
Program 10: Prime Number using Python.
>>> for n in range(2, 10):
... for x in range(2, n):
... if n % x == 0:
... print n, 'equals', x, '*', n/x
... break
... else:
... # loop fell through without finding a factor
... print n, 'is a prime number'
OUTPUT:
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3