Python Programs
#1 Write a python function to check the given number is even or odd
num=int(input("Enter any value: "))
def evenorodd(n):
if n%2==0:
print(num, 'is even number')
else:
print(num,'is odd number')
evenorodd(num)
output:
Enter any value: 12
12 is even number
Enter any value: 13
13 is odd number
#2 Write a python function to check the given number is positive or negative
num=int(input("Enter any value: "))
def posornegorzero(n):
if n>0:
print(num,'is positive number')
elif n==0:
print('the given number is',num)
else:
print(num,'is negative number')
posornegorzero(num)
output:
Enter any value: 8
8 is positive number
Enter any value: -9
-9 is negative number
Enter any value: 0
the given number is 0
Python Programs
#3 Write a python function to check the given number is divisible by 10 or not?
num=int(input("Enter any number: "))
def divby10(n):
if n%10==0:
print(num,'is divisible by 10')
else:
print(num,'is not divisible by 10')
divby10(num)
output
Enter any number: 12
12 is not divisible by 10
Enter any number: 50
50 is divisible by 10
#4 Write a Python function to print first n even numbers
num=int(input('Enter any number: '))
lst=[]
def nevennums(n):
for i in range(10000):
if i%2==0:
lst.append(i)
if len(lst)==n:
break
print(lst)
nevennums(num)
output
Enter any number: 10
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Enter any number: 5
[0, 2, 4, 6, 8]
Python Programs
#5 Write a Python function to print even numbers upto n
num=int(input('Enter any number: '))
def firstnevennums(n):
for i in range(n+1):
if i%2==0:
print(i)
firstnevennums(num)
output
Enter any number: 10
0
2
4
6
8
10
Enter any number: 11
0
2
4
6
8
10
#6 Write a Python function to print sum of digits of number n
num=int(input('Enter any number: '))
def sumofdigits(n):
s=0
for i in range(n+1):
s=s+i
print(s)
sumofdigits(num)
output:
Enter any number: 15
120
Python Programs
Enter any number: 4
10
#7 Write a Python function to print the largest number of three given values
num1=int(input('Enter first number: '))
num2=int(input('Enter second number: '))
num3=int(input('Enter third number: '))
def largestnumber(a,b,c):
if a>b and a>c:
print(a,'is the largest number of',b,'and',c)
elif b>c:
print(b,'is the largest number of',a,'and',c)
else:
print(c,'is the largest number of',a,'and',b)
largestnumber(num1,num2,num3)
output
Enter first number: 10
Enter second number: 11
Enter third number: 14
14 is the largest number of 10 and 11
Enter first number: 6
Enter second number: 3
Enter third number: 2
6 is the largest number of 3 and 2
#8 Write a Python function to reverse the given numner or string
st=input('Enter any string or number: ')
def reversestring(s):
st1=s[::-1]
print(st1)
reversestring(st)
output
Enter any string: Python Narayana
anayaraN nohtyP
Python Programs
Enter any string: 1234
4321
#or
st=input('Enter any string or number: ')
def reversestring(s):
st1=''.join(reversed(s))
print(st1)
reversestring(st)
output:
Enter any string or number: Django
ognajD
Enter any string or number: 7382
2837
#9 Write a Python function to print the factors of given numnber
num=int(input('Enter any value: '))
def factors(n):
for i in range(1,n+1):
if n%i==0:
print(i)
factors(num)
output:
Enter any value: 10
1
2
5
10
Enter any value: 20
1
2
4
5
10
20
Python Programs
#10 Write a Python function to print factorial of given number
num=int(input('Enter any number: '))
def factorial(n):
s=1
for i in range(1,n+1):
s=s*i
print(s)
factorial(num)
output:
Enter any number: 5
120
Enter any number: 3
6
#11 Write a Python function to swap the given two numbers
num1=int(input("Enter first number: "))
num2=int(input("Enter second number: "))
def swap(x,y):
temp=x
x=y
y=temp
print('first number is',num1,', after swaping',x)
print('second number is',num2,', after swaping',y)
swap(num1,num2)
output:
Enter first number: 13
Enter second number: 12
first number is 13 , after swaping 12
second number is 12 , after swaping 13
Python Programs
#12 Write a Python function to check the number is prime or not
num=int(input("Enter any number: "))
def primeornot(n):
for i in range(2,n):
if n%i==0:
print(num,'is not a prime')
break
else:
print(num,'is prime')
primeornot(num)
output
Enter any number: 12
12 is not a prime
Enter any number: 7
7 is prime
#13 Write a Python function to check the given number is palindrom or not
n=input("Enter any number: ")
def palindromeornot(num):
if num==str(num)[::-1]:
print(num,'palindrome number')
else:
print(num,'not a palindrome number')
palindromeornot(n)
output:
Enter any number: 12321
12321 palindrome number
Enter any number: 123432
123432 not a palindrome number
Python Programs
#14 check gven number is armstrong or not
num=int(input('Enter any three digit number: '))
def armstrongornot(n):
s=0
while n>0:
digit=n%10
s+=digit**3
n//=10
if num==s:
print(num,'is a armstrong number')
else:
print(num,'is not a armstrong number')
armstrongornot(num)
output:
-------
Enter any three digit number: 151
151 is not a armstrong number
Enter any three digit number: 153
153 is a armstrong number
#15 print fibonacci series for below n number
num=int(input("Enter any number: "))
def fibonacci(n):
t1=0
t2=1
for i in range(10000):
temp=t1+t2
t1=t2
t2=temp
if t1>=n:
break
print(t1)
fibonacci(num)
output:
Enter any number: 100
1
1
2
Python Programs
3
5
8
13
21
34
55
89
Enter any number: 10
1
1
2
3
5
8
#16 Write a Python function to print first 10 fibonacci series numbers
num=int(input('Enter any number: '))
def fibonacci1(n):
t1=0
t2=1
lst=[0]
temp=0
while temp<10000:
temp=t1+t2
t1=t2
t2=temp
lst.append(t1)
if len(lst)==n:
break
for i in lst:
print(i)
fibonacci1(num)
output:
Enter any number: 5
0
1
1
2
3
Python Programs
Enter any number: 7
0
1
1
2
3
5
8
#17 Write a Python function to print the given three values in asc and desc order
a=eval(input("Enter first value: "))
b=eval(input("Enter second value: "))
c=eval(input("Enter third value: "))
def orderofvalues(x,y,z):
if x>y and x>z:
if y>z:
print('the desc order is ',x,y,z)
print('the asc order is',z,y,x)
else:
print('the desc order is ',x,z,y)
print('the asc order is ',y,z,x)
elif y>x and y>z:
if x>z:
print('the desc order is ',y,x,z)
print('the asc order is ',z,x,y)
else:
print('the desc order is ',y,z,x)
print('the asc order is',x,z,y)
elif z>x and z>y:
if x>y:
print('the desc order is ',z,x,y)
print('the asc order is ',y,x,z)
else:
print('the desc order is ',z,y,x)
print('the asc order is ',x,y,z)
orderofvalues(a,b,c)
output:
Enter first value: 12
Enter second value: 11
Python Programs
Enter third value: 15
the desc order is 15 12 11
the asc order is 11 12 15
Enter first value: 3
Enter second value: 2
Enter third value: 1
the desc order is 3 2 1
the asc order is 1 2 3
#18 Write a Python to check the marital status, gender and age
marsta=input("Enter marital status(married or single): ").lower()
gen=input("Enter your gender(male or female): ").lower()
age=int(input("Enter your age: "))
if marsta=="married":
print("You are not allowed to marry again")
elif marsta=="single":
if gen=="male":
if age>=21:
print("Congrates, You are eligible to marry")
else:
print("Sorry, You are not eligible to marry")
elif gen=="female":
if age>=18:
print("Congrates, You are eligible to marry")
else:
print("Sorry, You are not eligible to marry")
else:
print('You entered invalid gender')
else:
print("You entered invalid marital status")
output:
Enter marital status(married or single): married
Enter your gender(male or female): male
Enter your age: 24
You are not allowed to marry again
Enter marital status(married or single): married
Enter your gender(male or female): female
Enter your age: 17
Python Programs
You are not allowed to marry again
Enter marital status(married or single): single
Enter your gender(male or female): male
Enter your age: 22
Congrates, You are eligible to marry
#19 Write a Python function to print nth table
num=int(input("Enter any number: "))
def tabledisplay(n):
for i in range(1,11):
print(n,"*",i,"=",n*i)
tabledisplay(num)
output
Enter any number: 10
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100
#20 Write a Python function to remove vowels from given string
st=input('Enter any string :')
v='aieouAEIOU'
st=list(st)
for i in st:
if i in v:
st=st.replace(i,'')
st=''.join(st)
print(st)
Python Programs
output
Enter any string :Python Narayana
Pythn Nryn
Enter any string :Django Framework
Djng Frmwrk
#21 Write a Python function to copy a given string into new variable and count
how many characters are copied
st=input('Enter any string: ')
def copystring(st):
c=0
st1=''
for i in st:
st1+=i
c=c+1
print(st1)
print(c)
copystring(st)
output
Enter any string: Python Narayana
Python Narayana
15
#22 Write a Python function to count no.of digits in a given number
num=input('Enter any number: ')
def countdigits(n):
n1=len(n)
print(n1)
countdigits(num)
Python Programs
output
Enter any number: 12345
5
Enter any number: 788
3
#23 Write a Python function to print power values based on two given base and
exponent values
base=int(input('Enter base value: '))
expo=int(input('Enter exponent value: '))
def power(m,n):
x=base**expo
print(x)
power(base,expo)
output
Enter base value: 3
Enter exponent value: 2
9
Enter base value: 5
Enter exponent value: 4
625
#24 Write a Python function to check whether user given string or number
x=input('Enter eaither string or number: ')
def strornum(m):
if m.isalpha()==True:
print(x,'is a string value')
elif m.isnumeric()==True:
print(x,'is a number value')
elif m.isalnum()==True:
print(x,'is alpha-numeric value')
else:
print(x,'is not a complete string or number or alpha-numeric value')
Python Programs
strornum(x)
output
Enter eaither string or number: Python
Python is a string value
Enter eaither string or number: 1038
1038 is a number value
Enter eaither string or number: cs1035
cs1035 is alpha-numeric value
[email protected] is not a complete string or number or alpha-numeric value
#24 Write a Python function to check whether the given character is vowel or
consonant
char=input('Enter any chararcter: ')
def vowelorconso(ch):
vowel='aeiouAEIOU'
if ch in vowel:
print(char,'is a vowel')
else:
print(char,'is a consonant')
vowelorconso(char)
output
Enter any chararcter: a
a is a vowel
Enter any chararcter: E
E is a vowel
Enter any chararcter: x
x is a consonant
Enter any chararcter: Y
Y is a consonant
Python Programs
#25 Write a Python function to print the follwing pattern
****
****
****
****
n=4
def pattern1(n):
for i in range(n):
for j in range(n):
print('*',end=' ')
print('\n')
pattern1(n)
#26 Write a Python function to print the following pattern
*
**
***
****
n=4
def pattern2(n):
for i in range(n+1):
for j in range(i):
print('*',end=' ')
print('\n')
pattern2(n)
#27 Write a Python function to print the following pattern
*
***
*****
*******
*********
n=5
def pattern3(n):
k=0
for i in range(1,n+1):
for j in range(1,(n-i)+1):
Python Programs
print(end=' ')
while(k != (2*i-1)):
print('* ',end='')
k=k+1
k=0
print('\n')
pattern3(n)
#28 write a Python function to print the following pattern
*
**
***
****
*****
n=4
def pattern4(n):
m=8
for i in range(n+1):
for j in range(m):
print(end=' ')
m=m-2
for k in range(i+1):
print('* ',end='')
print()
pattern4(n)
#29 Write a Python function print the following pattern
'''
*****
****
***
**
*
Python Programs
n=5
def pattern5(n):
for i in range(n+1):
for j in range(1,(n-i)+1):
print('*',end=' ')
print()
pattern5(n)