03 Flow Control Di Python
03 Flow Control Di Python
Python
Praktikum Komputasi
Minggu III
Semester II – Tahun Ajaran 2020/2021
Tujuan pembelajaran
• Mampu mempergunakan IF branching
• Mampu mempergunakan FOR-loops dan WHILE-loops.
• Mampu membuat dan mempergunakan function
IF branching
Conditional branch
If / Elif / Else
• If, elif, and else statements are used to implement conditional program
behavior
• Syntax:
if Boolean_value:
…some code
elif Boolean_value:
…some other code
else:
…more code
i = 1 1
while i < 6: 2
print(i) 3
i += 1 4
5
i += 1 is equal to i = i + 1
While loops example
Write a Python program to get the Fibonacci series between 0 to 50.
Note: The Fibonacci Sequence is the series of numbers :
0, 1, 1, 2, 3, 5, 8, 13, 21, ....
Every next number is found by adding up the two numbers before it.
1
x,y=0,1 1
2
3
while y<50: 5
8
print(y) 13
x,y = y,x+y 21
34
For loops
For loops
• for loops are a little different. They loop
through a collection of things.
• The for loop syntax has a collection and a
code block.
• Each element in the collection is accessed in
order by a reference variable
• Each element can be used in the code block.
Processing lists element-by-element
• A for loop is a convenient way to process every element in a list.
• There are several ways:
• Loop over the list elements
• Loop over a list of index values and access the list by index
• Do both at the same time
For loops example
• Write a Python program to find those numbers which are divisible by
7 and multiple of 5, between 1500 and 2700 (both included).
For loops example
import numpy as np
nl=[] # Membentuk list kosong
daftar = np.arange(1500,2701)
for x in daftar:
if (x%7==0) and (x%5==0):
nl.append(x)
print (nl)
[1505, 1540, 1575, 1610, 1645, 1680, 1715, 1750, 1785, 1820,
1855, 1890, 1925, 1960, 1995, 2030, 2065, 2100, 2135, 2170,
2205, 2240, 2275, 2310, 2345, 2380, 2415, 2450, 2485, 2520,
2555, 2590, 2625, 2660, 2695]
The range() function
• The range() function auto-generates sequences of numbers that can be used for
indexing into lists.
[1505, 1540, 1575, 1610, 1645, 1680, 1715, 1750, 1785, 1820,
1855, 1890, 1925, 1960, 1995, 2030, 2065, 2100, 2135, 2170,
2205, 2240, 2275, 2310, 2345, 2380, 2415, 2450, 2485, 2520,
2555, 2590, 2625, 2660, 2695]
Exercises
IF/WHILE/FOR
Exercise 1
Jika diketahui dengan bilangan riil. Hitunglah nilai yang memenuhi
persamaan:
Plot grafik
Exercise 1
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-5,5,101)
y = np.zeros(101)
for i in range(101):
if x[i]<-2:
y[i] = x[i]**3
elif x[i] <=3:
y[i] = 2*x[i]**2-16
else:
y[i] = 32-10*x[i]
# Membuat daftar berisi x dan y
hasil = np.zeros([101,2])
hasil[:,0], hasil[:,1] =x, y
# Cetak hasil perhitungan
print('Hasil perhitungan:')
header = ['x','y']
garis = '-'*22
print(garis)
print('{:^10s} {:^10s}'.format(*header))
print(garis)
for i in range(0,101,5): # Tampilkan setiap kelipatan 0.5
print('{:>7.2f} {:>12.4f}'.format(*hasil[i,:]))
print(garis)
# Plotting
plt.plot(x,y,'.-r')
plt.title('x vs. y')
plt.xlabel('Nilai x')
plt.ylabel('Nilai y')
Hasil perhitungan:
----------------------
x y
----------------------
-5.00 -125.0000
-4.50 -91.1250
-4.00 -64.0000
-3.50 -42.8750
-3.00 -27.0000
-2.50 -15.6250
-2.00 -8.0000
-1.50 -11.5000
-1.00 -14.0000
-0.50 -15.5000
0.00 -16.0000
0.50 -15.5000
1.00 -14.0000
1.50 -11.5000
2.00 -8.0000
2.50 -3.5000
3.00 2.0000
3.50 -3.0000
4.00 -8.0000
4.50 -13.0000
5.00 -18.0000
----------------------
Exercise 2
• Hitunglah nilai . Perhitungan dimulai dengan dengan inkremen
perhitungan, . Perhitungan dihentikan saat nilai
• Plot hasil perhitungannya!
Exercise 2
import matplotlib.pyplot as plt # Plotting hasil
import numpy as np plt.plot(t,y,'or')
ti = 0 plt.xlabel('t')
t = [ti] # Membuat list plt.ylabel('y')
yi = np.exp(2*ti)*np.cos(4*ti)
y = [yi] # Membuat list
while yi>0.001:
ti += 0.01
t.append(ti) # Menambah anggota list
yi = np.exp(2*ti)*np.cos(4*ti)
y.append(yi) # Menambah anggota list
Exercise 2 (alternative code)
import numpy as np # Plotting hasil
import matplotlib.pyplot as plt plt.plot(t,y,'or')
ti = 0 plt.xlabel('t')
t = np.array([ti]) # Membuat array plt.ylabel('y')
yi = np.exp(2*ti)*np.cos(4*ti)
y = np.array([yi]) # Membuat array
while yi>0.001:
ti += 0.01
t = np.append(t,ti) # Menambah anggota array
yi = np.exp(2*ti)*np.cos(4*ti)
y = np.append(y,yi) # Menambah anggota array
Exercise 2