Data Input/Output
Learn how to receive and process user input and output
Data Input/Output
In this section, we will learn how to write programs that interact with the user — receiving input and displaying output.
Outputting Data with print()
We already met print() in the previous module. Let's look at it more closely:
By default, print() separates multiple values with a space and adds a newline at the end. You can change both behaviors:
Variables
To store data in a program, we use variables. A variable is simply a named container that holds a value. In Python, you create a variable by writing its name, an equals sign, and the value you want to store:
From this point on, you can use the variable name anywhere in your program and Python will substitute its value.
The two most common types of variables you will encounter are numbers (like 42 or 3.14) and strings (like "Alice"). Numbers are used for calculations, while strings represent text and behave very differently — for example, 2 + 2 gives 4, but "2" + "2" gives "22".
Python is a strongly and dynamically typed language. Dynamically typed means you don't need to declare a variable's type — Python figures it out automatically. Strongly typed means Python will not silently convert between types for you: trying to add a number and a string will raise an error rather than guess what you meant. This is exactly the mistake we will see in a moment with input().
We will cover variables, naming rules, and data types in much more detail in the upcoming modules.
Receiving Input with input()
To receive data from the user, Python uses the input() function. It pauses the program and waits for the user to type something and press Enter:
You can pass a prompt string to input(), which will be displayed to the user before they type.
Important: input() Always Returns a String
This is a common source of mistakes. Whatever the user types, input() returns it as a string — even if they type a number:
Type Conversion
To work with numbers, you need to convert the input using int() or float():
Use int() for whole numbers and float() for decimal numbers:
Reading Multiple Values
Sometimes a problem gives you several values on one line, separated by spaces. You can read them all at once using :