0% found this document useful (0 votes)
2 views4 pages

python_practical_programs

The document contains practical Python programs covering 22 topics, including finding the largest of two numbers, checking if a number is even or odd, calculating factorials, identifying prime numbers, and handling strings and lists. It also includes examples of recursion, classes and objects, exception handling, and file operations. Each topic is presented with code snippets and brief explanations.

Uploaded by

varunisbet12345
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)
2 views4 pages

python_practical_programs

The document contains practical Python programs covering 22 topics, including finding the largest of two numbers, checking if a number is even or odd, calculating factorials, identifying prime numbers, and handling strings and lists. It also includes examples of recursion, classes and objects, exception handling, and file operations. Each topic is presented with code snippets and brief explanations.

Uploaded by

varunisbet12345
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 Practical Programs (All 22 Topics)

1. Largest of Two Numbers


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

largest = a if a > b else b


print("Largest number is:", largest)

2. Even or Odd
num = int(input("Enter a number: "))
print(f"{num} is Even" if num % 2 == 0 else f"{num} is Odd")

3. First 10 Natural Numbers


for i in range(1, 11):
print(i, end=" ")

4. Factorial (Loop)
num = int(input("Enter number: "))
fact = 1
for i in range(1, num+1):
fact *= i
print("Factorial:", fact)

4b. Factorial (Recursion)


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

print("Factorial:", factorial(5))

5. Prime Number
num = int(input("Enter number: "))
flag = True

if num < 2:
flag = False
else:
for i in range(2, int(num**0.5)+1):
if num % i == 0:
flag = False
break

print("Prime" if flag else "Not Prime")

6. Palindrome String
text = input("Enter string: ")
if text == text[::-1]:
print("Palindrome")
else:
print("Not Palindrome")

7. String Functions
text = "python programming"

print("Length:", len(text))
print("Count of 'm':", [Link]('m'))

words = [Link]()
print("Split:", words)

joined = "-".join(words)
print("Joined:", joined)

8. Print List Elements


lst = [10, 20, 30, 40]
for item in lst:
print(item)

9. Reverse List
lst = [1, 2, 3, 4, 5]
[Link]()
print(lst)

10. Search Element in List


lst = [10, 20, 30, 40]
x = int(input("Enter element to search: "))
print("Found" if x in lst else "Not Found")

11. Clear List


lst = [1, 2, 3]
[Link]()
print(lst)

12. Copy List


lst1 = [1, 2, 3]
lst2 = [Link]()
print(lst2)

13. Largest & Smallest in List


lst = [10, 5, 30, 2, 50]
print("Largest:", max(lst))
print("Smallest:", min(lst))
14. Length of Tuple
tup = (1, 2, 3, 4, 5)
print("Length:", len(tup))

15. Remove Tuple from List


lst = [(1,2), (3,4), (5,6)]
[Link]((3,4))
print(lst)

16. Sort Tuples


lst = [(2,3), (1,5), (4,1)]
[Link]()
print(lst)

17. Dictionary Operations


student = {"name": "Varun", "age": 20}

print(student["name"])
student["course"] = "BCA"
student["age"] = 21
[Link]("course")

print("Length:", len(student))
print(student)

18. Swap Two Numbers


a = 5
b = 10
a, b = b, a
print(a, b)

19. Recursion (Fibonacci)


def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)

for i in range(6):
print(fibonacci(i), end=" ")

20. Classes and Objects


class Student:
def __init__(self, name, marks):
[Link] = name
[Link] = marks

def display(self):
print(f"Name: {[Link]}, Marks: {[Link]}")

s1 = Student("Varun", 90)
[Link]()

21. Exception Handling


try:
a = int(input("Enter number: "))
b = int(input("Enter divisor: "))
print(a / b)

except ZeroDivisionError:
print("Cannot divide by zero!")

except FileNotFoundError:
print("File not found!")

finally:
print("Program Ended")

22. File Handling


with open("[Link]", "w") as f:
[Link]("Hello Python\n")

with open("[Link]", "r") as f:


print([Link]())

You might also like