Easy Python Programs for Beginners
Q: Write a program to check that given year is leap year or not.
i=int(input(“Enter Any Year: “))
if i % 4 ==0:
print(“{0} is a leap year”.format(i))
else:
print(“{0} is not a leap year”.format(i))
OUTPUT:
Enter Any Year: 2012
2012 is a leap year
Q: Write a program to find the vowels and consonents in the given string.
x=input(“Enter Any String: “)
print(“\nYour String: “,x)
c1=0;c2=0;c3=0;c4=0;c5=0;con=0
for i in x:
if i==’a’ or i==’A’:
c1=c1+1
elif i==’e’ or i==’E’:
c2=c2+1
elif i==’i’ or i==’I’:
c3=c3+1
elif i==’o’ or i==’O’:
c4=c4+1
elif i==’u’ or i==’U’:
c5=c5+1
else:
con=con+1
print(“\nVowel A=”,c1,”\tVowel E=”,c2,”\tVowel I=”,c3,”\tVowel O=”,c4,”\tVowel
U=”,c5,”\tVowel Consonent=”,con)
print(“\nThe Length Of Given String Is: “,len(x))
OUTPUT:
Enter Any String: hasan
Your String:
Vowel A= 2
Consonent= 3
hasan
Vowel E= 0
The Length Of Given String Is:
Vowel I= 0
5
Vowel O= 0
Vowel U= 0
Vowel
Q: Write a program to display table of given number in which user give range.
num=int(input(“Enter Any Number For Table: “))
r=int(input(“Till: “))
for i in range(1,(r+1)):
print(num,”X”,i,”=”,num*i)
OUTPUT:
Enter Any Number For Table: 6
Till: 10
6 X 1 = 6
6 X 2 = 12
6 X 3 = 18
6 X 4 = 24
6 X 5 = 30
6 X 6 = 36
6 X 7 = 42
6 X 8 = 48
6 X 9 = 54
6 X 10 = 60
Q: Write a program to find the prime numbers b/w the given range.
lower= int(input(‘Enter lower range:’))
upper= int(input(‘Enter upper range:’))
import math
for num in range (lower,upper+1):
if all(num%i!=0 for i in range (2,int(math.sqrt(num))+1)):
print (num)
OUTPUT:
Enter lower range:2
Enter upper range:15
2
3
5
7
11
13
Q: Write a program to display ASCII Code of keyboard input.
code=str(input(“Enter Any Alphabet: “))
print(“The ASCII Code Of”,code, “is”,ord(code))
OUTPUT:
Enter Any Alphabet: h
The ASCII Code Of h is 104
Q: Write a program to display all ASCII codes.
import string
for i in string.ascii_uppercase:
print(“The ASCII Code of”,i,”is”,ord(i))
for i in string.ascii_lowercase:
print(“The ASCII Code of”,i,”is”,ord(i))
OUTPUT:
The ASCII Code of A is 65
The ASCII Code of B is 66
The ASCII Code of C is 67
The ASCII Code of D is 68
The ASCII Code of E is 69
The ASCII Code of F is 70
The ASCII Code of G is 71
The ASCII Code of H is 72
The ASCII Code of I is 73
The ASCII Code of J is 74
The ASCII Code of K is 75
The ASCII Code of L is 76
The ASCII Code of M is 77
The ASCII Code of N is 78
The ASCII Code of O is 79
The ASCII Code of P is 80
The ASCII Code of Q is 81
The ASCII Code of R is 82
The ASCII Code of S is 83
The ASCII Code of T is 84
The ASCII Code of U is 85
The ASCII Code of V is 86
The ASCII Code of W is 87
The ASCII Code of X is 88
The ASCII Code of Y is 89
The ASCII Code of Z is 90
The ASCII Code of a is 97
The ASCII Code of b is 98
The ASCII Code of c is 99
The ASCII Code of d is 100
The ASCII Code of e is 101
The ASCII Code of f is 102
The ASCII Code of g is 103
The ASCII Code of h is 104
The ASCII Code of i is 105
The ASCII Code of j is 106
The ASCII Code of k is 107
The ASCII Code of l is 108
The ASCII Code of m is 109
The ASCII Code of n is 110
The ASCII Code of o is 111
The ASCII Code of p is 112
The ASCII Code of q is 113
The ASCII Code of r is 114
The ASCII Code of s is 115
The ASCII Code of t is 116
The ASCII Code of u is 117
The ASCII Code of v is 118
The ASCII Code of w is 119
The ASCII Code of x is 120
The ASCII Code of y is 121
The ASCII Code of z is 122
Q: Write A program using dictionary.
a={‘name’:”Barrack Obama”,’born’:”1961″,’president’:”20th January 2009″,’number’:”44th”}
print (a[“name”]+” was born in “+a[“born”]+” and became “+a[“number”]+” President of
America on “+a[“president”])
OUTPUT:
Barrack Obama was born in 1961 and became 44th President of America on 20th January 2009
Q: Write a program to find the largest number from given three numbers.
def max_of_three(a,b,c):
if a>b and a>c:
print(“\n”,a , “Is The Largest Number”)
if b>a and b>c:
print(“\n”,b , “Is The Largest Number”)
if c>a and c>b:
print(“\n”,c , “Is The Largest Number”)
else:
print(“All Numbers Are Equal”)
a=int(input(“Enter Any Number: “))
b=int(input(“Enter Any Number: “))
c=int(input(“Enter Any Number: “))
max_of_three(a,b,c)
OUTPUT:
Enter Any Number: 1
Enter Any Number: 5
Enter Any Number: 9
9 Is The Largest Number
Q: Write a program to find the Trace and Determinant Of a Matrix.
a=int(input(“Enter Number: “))
b=int(input(“Enter Number: “))
c=int(input(“Enter Number: “))
d=int(input(“Enter Number: “))
mat=[[a,b],[c,d]]
print(mat[0][0],mat[0][1])
print(mat[1][0],mat[1][1])
trace=a+c
det=a*d-b*c
print(“The Trace Of Given Numbers is: “,trace)
print(“The Determinant Of Given Numbers Is: “,det)
OUTPUT:
Enter Number: 1
Enter Number: 3
Enter Number: 4
Enter Number: 5
1 3
4 5
The Trace Of Given Numbers is: 5
The Determinant Of Given Numbers Is:
-7
Q: Write a program to display the factorial os the given keyboard input.
def factorial(n):
if n==0:
return 1
else:
return n * factorial(n-1)
n=int(input(“Enter Any Number For Factorial: “))
print (“Factorial of Given Number is: “,factorial(n))
OUTPUT:
Enter Any Number For Factorial: 6
Factorial of Given Number is: 720
Q: Write a program to find the grade of given percentage.
per=int(input(“Enter Your Percentage: “))
if per>=80:
print(“Your Grade Is A-One”)
elif per>=70:
print(“Your Grade Is A”)
elif per>=60:
print(“Your Grade Is B”)
else:
print(“Try Next Time”)
OUTPUT:
Enter Your Percentage: 75
Your Grade Is A
Q: Write a program to find the fabonacci series of given number.
n=int(input(“Enter Any Number: “))
a=0
b=1
c=[]
d=0
print(“The Fabonacci Series Of given Number is: “)
for i in range(0,n):
print(a)
u=a+b
a=b
b=u
c.append(a)
for j in range (0,len(c)):
d=d+c[j]
print (“The Sum of Whole Series Is: ” ,d)
OUTPUT:
Enter Any Number: 6
The Fabonacci Series Of given Number is:
0
1
1
2
3
5
The Sum of Whole Series Is: 20
Q: Write a program to demonstrate working of classes and object.
class Person:
def __init__ (self,name):
self.name=name
def say_hi (self):
print(“Hello, My Name Is”,self.name)
p=Person(“Maaz”)
p.say_hi()
OUTPUT:
Hello, My Name Is Maaz
Q: Write a program for the Temperature Conversion.
def FtoC():
temp=float(input(‘ENTER TEMPERATURE IN FARHENHEIT:’))
convert=(temp-32)*5/9
a=round(convert,2)
print(‘Your Temperature in Celcius is: ‘,a)
def CtoF():
temp=float(input(‘ENTER TEMPERATURE IN CELIUS:’))
convert=(temp*9/5)+32
b=round(convert,2)
print(‘Your Temperature in Farhenheit is: ‘,b)
while True:
print(‘\n__WELCOME TO TEMPERATURE CONVERSION__’)
print(‘\n PRESS 1 TO CHANGE TEMP FROM FARHENHEIT TO CELCIUS’)
print(‘\n PRESS 2 TO CHANGE TEMP FROM CELCIUS to FARHENHEIT ‘)
print(‘\n PRESS 0 TO EXIT’)
press=(input(‘\n ENTER YOUR CHOICE:’))
if press==’1′:
FtoC()
continue
elif press==’2′:
CtoF()
continue
elif press==’0′:
break
else:
print(‘INVALID CHOICE’)
break
OUTPUT:
__WELCOME TO TEMPERATURE CONVERSION__
PRESS 1 TO CHANGE TEMP FROM FARHENHEIT TO CELCIUS
PRESS 2 TO CHANGE TEMP FROM
CELCIUS
to FARHENHEIT
PRESS 0 TO EXIT
ENTER YOUR CHOICE:1
ENTER TEMPERATURE IN FARHENHEIT:25
Your Temperature in Celcius is: -3.89
__WELCOME TO TEMPERATURE CONVERSION__
PRESS 1 TO CHANGE TEMP FROM FARHENHEIT TO CELCIUS
PRESS 2 TO CHANGE TEMP FROM
CELCIUS
to FARHENHEIT
PRESS 0 TO EXIT
ENTER YOUR CHOICE:0
Q: Write a Guessing program using while loop.
n=8;
while True:
guess=int(input(“Enter Any Number: “))
if guess == n:
print(“You Guessed Right!”)
break
elif guess==0:
break
else:
print(“You Are Wrong”)
OUTPUT:
Enter Any Number: 6
You Are Wrong
Enter Any Number: 8
You Guessed Right!
Q: Write a program for Tuple.
zoo1=(“horse”,”dog”,”elephant”)
print(“Zoo=”,zoo1)
print(“The Number Of Animals in Zoo is: “,len(zoo1))
zoo2=(“giraffe”,”tiger”,zoo1)
print(“New Zoo=”,zoo2)
print(“The Number Of Animals in New Zoo is: “, len(zoo1)+len(zoo2)-1)
Zoo= (‘horse’, ‘dog’, ‘elephant’)
OUTPUT:
The Number Of Animals in Zoo is: 3
New Zoo= (‘giraffe’, ‘tiger’, (‘horse’, ‘dog’, ‘elephant’))
The Number Of Animals in New Zoo is: 5
Q: Write a program to demonstrate working of classes and object.
class Particle:
def __init__ (self,mass,velocity):
self.mass=mass
self.velocity=velocity
def momentum (self):
return (self.mass * self.velocity)
p=Particle(3.2,4.1)
p.momentum()
OUTPUT:
13.12
Q: Write a program to find the number that are divisible by 7 and not a multiple of 5
b/w 1000 and 1100.
def func(a):
if(a%7==0 and a%5!=0):
print (a)
for a in range(1000,1100,1):
func(a)
OUTPUT:
1001
1008
1022
1029
1064
1071
1078
1092
1036
1043
1057
1099
…