dupady
HomeFind a Tutor
Back to Blog
Technology in Education
Python

What Is a Variable in Python?

Variables are one of the first and most important concepts in Python programming — they are how your program stores, tracks, and works with information. This complete guide covers what a variable is, naming rules and conventions, all major data types, multiple assignment, reassignment, global and local variables, constants, common mistakes to avoid, and a 4-question multiple choice quiz with answers and explanations to test your understanding.

Adedeji Ajayi28 June 20264 min read

Introduction


When you write a program, you almost always need to store information — a name, a number, a score, a result. Variables are how you do that. They are one of the very first concepts every Python programmer learns, and understanding them well sets the foundation for everything else that follows.

Think of a variable as a labelled box. You put something inside the box, give the box a name, and whenever you need what is inside, you simply call the name.




What Is a Variable?

A variable in Python is a named location in memory that stores a value. You create a variable by giving it a name and assigning a value to it using the equals sign ( = ).



  • name stores a string
  • age stores an integer
  • score stores a float
  • is_student stores a boolean

The moment you write name = "Adedeji", Python creates a space in memory, labels it "name", and stores the value "Adedeji" inside it.




Variable Assignment

Assigning a variable means giving it a value. In Python, the = sign is the assignment operator — it does not mean "equal to" the way it does in mathematics. It means "store this value in this variable."



Python reads the right side first, evaluates it, then stores the result in the variable on the left.




Variable Naming Rules

Python has strict rules about what you can and cannot name a variable. Breaking these rules causes a syntax error.

Rules you must follow:

  • Variable names can only contain letters, numbers, and underscores ( _ )
  • Variable names must start with a letter or an underscore — never a number
  • Variable names are case-sensitive — name, Name, and NAME are three different variables
  • Variable names cannot be Python reserved keywords (see below)



Python reserved keywords — these cannot be used as variable names: False, None, True, and, as, assert, async, await, break, class, continue, def, del, elif, else, except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield




Variable Naming Conventions


Beyond the rules, Python programmers follow widely agreed conventions to make code readable and professional.

Snake case — the standard convention for variables in Python. Words are lowercase and separated by underscores.



Camel case — more common in other languages but sometimes seen in Python.



Descriptive names — always name variables clearly so anyone reading your code understands what the variable holds.





Data Types Stored in Variables

A variable in Python can store different types of data. Python automatically detects the type — you do not need to declare it.


Integer (int)

Whole numbers, positive or negative, with no decimal point.



Float (float)

Numbers with a decimal point.



String (str)

Text — any sequence of characters enclosed in single or double quotes.



Boolean (bool)

A value that is either True or False — nothing else.



Checking the Type of a Variable

Use the built-in type() function to check what type a variable is.





Multiple Assignment

Python allows you to assign values to multiple variables in a single line.





Reassigning Variables

Unlike some other programming languages, Python variables are not fixed to one data type. You can reassign a variable to a completely different type at any point.



Python handles this dynamically — this is called dynamic typing.




Constants in Python

Python does not have a built-in constant type the way some languages do. By convention, variables intended to be treated as constants are written in all capital letters. This signals to other programmers that the value should not be changed.



These are technically still variables in Python (nothing prevents them from being changed) but the naming convention communicates the intention clearly.




Global and Local Variables


Local Variables

A variable created inside a function exists only within that function. It cannot be accessed from outside.



Global Variables

A variable created outside all functions is a global variable. It can be accessed from anywhere in the program.



The global Keyword

If you need to modify a global variable from inside a function, use the global keyword.




Adedeji Ajayi

Adedeji Ajayi is a Data Scientist, AI Educator and Founder

Quick Quiz

Test Yourself

Choose the correct answer for each question.

1.Which of the following is a valid Python variable name?

2.What will the following code print? x = 5 x = x + 3 print(x)

3.What data type is the variable "result" in the following code?

4.What is the output of the following code? name, age, passed = "Sola", 17, True print(age)

Related Articles

Minimalist book cover design for the parenting book What Will Your Child Remember About You? by Adedeji Ajayi, featuring elegant black and teal typography on a soft cream background, a symbolic tree illustration at the center, and a clean professional layout with modern parenting and emotional legacy themes.
Technology in Education

Embracing digital education: What every parent needs to know right now.

Read Article
Modern classroom with students using laptops and digital learning tools while a teacher guides the lesson, featuring the quote “Technology is not coming to education. It is already here. The question now is what we do with it.”
Technology in Education

Technology is not coming to education. It Is already here. The question now is what we do with it.

Read Article
A split-scene image showing a child relying on AI on a laptop looking disengaged, contrasted with another child actively studying with a notebook while a concerned parent observes, highlighting the impact of AI on children's learning habits.
Technology in Education

Is AI Making Our Children Lazy Learners? Here's What Every Parent Needs to Know

Read Article
Chat on WhatsApp