Python Basic
�Soo Kyung Kim
Python Data structure:�String, List, Sets, Tuples, Dictionary
2
String Type
3
List Type
4
Lists
0
1
2
3
…
29
Seun Kim
Yeobin Paik
Seoyeon Ye
Minsuh Joo
5
Lists: Access and Assign
The first item from the back
6
Lists: Modifying Elements
Soo Kim
0
1
2
3
…
29
Seun Kim
Yeobin Paik
Seoyeon Ye
Minsuh Joo
7
Lists: Type
8
Slicing
9
Copy vs. Alias
del students_copy[-1]?
students is not affected!
del students_alias[-1]?
students is affected!
10
Parameters are Aliases!
11
Sets
→ True
12
Sets
13
Tuples
to differentiate from arithmetic operations
14
Tuples
15
Dictionaries
16
Dictionaries
17
Dictionaries
for <variable> in <dictionary>:
<block>
Jihye got grade A+
Sunghyun got grade A+
Jose got grade A0
Sora got grade B
18
Python Function
19
Defining Your Own Functions
�
def <function_name>(<parameters>):
<function_body>
def sum(a, b):
return a + b
20
Local Variables
def convert_to_fahrenheit(celsius):
a = 9 / 5
b = 32
return celsius * a + b
21
Namespace
22
Memory Models for Functions
23
Memory Models for Functions
So… what happens when you call a function?
24
Terminology & Overview
def convert_to_fahrenheit(celsius):
a = 9 / 5
b = 32
return celsius * a + b
convert_to_fahrenheit(36.5)
argument (values)
local variables
parameters
25
Memory Model Example
frames
def doubling(x):
return 2*x
x = 5
x = doubling(x+5)
objects
frames for namespaces
memory objects
26
Memory Model Example
frames
def doubling(x):
return 2*x
x = 5
x = doubling(x+5)
objects
global
27
Memory Model Example
frames
def doubling(x):
return 2*x
x = 5
x = doubling(x+5)
objects
global
func doubling()
doubling()
28
Memory Model Example
frames
def doubling(x):
return 2*x
x = 5
x = doubling(x+5)
objects
global
func doubling()
doubling()
5
x
29
Memory Model Example
frames
def doubling(x):
return 2*x
x = 5
x = doubling(x+5)
objects
global
func doubling()
doubling()
5
x
10
30
Memory Model Example
frames
def doubling(x):
return 2*x
x = 5
x = doubling(x+5)
objects
global
func doubling()
doubling()
5
x
10
doubling
31
Memory Model Example
frames
def doubling(x):
return 2*x
x = 5
x = doubling(x+5)
objects
global
func doubling()
doubling()
5
x
10
doubling
x
32
Memory Model Example
frames
def doubling(x):
return 2*x
x = 5
x = doubling(x+5)
objects
global
func doubling()
doubling()
5
x
10
doubling
x
20
33
Memory Model Example
frames
def doubling(x):
return 2*x
x = 5
x = doubling(x+5)
objects
global
func doubling()
doubling()
5
x
10
doubling
x
20
return
34
Memory Model Example
frames
def doubling(x):
return 2*x
x = 5
x = doubling(x+5)
objects
global
func doubling()
doubling()
5
x
20
35
Function Design
36
Guidelines for Designing New Functions
37
Guidelines for Designing New Functions
38
Function Design Example
optional, but useful for readability
39
Function Design Example (cont’d)
In Python, characters after # in the same line are ignored by the interpreter.
40
Function Design Example (cont’d)
def days_difference(day1: int, day2: int) -> int:
# Return the number of days between day1 and day2, which are both in the range 1-365 (thus indicating the day of the year)
return abs(day2 - day1)
41
Using Modules
42
Modules
43
Module: What Happens when Importing?
44
Module: What Happens when Importing?
my_math
45
Module: Warning
46
Module: Importing Specific Things
47
Module: Importing Specific Things
48
Module: Importing Specific Things
49
Module: Importing Specific Things
50
Writing a Module
51
Making Your Own Module
a = 5.0
b = 9.0
c = 32.0
def convert_to_celsius(fahrenheit: float) -> float:
return (fahrenheit - c) * a / b
def convert_to_fahrenheit(celsius: float) -> float:
return (celsius * b) / a + c
52
Making Your Own Module
53
Classes
54
Class
Class Person
Attributes: age, gender, name, address, nationality
Methods: change_name(), move(), immigrate_to()
55
Class Object
56
Built-in Classes
57