Name:s.
lokesh
branch:GAD
PYTHON PROGRAMS
1.Write a Python Program to Print Hello world!
print “hello world”
2.Write a Python Program to Add Two Numbers
a=1
b=2
print a+b
3.Write a Python Program to check whether the given number is Ever or Odd
n=input("enter a number to check")
if n%2==0:
print "its even"
if n%2==1:
print "its odd"
4.Write a Python program to check if a number is positive or negative
n=input("enter a number to check")
if n<0:
print "its negative"
if n>0:
print "its positive"
5.Write a Python Program to Swap Two Variables
a=1
b=2
temp=a
a=b
b=temp
print "the value of a after swapping is",a
print "the value of b after swapping is",b
6.Write a Python Program to find the Largest among three numbers
a=input("enter the first number")
b=input("enter the second number")
c=input("enter the third value")
if a>b and a>c:
print "a is largest"
elif b>a and b>c:
print "b is largest"
else:
print "c is the largest number"
7.Write a Python program to check vowel or consonant
n=input("enter a word")
if n=="A" or n=="a" or n=="E" or n=="e" or n=="I" or n=="i" or n=="O" or n=="o" or n=="u" or
n=="U":
print "the given word is a vowel"
else:
print "its a consonant"
8.Write a Python Program to check whether the given Year is Leap (or) not
n=input("enter a year")
if n%4==0 and n%100==0:
print ("its a leap year")
else:
print ("it's not a leap year")
9.Write a Python Program to print Fibonacci sequence
nterms = int(input("How many terms? "))
n1, n2 = 0, 1
count = 0
if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
n1 = n2
n2 = nth
count += 1
10.Write a Python Program that accepts an integer and find Factorial of a given number
num = int(input("Enter a number: "))
factorial = 1
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
11.Write a Python Program to Check Prime Number
num = 407
if num > 1:
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num)
break
else:
print(num,"is a prime number")
else:
print(num,"is not a prime number")
12.Write a Python Program to find the reverse of given number
n=input("enter the number that u want to reverse")
rev=0
while n>0:
r=n%10
rev=rev*10+r
n=int(n/10)
t=n
print "the reverse of the given number is",rev
13.Write a Python Program to Display the multiplication Table for the given number
n=input("enter a number to multiply")
i=0
while i<=10:
print i*n
i=i+1
14.Write a Python Program to print numbers from 1 to 10 by using Simple loop, while and
For loop
While loop:
i=0
while i<=10:
print i
i=i+1
For loop:
for i in range(0,11):
print i
15.Write a Python program to have an exception for division by zero using exception
handling
try:
a = int(input("Enter a:"))
b = int(input("Enter b:"))
c = a/b
except:
print("Can't divide with zero")
16.Write a Python Program that accepts a number and check whether it is palindrome or
not
n=input("enter the number that u want to check")
t=n
rev=0
while n>0:
r=n%10
rev=rev*10+r
n=int(n/10)
if t==rev:
print "its a palindrome"
else:
print "its not"
17.Write a Python Program to Make a Simple Calculator
n = int(input("enter the 1st no"))
m = int(input("enter the 2nd no"))
print (" 1- add, 2- sub , 3-mul , 4 - div")
o =int(input("enter your calculation process"))
if o == 1:
print (n+m)
elif o == 2:
print (n-m)
elif o== 3:
print (n*m)
elif o == 4:
print (n/m)
else :
print ("error")
18.Write a Python Program to Find the Sum of Natural Numbers
num = int(input("Enter a number: "))
if num < 0:
print("Enter a positive number")
else:
sum = 0
while(num > 0):
sum += num
num -= 1
print("The sum is",sum)
19.Write a Python Program to count the number of characters in a given string and store
them in Dictionary data structure
str=input("enter string : ")
f = {}
for i in str:
if i in f:
f[i] += 1
else:
f[i] = 1
print(f)
print len(str)
20.Write a Python Program to print addition of two square matrices
X = [[1,2,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[9,8,7],
[6,5,4],
[3,2,1]]
result = [[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]
print(result)