Python Programs by Narayana
Python Programs by Narayana
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
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
def divby10(n):
if n%10==0:
print(num,'is divisible by 10')
else:
print(num,'is not divisible by 10')
divby10(num)
output
nevennums(num)
output
Enter any number: 10
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
def firstnevennums(n):
for i in range(n+1):
if i%2==0:
print(i)
firstnevennums(num)
output
def sumofdigits(n):
s=0
for i in range(n+1):
s=s+i
print(s)
sumofdigits(num)
output:
#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
def reversestring(s):
st1=s[::-1]
print(st1)
reversestring(st)
output
#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
def factors(n):
for i in range(1,n+1):
if n%i==0:
print(i)
factors(num)
output:
def factorial(n):
s=1
for i in range(1,n+1):
s=s*i
print(s)
factorial(num)
output:
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:
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
#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:
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
output:
fibonacci1(num)
output:
#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:
#18 Write a Python to check the marital status, gender and 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:
def tabledisplay(n):
for i in range(1,11):
print(n,"*",i,"=",n*i)
tabledisplay(num)
output
for i in st:
if i in v:
st=st.replace(i,'')
st=''.join(st)
print(st)
Python Programs
output
#21 Write a Python function to copy a given string into new variable and count
how many characters are copied
def copystring(st):
c=0
st1=''
for i in st:
st1+=i
c=c+1
print(st1)
print(c)
copystring(st)
output
def countdigits(n):
n1=len(n)
print(n1)
countdigits(num)
Python Programs
output
#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
#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
#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
n=4
def pattern1(n):
for i in range(n):
for j in range(n):
print('*',end=' ')
print('\n')
pattern1(n)
n=4
def pattern2(n):
for i in range(n+1):
for j in range(i):
print('*',end=' ')
print('\n')
pattern2(n)
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)
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)
pattern5(n)