Python Guide

Python Basics: print(), Loops, and Functions

Three building blocks show up in almost every Python program you'll ever write: printing output, looping over data, and packaging logic into reusable functions. Here's how each one works.

print(): getting output

Python's simplest and most-used function sends text to the console:

print("Hello, World!")

Anything inside the parentheses gets printed — a string in quotes, a number, or a variable.

for loops: repeating over a list

A for loop runs a block of code once for each item in a collection. Python uses indentation (not curly braces) to mark what's inside the loop:

fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit)

Read it as "for each fruit in the list fruits, do this." The indented line runs once per item — three times here, printing each fruit on its own line.

Indentation matters: unlike most languages, Python uses whitespace to define what's inside a loop or function — not brackets. Inconsistent indentation is a genuine syntax error, not just a style issue.

Defining functions

A function packages a block of logic under a name, so you can reuse it instead of retyping it. Define one with def, and send a result back with return:

def add(a, b): return a + b result = add(4, 15) # 19

a and b are parameters — placeholders that get filled in with real values (4 and 15) each time the function is called. return sends the result back to wherever the function was called from.

Putting it together

These three combine naturally: a function that loops over a list and prints each transformed item is one of the most common patterns in real Python code, from data processing scripts to simple automation.

Try it yourself

Practice print(), loops, and functions with real interactive exercises and instant feedback.

Start practicing Python →