Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

03 Flow Control Di Python

The document discusses flow control in Python including IF branching, WHILE loops, and FOR loops. It provides examples and explanations of using conditional statements like if/elif/else and loops to control program flow. It also covers topics like indentation, range() function, and exercises involving calculating values that satisfy equations and plotting results.

Uploaded by

WidiantiDensiana
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

03 Flow Control Di Python

The document discusses flow control in Python including IF branching, WHILE loops, and FOR loops. It provides examples and explanations of using conditional statements like if/elif/else and loops to control program flow. It also covers topics like indentation, range() function, and exercises involving calculating values that satisfy equations and plotting results.

Uploaded by

WidiantiDensiana
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Flow Control in

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

Boolean  TRUE or FALSE statement


If / Elif / Else
• The Use of Indentation
• Python uses whitespace (spaces or tabs) to define code blocks.
• Code blocks are logical groupings of commands. They are always
preceded by a colon :
If / Elif / Else
• Python knows a code block has ended when the indentation is
removed.
• Code blocks can be nested inside others therefore if-elif-else
statements can be freely nested within others.
Example IF branching
nilai_ujian = 67
if nilai_ujian>=80:
nilai = 'A'
elif nilai_ujian>=70:
nilai = 'B'
elif nilai_ujian>=60:
nilai = 'C'
elif nilai_ujian>=50:
nilai = 'D'
else:
nilai = 'E'
print('Nilai = {}'.format(nilai))
While loops
Conditional loops
While Loops
• While loops have a condition and a code block.
• The indentation indicates what’s in the while loop.
• The loop runs until the condition is false.

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.

• Syntax: range(start, exclusive end, increment)

• range(0,4)  produces the sequence of numbers 0,1,2,3


• range(4)  0, 1, 2, 3
• range(-3,15,3)  -3, 0, 3, 6, 9, 12
• range(4,-3,2)  4, 2, 0, -2
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).

nl=[] # Membentuk list kosong


for x in range(1500, 2701):
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]
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

You might also like