noknow.dev
Sign inSign up
Course overview
Python Fundamentals
0 / 6 lessons0%

Getting Started

  • Hello, World!
  • Variables and Types
  • Working with Strings

Decisions & Loops

  • if / elif / else
  • for Loops and range()

Functions

  • Writing Your Own Functions

for Loops and range()

0m 00s

Loops in Python

Python's for loop iterates over a sequence. Use range() to generate number sequences:

for i in range(5):
    print(i)
# Output: 0, 1, 2, 3, 4

for i in range(1, 6):
    print(i)
# Output: 1, 2, 3, 4, 5

for i in range(0, 10, 2):
    print(i)
# Output: 0, 2, 4, 6, 8  (step size 2)

Iterating over a list

colors = ["red", "green", "blue"]
for color in colors:
    print(color)

Accumulating values

total = 0
for i in range(1, 6):
    total += i   # total = total + i
print(total)  # 15

Your task

Write a function factorial(n) that computes n!
n! = 1 × 2 × 3 × ... × n

Example: 5! = 1 × 2 × 3 × 4 × 5 = 120

Back
pythonCtrl+Enter to run
Output

Click "Run" to execute your code.