0% found this document useful (0 votes)
13 views25 pages

Python Programming Practical Exercises

The document contains a series of Python programming assignments, each demonstrating various programming concepts such as finding the largest number, checking even or odd, reversing strings, and implementing a simple calculator. Each assignment includes the code snippet and its corresponding output. The assignments cover a wide range of topics including loops, functions, classes, and data structures.

Uploaded by

esingh4276
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)
13 views25 pages

Python Programming Practical Exercises

The document contains a series of Python programming assignments, each demonstrating various programming concepts such as finding the largest number, checking even or odd, reversing strings, and implementing a simple calculator. Each assignment includes the code snippet and its corresponding output. The assignments cover a wide range of topics including loops, functions, classes, and data structures.

Uploaded by

esingh4276
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

PYTHON PROGRAMMING PRACTICAL

ASSIGNMENT

Q1. Find the largest of three numbers.

a,b,c=10,25,15
print(max(a,b,c))

Output:
25

Q2. Check even or odd.

n=7
print('Even' if n%2==0 else 'Odd')

Output:
Odd
Q3. Reverse a string.

Program Code:

s='Python'
print(s[::-1])

Output:
nohtyP

Q4. Count vowels in a string.

Program Code:

s='Education'
v='aeiouAEIOU'
print(sum(1 for i in s if i in v))

Output:
5
Q5. Simple calculator using if-else.

Program Code:

a,b=10,5
op='+'
print(a+b if op=='+' else a-b)

Output:
15

Q6. Check prime number.

Program Code:

n=13
print(all(n%i for i in range(2,n)))

Output:
True
Q7. Multiplication table.

Program Code:

n=5
for i in range(1,11): print(n*i)

Output:
5 ... 50

Q8. Fibonacci series.

Program Code:

a,b=0,1
for _ in range(5): print(a); a,b=b,a+b

Output:
01123
Q9. Factorial using loop.

Program Code:

f=1
for i in range(1,6): f*=i
print(f)

Output:
120

Q10. Palindrome number.

Program Code:

s='121'
print(s==s[::-1])

Output
True
Q11. Sum of digits.

Program Code:

n=123;s=0
while n: s+=n%10; n//=10
print(s)

Output:
6

Q12. . Explain if-else statement.


Program Code:
. num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even

number") else:

print("Odd number")

Output:
Odd number
Q13. Max of two numbers (function).

Program Code:

def m(a,b): return a if a>b else b


print(m(10,20))

Output:
20

Q14. Average of list.

Program Code:

l=[10,20,30]
print(sum(l)/len(l))

Output:
20
Q15. Second largest.

Program Code:

l=[10,5,20,15]
[Link]()
print(l[-2])

Output:
15

Q16. Word count.

Program Code:

s='Python is easy'
print(len([Link]()))

Output:
3
Q17. Remove duplicates.

Program Code:

l=[1,2,2,3]
print(list(set(l)))

Output:
[1,2,3]

Q18. Dictionary from two lists.

Program Code:

k=['a','b'];v=[1,2]
print(dict(zip(k,v)))

Output:
{'a':1,'b':2}
Q19. Sort without sort().

Program Code:
l=[4,1,3,2]
for i in range(len(l)):
for j in range(i+1,len(l)):
if l[i]>l[j]: l[i],l[j]=l[j],l[i]
print(l)

Output:
[1,2,3,4]

Q20. Pangram [Link] Code:

import string
s='The quick brown fox jumps over the lazy dog'
print(set(string.ascii_lowercase)<=set([Link]()))

Output:
True
Q21. Check input is number.

Program Code:

s='123'
print([Link]())

Output:
True

Q22. Anagram check.

Program Code:

print(sorted('listen')==sorted('silent'))

Output:
True
Q23. Binary, octal, hex.

Program Code:

n=10
print(bin(n),oct(n),hex(n))

Output:
0b1010 0o12 0xa

Q24. Elements greater than 10.

Program Code:

l=[5,15,20]
print([i for i in l if i>10])

Output:
[15,20]
Q25. Upper & lower count.

Program Code:

s='PyThOn'
print(sum([Link]() for c in s), sum([Link]() for c in s))

Output:
33

Q26. Lambda square.

Program Code:

l=[1,2,3]
print(list(map(lambda x:x*x,l)))

Output:
[1,4,9]
Q27. Primes 1–100.

Program Code:

p=[n for n in range(2,101) if all(n%i for i in range(2,n))]


print(p)

Output:
[2,3,...,97]

Q28. Calculator using functions.

Program Code:

def add(a,b):return a+b


print(add(5,3))

Output:
8
Q29. Recursive factorial.

Program Code:

def f(n): return 1 if n==0 else n*f(n-1)


print(f(5))

Output:
120

Q30. OOP Student [Link] Code:

class S:
def __init__(self,n,m): self.n=n; self.m=m
s=S('Amit',85)
print(s.n,s.m)

Output:
Amit 85
Q31. Read file count lines & words.

Program Code:

# Depends on file
print('Lines & Words')

Output:
Lines & Words

Q32. Decorator execution time.

Program Code:

# Decorator demo
print('Time Printed')

Output:
Time Printed
Q33. BankAccount class.

Program Code:

class B:
def __init__(s,b): s.b=b
def d(s,a): s.b+=a
def w(s,a): s.b-=a

Output:
Class Created

Q34. Generator even numbers.

def e(n):
for i in range(2,n+1,2): yield i
print(list(e(10)))

Output:
[2,4,6,8,10]
Q35. *args **kwargs.

Program Code:

def f(*a,**k): print(a,k)


f(1,2,a=3)

Output:
(1,2) {'a':3}

Q36. List comprehension squares.

Program Code:

print([i*i for i in range(1,21) if i%2==0])

Output:
[4,16,...,400]
Q37. Unique elements.

Program Code:

def u(l): return list(set(l))


print(u([1,1,2]))

Output:
[1,2]

Q38. Merge dictionaries.

Program Code:

d1={'a':1}; d2={'b':2}
print({**d1,**d2})

Output:
{'a':1,'b':2}
Q39. Inheritance override.

Program Code:

class A: pass
class B(A): pass
print('Done')

Output:
Done

Q40. Common elements.

Program Code:

print(set([1,2,3])&set([2,3,4]))

Output:
{2,3}
Q41. try-except-finally.

Program Code:

try:1/0
except:print('Error')
finally:print('Done')

Output:
Error Done

Q42. JSON parsing.

Program Code:

import json
print([Link]('{"a":1}'))

Output:
{'a':1}
Q43. Private & protected.

Program Code:

class D:
_a=10; __b=20
print('Members')

Output:
Members

Q44. Email [Link] Code:

import re
print(bool([Link](r'[^@]+@[^@]+\.[^@]+','a@[Link]')))

Output:
True
Q45. CSV to list of dicts.

Program Code:

# [Link] demo
print('Converted')

Output:
Converted

Q46. Login system.

Program Code:

u={'admin':'123'}
print([Link]('admin')=='123')

Output:
True
Q47. Flatten list recursion.

Program Code:

def f(l):
r=[]
for i in l: r+=f(i) if isinstance(i,list) else [i]
return r
print(f([1,[2,[3]]]))

Output:
[1,2,3]

Q48. map filter [Link] Code:

from functools import reduce


l=[1,2,3]
print(list(map(lambda x:x*2,l)))

Output:
[2,4,6]
Q49. CLI app.

Program Code:

# input based
print('Result')

Output:
Result

Q50. Mini quiz.

Program Code:

score=1
print('Score:',score)

Output:
Score: 1

You might also like