002-2. Iteration statement focusing on "for"
@
# We use for statement for iteratioin than if and while
# You can use "for" for all cases
# You can use "while" when "for" doesn't fit well
# Python doesn't have "for int i=0 ; i<=10 ; i++"
# Python uses range() to make data source and then interates data source by "for"
# starting(0), ending 9(interation 10 times), increment and decrement(1)
for i in range(0, 10, 1):
print(i, end=' ')
# This is for new line
print()
# I pass 2 arguments into range()
# starting(0), ending(9), default parameter(1) for increment and decrement
for i in range(0, 10):
print(i, end=' ')
print()
# I pass 1 argument into range()
# This one is much used with specifying the number of iteration
# default parameter(0) for starting, ending(9), default parameter(1) for increment and decrement
for i in range(10):
print(i, end=' ')
print()
# Quiz
# Make method to print "sum of odd numbers"
# and "sum of even numbers"
# out of all numbers under 100 over 1
# You can use multiple return values
# Method1
def sumOfOddEven():
# I initialize by 0
odd, even = 0, 0
for i in range(1, 100):
# odd += i
if i%2 == 1:
# even += i
else:
return odd, even
s1, s2 = sumOfOddEven()
print(s1, s1)
# output:
# 2500, 2450
# Method2
def sumOfOddEven():
# I initialize by 0
odd, even = 0, 0
# starting 1, ending 99, inc and dec2
for i in range(1, 100, 2):
odd += i
# starting 2, ending 99, inc and dec2
for i in range(2, 100, 2):
even += i
return odd, even
# Method3
def sumOfOddEven():
# I initialize by 0
odd, even = 0, 0
# starting 0, ending 99, inc and dec2
for i in range(0, 100, 2):
even += i
odd += i+1
return odd, even
s1, s2 = sumOfOddEven()
print(s1, s2)
print('-'*50)