** for variable name in list: **
fruits = ["apple" , "banana" , "strawberry"]
for fruit in fruits:
print("What is your favorite food" + fruit + "is")
Output result My favorite food is apple My favorite food is banana My favorite food is strawberry
** for variable name in dictionary: **
fruits = {"apple":"Apple" , "banana":"banana" , "strawberry":"Strawberry"}
for fruit_key in fruits:
print(fruit_key + "Is in Japanese" + fruits[fruit_key] + "is")
Output result apple is an apple in Japanese banana is Japanese banana strawberry is Japanese strawberry
** while conditional expression: **
x = 1
while x <= 5:
print(x)
x += 1
Output result 1 2 3 4 5
break End the iterative process Use with conditional expressions
numbers = [1 , 2 , 3 , 4 , 5]
for number in numbers:
print(number)
if number == 3:
break
In the above case, the process ends when the number reaches 3. Output result 1 2 3
continue Skip conditional expression processing
numbers = [1 , 2 , 3 , 4 , 5]
for number in numbers:
print(number)
if number % 2 == 0:
continue
Skip processing when divisible by 2 Output result 1 3 5
Recommended Posts