Python Lecture Hnin
Python Lecture Hnin
String literal
• triple quoted string support multiple line
• escape codes are embed when use triple quoted string
For Example
stringTest = ‘’’ Test
Susan’s car ‘’’’
escape sequence (\n, \’) is embedded.
variable name
• variable name can be letters, digit or _
• must start with letter
• some words are reserved – True, False, lamda, glboal, elif, assert, etc..
• python is case sensitive for variable name width != Width
• meaningful names are important for any programming languages
• if long variable name – practice (underscore or camel) case . For example: data_size or dataSize
print
• print function will print the information both static and dynamic.both Single or double quote can be used for
print
print (‘hello’)
print (“input’s”)
print (“Hi”, person)
• print with keyword sperator
print (“Hi”, person, ‘!’, sep = ‘ ’)
The above function will remove all blank from print function.
input-output
• direct assign to variable and ask user to enter the value for input
For example: person = input (‘enter your name’) # prompt format
print (‘hello’,person)
conversion from string input to integer
x = int(input("Enter an integer: "))
y = int(input("Enter another integer: "))
print(’The sum of ’, x, ’ and ’, y, ’ is ’, x+y, ’.’, sep=’’)
def functionname():
functionname()
function-parameter and local scope
• parameter can be defined for function and paramter can be used as local copy
for example: def functionname(num)
accept num as paramter and calling program shall pass 1 argument when call the function
num is declared as local scope meaning outside function can’t be used ‘num’ variable. same for
anything declared inside function
def functionname():
functionname()
List Type
• Lists are ordered sequences of arbitrary data.
• List are mutable (length can be changed and elements can be substituted)
• List defined with square bracket
print(“Result:”, s)
Dictionaries
• dictionaries is key value paired built by Python
• variable can be declared as dictionary
burglish = dict():
burglish[‘Youk p’] = ’Arrive’
burglish[‘Nay kg lar’] = “R u well’
print(burglish[‘Youk p’])
Dictionaries with string formatting
burglish = dict():
burglish[‘Youk p’] = ’Arrive’
burglish[‘Nay kg lar’] = “R u well’
print(burglish[‘Youk p’])
burglishFormat = “From burglish to english: {0},{1},{2}..”
withSubstitutions = burglishFormat.format(0 = ‘Youk p’, 1 = ‘Nay kg lar’)
print(withSubstitutions)
used object.methodname(paramters)
Dictionaries with ** notation
burglish = dict():
burglish[‘Youk p’] = ’Arrive’
burglish[‘Nay kg lar’] = “R u well’
print(burglish[‘Youk p’])
burglishFormat = “From burglish to english: {0},{1},{2}..”
withSubstitutions = burglishFormat.format(0 = ‘Youk p’, 1 = ‘Nay kg lar’)
print(withSubstitutions)
used object.methodname(paramters)
Object Oriented Programming with Python
• Python is object orientation language
• Better than Procedural language Car (class)
• objects has attributes & methods are essential part of Python wheel
• Object simplfied complex model Steering
Engine
• object is an instance of a class ( 1.. n)
roof
• Essential of OOP Inheritance, Polymorphism (many forms), Abstraction
(Define - implementation own), and Encapsulation (hide)
Forward()
Backward()
Break()
Class
Cat • classes have methods and attributes
• Class is a blueprint of objects
Attributes: • Object is an instance of class.
Color
Type class Cat(object):
pass
Methods:
Eat
brownie = Cat()
Sleep
print(brownie)
Poop
nyomi = Cat()
print (nyomi)
Data Modeling
Encapsulation This property hides unnecessary details and makes it easier to manage the program structure. Each object’s implementation
and state are hidden behind well-defined boundaries and that provides a clean and simple interface for working with them. One way to
accomplish this is by making the data private.
Inheritance Inheritance, also called generalization, allows us to capture a hierarchal relationship between classes and objects. For instance, a
cat is generalization/inheritance of animal. Inheritance is very useful from a code reuse perspective.
Abstraction This property allows us to hide the details and expose only the essential features of a concept or object. Example: knows how
function works and hide details of how it works. A person driving a scooter knows that on pressing a horn, sound is emitted, but he has no idea
about how the sound is actually generated on pressing the horn.
Polymorphism Poly-morphism means many forms. That is, a thing or action is present in different forms or ways. One good example of
polymorphism is constructor overloading in classes. For example, an animal barks but dog and cat will have different sounds.
Code Sample
#code to explain simple class Cat.EyeColor=”White” Cat (class)
class Cat(object): self.color
def getColor (self): Brownie (object 1) self.age
return self.color
self.color getColor()
def setColor (self, colorcode):
getColor() setColor(self, colorCode)
self.color = colorcode
setColor(self, brown)
Nyomi (object 2)
brownie = Cat() //instance1 self.color
brownie.setColor("brown") getColor()
print(brownie.getColor()) setColor(self, black)
def __init__(self):
self.color = 'White' brownie = Cat() #instance1
brownie.setColor("brown")
print(brownie.getColor())
Programmer Tester
Salary Hrsalary
getMonthlySalary getHrSalary
Multi-inheritance
Employee Student
Name SchoolName
counter degree
getName getter/setter
getCounter getter/setter
Programmer Tester
Salary Hrsalary
getMonthlySalary getHrSalary
Polymorphism
• Represents many forms
• Conform same function
• Implement different
• different signature, different data type could be
Dog Barks
Meows
Cat
Polymorphism with Inheritance:
Animal
Dog Cat
Encapsulation
Black Animal
Name
Age
Color
breed
Dog
type