Technology · Python
Variables and Data Types in Python: A Beginner's Guide
Learn Python variables and data types — integers, floats, strings, booleans, lists, tuples, and dictionaries with clear examples.
Anurag Verma
6 min read · Updated Sep 8, 2026
Sponsored
Python variables and data types are the first concepts most Python beginners need to learn before writing real programs. A variable is a name that points to a value, and Python decides the data type (integer, float, string, or boolean) from whatever you assign, without you declaring it up front. This guide covers how Python variables work, the core data types you’ll use daily, and how to check or convert between them. To make a variable, give it a name and assign it a value with the assignment operator (=). Here’s an example:
x = 5
Here we make a variable called x and assign it the value 5. Now, whenever we refer to x in our program, Python will replace it with the value 5.
Variable naming rules
Python variable names must start with a letter or the _ character, then any mix of letters, digits, and _. Names are case sensitive, so age and Age are two different variables holding two different values. You also cannot use a reserved keyword like if, else, while, or def as a name; Python raises a SyntaxError if you try.
-
Variable names must start with a letter or an underscore (_), followed by any combination of letters, underscores, and digits.
-
Variable names are case sensitive, so
xandXare two different variables. -
There are some reserved keywords in Python that you can’t use as variable names, such as
if,else,while, anddef.
The convention most Python code follows for variable and function names is snake_case: lowercase words separated by underscores, like user_name or total_price. Sticking to one convention matters more than which one you pick, because a mix of userName and user_name in the same file makes typos in either direction much harder to spot. The official Python style guide, PEP 8, documents this and the rest of the naming conventions the standard library itself follows.
The common data types
Python has several data types, each of which represents a different type of value. Here are some of the most common Python data types:
-
Integers: Integers (or
intfor short) represent whole numbers. For example:x = 5 y = -10 -
Floating-point numbers: Floating-point numbers (or
float) represent numbers with decimal places. For example:x = 3.14 y = -0.5 -
Strings: Strings (or
str) represent text. They’re created by enclosing a sequence of characters in single or double quotes. For example:x = 'Hello, world!' y = "Python is awesome" -
Booleans: Booleans (or
bool) represent True or False values. For example:x = True y = False
Under the hood, bool is actually a subclass of int: True behaves like 1 and False like 0 in arithmetic, which is why True + True evaluates to 2. Python integers also have no fixed size limit the way they do in languages like Java or C; a Python int can grow to hold a number as large as your machine’s memory allows.
There are more complex data types in Python, such as lists, tuples, and dictionaries. Our guide to Python’s built-in data structures covers those in depth, but the short version, mutability, is worth knowing here too.
Mutable vs immutable types
Some Python data types can be changed after creation, and some can’t; that distinction is called mutability, and it causes some of the more confusing bugs beginners run into. Strings, integers, floats, booleans, and tuples are all immutable: once created, their value can’t change. Lists and dictionaries are mutable: you can add, remove, or reassign their contents in place.
name = "Ann"
name = name + " Lee" # this creates a new string; it doesn't modify "Ann" in place
scores = [90, 85]
scores.append(70) # this modifies the same list in memory
The practical consequence: if you assign a mutable object to a second variable, both names point at the same underlying data, so changing it through one name changes what you see through the other.
a = [1, 2, 3]
b = a
b.append(4)
print(a) # [1, 2, 3, 4] — a changed too, because a and b point to the same list
If you wanted an independent copy instead, you’d need b = a.copy(). It’s one of the first real “gotchas” in Python, and worth internalizing early. Our step-by-step guide to Python basics walks through more of these early landmines.
Checking a variable’s type
The type() function can be used to determine the data type of a variable. As an example:
x = 5
print(type(x)) # Output: <class 'int'>
y = 'Hello, world!'
print(type(y)) # Output: <class 'str'>
Type conversion
Python has built-in functions for converting between different data types. For example, you can use the int() function to convert a string to an integer, or the str() function to convert a float to a string. Here are a couple of examples:
x = '5'
y = int(x) # Convert string to integer
print(y) # Output: 5
x = 3.14
y = str(x) # Convert float to string
print(y) # Output: '3.14'
Conversion only works when the value actually represents the target type: int("42") succeeds, but int("forty-two") raises a ValueError. This shows up constantly when parsing user input or a CSV file, where every value arrives as a string regardless of what it represents.
Formatting values with f-strings
Once you have variables of different types, you’ll usually want to combine them into one message, and an f-string is the most direct way to do that. Prefix a string with f and drop any variable inside curly braces, and Python inserts its value automatically:
name = "Ann"
age = 29
print(f"{name} is {age} years old.") # Ann is 29 years old.
F-strings also handle basic formatting, like fixing the number of decimal places on a float:
price = 19.999
print(f"${price:.2f}") # $20.00
F-strings arrived in Python 3.6 and have mostly replaced the older .format() and %-style formatting in new code, because they read left to right the same way the finished message does.
The None type
Python has a special data type called None. It is similar to null in other programming languages and represents the absence of a value. You can use None as a default value for a variable that hasn’t yet been assigned a value, or as a placeholder for a value that hasn’t yet been computed. Here’s an example:
x = None
if x is None:
print("x has no value yet")
else:
print("x has a value of", x)
Once variables and data types feel natural, list comprehensions are usually the next thing worth learning: a shorter, often faster way to build a list from an existing one. Our guide to Python list comprehensions picks up from exactly this point.
Frequently asked questions
- Do I need to declare a variable's type in Python?
- No. Python infers the type from the value you assign, and the same name can hold an integer now and a string later. That flexibility is convenient and it is also where a lot of beginner bugs come from, because a function receiving the wrong type often fails somewhere far from the assignment. Type hints exist for exactly this reason: they document your intent and let a checker catch mismatches without changing how the code runs.
- What's the difference between an integer and a float?
- An integer is a whole number with no fractional part; a float carries decimal places and is stored as a binary approximation. That approximation is why 0.1 + 0.2 does not give exactly 0.3 in Python or in most languages. For counting things, use int. For money, use the decimal module rather than float, because the approximation error compounds in ways that matter when the numbers are currency.
- Why does Python have None instead of just leaving a variable unset?
- Because "has no value yet" and "does not exist" are different states, and confusing them hides bugs. A variable set to None exists and can be passed around, checked, and given a real value later. An undefined name raises NameError. Using None as a placeholder for a value not yet computed makes that intent explicit, and checking it with is None rather than == None is the convention because it compares identity rather than invoking equality logic.
- When does type conversion fail?
- When the string does not represent the target type. int("42") works and int("forty-two") raises ValueError, as does int("4.2") because that is a float literal, not an integer one. This is a common trap with user input and CSV parsing, where everything arrives as a string and one bad row stops the whole job. Wrap the conversion in a try, or validate before converting.
- Are variable names really case sensitive?
- Yes, and it catches people out. x and X are two completely separate variables, as are userName and username. Python will not warn you; it will simply create a second variable and leave the first one holding its old value, which produces a bug that reads like the assignment did not take. The usual defence is a consistent convention, snake_case for variables and functions, applied without exception.
Sponsored
Sponsored
Discussion
Join the conversation.
Comments are powered by GitHub Discussions. Sign in with your GitHub account to leave a comment.
Sponsored