002-1. Controlling statements
@
# Controlling statement
# Conditional statement
# if, if else, else if, elif
# switch(which Python doesn't have)
# Iteratation statement
# for
# do while
# while
# foreach
# Jump statement
# continue
# break
# return
# yield
# throw
@
ns = re.findall(r'[0-9]+', db)
# We call r'' as raw string
# We need to designate "string for regular expression"
# not them to conflict other Python string
db = '''
3412 Bob 123
3834 Jonny 333
1248 Kate 634
1423 Tony 567
2567 Peter 435
3567 Alice 535
1548 Kerry 534
'''
# output:
# whitespace
# 3412 Bob 123
# 3834 Jonny 333
# 1248 Kate 634
# 1423 Tony 567
# 2567 Peter 435
# 3567 Alice 535
# 1548 Kerry 534
# whitespace
db = '''3412 Bob 123
3834 Jonny 333
1248 Kate 634
1423 Tony 567
2567 Peter 435
3567 Alice 535
1548 Kerry 534'''
# output:
# 3412 Bob 123
# 3834 Jonny 333
# 1248 Kate 634
# 1423 Tony 567
# 2567 Peter 435
# 3567 Alice 535
# 1548 Kerry 534
db = '''3412 Bob 123
3834 Jonny 333
1248 Kate 634
1423 Tony 567
2567 Peter 435
3567 Alice 535
1548 Kerry 534'''
# output:
# 3412 Bob 123
# 3834 Jonny 333
# 1248 Kate 634
# 1423 Tony 567
# 2567 Peter 435
# 3567 Alice 535
# 1548 Kerry 534
db = "3412 Bob 123"
"3834 Jonny 333"
"1248 Kate 634"
"1423 Tony 567"
"2567 Peter 435"
"3567 Alice 535"
"1548 Kerry 534"
# The followings are not assigned into variable db
# "3834 Jonny 333"
# "1248 Kate 634"
# "1423 Tony 567"
# "2567 Peter 435"
# "3567 Alice 535"
# "1548 Kerry 534"
db = "3412 Bob 123"\
"3834 Jonny 333"\
"1248 Kate 634"\
"1423 Tony 567"\
"2567 Peter 435"\
"3567 Alice 535"\
"1548 Kerry 534"
# Above one shows in one line
db = "3412 Bob 123\n"\
"3834 Jonny 333\n"\
"1248 Kate 634\n"\
"1423 Tony 567\n"\
"2567 Peter 435\n"\
"3567 Alice 535\n"\
"1548 Kerry 534"
@
# Day_02_01_loop.py
# Quiz
# You get number ranged from 0 to 3 and assign it to variable count
# Make logic to say greeting as much as times you get as input number
# Consider various kinds of code
# Tip: If you're not using some logics, you can make them as body of method
# because they're not used until you explicitly invoke that method
# Method1
def not_used_1():
count = int(input('count : '))
if count == 1:
print('Good morning!')
elif count == 2:
print('Good morning!')
print('Good morning!')
elif count == 3:
print('Good morning!')
print('Good morning!')
print('Good morning!')
# Following code is not recommended
# Suppose we get number 1 then if statement runs 3 times
# even if below 2 if statements don't need to be executed
# Multiple if statements can be used
# when you choose multiple values out of multiple conditions
# So, when you choose one value out of multiple values
# you'd better use elif
if count == 1:
print('Good morning!')
if count == 2:
print('Good morning!')
print('Good morning!')
if count == 3:
print('Good morning!')
print('Good morning!')
print('Good morning!')
# Method2
# I will test with explicitly declared constant
count = 3
if count > 0:
print('Good morning!')
# count = count -1
count -= 1
if count > 0:
print('Good morning!')
count -= 1
if count > 0:
print('Good morning!')
count -= 1
# Method3
count = 3
i = 0
if i < count:
print('Good morning!')
i += 1
if i < count:
print('Good morning!')
i += 1
if i < count:
print('Good morning!')
i += 1
# Copy and indentedly paste
# count = 2
# i = 0
# if i < count:
# print('Good morning!')
# i += 1
# if i < count:
# print('Good morning!')
# i += 1
# if i < count:
# print('Good morning!')
# i += 1
# if i < count:
# print('Good morning!')
# i += 1
# Refactor above code by using "while"
count = 2
i = 0
# while condition:
while i < count:
print('Good morning!')
i += 1
# Above steps to implement iteration with if, while, etc
# are most important step in learning programming
@
# To use iteration statement well, you should find rule from source
# If we say 1 3 5 7 9 as odd number, it's not good answer
# In programming, answer should be specific
# 1 3 5 7 9 1, 9, 2 starting number 1 , ending number 9, increment or decrement step
# 0 1 2 3 4 0, 4, 1
# 5 4 3 2 1 5, 1, -1
# You can process over 80 percent of source by above rules
# Remainders are difficult jobs to deal with
# Display "hello" 5 times
i = 1 # starting
while i <= 9: # ending
print('hello')
i += 2 # increment and decrement
# Display "hello" 5 times
# Recommended one
i = 0 # starting
while i <= 4: # ending
print('hello')
# Python doesn't use i++
i += 1 # increment and decrement
# Display "hello" 5 times
i = 5 # 시작
while i >= 1: # 종료
print('hello')
i -= 1 # 증감
# Quiz1
# Make function which prints numbers from 0 to 99
# Quiz2
# Make function which prints 10 numbers in one line from 0 to 99 0~99
# When you use print(), if elements are shown in 100 lines, it's bothering to see at once
# If you want to all elements in one line, you can use end=' ' as 2nd argument
# print('hello', end=' ')
# output:
# hello hello ... hello
# staring 0, ending 99, inc and dec 1
def show100():
# staring 0
i = 0
# ending 99
# 100 is better because you're printing 100 numbers
while i < 100:
print(i, end=' ')
# inc and dec 1
i += 1
def show100With10InOneLine():
# staring 0
i = 0
# ending 99
# 100 is better because you're printing 100 numbers
while i < 100:
print(i, end=' ')
# You make new line, one time per 10
if i%10 == 9:
print()
# inc and dec 1
i += 1
# Writing i += 1 is not good.
# If you write i += 1
# upper part(i becomes variable because it changes in the middle of logic),
# you should write i%10 == 0
# The meaning that I want to make new line one time per 10 is rather broken
# Skilled ones write
# "increment and decrement" lower part(i can be constant because it doesn't change)
# i = 0
# while i < 100:
# print(i, end=' ')
# i += 1
#
# if i%10 == 0:
# print()
# show100With10InOneLine()
# 0 1 2 3 4 5 6 7 8 9
# 10 11 12 13 14 15 16 17 18 19
# 20 21 22 23 24 25 26 27 28 29
# 30 31 32 33 34 35 36 37 38 39
# 40 41 42 43 44 45 46 47 48 49
# 50 51 52 53 54 55 56 57 58 59
# 60 61 62 63 64 65 66 67 68 69
# 70 71 72 73 74 75 76 77 78 79
# 80 81 82 83 84 85 86 87 88 89
# 90 91 92 93 94 95 96 97 98 99