PyComplete Python Course
Beginner 40 min

Module 02 โ€” Print, Variables & Basic Output

Talking back to the screen, storing values with names, and Python's core data types.

Prerequisite: Module 01 โ€” Introduction & Setup

After this lesson, you will be able to

  • Use print() with multiple values, separators, and escape sequences
  • Create and update variables, and understand Python's naming rules
  • Use Python as a calculator with +, -, *, /, //, %, **
  • Identify a value's type using type()

Concept & Syntax

print() is how a Python program talks to the outside world. A variable is a named box in your computer's memory that holds a value โ€” think of it as a sticky note with a name on it, stuck to a piece of data. Once you understand these two things, you can already build something that computes and reports results, which is the heart of programming.

Variables matter because programs need to remember things between steps: a user's age, a running total, a game score. Without a name for a value, you'd have no way to refer back to it later in your code.

Syntax
variable_name = value
print(value1, value2, sep=", ", end="\n")

Examples

print() with several values Very Easy

print() with several values
print("Score:", 95, "/", 100)
OutputScore: 95 / 100

print() accepts any number of items separated by commas and automatically puts a space between them.

Escape sequences Easy

Escape sequences
print("She said, \"Python is fun!\"")
print("Line one\nLine two")
print("Tab\tSeparated")
OutputShe said, "Python is fun!" Line one Line two Tab Separated

A backslash before a character gives it special meaning: \" is a literal quote, \n is a new line, and \t is a tab.

Raw strings and emoji Intermediate

Raw strings and emoji
path = r"C:\Users\name\Documents"
print(path)
print("Python is fun ๐Ÿ")
OutputC:\Users\name\Documents Python is fun ๐Ÿ

Prefixing a string with r makes it a raw string, so backslashes are treated literally โ€” essential for Windows file paths. Emoji are just Unicode characters and print like any other text.

Python as a calculator Easy

Python as a calculator
print(7 + 3)
print(7 - 3)
print(7 * 3)
print(7 / 3)
print(7 // 3)
print(7 % 3)
print(7 ** 2)
Output10 4 21 2.3333333333333335 2 1 49

/ always gives a decimal result, // gives the whole-number (floor) division, % gives the remainder, and ** raises to a power.

Variables and type() Real World

Variables and type()
age = 25
price = 19.99
name = "Maria"
is_member = True
print(type(age), type(price), type(name), type(is_member))
Output<class 'int'> <class 'float'> <class 'str'> <class 'bool'>

Python figures out a value's type automatically. The four basics here are integers, floating-point numbers, strings (text), and booleans (True/False).

Visual Explanation

name Python
A variable is a name pointing at a value stored in memory.
๐Ÿ” What happens internally? (advanced)

Under the hood, Python compiles your source into bytecode and runs it on a virtual machine (the CPython interpreter). Variables are names in a namespace dictionary pointing at objects in memory โ€” which is exactly what the memory panel in the tracer above is showing you.

See variables appear in memory as each line runs

Use the controls below to run this example one line at a time and watch the interpreter's memory update live.

Code

Code
x = 10
y = 20
result = x + y
print(result)

Interpreter state

Console output

Common Mistakes

โœ— Common mistake 1

Variable names in Python must start with a letter or underscore, contain no spaces, and can't be a reserved word like <code>print</code> or <code>class</code>.

โœ— Avoid this
This raises SyntaxError
2nd_place = "Silver"
my variable = 5
โœ“ Better approach
Fixed
second_place = "Silver"
my_variable = 5
โœ— Common mistake 2

Forgetting quotes around text makes Python think you're referring to a variable that doesn't exist yet.

โœ— Avoid this
This raises NameError: name 'Hello' is not defined
print(Hello)
โœ“ Better approach
Fixed
print("Hello")
โœ“ Best practices
  • Use snake_case for variable names: total_price, not TotalPrice.
  • Pick descriptive names โ€” age beats a once code gets longer than a few lines.
  • Use raw strings (r"...") for Windows file paths and regular expressions.
  • Check a value's type with type() whenever a bug looks type-related.

Exercise & Challenge

Exercise 1 โ€” Fill in the blank

Given this code
count = 42
____________________

Complete the line so that it prints exactly: Total: 42 apples (with the word "Total:", a space, the number, a space, and "apples"). Type only the missing print() call.

๐Ÿ† Challenge

Write a program that stores the price of an item and a quantity in two variables, then prints the total cost formatted to two decimal places.

  • Use variables named price and quantity
  • Compute total = price * quantity
  • Print something like 'Total: $59.97'
๐Ÿ’ก Need a hint?

You can round with round(total, 2), or format with an f-string like f"{total:.2f}".

๐Ÿ” Show a sample solution
Python
price = 19.99
quantity = 3
total = price * quantity
print(f"Total: ${total:.2f}")

Quiz

Answer every question, then submit to see your score and explanations.

1. What does 7 // 3 evaluate to in Python?

2.33
2
3
1
// is floor (whole-number) division, so 7 // 3 is 2.

2. Which escape sequence inserts a new line inside a string?

\t
\n
\\
\s
\n represents a newline character.

3. Which of these is a valid Python variable name?

2nd_place
my-variable
_score
class
Names can start with an underscore or letter; hyphens aren't allowed and 'class' is a reserved keyword.

4. What type does type(19.99) report?

int
str
float
bool
Numbers with a decimal point are floats.

5. What does a raw string like r"C:\Users" do?

Converts text to uppercase
Treats backslashes as literal characters, not escape codes
Removes all spaces
Runs the string as code
The r prefix disables escape-sequence processing, which is handy for Windows paths.

Summary

  • print() displays one or more values, automatically spacing and newlining them.
  • Variables are names pointing at values; naming rules matter (no spaces, no reserved words).
  • Python's core math operators include //, %, and ** alongside the familiar +, -, *, /.
  • type() tells you exactly what kind of value you're working with.

Related lessons

Saved privately in your browser โ€” no account needed.