Lecture 02 Python II
Lecture 02 Python II
PYTHON PROGRAMMING
2
FUNCTIONS
3
FUNCTIONS
• Defining a function
• The keyword def introduces a function definition.
• It must be followed by the function name and the
parenthesized list of formal parameters.
• The statements that form the body of the function start at
the next line, and must be indented.
def firstFcn():
print("Printing from a function.")
def secondFcn(name):
print("You are " + name)
• Return a value
def thirdFcn(n, m):
p = n+ 2*m
return p
5
arguments1.py
CALLING A FUNCTION
a = thirdFcn(1,2)
print(thirdFcn(2,3))
6
arguments1.py
ARGUMENTS IN FUNCTIONS
defaultArgFcn("Joseph")
defaultArgFcn("Mary", "Chicago")
You are Joseph in HK
You are Mary in Chicago 7
TYPES OF ARGUMENTS
Positional arguments
• Positional arguments are values that are passed into a function based on the order in
which the parameters were listed during the function definition.
defaultArgFcn("John", "Hong Kong")
>> Hello John, you live in Hong Kong
Keyword arguments
• Keyword arguments (or named arguments) are values that, when passed into a
function, are identifiable by specific parameter names. A keyword argument is
preceded by a parameter and the assignment operator, = .
defaultArgFcn(name ="John", area ="Hong Kong")
>> Hello John, you live in Hong Kong
defaultArgFcn(area ="KLN", name = "Paul")
8
>> Hello Paul, you live in KLN
IMPORT STATEMENT
9
TUPLE
10
TUPLE
• Access items
• Tuple items by referring to the index number, inside square
brackets:
print(t1[2])
>> cake
print(t2[1:3])
>>(2, 3)
12
BASIC TUPLES OPERATIONS
13
INDEXING, SLICING, MATRIXES, MIN
AND MAX
t1 = ("apple", "box", "cake", "disk")
t2 = (1, 2, 3, 4, 5 )
14
ARRAYS
15
ARRAY DEFINITION
Arr1
• Array is the simplest data structure where each data element can be
randomly accessed by using its index number.
16
ARRAYS IN PYTHON
• Python does not have built-in support for Arrays, but Lists can be used
instead.
• Python’s lists are more flexible than above-mentioned arrays
• Arrays are available via libraries such as “array” and “numpy”
17
ACCESS THE ELEMENTS OF A LIST
print(sports[2])
>>badminton
18
CHANGE AN ELEMENT IN A LIST
>> tennis
sports[-1]
>>squash
sports[-3]
>>basketball 19
LENGTH OF A LIST
• Example:
len(sports)
>> 4
20
RANGE OF INDEX
• We cannot use index greater than or equal to the length of the list.
• For example, we have created a list with length 4.
• Then the valid expressions to access the elements of this list will be
sports[0] to sports[3] (length-1).
• Whenever you used the value greater than or equal to the length of the
list
(e.g. sports[9] or sports[20]), it gives
IndexError: list index out of range.
21
LOOP THROUGH A LIST
• To show the all content in a list, just print the variable name.
print(sports)
>> ['soccer', 'basketball', 'badminton', 'squash’]
• The following example outputs all elements in the sports list with for loop:
• Example
for a in sports:
print(a)
>>
soccer
basketball
tennis
Squash 22
LIST OPERATIONS
sports
Remove the first occurrence of the element with the specified value
sports.remove("squash")
sports
>>['soccer', 'basketball', 'swimming’]
24
LIST COPYING
25
LIST COPYING
2. Shallow copy
A shallow copy creates a new object which stores the reference of the original
elements. For list of strings, changes in one copy will not be reflected in the other copy.
26
ASSIGNMENT VS SHALLOW COPY
28
CLASS AND OBJECTS
Object
class
Rockie
Coolman
29
Smarty
CLASS DEFINITIONS
30
CLASS AND OBJECTS
31
DEFINE A CLASS IN PYTHON
• All class definitions start with the class keyword, which is followed by the name of
the class and a colon (:).
• By convention Python class names are written in CapitalizedWords notation, such as Cinema,
WineGlass, OfficeForIT.
• The properties that all Dog objects must have are defined in a method
called __init__().
• Every time a new Dog object is created, .__init__() sets the initial state of the object by
assigning the values of the object’s properties. That is, __init__() initializes each new
instance of the class.
• You can give __init__() any number of parameters, but the first parameter will always be a
variable called self.
• When a new class instance is created, the instance is automatically passed to the self
parameter in __init__() so that new attributes can be defined on the object.
class Dog:
def __init__(self, name, age):
self.name = name
32
self.age = age oop1.py
INSTANTIATE AN OBJECT IN PYTHON
d1 = Dog("Rockie", 5)
d2 = Dog("Coolman", 2)
33
ACCESS INSTANCE ATTRIBUTES
d2.name Coolman
d2.age 2
34
INSTANCE METHODS
class Dog:
35
oop2.py