OOP LAB ASSIGNMENT
NAME: USWAH ZAHID
REG NO: FA24-BED-076
Variables
#Q1) Basic variable declaration
x = 10
y = 3.14
z = 'Hello'
print(x, y, z)
#Q2) Swapping values using temporary variable
a=5
b=7
temp = a
a=b
b = temp
print('Swapped:', a, b)
#Q3) Assigning same value to multiple variables
p = q = r = 100
print(p, q, r)
#Q4) Local and global variable example
global_var = 'I am Global'
def test_scope():
local_var = 'I am Local'
print(local_var)
print(global_var)
test_scope()
Data Types
#Q1) Checking different data types
print(type(10))
print(type(3.14))
print(type('Python'))
print(type(True))
#Q2) List data type
list_var = [1, 2, 3]
print(type(list_var))
#Q3) Type casting integer to string
num = 50
num_str = str(num)
print(num_str, type(num_str))
#Q4) Mutable vs Immutable types
x = [1, 2, 3]
x[0] = 99
print('List after change:', x)
y = (1, 2, 3)
print('Tuple cannot be changed:', y)
Casting
#Q1) String to integer and float
s = '100'
print(int(s))
print(float(s))
#Q2) Float to integer
print(int(5.9))
#Q3) Boolean casting
print(bool(1))
print(bool(0))
#Q4) String to integer addition
user_str = '20'
print(int(user_str) + 5)
List
#Q1) Access list element
fruits = ['apple', 'banana', 'mango', 'grape', 'orange']
print(fruits[2])
#Q2) Add and remove list elements
fruits = ['apple', 'banana', 'mango']
[Link]('pear')
[Link]('banana')
print(fruits)
#Q3) List slicing
fruits = ['apple', 'banana', 'mango', 'grape', 'orange']
print(fruits[:3])
#Q4) Sorting list
nums = [5, 3, 8, 1]
[Link]()
print('Ascending:', nums)
[Link](reverse=True)
print('Descending:', nums)
Tuple
#Q1) Length of tuple
t = (10, 20, 30)
print(len(t))
#Q2) Accessing tuple element
t = (10, 20, 30)
print(t[1])
#Q3) Nested tuple
nested = (1, (2, 3), 4)
print(nested)
# Q4 - Converting tuple to list and modifying
t = (1, 2, 3)
temp_list = list(t)
temp_list.append(4)
t = tuple(temp_list)
print("Modified tuple:", t)
Set
#Q1) Removing duplicates automatically
s1 = {1, 2, 2, 3, 4}
print('Unique set:', s1)
#Q2) Union and intersection
s1 = {1, 2, 3, 4}
s2 = {3, 4, 5, 6}
print('Union:', s1 | s2)
print('Intersection:', s1 & s2)
#Q3) Adding and removing elements
s1 = {1, 2, 3}
[Link](10)
[Link](3)
print('After add/remove:', s1)
# Q4 - Checking if an element exists in a set
fruits = {"apple", "banana", "cherry"}
if "banana" in fruits:
print("Banana is in the set")
else:
print("Banana is not in the set")
Dictionary
#Q1) Create and print dictionary
student = {'name': 'Ali', 'roll': 101, 'marks': 85}
print(student)
#Q2) Access dictionary value
student = {'name': 'Ali', 'roll': 101, 'marks': 85}
print(student['name'])
#Q3) Add new key-value pair
student = {'name': 'Ali', 'roll': 101, 'marks': 85}
student['grade'] = 'A'
print(student)
#Q4) Iterate through dictionary
student = {'name': 'Ali', 'roll': 101, 'marks': 85}
for key in student:
print(key, ':', student[key])
If Else
#Q1) Check even or odd
num = 6
if num % 2 == 0:
print('Even')
else:
print('Odd')
#Q2) Find largest among three numbers
a, b, c = 10, 20, 15
if a > b and a > c:
print('Largest:', a)
elif b > c:
print('Largest:', b)
else:
print('Largest:', c)
#Q3) Pass or fail condition
marks = 45
if marks >= 50:
print('Pass')
else:
print('Fail')
#Q4) Positive, negative or zero
n=0
if n > 0:
print('Positive')
elif n < 0:
print('Negative')
else:
print('Zero')
While Loop
#Q1) Print numbers 1 to 10
i=1
while i <= 10:
print(i, end=' ')
i += 1
#Q2) Find factorial
n=5
fact = 1
i=1
while i <= n:
fact *= i
i += 1
print('Factorial:', fact)
#Q3) Print multiplication table
n=3
i=1
while i <= 10:
print(n, 'x', i, '=', n*i)
i += 1
#Q4) Reverse a number
num = 1234
rev = 0
while num > 0:
digit = num % 10
rev = rev * 10 + digit
num //= 10
print('Reversed:', rev)
For Loop
#Q1) Iterate through list
fruits = ['apple', 'banana', 'mango']
for fruit in fruits:
print(fruit)
#Q2) Sum of first 10 numbers
s=0
for i in range(1, 11):
s += i
print('Sum:', s)
#Q3) Print even numbers
for i in range(2, 21, 2):
print(i, end=' ')
#Q4) Star pattern
for i in range(1, 6):
print('*' * i)
Functions
#Q1) Addition function
def add(x, y):
return x + y
print(add(5, 7))
#Q2) Function with default parameter
def greet(name='Guest'):
print('Hello', name)
greet()
greet('Ali')
#Q3) Sum of three numbers
def total(a, b, c):
return a + b + c
print(total(1, 2, 3))
#Q4) Recursive factorial
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n-1)
print('Factorial:', factorial(5))