Professional Documents
Culture Documents
Kuliah #3 Dan #4 Alprog - Penyeleksian Dan Perulangan
Kuliah #3 Dan #4 Alprog - Penyeleksian Dan Perulangan
DASAR PYTHON,
PENYELEKSIAN &
PERULANGAN
DASAR-DASAR BAHASA
PYTHON
12/04/2023 2
Tujuan Pembelajaran
12/04/2023 3
Outline of this Lesson
12/04/2023 4
Brief History of Python
• The programming language Python was conceived in the late 1980s,[1] and
its implementation was started in December 1989[2] by Guido van Rossum
at CWI in the Netherlands as a successor to ABC capable of
exception handling and interfacing with the Amoeba operating system
• Python was named after the BBC TV show Monty Python's Flying Circus
• Python 2.0 was released on October 16, 2000
• Python 3.0, a major, backwards-incompatible release, was released on
December 3, 2008
• Written in C
• Open source
12/04/2023 5
Why Use Python (1)
12/04/2023 6
Why Use Python (2)
12/04/2023 7
Installing Python
• Install anaconda
12/04/2023 8
Example
codes (1)
12/04/2023 9
Example
codes (2)
12/04/2023 10
Operator Artimetika
12/04/2023 11
Example codes (3)
12/04/2023 12
Challenges (1)
12/04/2023 13
Challenges
(2)
12/04/2023 14
Outline
• Penyeleksian • Loop
• Penyeleksian Tunggal • Perulangan for
• Penyeleksian Ganda • Range
Penyeleksian multi • Perintah break
• Menentukan pilihan • Perintah continue
• Contoh-contoh kasus penyeleksian • Perintah pass
• Perulangan while
• Loop bersarang
Contoh kode
• For loop
• While loop
For loop Syntax
Contoh
range()
• Example 1: • Example 2:
for x in range(6): for x in range(2, 6):
print(x) print(x)
• Output: • Output:
0 2
1 3
2 4
3 5
4
5
Some Examples (2)
• Example 3: • Example 4:
for x in range(2, 30, 3): a=[“banana”, “apple”, ”jackfruit”]
print(x) for element in a:
print(element)
• Outputs:
2 • Outputs:
5
8 Banana
11 Apple
14 jackfruit
17
20
23
26
29
Some Examples (3)
Step Values for the range Function
• Example 3: Program requests
• amount deposited
• annual rate of interest
• then calculates balance after each quarter-year for four
quarters.
• Example 3, cont.
• Syntax
For-Else Flowchart
Example
for x in range(6):
print(x)
else:
print("Finally finished!")
0
1
2
3
4
5
Finally finished!
Break Statement
• The break statement, like in C, breaks out of the innermost enclosing for
or while loop.
• Loop statements may have an else clause; it is executed when the loop
terminates through exhaustion of the iterable (with for) or when the
condition becomes false (with while), but not when the loop is terminated
by a break statement.
• When used with a loop, the else clause has more in common with
the else clause of a try statement than it does with that of if statements: a
try statement’s else clause runs when no exception occurs, and a
loop’s else clause runs when no break occurs. For more on
the try statement and exceptions, see Handling Exceptions.
• Example: • Example 2:
fruits = ["apple", "banana", "cherry"] fruits = ["apple", "banana", "cherry"]
for x in fruits: for x in fruits:
print(x) if x == "banana":
if x == "banana": break
break print(x)
• Output:
• Ouput: apple
apple
banana
Continue Statement
• With the continue statement we can stop the current iteration of the loop,
and continue with the next
Some Examples
• Example 1:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)
• Output:
apple
cherry
• for x in "banana":
print(x)
Looping Through Arithmetic Progression of
Numbers
• Example 2: Program displays a table showing the population each year until
2018.
• Syntax
If the check fails, then the control won’t enter into the loop instead will get
transferred to the next statement. Whereas if the condition passes, then the
statements inside the loop shall execute.
This cycle would repeat itself until the while condition fails or returns false. When
such a situation would occur, the loop would break and pass control to the next
executable statement.
While Flowchart
Examples:
• i=1 • i=1
while i <= 6: while i < 6:
print(i) print(i)
i += 1 if i == 3:
• Output: break
i += 1
1
• Output
2
1
3
2
4
3
5
6
• Contoh:
i=1 • Output:
while i < 6: 1
2
print(i) 3
i += 1 4
else: 5
i is no longer less than 6
print("i is no longer less than 6")
• i=0
while i < 6:
i += 1
if i == 3:
continue
The while Loop
FIGURE 3.24
Program Containing
an Infinite Loop.
for x in adj:
for y in fruits:
print(x, y)
red apple
red banana
red cherry
big apple
big banana
big cherry
tasty apple
tasty banana
tasty cherry
Nested for Loops
• for loops cannot be empty, but if you for some reason have a for loop with
no content, put in the pass statement to avoid getting an error.
• for x in [0, 1, 2]:
pass
Latihan