Sie sind auf Seite 1von 5

Python Program Manual

1. Write python program to print Hello World


Type the following source code in a test.py file:

print("Hello, Python!")

Now save the file and run the code as

$ python test.py

This produces the following result:

Hello, Python!

2. Write python program to store data in list and then try to print them.
#!/usr/bin/python

list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]


tinylist = [123, 'john']

print (list) # Prints complete list


print (list[0]) # Prints first element of the list
print (list[1:3]) # Prints elements starting from 2nd till 3rd
print (list[2:]) # Prints elements starting from 3rd element
print (tinylist * 2) # Prints list two times
print (list + tinylist) # Prints concatenated lists

This produce the following result −

['abcd', 786, 2.23, 'john', 70.200000000000003]


abcd
[786, 2.23]
[2.23, 'john', 70.200000000000003]
[123, 'john', 123, 'john']
['abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john']
3. Write python program to print list of numbers using range and different type of loops
Create list

>>> places= ['srinagar','kashmir','jammu']

>>> print("name of places", places)

Output:

name of places ['srinagar', 'kashmir', 'jammu']

Now print this list using while loop:

>>> count = 0
>>> while (count < len(places)):
print(places[count])
count = count+1
Output:
srinagar
kashmir
jammu

Now print using simple for loop:


>>> for i in places:
print(i)
Output:
srinagar
kashmir
jammu

Now print using range:


>>> for i in range(len(places)):
print(places[i])

Output:
srinagar
kashmir
jammu

4. Write python program to let user enter some data in string and then verify data and
print welcome to user.
>>>str1=input("Enter string:")
str2='Hello'
if str1==str2:
print("Welcome")
else:
print("Not allowed")

If user enters input: ‘Hello’, output:


Welcome
If user enters any other string say ‘Hi’, output:
Not allowed

5. Write python program in which a function is defined and calling that function prints
Hello World.
Function definition is here
>>> def printme():
print ('Hello World')

printme() # Now you can call printme function

6. Write a python program to find the sum of numbers in a list.


>>>numbers=[2,3,4,5,6]
sum=0
for i in numbers:
sum= sum + i
print(sum)
Output:
20

7. Write a python program to find the maximum number in a list.


>>>numbers=[2,3,4,5,6]
max=numbers[0]
for i in numbers:
if i > max:
max = i
print(max)
Output:
6

8. Write a python program in which a function is defined which checks whether a number
is even or odd.
>>>def check(no):
if (no%2==0):
print ("No. is EVEN ")
else:
print ("No. is ODD ")

check(5)
check(4)
Output:
No. is EVEN
No. is ODD
9. Write a python program to find area of a circle by taking value of radius as input from
the user.
>>>rad=input("Enter radius of circle:")
area=3.14*rad*rad
print(area)

Suppose you provide 2 as input, then output is:


12.56

10. Write a python program to find factorial of a number.


>>>n=input("Enter number:")
fact=1
while(n>0):
fact=fact*n
n=n-1
print("Factorial of the number is: ")
print(fact)

Suppose you provide 3 as input, then output is:


6

Das könnte Ihnen auch gefallen