python_programs
python_programs
Solution
a=10.3
if type(a)==complex or -999999<=a<=999999:
print("number ", a)
or
If else
Program Test case
Question n is greater: 20
write a program to find out the greates of two
number and print the number
Solution
m=10
n=20
if m<n:
print("n is greater:",n)
else:
print("m is greater:",m)
Solution
n=input("enter the character")
li=[10,20.5,30+2j,'str', (34,),{2:'str'}]
m=[]
d={}
if n in li:
m+=[n]
print(m)
else:
d[n]=(n**2)
print(d)
nested if condition
Program Test case
Question enter the value 10
write a pgm to check whether the given value is 1000
present inside the collection or not if the value is {'hai': 10} 1000
present replace the value with cube of that value, enter the value20
and if the value is present in even position delete {'hai': 10}
the value and if given value is not present inside the 10
collection and extract end value,start value, if the enter the value20.06 8072.216215999999
value is present in odd position extract the values {'hai': 10}
starting from that point to end in reverse order 10
enter the valueFalse
Solution 0
st=eval(input("enter the value")) [{'hai': 10}, {}, (199,), [10], 'pys', 0]
li=[10,20.06,30+7j,False,'pys',[10],(199,),{},{'hai':10}]
if st in li:
res=li.index(st)
r=li[res]=st**3
print(r)
if st in li:
if li.index(st)%2==0:
del li[li.index(st)]
print(li)
if st not in li:
print(li[-1],li[0])
if st in li:
if li.index(st)%2!=0:
r=li[li.index(st):][::-1]
print(r)
While loop
Program Test case
Question :- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Write a program to print the series of 20 natural
values
Solution :-
i=1
while i<=20:
print(i,end=" ")
i=i+1
Question :- ZYXWVUTSRQPONMLKJIHGFEDCB
Write a program to print the series of upper case A
characters in reverse
Solution :-
i=90
while i>=65:
print(chr(i),end=" ")
i=i-1
Solution
st=['com', 'in', 'edu', 'com']
li=[]
i=0
while i<len(st):
if st[i] not in li:
li=li+[st[i]]
i=i+1
print(li)
For loop:-
Program Test case
Question 5
write a program to findout length of the
collection without using len function
Solution
count=0
for i in 'hello':
count=count+1
print(count)
Program Test case
Question ['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'd']
Wap to store all the data items inside the
collection without using built in function
Solution
st='helloword'
li=[]
for i in st:
li=li+[i]
print(li)
Solution
st='helloword'
s=''
for i in st:
s=i+s
print(s)
for i in st:
if 'A'<=i<='Z':
output:-
HLOR
HEOWO
-------------------------------------------------------------------------------------------------------------------------------------
for i in st:
if 'a'<=i<='z':
output:-
ellwd
-----------------------------------------------------------------------------------------------------------------
for i in st:
output:-
#$$%^^
@#^&
------------------------------------------------------------------------------------------------------------------------------------
for i in st:
if '0'<=i<='9':
output:-
123
123
-------------------------------------------------------------------------------------------------------------------------------------
*write a pgm to extract and store the special characters inside the collection
s=''
for i in st:
s=s+i
print(s)
output:-
123
!@#$%
======================================================================
*write a pgm to split the string and all the data items are store inside the collection
li=[]
s=''
for i in st:
if i!=' ':
s=s+i
else:
li=li+[s]
s=''
if len(st)!=0:
li=li+[s]
print(li)
==================================================================================
*write a pgm to split the string and all the data items are store inside the collection in reverse order
li=[]
s=''
for i in st:
if i!=' ':
s=i+s
else:
li=li+[s]
s=''
if len(st)!=0:
li=li+[s]
print(li)
=========================================================================
*write a pgm to split the string and all the data items are store inside the collection in reverse order
li=[]
s=''
for i in st:
if i!=' ':
s=s+i
else:
li=li+[s]
s=''
if len(st)!=0:
li=li+[s]
print(li[::-1])
=============================================================================
*input:-'PySpIdErS'
output:-pYsPiDeRs'
st='PySpIdErS'
s=''
for i in st:
if 'A'<=i<='Z':
s=s+(chr(ord(i)+32))
elif 'a'<=i<='z':
s=s+(chr(ord(i)-32))
print(s)
output:- pYsPiDeRs
=======================================================================
*input:-'PySpIdErS123#$%'
output:-pYsPiDeRs123#$%
t='PySpIdErS123#$%'
s=''
for i in st:
if 'A'<=i<='Z':
s=s+(chr(ord(i)+32))
elif 'a'<=i<='z':
s=s+(chr(ord(i)-32))
else:
s=s+i
print(s)
output:- pYsPiDeRs123#$%
===========================================================================
st='PySpIdErS123#$%'
count=0
for i in st:
if i in 'AEIOUaeiou':
count=count+1
print(count)
output:- 2
==========================================================================
a=0
b=1
i=0
while i<n:
c=a+b
a=b
b=c
i=i+1
print(c,end=" ")
output:-
12358
1 2 3 5 8 13 21 34 55
=================================================================================
output:-{'vowel': ['e', 'o', 'a', 'i', 'i'], 'consonent': ['h', 'l', 'l', 'h'], 'num': ['1', '2', '3'], 'special character':
[' ', ' ', '$',
'&', '#']}
d={'vowel':[],'consonent':[],'num':[],'special character':[]}
for i in st:
if 'A'<=i<='Z' or 'a'<=i<='z':
if i in 'AEIOUaeiou':
d['vowel']+=[i]
else:
d['consonent']+=[i]
elif '0'<=i<='9':
d['num']+=[i]
else:
d['special character']+=[i]
print(d)
output:- {'vowel': ['e', 'o', 'a', 'i', 'i'], 'consonent': ['h', 'l', 'l', 'h'], 'num': ['1', '2', '3'], 'special character':
[' ', ' ', '$',
'&', '#']}
=================================================================================
s=""
l={}
for i in st:
if i!=' ':
s+=i
else:
l[s]=len(s)
s=''
if len(s)!=0:
l[s]=len(s)
print(l)
============================================================================
l=[]
d={}
for i in st:
if i!=' ':
s=s+i
else:
l=l+[s]
s=''
if len(s)!=0:
l+=[s]
for j in l:
count=0
for k in j:
count+=1
d[j]=count
print(d)
==========================================================================
* input:-['google.com', 'apple.in','youtube.co.in','facebook.in']
l=['goog;e.com', 'apple.in','youtube.co.in','facebook.in']
li=[]
for i in l:
s=''
for j in i:
if j!='.':
s=s+j
else:
li+=[s]
break
print(li)
=========================================================================
*write a pgm to print the series of natural numbers with the given range by using range function
for i in range(1,n+1):
print(i,end=' ')
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
*write a pgm to print the series of odd numbers with the given range by using range function
for i in range(1,n+1):
if i%2!=0:
print(i,end=' ')
output:-
1 3 5 7 9 11 13 15 17 19
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29
===========================================================================
*write a pgm to print the series of even numbers with the given range by using range function
for i in range(1,n+1):
if i%2==0:
print(i,end=' ')
output:-
2 4 6 8 10 12 14
=====================================================================
*write a pgm to print the series of uppercase letters by using range function
for i in range(65,91):
print(chr(i),end=' ')
output:- A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
=============================================================================
*write a pgm to print the series of lowercase letters by using range function
for i in range(97,123):
print(chr(i),end=' ')
output:- a b c d e f g h i j k l m n o p q r s t u v w x y z
===============================================================================
*write a pgm to print the series of lowercase letters by using range function
for i in range(48,57):
print(chr(i),end=' ')
output:- 0 1 2 3 4 5 6 7 8 9
============================================================================
*write a program to print the fibanocii series using for loop and range function
a=0
b=1
for i in range(0,n-2):
c=a+b
a=b
b=c
print(c,end=" ")
output:-
1235
=======================================================================
*write a pgm to store the fibinocii numbers in the collection within given range
a=0
b=1
l=[]
l+=[a]
l+=[b]
for i in range(0,n-2):
c=a+b
a=b
b=c
l+=[c]
print(l,end=" ")
output:-
[0, 1, 1, 2, 3, 5, 8, 13]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]
============================================================================
a=0
b=1
flag=False
if n in [0,1]:
for i in range(0,n):
c=a+b
a=b
b=c
if c==n:
flag=True
break
if flag==True:
else:
output:-
=================================================================
pattern programs:
1)for i in range(0,5):
for j in range(0,5):
print(i,j,end=" ")
print()
output:
0001020304
1011121314
2021222324
3031323334
4041424344
======================================================================
2)for i in range(0,5):
for j in range(0,5):
print('*',end=" ")
print()
output:
*****
*****
*****
*****
*****
================================================================================
3)for i in range(0,5):
for j in range(0,5):
if i==4:
print('*',end=" ")
else:
print()
output:-
*****
========================================================================
4)for i in range(0,5):
for j in range(0,5):
if i==0:
print('*',end=" ")
else:
print()
output:-
*****