How to Use Variables in Python: A Complete Beginner’s Guide

Published on: May 8, 2026
Reading time: 6 minutes
notebook com código saindo da tela

Variables are one of the first things every programmer learns in Python. They help you store information, reuse data, and create dynamic programs.

If you want to build games, websites, automations, or data analysis projects, understanding variables is essential. The good news is that Python makes variables simple and beginner-friendly.

In this guide, you will learn what variables are, how they work, how to create them, common mistakes to avoid, and practical examples you can use today.

If you are completely new to Python, you may also enjoy reading Python for Beginners: Complete Guide.

What Are Variables in Python?

A variable is a name that stores a value inside your program.

Think of it like a labeled box. You put data inside the box, and you can use it later whenever you need.

Here is a simple example:

Python
name = "John"
age = 25

In this example:

  • name stores the text “John”
  • age stores the number 25

Variables help your code become flexible and easier to understand.

Tip: Variables can store many types of data, including text, numbers, lists, and more.

How to Create Variables in Python

Creating a variable in Python is very simple. You only need:

  1. A variable name
  2. The equals sign =
  3. A value

Example:

Python
city = "London"
temperature = 18
is_raining = False

Python automatically understands the type of data you assign.

This is one reason why Python is popular among beginners.

If you still need help setting up Python, check how to install Python.

Variable Naming Rules

Python has rules for naming variables. Following these rules helps avoid errors.

Valid Variable Names

Python
username = "Anna"
user_age = 20
score1 = 100

Invalid Variable Names

Python
1score = 100
user-name = "Anna"
class = "Python"

These examples fail because:

  • Variables cannot start with numbers
  • Hyphens are not allowed
  • Reserved Python keywords cannot be used

Best Practices for Naming Variables

  • Use clear names
  • Keep names short but meaningful
  • Use lowercase with underscores
  • Avoid confusing abbreviations

Good example:

Python
student_name = "Mike"

Bad example:

Python
sn = "Mike"

You can learn more about clean coding styles in the official PEP 8 documentation.

Different Types of Variables in Python

Python variables can store different kinds of data.

Data TypeExampleDescription
String“Hello”Text values
Integer10Whole numbers
Float3.14Decimal numbers
BooleanTrueTrue or False values
List[1, 2, 3]Collection of items

Example:

Python
message = "Hello"
price = 19.99
online = True
numbers = [1, 2, 3]

If you want a deeper explanation of Python data types, read Python Data Types.

How to Print Variables

You can display variable values using the print() function.

Python
name = "Sarah"

print(name)

Output:

Sarah

You can also combine variables with text.

Python
name = "Sarah"

print("Hello", name)

Output:

Hello Sarah

Another modern way is using f-strings.

Python
name = "Sarah"
age = 22

print(f"{name} is {age} years old")

F-strings are cleaner and easier to read.

You can explore more examples in this guide about print() in Python.

Changing Variable Values

Variables can change during program execution.

Example:

Python
score = 10

score = 20

print(score)

Output:

20

The old value is replaced by the new value.

This makes programs dynamic and interactive.

Here is a real-world example:

Python
bank_balance = 1000

bank_balance = bank_balance - 200

print(bank_balance)

Output:

800

Using Variables With User Input

Variables become even more useful when combined with user input.

Python
name = input("What is your name? ")

print("Welcome", name)

The program stores whatever the user types.

You can also convert input values.

Python
age = int(input("Enter your age: "))

print(age + 5)

Without int(), Python treats input as text.

If you want to understand user input better, read this complete guide about input() in Python.

Variable Scope Explained Simply

Variable scope means where a variable can be used.

There are two common types:

  • Local variables
  • Global variables

Local Variable Example

Python
def greet():
    message = "Hello"

    print(message)

greet()

The variable message only exists inside the function.

Global Variable Example

Python
message = "Hello"

def greet():
    print(message)

greet()

Now the variable can be accessed outside the function too.

Understanding scope helps prevent bugs in larger projects.

You can dive deeper into this topic in Variable Scope in Python.

Common Mistakes Beginners Make

Many beginners make small mistakes with variables. Here are the most common ones.

1. Using Undefined Variables

Python
print(username)

This causes an error if the variable was never created.

2. Mixing Data Types Incorrectly

Python
age = 20

print("Age: " + age)

This creates an error because Python cannot combine text and integers directly.

Correct version:

Python
print("Age:", age)

3. Overwriting Variables Accidentally

Python
name = "John"

name = 50

The variable changes from text to number.

This may confuse your program later.

4. Poor Variable Names

Python
x = "Michael"

It works, but nobody knows what x means.

Better:

Python
customer_name = "Michael"

If you want to improve your debugging skills, see how to debug Python in VS Code.

Practical Examples Using Variables

Here are some simple beginner projects using variables.

Temperature Converter

Python
celsius = 30

fahrenheit = (celsius * 9/5) + 32

print(fahrenheit)

Simple Calculator

Python
num1 = 10
num2 = 5

result = num1 + num2

print(result)

Password Greeting

Python
username = "admin"

print("Welcome", username)

Projects like these help reinforce your understanding.

You may also enjoy trying building a calculator in Python.

Why Variables Are Important in Python

Variables are used in almost every Python program.

Without variables, your code would be repetitive and difficult to manage.

Variables help you:

  • Store information
  • Reuse values
  • Create dynamic programs
  • Process user input
  • Build real-world applications

They are one of the core foundations of programming logic.

Important: Mastering variables early makes learning loops, functions, and classes much easier later.

The official Python documentation also explains variables and basic syntax clearly.

Conclusion

Learning how to use variables in Python is one of the most important first steps in programming.

Variables allow you to store data, reuse information, accept user input, and build flexible applications.

In this guide, you learned:

  • What variables are
  • How to create them
  • Naming rules
  • Data types
  • Variable scope
  • Common beginner mistakes
  • Practical examples

The best way to improve is through practice.

Start creating small scripts and experiment with different variables every day. Even simple exercises can build strong programming skills over time.

Frequently Asked Questions (FAQ)

1. What is a variable in Python?

A variable stores data that your program can use later.

2. How do I create a variable in Python?

Use a name, the equals sign, and a value.

3. Can variable values change?

Yes. Variables can store new values anytime.

4. Does Python require variable types?

No. Python detects data types automatically.

5. Can variable names contain spaces?

No. Use underscores instead of spaces.

6. What is a local variable?

A local variable only works inside a function.

7. What is a global variable?

A global variable works throughout the program.

8. Why do beginners struggle with variables?

Mostly because of naming mistakes and type confusion.

9. Can variables store text and numbers?

Yes. Variables can store many data types.

10. What is the best way to practice variables?

Create small projects and modify example code daily.

Share:

Facebook
WhatsApp
Twitter
LinkedIn

Article content

    Related articles

    ícone de loop com o texto 'While' abaixo
    Fundamentals
    Foto do Leandro Hirt

    How to Use While Loops in Python with Practical Examples

    Learning how to use a while loop in Python is one of the biggest steps for beginners. While loops help

    Ler mais

    Tempo de leitura: 6 minutos
    08/05/2026
    Imagem de um quadro negro com várias interrogações no lugar de lâmpadas, e uma lâmpada conectada no meio
    Fundamentals
    Foto do Leandro Hirt

    How to Use If, Elif, and Else in Python: Master Conditional Logic

    Conditional statements are one of the most important parts of programming. They allow your code to make decisions based on

    Ler mais

    Tempo de leitura: 6 minutos
    08/05/2026
    Imagem do Logo do Python dentro de um celular
    Fundamentals
    Foto do Leandro Hirt

    How to Run Python on Your Phone: A Step-by-Step Guide

    Learning Python no longer requires a powerful desktop computer. Today, you can write, test, and run Python code directly from

    Ler mais

    Tempo de leitura: 7 minutos
    08/05/2026
    Homem pensando olhando para o logo do Python
    Fundamentals
    Foto do Leandro Hirt

    Python for Beginners: Learn to Code from Scratch

    Python is one of the best programming languages for beginners. It has a simple syntax, a huge community, and many

    Ler mais

    Tempo de leitura: 7 minutos
    08/05/2026
    Homem pensando à frente de um notebook com código de programação
    Fundamentals
    Foto do Leandro Hirt

    Python Indentation Made Simple: A Beginner’s Guide

    When you start learning Python, one concept appears almost immediately: indentation. Many beginners feel confused when they see spaces changing

    Ler mais

    Tempo de leitura: 6 minutos
    08/05/2026
    texto 'Roadmap' com o logo do Python à direita
    Fundamentals
    Foto do Leandro Hirt

    Master Python in 2026: The Ultimate Step-by-Step Roadmap

    If you want to learn Python in 2026, you are making a great choice. Python is the most popular programming

    Ler mais

    Tempo de leitura: 11 minutos
    08/05/2026