1.
Demonstrate different data types and display their types
Code:
a = 10
b = 3.14
c = "Hello"
d = True
e = [1, 2, 3]
f = (4, 5)
g = {"key": "value"}
h = {1, 2, 3}
i = None
variables = [a, b, c, d, e, f, g, h, i]
for var in variables:
print(f"Value: {var}, Type: {type(var)}")
Output:
Value: 10, Type: <class 'int'>
Value: 3.14, Type: <class 'float'>
Value: Hello, Type: <class 'str'>
Value: True, Type: <class 'bool'>
Value: [1, 2, 3], Type: <class 'list'>
Value: (4, 5), Type: <class 'tuple'>
Value: {'key': 'value'}, Type: <class 'dict'>
Value: {1, 2, 3}, Type: <class 'set'>
Value: None, Type: <class 'NoneType'>
2. Fibonacci series using for loop
Code:
n = 10
a, b = 0, 1
print("Fibonacci Series:")
for _ in range(n):
print(a, end=' ')
a, b = b, a + b
Output:
Fibonacci Series:
0 1 1 2 3 5 8 13 21 34
3. Check if a year is leap year
Code:
year = 2024
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
Output:
2024 is a leap year.
4. Conditional statements example
Code:
num = 5
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Output:
Positive number
5. Merge, remove duplicates, sort two lists
Code:
def merge_lists(l1, l2):
result = list(set(l1 + l2))
[Link](reverse=True)
return result
list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
print("Merged and sorted list:", merge_lists(list1, list2))
Output:
Merged and sorted list: [6, 5, 4, 3, 2, 1]
6. Recursive factorial function
Code:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
print("Factorial of 5:", factorial(5))
Output:
Factorial of 5: 120
7. BankAccount class with deposit/withdraw
Code:
class BankAccount:
def __init__(self, balance=0):
[Link] = balance
def deposit(self, amount):
[Link] += amount
print(f"Deposited: {amount}, New Balance: {[Link]}")
def withdraw(self, amount):
if amount > [Link]:
print("Insufficient balance")
else:
[Link] -= amount
print(f"Withdrawn: {amount}, New Balance: {[Link]}")
account = BankAccount()
[Link](1000)
[Link](500)
[Link](600)
Output:
Deposited: 1000, New Balance: 1000
Withdrawn: 500, New Balance: 500
Insufficient balance
8. Open and read a text file, count words
Code:
text = "This is a sample text file with some words."
words = [Link]()
print("Number of words:", len(words))
Output:
Number of words: 8
9. Determine if number is positive, negative, or zero
Code:
num = -3
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Output:
Negative number
10. Multiplication table using nested loops
Code:
for i in range(1, 11):
for j in range(1, 11):
print(f"{i*j:4}", end=' ')
print()
Output:
Output is a formatted 10x10 multiplication table
11. Evaluate mathematical expression
Code:
expr = "3 + 5 * 2"
result = eval(expr)
print("Result:", result)
Output:
Result: 13
12. Find largest of three numbers using conditionals
Code:
a, b, c = 10, 25, 17
if a >= b and a >= c:
largest = a
elif b >= a and b >= c:
largest = b
else:
largest = c
print("Largest number is:", largest)
Output:
Largest number is: 25
13. List operations demonstration
Code:
nums = [1, 2, 3, 4, 5]
[Link](6)
[Link](0, 0)
[Link](3)
[Link]()
[Link]()
print("Modified list:", nums)
Output:
Modified list: [6, 5, 4, 2, 1, 0]
14. Open and read text file, count words (again)
Code:
text = "Repeat text reading for word count."
words = [Link]()
print("Word count:", len(words))
Output:
Word count: 6
15. Vehicle class with Car and Bike subclasses
Code:
class Vehicle:
def display_info(self):
print("This is a vehicle")
class Car(Vehicle):
def display_info(self):
print("This is a car")
class Bike(Vehicle):
def display_info(self):
print("This is a bike")
v = Vehicle()
c = Car()
b = Bike()
v.display_info()
c.display_info()
b.display_info()
Output:
This is a vehicle
This is a car
This is a bike
16. Union and intersection of two sets
Code:
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
print("Union:", a | b)
print("Intersection:", a & b)
Output:
Union: {1, 2, 3, 4, 5, 6}
Intersection: {3, 4}