Python Tutorial for Beginners - 6. Loops and Iterations - For-While Loops

Table of Contents

  1. Run Code 1 in Python
  2. Run Code 2 in Python
  3. Run Code 3 in Python
  4. Run Code 4 in Python
  5. Run Code 5 in Python
  6. Run Code 6 in Python

Run Code 1 in Python

1
2
3
4
nums = [1, 2, 3, 4, 5]

for num in nums
    print(num)

start.py

Run Code 2 in Python

1
2
3
4
5
6
7
nums = [1, 2, 3, 4, 5]

for num in nums
    if num == 3
      print('Found!')
      break
    print(num)

start.py

Run Code 3 in Python

1
2
3
4
5
6
7
nums = [1, 2, 3, 4, 5]

for num in nums
    if num == 3
      print('Found!')
      continue
    print(num)

start.py

Run Code 4 in Python

1
2
3
4
5
nums = [1, 2, 3, 4, 5]

for num in nums
    for letter in 'abc'
        print(num, letter)

start.py

Run Code 5 in Python

1
2
for i in range(1, 11)
    print(i)

start.py

Run Code 6 in Python

1
2
3
4
5
6
7
x = 0

while True:
    # if x == 5:
    #     break
    print(x)
    x += 1

start.py