Back to Blog
Lesson 2 of the Python: Programming from Zero with Python course
PythonJuly 12, 20263 min read

Variables and Data Types: Python Programming from Zero

Learn to store and display data in Python. This lesson covers declaring variables, assigning strings and integers, and printing output to the console.

pythonprogrammingvariablesbeginnerscoding

Previously in this course, we covered Setting Up the Python Environment, where you configured your editor and executed your first script. Now that your environment is ready, we need a way to store the information your programs will process.

In programming, we use variables to act as labeled containers for data. Without them, our code would be static and unable to track changing information like user input or API responses.

Understanding Variables and Python Syntax

Think of a variable as a sticky note on a cardboard box. You write a name on the note (the variable name) and put something inside the box (the value). Python uses a straightforward syntax for this: variable_name = value.

This is called an assignment. The equals sign (=) doesn't mean "is equal to" in the mathematical sense; it means "take the value on the right and store it in the container on the left."

Essential Data Types

While Python can store many kinds of information, we’ll start with the two most common types you’ll use in our upcoming data-processing CLI:

  1. Strings (str): Text data. They must be wrapped in quotes—either single ('hello') or double ("hello").
  2. Integers (int): Whole numbers without decimals (e.g., 42, -10, 0).

Worked Example: Storing and Printing Data

Let’s write a simple script that stores a user’s name and their age, then displays that information in the console using the print() function.

PYTHON
# Declare a string variable
user_name = "Alex"

# Declare an integer variable
user_age = 28

# Print the variables to the console
print(user_name)
print(user_age)

When you run this, Python looks up the labels user_name and user_age, retrieves the values stored inside them, and sends them to your terminal.

Hands-on Exercise: Building the Foundation

We are going to start the groundwork for our CLI tool. Open your code editor and create a new file named tracker.py.

  1. Create a variable named project_name and assign it a string describing a project (e.g., "Data Collector").
  2. Create a variable named version_number and assign it an integer (e.g., 1).
  3. Use the print() function to display both variables.

Your output should look like this:

TEXT
Data Collector
1

Common Pitfalls

Even experienced engineers occasionally trip over these three common issues:

  • Case Sensitivity: Python is case-sensitive. user_name and User_name are treated as two entirely different variables. Always use snake_case (all lowercase with underscores) for variable names to keep your code readable and consistent.
  • Forgetting Quotes: If you write name = Alex without quotes, Python will look for a variable named Alex instead of treating it as text. This will trigger a NameError.
  • Reassignment Confusion: You can change the value of a variable at any time. If you write x = 5 then x = 10, the number 5 is gone. Be careful not to overwrite data you still need.

FAQ

Can I store decimals in variables? Yes, those are called floats. We will cover them in the next lesson when we discuss arithmetic.

Does it matter if I use single or double quotes for strings? In Python, no. You can use 'hello' or "hello" interchangeably. Just be consistent within your project.

What is the maximum size of an integer? Python integers have arbitrary precision, meaning they can be as large as your computer's memory allows. You don't need to worry about "overflow" errors common in languages like C.

Recap

In this lesson, you learned that variables are the backbone of data storage. By using simple Python syntax for assignment and understanding the difference between data types like integers and strings, you have taken the first step toward building dynamic programs. You now have the ability to hold data in memory and output it to the user.

Up next: Arithmetic Operations, where we will perform calculations on these variables to make our CLI tool actually process data.

Similar Posts