- strings
- branching - if/elif/else
- indentation
- while loops
15/03/2023
while <condition>: do_something do_something_else do_something_more
k = 5 i = 1 while i <= k: print(i) i = i + 1
## 1 ## 2 ## 3 ## 4 ## 5
k = 15 i = 1 while i <= k: if i % 2 == 1: print(i) i = i + 1
## 1 ## 3 ## 5 ## 7 ## 9 ## 11 ## 13 ## 15
k = 15 i = 1 while i <= k: print(i) i = i + 2
## 1 ## 3 ## 5 ## 7 ## 9 ## 11 ## 13 ## 15
number = 28 i = 1 while i <= number: if number % i == 0: print(i) i = i + 1
## 1 ## 2 ## 4 ## 7 ## 14 ## 28
number = 28 i = 2 isPrime = True while i < number and isPrime: if number % i == 0: isPrime = False i = i + 1 if isPrime: print(number, "is prime.") else: print(number, "is not prime.")
## 28 is not prime.
number = 29 i = 2 isPrime = True while i < number and isPrime: if number % i == 0: isPrime = False i = i + 1 if isPrime: print(number, "is prime.") else: print(number, "is not prime.")
## 29 is prime.
for
loop allows iterating in a fixed sequencefor <variable> in <sequence of values>: do_something
<variable>
takes a value from the <sequence of values>
range
function provides a sequence of integer valuesrange(5)
means numbers from 0
(inclusive) to 5
(exclusive)
0, 1, 2, 3, 4
for i in range(5): print(i)
## 0 ## 1 ## 2 ## 3 ## 4
range(start, stop, step)
range(5)
it means range(stop = 5)
start
defaults to 0step
defaults to 1range(4, 10, 1)
means
start = 4
stop = 10
step = 1
4, 5, 6, 7, 8, 9
for i in range(4, 10, 1): print(i)
## 4 ## 5 ## 6 ## 7 ## 8 ## 9
range(2, 8, 2)
means
start = 2
stop = 8
step = 2
2, 4, 6
stop
is exclusivefor i in range(2, 8, 2): print(i)
## 2 ## 4 ## 6
range(10, 0, -1)
means
start = 10
stop = 0
step = -1
10, 9, 8, 7, 6, 5, 4, 3, 2, 1
for i in range(10, 0, -1): print(i)
## 10 ## 9 ## 8 ## 7 ## 6 ## 5 ## 4 ## 3 ## 2 ## 1
break
statement is used to immediately exit a loop
while
and for
loops both workwhile <condition_1>: # Loop 1 while <condition_2>: # Loop 2 <expression_a> break # Exits loop 2 <expression_b> <expression_c>
for i in range(10, 20, 1): print(i) if i % 7 == 0: break
## 10 ## 11 ## 12 ## 13 ## 14
for
loop, as well as break
number = 29 for i in range(2, number): # step defaults to 1 if number % i == 0: print(number, "is not prime.") break if i == (number - 1): print(number, "is prime.")
## 29 is prime.
range(2, number)
?for
loops
break
for
loop using a while
loopwhile
loops
while
loop using a for
loopfor i in [1, 3, 9, 24]: print(i)
## 1 ## 3 ## 9 ## 24
for i in ["a", "b", "c", "d"]: print(i)
## a ## b ## c ## d
for i in ["a", 1, "c", True]: print(type(i))
## <class 'str'> ## <class 'int'> ## <class 'str'> ## <class 'bool'>
name = "burkay genc" for i in range(len(name) - 1, -1, -1): print(name[i], end = "")
## cneg yakrub
s = "Check if a substring exists in a string" ss = "st" for i in range(len(s) - len(ss)): if s[i:(i + len(ss))] == ss: print(i)
## 14 ## 24 ## 33
sequence = [4, 7, 1, 0, -8, 12, -3, 1, 4, 11, 9, -2, 10]
sequence = [4, 7, 1, 0, -8, 12, -3, 1, 4, 11, 9, -2, 10] max = sequence[0] for i in sequence: if i > max: max = i print(max)
## 12
sequence = [4, 7, 1, 0, -8, 12, -3, 1, 4, 11, 9, -2, 10]
sequence = [4, 7, 1, 0, -8, 12, -3, 1, 4, 11, 9, -2, 10] max = 0 for i in range(len(sequence)): if sequence[i] > sequence[max]: max = i print(max)
## 5
1, 1, 2, 3, 5, 8, 13, 21, ...
k
Fibonacci numbersk
Fibonacci numbersk = 15 F = 1 F_1 = 0 F_2 = 0 i = 1 while i <= k: print(F) F_2 = F_1 F_1 = F F = F_1 + F_2 i = i + 1
## 1 ## 1 ## 2 ## 3 ## 5 ## 8 ## 13 ## 21 ## 34 ## 55 ## 89 ## 144 ## 233 ## 377 ## 610
Guess is too high
, and ask for a new guessGuess is too low
, and ask for a new guesskayak
kelek
abcdcba
Korkma sönmez bu şafaklarda yüzen al sancak
and the character k
, the program should output 3K
and k
are assumed to be different!