0% found this document useful (0 votes)
10 views5 pages

Day 5 & 6 Python Internship - Ipynb - Colab

The document covers various concepts of Object-Oriented Programming (OOP) including classes, objects, encapsulation, inheritance, abstraction, and polymorphism. It also demonstrates exception handling, method overriding, method overloading, and the use of regular expressions in Python. Additionally, it includes examples of lambda functions and their applications.

Uploaded by

pullurigowtham
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)
10 views5 pages

Day 5 & 6 Python Internship - Ipynb - Colab

The document covers various concepts of Object-Oriented Programming (OOP) including classes, objects, encapsulation, inheritance, abstraction, and polymorphism. It also demonstrates exception handling, method overriding, method overloading, and the use of regular expressions in Python. Additionally, it includes examples of lambda functions and their applications.

Uploaded by

pullurigowtham
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
You are on page 1/ 5

#OOPs - class, object, encupsulation, inheritance, abstraction, polymorphism

#exception handling

import random, string


# print(string.ascii_lowercase)
# print(string.ascii_uppercase)
# print(string.digits)
# print(string.punctuation)

a=string.digits
b=string.ascii_lowercase
c=string.ascii_uppercase
d=string.punctuation
def random_string():
random=''.join(random.choices(string.ascii_lowercase + string.ascii_uppercase + string.punctuation + string.digits,k=5))
if str(a) & str(b) & str(c) & str(d) in random:
print(random)
else:
random_string()

x=""
y=int(input("Enter:"))
x=y

print(x)

Enter:6
6

#Defining a Class
class Car:
def __init__(self):
print("Demo")
def display(self):
print("Constructor")
b=Car()
b.display()

Demo
Constructor

class student:
def __init__(self,name,age):
self.name=name
self.age=age
def display(self):
print("My name:",self.name,"and my age :",self.age)
b=student("Gowtham",20)
b.display()

My name: Gowtham and my age : 20

class car:
def __init__(self,model,name,color):
self.model=model
self.name=name
self.color=color
def detail(self):
print(f"This car is ",self.name," and the model is ",self.model," and the color is ",self.color)
def drive(self):
print(f"The ",self.name," is in Driving mode")
def stop(self):
print(f"The ",self.name," is stoped")
car_1=car(2025,"BMW","Black")
car_2=car(2024,"Audi","Blue")

car_1.detail()
car_2.detail()
car_1.drive()
car_2.stop()
This car is BMW and the model is 2025 and the color is Black
This car is Audi and the model is 2024 and the color is Blue
The BMW is in Driving mode
The Audi is stoped

#encapsulation
class car:
def open_door(self): #public
print("door is opened")
def _accelerate(self): #protected
print("Throttle Increased")
def __enginestart(self): #private
print("Engine Started")
car_1=car()
car_1.open_door()
car_1._accelerate()
car_1._car__enginestart() #to access private... you have to give _car as prefix

door is opened
Throttle Increased
Engine Started

#polymorphism
class father:
def role(self):
print("taking care of family")
class employee(father):
def role(self):
print("Working in Office")
dad=father()
emp=employee()
dad.role()
emp.role()

taking care of family


Working in Office

#inheritance
class father:
def action(self):
print("Take the car")
class son(father):
def music(self):
print("Singing Pop music")
a=son()
a.action()

Take the car

#Abstraction
class animal:
def sound(self):
pass
class dog(animal):
def sound(self):
print("Bow Bow")
class cat(animal):
def sound(self):
print("Meow Meow")
a=animal()
dog().sound()
cat().sound()

Bow Bow
Meow Meow
#Exception Handling

try:
num=int(input("Enter a Number: "))
result=10/num
except ZeroDivisionError:
print("Cannot divide by zero")
except ValueError:
print("Invalid Input")
else:
print(result)
finally:
print("Program Executed")

Enter a Number: dfh


Invalid Input
Program Executed

def add_number(*args):
total=1
for num in args:
total*=num
print("Total: ",total)
add_number(1,2)
add_number(1,2,3,4)

Total: 2
Total: 24

#method overridding
class mom:
def call(self):
print("hello")
class son(mom):
def call(self):
print("Hi dude, whatsup")

s=son()
s.call()

Hi dude, whatsup

#method overloading

class calculator:
def add(self,*args):
total1=0
total2=0
total3=1
total4=1
for num in args:

total1+=num
total2-=num
total3*=num
total4/=num
print("\nPlease find the calculation for the arguments given")
print("Addition: ",total1)
print("Subtraction: ",total2)
print("Multiplication: ",total3)
print("Division: ",total4)

cal=calculator()
cal.add(1,2)
cal.add(1,2,3)
cal.add(1,2,3,4,5,6,7,8,9,10)

Please find the calculation for the arguments given


Addition: 3
Subtraction: -3
Multiplication: 2
Division: 0.5

Please find the calculation for the arguments given


Addition: 6
Subtraction: -6
Multiplication: 6
Division: 0.16666666666666666

Please find the calculation for the arguments given


Addition: 55
Subtraction: -55
Multiplication: 3628800
Division: 2.7557319223985894e-07

class animal:
def sound(self):
print("Animal Sound")
class dog(animal):
def sound(self):
super().sound()
print("Bow Bow")
dog().sound()
# class cat(animal):
# def sound(self):
# print("Meow Meow")

Animal Sound
Bow Bow

#Regular Functions

# | Pattern | Meaning | |

# | `\d` | Matches any digit (0-9) | |


# | `\D` | Matches any non-digit | |
# | `\w` | Matches any alphanumeric character (a-z, A-Z, 0-9, \_) | |
# | `\W` | Matches any non-alphanumeric character | |
# | `\s` | Matches any whitespace character (space, tab, newline) | |
# | `^` | Matches start of string | |
# | `$` | Matches end of string | |
# | `+` | One or more repetitions | |
# | `{n}` | Exactly n repetitions | |
# | `{n,m}` | Between n and m repetitions | |
# | `.` | Matches any character except newline | |
# | \` | \` | Acts like OR between patterns |

import re
text = "Hello Arun, your number is 9876543210. Email me at vishnu@example.com on 12-06-2025."
# Match digits
digits = re.findall(r'\d+', text)
print("Digits:", digits)
# Match non-digits
non_digits = re.findall(r'\D+', text)
print("Non-digits:", non_digits)
# Match words (alphanumeric sequences)
words = re.findall(r'\w+', text)
print("Words:", words)
# Match non-word characters (symbols, spaces, punctuations)
non_words = re.findall(r'\W+', text)
print("Non-words:", non_words)
# Match a 10-digit phone number pattern
phone = re.findall(r'\d{10}', text)
print("Phone number:", phone)
# Match an email pattern
email = re.findall(r'\w+@\w+\.\w+', text)
print("Email:", email)
# Match date pattern (like 12-06-2025)
date = re.findall(r'\d{2}-\d{2}-\d{4}', text)
print("Date:", date)
# Match if string starts with 'Hello'
starts_with_hello = re.match(r'Hello', text)
print("Starts with 'Hello':", bool(starts_with_hello))
# Match if string ends with a period
ends_with_dot = re.search(r'\.$', text)
print("Ends with dot:", bool(ends_with_dot))
# Replace all digits with 'X'
replace_digits = re.sub(r'\d', 'X', text)
print("Replace digits:", replace_digits)
# Split string by spaces
split_by_space = re.split(r'\s', text)
print("Split by space:", split_by_space)

Digits: ['9876543210', '12', '06', '2025']


Non-digits: ['Hello Arun, your number is ', '. Email me at vishnu@example.com on ', '-', '-', '.']
Words: ['Hello', 'Arun', 'your', 'number', 'is', '9876543210', 'Email', 'me', 'at', 'vishnu', 'example', 'com', 'on', '12', '06', '2
Non-words: [' ', ', ', ' ', ' ', ' ', '. ', ' ', ' ', ' ', '@', '.', ' ', ' ', '-', '-', '.']
Phone number: ['9876543210']
Email: ['vishnu@example.com']
Date: ['12-06-2025']
Starts with 'Hello': True
Ends with dot: True
Replace digits: Hello Arun, your number is XXXXXXXXXX. Email me at vishnu@example.com on XX-XX-XXXX.
Split by space: ['Hello', 'Arun,', 'your', 'number', 'is', '9876543210.', 'Email', 'me', 'at', 'vishnu@example.com', 'on', '12-06-20

#lambda arguments : expression

# Simple addition using lambda


add = lambda a, b: a + b
add(5, 3)
# Square of a number using lambda
square = lambda x: x * x
print("Square:", square(4))
# Find maximum of two numbers using lambda 
maximum = lambda a, b: a if a > b else b
print("Maximum:", maximum(10, 20))
# Lambda with map() to square each number in a list
nums = [1, 2, 3, 4, 5]
squares = list(map(lambda x: x**2, nums))
print("Squares using map():", squares)
# Lambda with filter() to get even numbers from a list
evens = list(filter(lambda x: x % 2 == 0, nums))
print("Even numbers using filter():", evens)
# Lambda with sorted() to sort list of tuples by second element
pairs = [(1, 5), (3, 2), (4, 9)]
sorted_pairs = sorted(pairs, key=lambda x: x[1])
print("Sorted pairs by second element:", sorted_pairs)
# Lambda with reduce() to find the product of all numbers
from functools import reduce
product = reduce(lambda x, y: x * y, nums)
print("Product of numbers using reduce():", product)

Square: 16
Maximum: 20
Squares using map(): [1, 4, 9, 16, 25]
Even numbers using filter(): [2, 4]
Sorted pairs by second element: [(3, 2), (1, 5), (4, 9)]
Product of numbers using reduce(): 120

You might also like