Concepts / The Print Function

The Print Function

def say_hello(): # block belonging to the function print 'hello world' # End of function say_hello() # call the function say_hello() # call the function again

  • Programming

What Print Does

The print function is one of the most fundamental tools in Python. Its job is simple: take whatever you give it and display that information on the screen. When you write print('hello world'), Python sends the text 'hello world' to your output, and you see it appear. Print doesn't change your data, doesn't store anything for later, and doesn't do any computation—it just shows you what you ask it to show.

Think of print as a messenger. You hand it a message, and it delivers that message to your screen. Every time you call print, a new line of output appears. This makes print invaluable for understanding what your code is doing at each step.

Defining Versus Calling

A critical distinction in programming is the difference between defining a function and calling it. When you write def say_hello():, you are telling Python what the function should do—you are creating a blueprint. The code inside the function does not run at that moment. It only runs when you actually call the function by writing say_hello().

Definition creates the blueprint. Call executes the blueprint. Until you call the function, the code inside it sits dormant. This separation is essential because it lets you define a function once and then use it many times without rewriting the code.

encounters definitionreads function bodystores blueprintencounters first callexecutes function bodyencounters second callexecutes function body againPython reads yourcodedef say_hello():print('hello world')stored, not executed yetFunction definitioncompletesay_hello()first callprint('hello world')runssay_hello()second callprint('hello world')runs again
What happens when you define a function versus when you call it? In what order do the lines execute?

How Indentation Marks Function Boundaries

In Python, indentation is not just for readability—it is the syntax rule that determines which lines belong inside a function and which are outside. Every line indented under the def line is part of the function's body. The moment you return to the original indentation level, you have exited the function.

def say_hello():function definition startsprint('hello world')indented = inside functionsay_hello()no indent = outsidefunctionsay_hello()no indent = outsidefunction
Which lines belong inside the function and which are outside? How does indentation determine what's part of the function?
python

Tracing Execution with Print

Let's trace through what happens when Python runs the code above. First, Python reads the def line and stores the blueprint for say_hello. It does not execute print('hello world') yet. Then Python encounters the first say_hello() call. Now it jumps into the function, executes print('hello world'), and the text appears on screen. Then Python returns to where it was and encounters the second say_hello() call. It jumps into the function again, executes print('hello world') a second time, and returns.

Output
hello world
hello world

The output shows two lines because print was called twice. Each call to say_hello() caused the function body to run, and each execution of print('hello world') produced one line of output.

Print with Parameters and Local Scope

Print becomes more powerful when combined with function parameters. You can pass values into a function and then print them. This is where understanding scope becomes important. When you define a variable inside a function, it exists only within that function. When you use print to display it, you are showing the local value, not affecting any variable with the same name outside the function.

Print with Local Variables

Define a function called print_max that takes two parameters, a and b, and prints the larger of the two numbers.

Define the function with two parameters: Write def print_max(a, b): to create a function that accepts two inputs.

Use an if statement to find the larger value: Inside the function, check if a > b. If true, the larger value is a. Otherwise, it is b.

Print the result: Use print to display the larger number. The print function will show the value stored in the local variable.

Call the function with different values: Call print_max(5, 3) and print_max(10, 8) to see print display different results each time.

Each call to print_max produces one line of output showing the larger of the two numbers passed in.

python
Output (expected)
5
10

Global Variables and Print

When you define a variable outside any function, it is a global variable. A function can read a global variable and print its value. However, if you assign a new value to a variable inside a function, Python treats that as a local variable, separate from the global one. This is an important distinction because it affects what print displays.

python
Output (expected)
5
10
5

In this example, the first print inside show_x() displays 5 because it reads the global x. The second print displays 10 because the assignment x = 10 created a local variable. After the function returns, the final print displays 5 again, confirming that the global x was never changed by the function.

Common Mistakes with Print

  • Forgetting that defining a function does not execute it

    The def line only creates the blueprint. Python does not run the code inside until you call the function.

    Fix: Always call the function: say_hello(). Without the parentheses and call, the function body never runs.

  • Confusing print output with variable assignment

    print() displays text but returns None. The variable x becomes None, not 5.

    Fix: If you need to store a value, assign it directly: x = 5. Use print only to display values, not to create them.

  • Assuming a local variable inside a function affects the global variable with the same name

    Assignment inside a function creates a local variable. The global x remains unchanged.

    Fix: If you need to modify a global variable from inside a function, use the global keyword: global x; x = 10.

  • Printing before defining a variable

    Python will raise a NameError because y does not exist yet.

    Fix: Always define a variable before printing it: y = 5; print(y).

When to Use Print for Debugging

Print is a powerful debugging tool. When your code is not behaving as expected, add print statements to see what values your variables hold at different points. This helps you trace the execution and spot where things go wrong.

Place print statements at key moments: right after a variable is assigned, before and after a function call, and inside conditional blocks to see which branch executes. This creates a trail of output that shows you exactly what your program is doing.

python
Output (expected)
Entered calculate_total with price = 10 and quantity = 5
Calculated total = 50
Final result = 50

The print statements here show you the values entering the function, the intermediate calculation, and the final result. This makes it easy to verify that each step is working correctly.

Practice

EASY

Write a function called greet that takes one parameter, name, and prints a greeting message that includes that name. Call the function three times with different names and observe the output.

Hints
  • Use def greet(name): to define the function.
  • Inside the function, use print to display a message that includes the name parameter.
  • Call greet('Alice'), greet('Bob'), and greet('Charlie') to test it.
MEDIUM

Define a global variable called counter set to 0. Create a function that prints the current value of counter, then increments it by 1. Call the function three times and explain what you see in the output. Why does counter not increase after the function returns?

Hints
  • Remember that assignment inside a function creates a local variable unless you use the global keyword.
  • Print counter before and after incrementing it inside the function.
  • After the function returns, print the global counter to see if it changed.

Summary

  1. The print function sends output to the screen. It does not modify data or store values—it only displays them.
  2. Defining a function with def creates a blueprint. The code inside does not run until you call the function by writing its name with parentheses.
  3. Indentation determines which lines belong inside a function. Lines indented under def are part of the function body; lines at the original indentation level are outside.
  4. Functions can have parameters and local variables. Print can display these local values without affecting global variables with the same name.
  5. Use print statements strategically during debugging to trace your program's execution and verify that variables hold the values you expect.

Key Takeaways

  • The print function displays output on the screen without modifying data or storing values.
  • Defining a function with def is different from calling it—definition creates a blueprint, and calling executes the code inside.
  • Indentation in Python determines which lines belong inside a function and which are outside.
  • Functions can use local variables and parameters; print displays these without affecting global variables of the same name.
  • Print is a valuable debugging tool for tracing program execution and verifying variable values at key points.