Phyton
Phyton
Python – Part 1
digitalent.kominfo.go.id
Outline
• Python: Overview
• Types, expressions, variables
• String operations
• Lists, Tuples
• Sets
• Dictionaries
• Conditions and Branching
Programming Language Not Python the snake
Cross platform
Freely usable including
for commercial purpose
Why Python?
Python is popular
versus
Python is powerful
• Many big names use Python in their product
Python is versatile
• Image processing using Python - https://opencv.org
• Game development using Python - https://www.pygame.org/
• Data visualization using Python - https://matplotlib.org
• Natural language processing using Python - https://www.nltk.org
• Deep learning using Python -
https://github.com/tensorflow/tensorflow
• Web development using Python - https://www.djangoproject.com
Let’s get started …
First Python program: Revisited
• You’ve seen your first few Python programs when we discussed Jupyter Notebook
in the previous session.
• So, let’s open a new Notebook.
• Make sure Jupyter Notebook has been set up in your computer before continuing.
• The first thing to note, a Python code line that begins with a hash symbol will be
considered a comment, which will not be executed.
Syntax error message
• A mistake in writing down a Python command will yield an error
message when executed.
Semantic error
• If there is a mistake in the program logic, Python will not tell you a
mistake if no syntax error is detected.
• E.g., suppose the intention is to print ‘Hello Python 101’, but we accidentally
write ‘Hello Python 102’, then Python won’t complain although the code is
wrong.
Outline
• Python: Overview
• Types, Expressions, Variables
• String operations
• Lists, Tuples
• Sets
• Dictionaries
• Conditions and Branching
Python types
Python types Example expression
int 11, -14, 0, 2
float 21.3201, 0.0, 0.8, -2.34
str “Hello Python 101”
S e m a r a n g T a w a n g
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
S e m a r a n g T a w a n g
-15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
String length
• Length of a string can be obtained via len() function
String slicing
• You can get a substring by slicing.
• s[m:n] is a substring of s taken from character at index m until character at index n-1
• If n ≤ m, then the slicing yields an empty sequence.
S e m a r a n g T a w a n g
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
-15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
String striding
• s[m:n:p] operates like s[m:n], but starting from index m, we move p
steps at a time from left to right
S e m a r a n g T a w a n g
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
-15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
String slicing and striding default value
• Omitting m, n, or p in the expression s[m:n] and s[m:n:p] causes Python to use their
default values: m = 0, n = len(s), p = 1
S e m a r a n g T a w a n g
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
-15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
String concatenation
• We use “+” to concatenate strings
String replication
• Multiply a string with a number (using “*”) yields a new string
containing the replication of the old string.
Strings are immutable!
• String is immutable: you cannot change • But you can reassign the
the value of characters in a string. variable to a new string
String: escape sequences
• Certain characters are difficult to input (e.g., for print() function), so we
use prefix it with a backslash “\” to use them.
• E.g., newline, tab, and the backslash itself.
• Printing backslash character without escaping can be done using raw string notation
(with the ‘r’ prefix)
String: sequence methods and string methods
• The previous methods/operations (indexing, slicing, striding) on
strings also work on other types of sequences (discussed later)
• Other than those, there are also methods specific to strings.
• Strings are immutable. So, we apply a method to a string A, the
method will return a new string B.
A Method B
String: Get uppercase version of a string
A = “Surabaya is the city of heroes”
• upper() returns uppercase version of
the string
upper()
S e m a r a n g T a w a n g
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
More string operations …
• See https://docs.python.org/3/library/stdtypes.html#string-methods
Outline
• Python: Overview
• Types, Expressions, Variables
• String operations
• Lists, Tuples
• Sets
• Dictionaries
• Conditions and Branching
Tuples
• Tuple is an ordered sequence
• Tuples are written as comma-separated items within parentheses.
0 “Alice”
1 85
2 3.8
Tuple: accessing elements (negative index)
-3 “Alice”
-2 85
-1 3.8
Tuple concatenation
• Like string, tuple can be concatenated resulting in a new tuple.
0 1 2 3 4
Tuple slicing
• We can slice tuples like string
grades
(95, 85, 90, 72, 68, 82, 85, 90, 75)
grades1
Tuples are immutable (like strings)
grades
(95, 85, 90, 72, 68, 82, 85, 90, 75)
grades1
grades (95, 85, 90, 72, 68, 82, 85, 90, 75)
nt[2][1][0]
nt[2][1][1] nt[4][1][0]
nt[4][1][1]
'B’
'a’ 5 7
Lists
• Lists are also ordered sequence, but mutable (unlike tuples or strings)
• Lists are written inside square brackets
• Lists can be nested, containing other lists
• In fact, lists and tuples can containing anything including lists and tuples
Lists: Accessing elements
• Elements of lists can be accessed using index in the same way as
tuples or strings.
Lst[3] is changed
• Creating empty set can only be done using set() function, not curly braces!
Not a set!
Set: adding elements
“Bandung”, “Bogor”, “Depok”
“Bandung”,
“Bogor”, “Depok”
Set: check if the set contain the given
element
“Bandung”, “Bogor”,
“Depok”
Set intersection
“Bogor”, “Depok”
Set union
“Bogor”, “Depok”,
“Bogor”, “Depok”
“Jakarta”
Subset test
List Dictionary
Index Element Key: is an index by label Value
“Jakarta” 10.1
“Surabaya” 3.4
“Bogor” 1.1
“Depok” 1.8
“Bandung” 2.5
“Medan” 2.2
“Makassar” 1.4
“Denpasar” 0.9
Dictionary: accessing elements using key
“Jakarta” 10.1
“Surabaya” 3.4
“Bogor” 1.1
“Depok” 1.8
“Bandung” 2.5
“Medan” 2.2
“Makassar” 1.4
“Denpasar” 0.9
Dictionary: adding elements
“Jakarta” 10.1 “Jakarta” 10.1
“Surabaya” 3.4 “Surabaya” 3.4
“Bogor” 1.1 “Bogor” 1.1
“Depok” 1.8 “Depok” 1.8
“Bandung” 2.5 “Bandung” 2.5
“Medan” 2.2 “Medan” 2.2
“Makassar” 1.4 “Makassar” 1.4
“Denpasar” 0.9 “Denpasar” 0.9
“Ambon” 0.4
Dictionary: adding elements
“Jakarta” 10.1
“Surabaya” 3.4
“Bogor” 1.1
“Depok” 1.8
“Bandung” 2.5
“Medan” 2.2
“Makassar” 1.4
“Denpasar” 0.9
Dictionary: deleting elements
“Jakarta” 10.1 “Jakarta” 10.1
“Surabaya” 3.4 “Bogor” 1.1
“Bogor” 1.1 “Depok” 1.8
“Depok” 1.8 “Bandung” 2.5
“Bandung” 2.5 “Medan” 2.2
“Medan” 2.2 “Makassar” 1.4
“Makassar” 1.4 “Denpasar” 0.9
“Denpasar” 0.9 “Ambon” 0.4
“Ambon” 0.4
Dictionary: deleting elements
“Jakarta” 10.1
“Surabaya” 3.4
“Bogor” 1.1
“Depok” 1.8
“Bandung” 2.5
“Medan” 2.2
“Makassar” 1.4
“Denpasar” 0.9
“Ambon” 0.4
Dictionary: check if element is in dictionary
“Jakarta” 10.1
“Bogor” 1.1
“Depok” 1.8
“Bandung” 2.5
“Medan” 2.2
“Makassar” 1.4
“Denpasar” 0.9
“Ambon” 0.4
Dictionary: get all keys using keys() method
“Jakarta” 10.1
“Bogor” 1.1
“Depok” 1.8
“Bandung” 2.5
“Medan” 2.2
“Makassar” 1.4
• keys() returns a list-like object containing the
“Denpasar” 0.9
dictionary’s keys (which can be converted to a
“Ambon” 0.4 list or other collection
Dictionary: get all keys using keys() method
“Jakarta” 10.1
“Bogor” 1.1
“Depok” 1.8
“Bandung” 2.5
“Medan” 2.2
“Makassar” 1.4
“Denpasar” 0.9
“Ambon” 0.4
Dictionary: get all values using values() method
“Jakarta” 10.1 • values() method is similar, but for getting all
“Bogor” 1.1 values in the dictionary
“Depok” 1.8
“Bandung” 2.5
“Medan” 2.2
“Makassar” 1.4
“Denpasar” 0.9
“Ambon” 0.4
More dictionary methods …
• See https://docs.python.org/3/library/stdtypes.html#mapping-types-
dict
Outline
• Python: Overview
• Types, Expressions, Variables
• String operations
• Lists, Tuples
• Sets
• Dictionaries
• Conditions and Branching
Comparison operators
Operands Comparison
(Values) Boolean
Operator
Comparison: Equality
5 x == 8 Boolean
Comparison: Equality
5 == 8? False
Comparison: Equality
8 x == 8 Boolean
Comparison: Equality
8 == 8? True
Comparison: Greater than, Less than, etc.
because 1 is equal to 1
Comparing strings
• Two strings (or generally, sequences) A and B are the same if their
length is the same and for each position i, A[i] is equal to B[i].
The if Statement
• Suppose entrance to a tourist attraction is only given to those whose age is at
most than 12 years old.
• E.g., if age is 13, 14, or more, then entrance is not granted and the person just moves
on;
• if age is 12, 11, or less, then entrance is granted and (s)he moves on after enjoying
the ride.
• We can write this branching using if statement
if CONDITION:
do_something_only_when_condition_holds
everyone_do_something
• After the if-condition, the statements that we want to execute only when the if-
condition is true MUST be written with an indentation!
The if Statement
Because 11 is less than or equal to 12, if-
condition holds
This is executed
This is NOT executed
This is executed, regardless of the if-condition
The else
statement
Executed
NOT executed
NOT executed
elif-condition is True,
13 is equal to 13
Executed
NOT executed
elif-condition is False,
15 is not equal to 13
Executed
Boolean Logic
operands/ Boolean
values Operator
Logic operator: not
A B A or B
False False False
False True True
True False True
True True True
Logic operator: or
The condition is true when pub_year is a
number from {…, 1978, 1979, 1990, 1991, …}
A B A and B
False False False
False True False
True False False
True True True
Logic operator: and
The condition is true when pub_year is a
number from {1980, 1981, …, 1989}
digitalent.kominfo
digitalent.kominfo
DTS_kominfo
Digital Talent Scholarship 2019
digitalent.kominfo.go.id