Importat Python Programs



PYTHON PROGRAMS




1. Write a Hello World Python Program


#!/usr/bin/python


print "Hello World!";




2. 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




3. 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);



4. Write a python program to perform functions in tuples


#Python Tuples


print("\t \t \t Python tuples");
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2  ) tinytuple = (123, 'john') print("\ncomplete tuple :",tuple);


print("Prints first element of the tuple:", tuple[0]); print("Prints elements starting from 1nd to 3th:", tuple[1:4]); print("Prints elements starting from 3rd element:", tuple[2:]); print("Prints tuple two times:", tinytuple * 2    );
print ("concatenated tuple:",tuple + tinytuple );




5. Write a python program to perform functions in Dictionary.


#Python Dictionary
tinydict = {'name': 'john','code':6734, 'dept': 'sales'}
print ("Python Dictionary:" );
print ("complete dictionary",tinydict );
print ("Key :",tinydict.keys()); # Prints all the keys print ("values:",tinydict.values()); # Prints all the values



6. Write a python Program to select odd number from the lists


#!/usr/bin/python
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]);


7. Conditional statement in Python


>>> x = int(raw_input("Please enter an integer: "))
>>> if x < 0:
... x = 0
... print 'Negative changed to zero'
... elif x == 0:
... print 'Zero'
... elif x == 1:
... print 'Single'
... else:
... print 'More'


8. For Statement in Python


>>> # Measure some strings:
... a = ['cat', 'window', 'defenestrate']
>>> for x in a:
... print x, len(x)


cat 3
window 6


9. The Range() and Len() in Python


>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for i in range(len(a)):
... print i, a[i]
...



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'
..




11. Simple Class Program in python


>>> class Complex:
... def    init   (self, realpart, imagpart):
... self.r = realpart
... self.i = imagpart
...
>>> x = Complex(3.0, -4.5)
>>> x.r, x.i
(3.0, -4.5)




12. Module Program in Python (Fibonacci Series)


A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a
module, the modules name (as a string) is available as the value of the global variable    name   . For instance, use your favorite text editor to create a file called fibo.py in the current directory with the following contents:
Step  1:
Fibo.py
def fib(n):   # write Fibonacci series up to n a, b = 0, 1
while b < n:
print b,
a, b = b, a+b


Step 2:
>>> import fibo


Step 3:
>>> fibo.fib(1000)


simple python program | python help | execute python program from windows cmd | any python program | self terminate a python program | python builder | checking python version | python program download | python programming | python program example | python tutorial | python script | Python Programming for Beginners | Linux

0 comments:

Post a Comment