Python Variables

Image

A variable in Python is like a container that stores a piece of information. You give the container a name (the variable’s name), and you put something inside it (the variable’s value). Later in your program, you can look inside the container, use what’s there, or even replace it with something new.

Everyday Analogy

Think about jars in your kitchen. Each jar might have a label on it:

  • A jar labelled “Sugar” contains sugar.
  • A jar labelled “Flour” contains flour.

The label helps you know what’s inside without needing to check every time. A variable is the same:

Here, sugar is a variable holding the text "white crystals", and flour is a variable holding "fine powder".

Key Things to Know

  • A variable has a name (the label).
  • A variable has a value (what’s inside).
  • You can change the value at any time.

Example:

Practice Task

  1. Make a variable called favourite_colour and set it to "blue".
  2. Print it out.
  3. Change it to "green" and print again.

Creating a Variable

To create a variable, you just give it a name and use the = sign to put something inside.

Here:

  • name stores the string "Alice".
  • age stores the number 25.

You can print them to check what they contain:

Practice Task

  1. Create a variable called pet and store "cat".
  2. Create a variable called years and store 3.
  3. Print both variables.

Why Naming Variables Matters

Good names make your code easier to read. Imagine this:

It works, but it’s not clear what’s happening. Compare it to this:

Now it’s obvious we’re adding apples and oranges.


Rules for Variable Names

  • Must start with a letter or an underscore (_), not a number.
  • Can only contain letters, numbers, and underscores.
  • Python is case-sensitive: name and Name are different.
  • Cannot use reserved words like if, while, or class.

✅ Good: first_name, score1, _hidden_value
❌ Bad: 1name, class, total-score

Practice Task

  1. Create a variable called first_name with your own name.
  2. Try to create a variable called 1age. What happens?
  3. Fix it by changing the name to age1.

Hungarian Notation (Quick Note)

In older programming styles, people sometimes used Hungarian notation, where variable names start with letters showing their type:

In Python, this is not needed. Today, most Python programmers prefer simple names like name and age.

👉 You might see Hungarian notation in old code, but you don’t need to use it.

What Variables Can Store: Data Types

A variable can store different kinds of information, called data types.

The most common are:

  • Integer (int): whole numbers, e.g. 5, -10
  • Float: decimal numbers, e.g. 3.14, 0.5
  • String (str): text in quotes, e.g. "Hello"
  • Boolean (bool): True or False

Examples:

Mixing Types (Common Beginner Mistake)

You can’t always mix types together.

This fails because you can’t add a number to text directly.

✅ Correct way:

Output:

Practice Task

  1. Create a variable called temperature with the number 18.5.
  2. Create a variable called unit with the text "Celsius".
  3. Print them together to show 18.5 Celsius.
    (Hint: turn the number into text with str(temperature))

Changing What’s Inside a Variable

Variables can change as your program runs.

The second assignment overwrites the first one.

Practice Task

  1. Make a variable called points with the value 0.
  2. Print it.
  3. Change it to 100 and print again.

Summary

  • A variable is like a labelled box that holds information.
  • Use clear names so your code makes sense.
  • Follow naming rules (start with a letter, avoid reserved words, case matters).
  • Hungarian notation is old-fashioned — you don’t need it in Python.
  • Variables can store numbers, text, or True/False values.
  • Be careful when mixing types (like numbers with text).
  • Variables can be changed at any time.

✅ With these basics, you can now start writing small Python programs that remember names, numbers, and other information.

Main Topic

Python Fundamentals

A colourful cartoon-style landscape illustration shows a person sitting at a computer terminal, looking overwhelmed by the amount of information on the screen. Papers and symbols float around them, representing different programming concepts. The word “Fundamentals” is clearly written above the scene, highlighting the focus on beginner Python basics.

Beginner’s guide to Python fundamentals: strings, numbers, booleans, syntax, and text manipulation explained simply and clearly.

Other Tutorials in this Topic

A playful cartoon-style landscape illustration shows a large elephant on the left and a small cat on the right. Between them, a bold greater than ( > ) symbol points toward the cat, representing a Python comparison operator in a fun way.

Python Operators

Beginner’s guide to Python operators: arithmetic, comparison, logical, assignment, membership, and identity explained with examples.

A colourful cartoon-style landscape illustration shows a smiling robot holding up two placards: one clearly labelled True and the other False. The background is bright and cheerful, symbolising the concept of Boolean values in Python programming.

Python Booleans

Learn Python Booleans: True or False values, comparisons, logic operators, and decision-making with simple examples.

ChatGPT said: A colourful cartoon-style landscape illustration shows two large labelled containers: one marked Integers filled with whole numbers like 7, 0, and -3, and another marked Floats filled with decimal numbers like 3.14, -0.5, and 2.0. A cheerful character is sorting the numbers into the correct containers, visually explaining the difference between integers and floating-point numbers in Python.

Python Numbers

Learn Python numbers: integers and floats, arithmetic operations, conversions, rounding, precision issues, and practical exercises for beginners…

A colourful cartoon-style landscape illustration shows the text “python ” written across the scene. Above the gap, a labelled box with the word “strings” is tilted, pouring its contents into the space, visually representing a variable being used to fill in a placeholder.

Python Strings – Part 4

Discover Python string formatting: use f-strings, .format(), and number formatting to neatly combine variables and text into…

A colourful cartoon-style landscape illustration shows the words “python strings” in lowercase being fed into a large whimsical machine. On the other side, the text emerges transformed into “PYTHON STRINGS” in uppercase letters, symbolising string methods like .upper(). The machine has gears and pipes, giving a fun and playful feel.

Python Strings – Part 3

Learn Python string methods: change case, remove spaces, replace text, split and join words, and check content…

A digital landscape illustration shows a beige fabric banner with the words “Python Strings” written across it. The material is ripped down the centre, with large visible stitches crudely sewing the two halves back together, symbolising fixing or escaping characters in strings. The background is simple and muted, drawing attention to the torn fabric.

Python Strings – Part 2

Explore Python strings further: escape characters, multi-line strings, and slicing to extract parts of text, with simple…

A colourful cartoon-style landscape illustration shows festive bunting strung across the top, with triangular flags spelling out “Python Strings” in bright, playful letters. The background is light and cheerful, giving a fun and welcoming atmosphere.

Python Strings – Part 1

Learn Python strings: text in quotes, printing, joining with +, counting length, and accessing characters by position…

A colourful cartoon-style illustration shows a confused student with orange hair shrugging in front of a computer screen. The screen displays a Python function with a missing indentation error:

Python Syntax

Learn Python syntax basics with clear examples and beginner-friendly practice exercises.

A cartoon-style illustration shows a person’s hands typing on a laptop running Visual Studio Code in dark mode. The screen displays a Python file with the code print("Hello, world!"), alongside a large, colourful Python logo. The background is bright blue, creating a vibrant and engaging learning atmosphere.

Start Coding Python with VS Code

Beginner’s guide to installing Python, setting up VS Code, and getting ready for coding.