python_exercises_notes
python_exercises_notes
import department.dept
from department import dept
from department.dept import get_department, put_department
# User Input
name = input('What is your name? ')
print('Hi', name)
# Type Conversion
birth_year = input('Enter your birth year: ')
age = 2024 - int(birth_year)
print(age)
print(type(birth_year), type(age))
# String Operations
msg = 'python for "beginners"'
print(msg)
print(msg[0:3])
print(msg.upper())
print(len(msg))
print(msg.find('for'))
print(msg.replace("python", "coding"))
print('python' in msg)
# Arithmetic Operators
x, y = 5, 10
print(x + y, y - x, x * y, y / x, y % x, y // x, x ** 3)
x += 1
print(x)
# Conditional Statements
if x > y:
print('x > y')
elif x < y:
print('x < y')
else:
print('x == y')
# Weight Converter
weight = float(input('Weight: '))
type = input('(L)bs or (K)g: ').lower()
if type == 'k':
print(weight * 2.2)
else:
print(weight * 0.45)
# Loops
n = int(input('Which table to print: '))
i = 1
while i <= 10:
print(f'{n} x {i} = {n * i}')
i += 1
# Guessing Game
guess = 10
no_of_chances = 1
while no_of_chances <= 3:
number = int(input("Guess: "))
if guess == number:
print('You won!')
break
no_of_chances += 1
else:
print('You lose')
# Finding Min/Max
numbers = [4, 6, 12, 34, 2]
smallest = numbers[0]
for num in numbers:
if num < smallest:
smallest = num
print(smallest)
# 2D Lists
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(matrix[0][0])
# Tuples
nums = (1, 5, 7)
print(nums[0], 5 in nums, nums.count(5))
# Unpacking
x, y, z = (1, 2, 3)
print(x, y, z)
# Dictionaries
customer = {"name": "Sridhar", "age": 30}
print(customer["name"])
print(customer.get("phone", "Not found"))
# Function
def greet_user(first_name, last_name):
print(f'Hello {first_name} {last_name}')
greet_user("Sridhar", "Nithyanandam")
# Exceptions
try:
age = int(input("Age: "))
print(30000 / age)
except ZeroDivisionError:
print("Cannot divide by zero")
except ValueError:
print("Invalid input")
# Area Function
def area_rectangle(length, breadth):
return length * breadth
print(area_rectangle(40, 50))
print(utils.smallest_number([5, 7, 2, 78]))
print(smallest_number([5, 7, 2, 78]))
print(biggest_number([5, 7, 2, 78]))
# Packages
department.dept.get_department()
dept.get_department()
get_department()
put_department()
# Classes
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def walk(self):
print("I can walk")
def talk(self):
print("I can talk")
point = Point(456, 346)
print(point.x, point.y)
class Person:
white_color_employee = 0.1
blue_color_employee = 0.2
white_color_employee_salary = 10000
blue_color_employee_salary = 1000
def talk(self):
print(f'{self.name} can talk')
def get_salary_details(self):
if self.designation == 'WHITE COLOR EMPLOYEE':
allowance = self.white_color_employee_salary * self.white_color_employee
total_salary = allowance + self.white_color_employee_salary
elif self.designation == 'BLUE COLOR EMPLOYEE':
allowance = self.blue_color_employee_salary * self.blue_color_employee
total_salary = allowance + self.blue_color_employee_salary
print(f'{self.name} has total salary {total_salary}')
# Inheritance
class Mammal:
def walk(self):
print("walk")
class Aquatic:
def swim(self):
print("swim")
class Dog(Mammal):
def sound(self):
print("sound")
class Cat(Mammal):
def bark(self):
print("bark")
def walk(self):
print("cat walk")
class Pig(Mammal):
pass
dog = Dog()
dog.walk()
dog.sound()
cat = Cat()
cat.walk()
cat.bark()
pig = Pig()
pig.walk()
tortoise = Tortoise()
tortoise.walk()
tortoise.swim()