# after the if statement is executed.
#2 for statement
for i in range(1, 10):
print(i)
else:
print('The for loop is over')
a_list = [1, 3, 5, 7, 9]
for i in a_list:
print(i)
a_tuple = (1, 3, 5, 7, 9)
for i in a_tuple:
print(i)
a_dict = {'Tom':'111', 'Jerry':'222', 'Cathy':'333'}
for ele in a_dict:
print(ele)
print(a_dict[ele])
for key, elem in a_dict.items():
print(key, elem)
#3. while example
number = 59
guess_flag = False
while guess_flag == False:
guess = int(input('Enter an integer : '))
if guess == number:
# New block starts here
guess_flag = True
# New block ends here
elif guess < number:
# Another block
print('No, the number is higher than that, keep guessing')
# You can do whatever you want in a block ...
else:
print('No, the number is a lower than that, keep guessing')
# you must have guessed > number to reach here
print('Bingo! you guessed it right.')
print('(but you do not win any prizes!)')
print('Done')
4..
#5 break & continue example
number = 59
while True:
guess = int(input('Enter an integer : '))
if guess == number:
# New block starts here
break
# New block ends here
if guess < number:
# Another block
print('No, the number is higher than that, keep guessing')
continue
# You can do whatever you want in a block ...
else:
print('No, the number is a lower than that, keep guessing')
continue
# you must have guessed > number to reach here
print('Bingo! you guessed it right.')
print('(but you do not win any prizes!)')
print('Done')
#continue and pass difference
a_list = [0, 1, 2]
print("using continue:")
for i in a_list:
if not i:
continue
print(i)
print("using pass:")
for i in a_list:
if not i:
pass
print(i)