0% found this document useful (0 votes)
7 views28 pages

python_programs

The document contains a series of programming exercises with corresponding test cases and solutions. Each program addresses a specific task such as checking divisibility, determining memory addresses, validating characters, and manipulating collections. The solutions utilize various programming constructs including conditionals, loops, and input handling.

Uploaded by

Omkar Gavade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views28 pages

python_programs

The document contains a series of programming exercises with corresponding test cases and solutions. Each program addresses a specific task such as checking divisibility, determining memory addresses, validating characters, and manipulating collections. The solutions utilize various programming constructs including conditionals, loops, and input handling.

Uploaded by

Omkar Gavade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

If

Program Test case


Question:- 1
1 .Write a program to check whether given enter the number :10
value is divisible by 6 or not if divisible by 6 the leftshifted number: 80
print the cube of that value or else 2
perform the left shift operation step is 3. enter the number :6
Solution:- the cube of given number 216
n=int(input("enter the number :")) 3
if n % 6 == 0: enter the number :2.5
m=n*n*n Traceback (most recent call last):
print(m) File
else: "C:/Users/admin/AppData/Local/Programs/Python/Pyt
m=n << 3 hon310/p1.py", line 1, in <module>
print(m) n=int(input("enter the number :"))
ValueError: invalid literal for int() with base 10: '2.5'

Program Test case


Question:- 1
Write a program check two vales are poiting to enter the first value125
the same memory or not if same memory display enter the second value126
the address of the two values or else display the type of the value <class 'int'>
type of the values 2
enter the first value135
Solution :- enter the second value135
a=eval(input("enter the first value")) id of the value 2079453483440
b=eval(input("enter the second value")) 3
if a==b: enter the first value"hji"
enter the second value"hij"
print("id of the value",id(a)) type of the value <class 'str'>
else:
print("type of the value",type(a))

Program Test case


Questions :- 1
Write a program to check whether given enter the characterhaii
collection length is even print the reverse order iiah
of collection or else print the odd position value 2
inside the collection enter the characterhello
el
Solution :-
s=input("enter the character")
if len(s)%2==0:
print(s[::-1])
else:
print(s[1::2])
Program Test case
Question 1
write a program to check whether enter the number 10
the given input is number or not number 10
2
Solution enter the number a
Traceback (most recent call last):
a=eval(input("enter the number ")) File
if type(complex(a))==complex or "C:/Users/admin/AppData/Local/Programs/Python/Python31
type(float(a))==float or 0/p11.py", line 1, in <module>
type(int(a))==int: a=eval(input("enter the number "))
print("number",a) File "<string>", line 1, in <module>
NameError: name 'a' is not defined
3
enter the number 1+1j
number (1+1j)
4
enter the number 0.1
number 0.1

Program Test case


Question number 10.3
write a program to check whether the given
input is number or not

Solution
a=10.3
if type(a)==complex or -999999<=a<=999999:
print("number ", a)

Program Test case


Question 1
write a program to check whether the given enter the character10
input is present in the collection or not given chaacter is present
Solution 2
a=int(input("enter the character")) enter the character20
if a in [10, 20, 30,'haii'] : given chaacter is presesnt
print("given chaacter is presesnt ")
Program Test case
Question 1
write a program to check whether the given enter the charactern
input is alphabet or not given character is alphabet n
2
Solution enter the characterh
a=input("enter the character") if 65<=ord(a)<=90 given character is alphabet h
or 97<=ord(a)<=122:
print("given character is alphabet",a)

Program Test case


Question 1
write a program to check whether the given ënter the characterg
input is character or not the given input is charcter
2
Solution ënter the character10
a=input("ënter the character") the given input is charcter
if type(a)==str:
print("the given input is charcter")

Program Test case


Question 1
write a program to check whether the given ënter the characterM
input is uppercase or not given character is uppercase M
2
Solution ënter the characterm
char=input("ënter the character")
if 'A'<=char<='Z':
print("given character is uppercase",char)

or

char=input("ënter the character")


if 65<=ord(char)<=90:
print("given character is uppercase",char)
Program Test case
Question 1
write a program to check whether the given enter the character K
input is lowecase or not 2
ënter the character l
Solution given character is lowercase l
char=input("ënter the character")
if 'a'<=char<='z':
print("given character is lowerercase",char)
or
char=input("ënter the character")
if 97<=ord(char)<=122:
print("given character is lowercase",char)

Program Test case


Question 1
write a program to check whether the given enter the character k
input is ASCII number or not 2
enter the character 8
Solution given input is ASCII number 8
a=input("ënter the character")
if 48<=ord(a)<=57:
print("given input is ASCII number",a)

Program Test case


Question 1
write a program to check whether the given ënter the character l
input is special character or not 2
ënter the character*
Solution given character is special character*
a=input("ënter the character")
if not (48<=ord(a)<=57 or 65<=ord(a)<=90 or
97<=ord(a)<=122):
print("given character is special character",a)

Program Test case


Question 1
write a program to check whether the given ënter the number78
input is even number or not given number is even78
2
Solution ënter the number 49
a=int(input("ënter the number"))
if a%2==0:
print("given number is even",a)

Program Test case


Question 1
)write a program to check whether the given enter the number 78
input is odd number or not 2
enter the number 49
Solution given number is odd 49
a=int(inpt("ënter the number"))
if a%2!=0:
print("given number is odd",a)
or
a=int(inpt("ënter the number"))
if not a%2==0:
print("given number is odd",a)

Program Test case


Question 1
write a program to check the number is greatest enter the number10
or not enter the number20
2
Solution enter the number30
a=int(input("enter the number")) enter the number20
b=int(input("enter the number")) a is greater 30
if a>b:
print("a is greater",a)

Program Test case


Question 1
write a program to check the number is lesser or enter the number 40
not enter the number 30
2
Solution enter the number 30
a=int(input("enter the number")) enter the number 40
b=int(input("enter the number")) a is lesser 30
if a<b:
print("a is lesser",a)
If elif

Program Test case


Question :- 1
Write a program to find the smallest of 3 enter a value10
numbers enter b value20
enter c value30
Solution :- 10 is a smallest value
a=eval(input("enter a value")) 2
b=eval(input("enter b value")) enter a value25
c=eval(input("enter c value")) enter b value20
if a<b and a<c: enter c value23
print(f"{a} is a smallest value") 20 is a smallest value
elif b<c: 3
print(f"{b} is a smallest value") enter a value1.0
else: enter b value1.05
print(f"{c} is a smallest value") enter c value0.99
0.99 is a smallest value

Program Test case


Question :- 1
Write a program to check whether given enter the valuea
character is vowel print the ascii value with 97
string format. if that value is number then 5 2
reputation the value. If value is consonant store enter the value10
the value and ascii value inside the set 1010101010
3
Solution :- enter the valuef
a=input("enter the value") {'f', 102}
if a in 'aeiouAEIOU':
print(str(ord(a)))
elif ('A'<=a<='Z' or 'a'<=a<='z') and a not in
'aeiouAEIOU':
print({a,ord(a)})
elif '0'<=a<='9':
print(a*5)
else:
print("given character is special symbol")

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)

Program Test case


Question enter the number10
write a program to find out the greates of two enter the number24
number and print the number n is greater: 24
Solution
m=int(input("enter the number"))
n=int(input("enter the number"))
if m<n:
print("n is greater:",n)
else:
print("m is greater:",m)

Program Test case


Question 1
write a program to check whether given enter the character K
character is uppercase then that character is k
converted to lowercase , if it is a lowercase 2
convert into upper enter the character k
K
Solution
n=input("enter the character")
if 'A'<=n<='Z':
print(chr(ord(n)+32))
else:
print(chr(ord(n)-32))
Program Test case
Question enter the character10
write a program to check whether the given [10]
value is present inside the collection or not, if enter the character20
the value is present that value stored in list or {20: 400}
else that value stored in a dictionary and value enter the character'str'
consider as a power of the value ['str']

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

Program Test case


Question :- 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Write a program to print the series of 20 natural
values in reverse order
Solution :-
i=20
while i>=1:
print(i,end=" ")
i=i-1

Program Test case


Question :- ABCDEFGHIJKLMNOPQRSTUVWXY
Write a program to print the series of upper case Z
characters
Solution :-
i=65
while i<=90:
print(chr(i),end=" ")
i=i+1
Program Test case

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

Program Test case


Question 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2 0
Write a program to print the series of even
number 0to 30 in reverse order
Solution
i=30
while i>=0:
if i%2==0:
print(i,end=" ")
i=i-1

Program Test case


Question 1
write a pgm to print the factorial of the given enter the integer value4
number 24
Solution 2
start=int(input("enter the integer value")) enter the integer value10
fact=1 3628800
while start>=1: 3
fact=fact*start enter the integer value2
start=start-1 2
print(fact)
Program Test case
Question ['google.com', ' yahoo.in', ' facebook.edu', '
write a pgm to split the collection input:- pyspider.com']
"google.com yahoo.in facebook.edu
pyspider.com"
output:-['google.com', 'yahoo.in',
'facebook.edu', 'pyspider.com']
Solution
st="google.com yahoo.in facebook.edu
pyspider.com"
s=""
li=[]
i=0
while i<len(st):
if st[i]!=" ":
s=s+st[i]
else:
li+=[s]
s=" "
i=i+1
if len(s)!=0:
li=li+[s]
print(li)

Program Test case


Question [' facebook.edu', ' yahoo.in', 'google.com', '
In reverese order pyspider.com']
Solution
st="google.com yahoo.in facebook.edu
pyspider.com"
s=""
li=[]
i=0
while i<len(st):
if st[i]!=" ":
s=s+st[i]
else:
li=[s]+li
s=" "
i=i+1
if len(s)!=0:
li=li+[s]
print(li)

Program Test case


Question [' google', ' yahoo', ' facebook', '
input:-["google.com", "yahoo.in", "facebook.edu", pyspider']
"pyspider.com"]
output:-[' google', ' yahoo', ' facebook', ' pyspider']
Solution
st=["google.com","yahoo.in","facebook.edu","pyspider.com"]
li=[]
i=0
while i<len(st):
s=" "
j=0
while j<len(st[i]):
if st[i][j]!=".":
s=s+st[i][j]
else:
li=li+[s]
j=j+1
i=i+1
print(li)

Program Test case


Question ['google', 'yahoo', 'facebook',
Using break 'pyspider']
Solution
st=["google.com","yahoo.in","facebook.edu","pyspider.com"]
li=[]
s=""
i=0
while i<len(st):
j=0
while j<len(st[i]):
if st[i][j]!=".":
s=s+st[i][j]
else:
li=li+[s]
s=""
break
j=j+1
i=i+1
print(li)

Program Test case


Question ['com', 'in', 'edu', 'com']
input:-["google.com", "yahoo.in", "facebook.edu",
"pyspider.com"]
output:- ['com', 'in', 'edu', 'com']
Solution
st=["google.com","yahoo.in","facebook.edu","pyspider.com"]
li=[]
s=""
i=0
while i<len(st):
j=0
temp=st[i][::-1]
while j<len(temp):
if temp[j]!=".":
s=temp[j]+s
else:
li=li+[s]
s=""
break
j=j+1
i=i+1
print(li)

Program Test case


Question ['com', 'in', 'edu']

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)

Program Test case


Question ['d', 'r', 'o', 'w', 'o', 'l', 'l', 'e', 'h']
For reverse
Solution
st='helloword'
li=[]
for i in st:
li=[i]+li
print(li)

Program Test case


Question drowolleh
Wap to reverse the string without using slicing

Solution
st='helloword'
s=''
for i in st:
s=i+s
print(s)

Program Test case


Question Helloworld&*
Wap to convert upper case to lower case
Solution
st='HELLOWORLD&*'
s=''
for i in st:
if 'A'<=i<='Z':
s=s+(chr(ord(i)+32))
else:
s=s+i
print(s)
Program Test case
Question HELLOWORLD&*
Wap to convert lower case to upper case
Solution
st='helloworld&*'
s=''
for i in st:
if 'a'<=i<='z':
s=s+(chr(ord(i)-32))
else:
s=s-i
print(s)

*write a pgm to print the uppercase characters

st=input("enter the character")

for i in st:

if 'A'<=i<='Z':

print(i , end=' ')

output:-

• enter the characterHeLlOwoRld

HLOR

• enter the character HEllOWOrld123

HEOWO

-------------------------------------------------------------------------------------------------------------------------------------

* write a pgm to print the lowercase characters

st=input("enter the character")

for i in st:

if 'a'<=i<='z':

print(i , end=' ')

output:-

• enter the character HellOwORLd123

ellwd

• enter the character haii123$%^


haii

-----------------------------------------------------------------------------------------------------------------

*write a pgm to print the special characters

st=input("enter the character")

for i in st:

if not ('a'<=i<='z' or 'A'<=i<='Z' or '0'<=i<='9'):

print(i , end=' ')

output:-

• enter the character hello123#$$%^^

#$$%^^

• enter the character hai1@2#4^&

@#^&

------------------------------------------------------------------------------------------------------------------------------------

*write a pgm to print the ASCII numbers

st=input("enter the character")

for i in st:

if '0'<=i<='9':

print(i , end=' ')

output:-

• enter the character hai123$%^

123

• enter the character heklloworld123

123

-------------------------------------------------------------------------------------------------------------------------------------

*write a pgm to extract and store the special characters inside the collection

st=input("enter the character")

s=''

for i in st:

if not ('a'<=i<='z' or 'A'<=i<='Z' or '0'<=i<='9'):

s=s+i
print(s)

output:-

• enter the character heklloworld123

123

• enter the character haii123!@#$%

!@#$%

======================================================================

*write a pgm to split the string and all the data items are store inside the collection

st=input("enter 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)

output:- enter the collectionhai how are you

['hai', 'how', 'are', 'you']

==================================================================================

*write a pgm to split the string and all the data items are store inside the collection in reverse order

st=input("enter the collection")

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)

output:- enter the collectionhaii how are you

['iiah', 'woh', 'era', 'uoy']

=========================================================================

*write a pgm to split the string and all the data items are store inside the collection in reverse order

st=input("enter 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[::-1])

output:- enter the collectionhello hai hiw are you

['you', 'are', 'hiw', 'hai', 'hello']

=============================================================================

*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#$%

===========================================================================

*write a pgm to count the vowels in the given string

st='PySpIdErS123#$%'

count=0

for i in st:

if i in 'AEIOUaeiou':

count=count+1

print(count)

output:- 2
==========================================================================

* write a program to print the fibanocii series using whike loop

n=int(input("enter the integer value"))

a=0

b=1

i=0

while i<n:

c=a+b

a=b

b=c

i=i+1

print(c,end=" ")

output:-

• enter the integer number5

12358

• enter the integer number9

1 2 3 5 8 13 21 34 55

• enter the integer number15

1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

=================================================================================

*input:-="hello haii 123$&#"

output:-{'vowel': ['e', 'o', 'a', 'i', 'i'], 'consonent': ['h', 'l', 'l', 'h'], 'num': ['1', '2', '3'], 'special character':
[' ', ' ', '$',

'&', '#']}

st="hello haii 123$&#"

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':
[' ', ' ', '$',

'&', '#']}

=================================================================================

*input:-"hello pyspider how are y0u"

output:-{'hello': 5, 'pyspider': 8, 'how': 3, 'are': 3, 'you': 3}

st="hello pyspider how are y0u"

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)

output:- {'hello': 5, 'pyspider': 8, 'how': 3, 'are': 3, 'you': 3}

============================================================================

*input:="hello pyspider how are you"

output:{'hello': 5, 'pyspider': 8, 'how': 3, 'are': 3, 'you': 3}

st="hello pyspider how are you"


s=''

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)

output:- {'hello': 5, 'pyspider': 8, 'how': 3, 'are': 3, 'you': 3}

==========================================================================

* input:-['google.com', 'apple.in','youtube.co.in','facebook.in']

output:-['google', 'apple', 'youtube', 'facebook']

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)

output:- ['google', 'apple', 'youtube', 'facebook']

=========================================================================

*write a pgm to print the series of natural numbers with the given range by using range function

n=int(input("enter the end value"))

for i in range(1,n+1):

print(i,end=' ')

output:- enter the end value20

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

n=int(input("enter the end value"))

for i in range(1,n+1):

if i%2!=0:

print(i,end=' ')

output:-

• enter the end value20

1 3 5 7 9 11 13 15 17 19

• enter the end value30

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

n=int(input("enter the end value"))

for i in range(1,n+1):

if i%2==0:

print(i,end=' ')

output:-

• enter the end value15

2 4 6 8 10 12 14

• enter the end value27


2 4 6 8 10 12 14 16 18 20 22 24 26

=====================================================================

*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

n=int(input("enter the integer number"))

a=0

b=1

for i in range(0,n-2):

c=a+b

a=b

b=c

print(c,end=" ")

output:-

• enter the integer number 6

1235

• enter the integer number9


1 2 3 5 8 13 21

• enter the integer number15

1 2 3 5 8 13 21 34 55 89 144 233 377

=======================================================================

*write a pgm to store the fibinocii numbers in the collection within given range

n=int(input("enter the integer number"))

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:-

• enter the integer number8

[0, 1, 1, 2, 3, 5, 8, 13]

• enter the integer number17

[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]

============================================================================

*write a pgm to check whether given number is fibnocii number or not

n=int(input("enter the integer number"))

a=0

b=1

flag=False

if n in [0,1]:

print("given value is fibonocii")


else:

for i in range(0,n):

c=a+b

a=b

b=c

if c==n:

flag=True

break

if flag==True:

print("given value is fibonocii")

else:

print("given value is not fibonocii")

output:-

• enter the integer number 4

given value is not fibonocii

• enter the integer number 56

given value is not fibonocii

• enter the integer number 78

given value is not fibonocii

=================================================================

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(' ',end=" ")

print()

output:-

*****

========================================================================

4)for i in range(0,5):

for j in range(0,5):

if i==0:
print('*',end=" ")

else:

print(' ',end=" ")

print()

output:-

*****

You might also like