python-foundations

Introduction to Python

Learn Python from scratch — variables, functions, and clean code.

2 modules · 6 lessons · free to read

What you'll learn

  • Write and run your first Python program
  • Understand variables, types, and functions
  • Read and write clean Python code

01Hello, World!

Your first Python functions — from a simple return to dynamic f-strings.

1.What is Python?

Python is a programming language known for its clear, readable syntax. Every Python program you write is executed — Python reads your code line by line and runs it.

python
print("Hello!") # Python runs this and displays Hello!

Functions are the building blocks of Python programs. You define one with the def keyword, give it a name, and use return to send a value back.

python
def greet(): return "Hello!"
Practice Lesson 1

2.Your First Python Function

A function is a named block of code you can call by name. You define one with def, followed by the function's name and parentheses. The pass keyword is a placeholder that does nothing — useful while you're building up.

python
def greet(): pass # placeholder — does nothing yet

The return keyword sends a value back to whoever called the function. String literals are text enclosed in quotes.

python
def greet(): return "Hello, World!"
Practice Lesson 2

3.String Formatting with f-strings

Hard-coded strings only work for one specific value. If you want to greet different people, you need a variable. String concatenation works, but it's clunky and error-prone.

python
name = "Alice" greeting = "Hello, " + name + "!"

f-strings let you embed variables directly inside a string using curly braces. The f prefix before the opening quote tells Python to interpret {name} as the variable's value.

python
name = "Alice" greeting = f"Hello, {name}!"

Constraints

  • Use an f-string (f'...')
  • Parameter is named name
Practice Lesson 3

02Control Flow

Make decisions and repeat actions — the two most fundamental control structures in programming.

1.Making Decisions with if/else

An if statement runs a block of code only when a condition is True. The else block runs when the condition is False.

python
if x > 0: print("positive") else: print("not positive")

When there are more than two cases, elif (short for "else if") adds extra branches.

python
if x > 0: return "positive" elif x < 0: return "negative" else: return "zero"

Constraints

  • Use if, elif, and else
  • Return the string, not print it
Practice Lesson 1

2.Counting Down with while

A while loop repeats its body as long as a condition is True. You need a variable to track state and update it each iteration, otherwise the loop runs forever.

python
count = 3 while count >= 0: print(count) count -= 1

To collect results, start with an empty list and append each value. Return the list after the loop.

python
def countdown(n): result = [] while n >= 0: result.append(n) n -= 1 return result

Constraints

  • Return a list, not individual values
  • Include 0 in the result
Practice Lesson 2

3.Summing a List with for

A for loop iterates over every item in a sequence. Unlike while, you do not need to manage a counter — Python hands you each item in turn.

python
for item in [1, 2, 3]: print(item) # prints 1, then 2, then 3

To accumulate a running total, start a variable at zero and add each item to it inside the loop.

python
def sum_list(numbers): total = 0 for n in numbers: total += n return total

Constraints

  • Return an integer, not a list
  • An empty list should return 0
Practice Lesson 3

Frequently Asked Questions

Which function is used to display output in Python?
print(). print() is the built-in Python function for writing output to the console.
What keyword does a function use to send a value back to the caller?
return. The return keyword sends a value back from a function to wherever it was called.
How do I python is a language that runs code you write. Prove it by writing a function that returns True.?
Python is a programming language known for its clear, readable syntax. Every Python program you write is executed — Python reads your code line by line and runs it.
How do I write a function named greet that returns the string 'Hello, World!' — the classic first program.?
A function is a named block of code you can call by name. You define one with `def`, followed by the function's name and parentheses. The `pass` keyword is a placeholder that does nothing — useful while you're building up.
How do I use an f-string to greet someone by name. make_greeting('Python') should return 'Hello, Python!'.?
Hard-coded strings only work for one specific value. If you want to greet different people, you need a variable. String concatenation works, but it's clunky and error-prone.
Which keyword starts a conditional block in Python?
if. if is the keyword that introduces a conditional block in Python.
What does a while loop do?
Repeats code as long as a condition is True. A while loop repeats its body as long as the condition evaluates to True.
What does a for loop iterate over?
Any sequence (list, string, range, etc.). A for loop works on any iterable — lists, strings, ranges, tuples, and more.
How do I write classify_number(n) that returns "positive" if n > 0, "negative" if n < 0, or "zero" if n == 0.?
An if statement runs a block of code only when a condition is True. The else block runs when the condition is False.
How do I write countdown(n) that returns a list counting down from n to 0, inclusive.?
A while loop repeats its body as long as a condition is True. You need a variable to track state and update it each iteration, otherwise the loop runs forever.
How do I write sum_list(numbers) that returns the sum of all integers in the list. Return 0 for an empty list.?
A for loop iterates over every item in a sequence. Unlike while, you do not need to manage a counter — Python hands you each item in turn.

Ready to write code?

Theory is just the start. Write real code, run tests, build the habit.

Open the playground →