OBJECT ORIENTED PROGRAMMING
WITH PYTHON
Second Class
1st Semester
Tuples
Unpacking
Dictionarires
Functions
Parameters
Keyword Arguments
Return Statement
TUPLES
Tuples are similar to list, so we can use them to store a list
of items but unlike lists we can't modify them (can’t add
new items, can’t remove existing item), thus tuples is
immutable.
Numbers=(1,2,3) Here the methods
Numbers. related to tuples will be
listed (count and index)
count to count the number of occurrence of an item
Index to find the index of the first occurrence if an item
Numbers=(1,2,3)
Numbers[0]=10
print(Numbers[0])
UNPACKING
A powerful feature in Python coding
coordinateds=(1,2,3)
coordinateds[0] * coordinateds[1] * coordinateds[2]
X= coordinateds[0]
Y= coordinateds[1]
Z= coordinateds[2]
In Python we can use unpacking feature too unpack list to variables
X,Y,Z= coordinateds
Note:
This is not limited to Tuples it’s also
can be used with lists
DICTIONATRIES
Use dictionary in situations were we want to store information that come at key-value pair
Example:
Think of a customer, a customer has attributes (information) like name, Email, phone,
address and so on
Each of this attribute has a value key like
name : Jonh Smith
Email : john@[Link]
Phone : 008645876
Here we have a bunch of key-value pairs
Now using the Dictionary we can store key-value pairs
DICTIONATRIES
Customer= {
“name” : “Jonh Smith”
“ Email” : “john@[Link]”
“age” : 30
“Is_verified”=True
}
We can reach any item using square brackets [ ]
print (customer[“name”]) #Returns John Smith
print (customer[“birthdate“]) #Returns error message
print (customer[“Name“]) #Returns error message
DICTIONATRIES
To solve the error problem we can use get()
print ([Link](“Name”)) #Returns None
Note: None is an object represents the absence of value.
Also we can use the default value like
print ([Link](“birthdate“,”Jan 1 1980”) #Returns Jan 1 1980
To update the key value
customer[“name”] = “Jack Smith”
To add a new key
customer[“birthdate”] = “Jan 1 1980”
EXERCISE
Phone: 1234
Write a program that translates the numbers(digits) into words like
One Two Three Four
Solution
phone=input(“Phone: “)
digits_mapping={
“1” : ”One”,
“2” : “Two”
“3” :”Three”
“4”: “Four”
}
for ch in phone:
output+= digits_mapping.get(ch,”!”) +” “ Phone: 12345
print(output) One Two Three Four !
EMOJI CONVERTER
Write an application that maps characters :) to and :( to
Solution
message=input("> ") I am happy :) ---> I am happy
words = [Link](' ') I am sad ): ---> I am sad
emojis = {
":)":"😊",
":(":"😥 "
}
output =‘ ‘
for word in words:
print(word)
output+=[Link](word,word)+' '
print(output)
FUNCTIONS
The function is the better way to organize our code, we sometimes need to breakup our
code into smaller, manageable and more maintainable chunks.
When building large complex programs we should break up out code into smaller
reusable chunks which we call function to better organize our code.
Let us write a simple program for printing a greeting messages
print (‘Hi there!’)
print(‘ Welcome to our class’)
If we need these printing in another programs, we can put them in function that we can
reuse.
FUNCTIONS
def greet_member():
print (‘Hi there!’)
print(‘ Welcome to our class’)
print(“Start”)
greet_member()
print(“Finish”)
Function’s name should be lower characters
If multiple words separate them with underscore _
Always and always use meaningful descriptive names
Use parenthesis () followed by :
PARAMETERS
We can add the name of the user to add it to the greet messages
def greet_member(name):
print (f‘Hi {name} !’) Start
print(‘ Welcome to our class’) Hi John
Welcome to our class
Finish
print(“Start”) Start
greet_member(“John”) Hi Mary
greet_member(“Mary”) Welcome to our class
print(“Finish”) Finish
KEYWORD ARGUMENTS
def greet_member(first_name,last_name):
print (f‘Hi {fist_name} {last_name} !’)
print(‘ Welcome to our class’)
print(“Start”)
greet_member(last_name=‘Smith’,first_name=‘Sara’)
print(“Finish”)
Here the sequence of the keyword arguments is not important, not like
the Positional arguments mentioned earlier
KEYWORD ARGUMENTS
Note1:
We can use the keyword arguments when we have set of numerical arguments like
shipping number and discount, in order to make the code more readable.
calc_cost (total=50,shipping=5,discount=0.1)
Note2:
We can use the keyword arguments and the positional arguments at the same time, only if
the positional arguments is mentioned first.
greet_member(last_name=‘Smith’,first_name=‘Sara’)
RETURN STATEMENT
Return function used to return value of the function, now let’s write a function
that calculate the square of the number
def square(number)
return number*number
result= square(3)
print(result)
def square(number)
return number*number
print(square(3))
RETURN STATEMENT
Now, if we removed the return from the previous function and execute
def square(number)
return number*number
print(square(3)) None
From this we conclude that, by default all functions in Python return None. And
we can change that using return statement
CREATING REUSABLE FUNCTIONS
Exercise:
Reorganize the Emoji's Converter code to function, since this program can be used in
chat application, email applications and so on.
def emoji_converter(message):
words = [Link](' ')
emojis = {
":)":"😊",
":(":“"
}
output =""
for word in words:
output += [Link](word, word) + ' '
return output
#output=""
message=input("> ")
emoji_message=emoji_converter(message)
print(emoji_message)