0% found this document useful (0 votes)
3 views

python_exercises_notes

The document is a Python script that covers various programming concepts including basic print statements, variables, user input, type conversion, string operations, arithmetic operators, conditional statements, loops, lists, tuples, dictionaries, functions, exception handling, classes, and inheritance. It includes examples of a weight converter, a guessing game, and demonstrates the use of built-in functions from imported modules. Additionally, it showcases the implementation of classes and inheritance with examples of different animal classes.

Uploaded by

Sree Dharan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

python_exercises_notes

The document is a Python script that covers various programming concepts including basic print statements, variables, user input, type conversion, string operations, arithmetic operators, conditional statements, loops, lists, tuples, dictionaries, functions, exception handling, classes, and inheritance. It includes examples of a weight converter, a guessing game, and demonstrates the use of built-in functions from imported modules. Additionally, it showcases the implementation of classes and inheritance with examples of different animal classes.

Uploaded by

Sree Dharan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

import utils

from utils import smallest_number, biggest_number

import department.dept
from department import dept
from department.dept import get_department, put_department

# Basic Print Statements


print('Hello World')
print("hello world")
print("hello world " * 5)

# Variables and Data Types


inputs = 5
print(inputs)
inputs = 5 * 2
print(inputs)
inputs = 5.1 * 2
print(inputs)
inputs = "Sridhar here"
print(inputs)
inputs = True
print(inputs)
inputs = False
print(inputs)

# 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))

weight = input("Your weight in pounds: ")


weight_kg = float(weight) * 0.45
print(weight_kg)

# 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')

# Lists and Operations


numbers = [5, 2, 5, 2, 2]
for item in numbers:
print('x' * item)

# 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 __init__(self, name, designation):


self.name = name
self.designation = designation

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}')

person1 = Person("Sridhar", "WHITE COLOR EMPLOYEE")


person1.talk()
person1.get_salary_details()

# 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

class Tortoise(Mammal, Aquatic):


pass

dog = Dog()
dog.walk()
dog.sound()

cat = Cat()
cat.walk()
cat.bark()

pig = Pig()
pig.walk()

tortoise = Tortoise()
tortoise.walk()
tortoise.swim()

You might also like