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
3 min read
Sponsored
A variable in Python is a name that refers to a value. It lets you store and manipulate data in your program. 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:
-
Variable names must start with a letter or 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.
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
There are more complex data types in Python, such as lists, tuples, and dictionaries, but these are the most common ones.
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'
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) Sponsored
More from this category
More from Technology
Federal Agency Website Funding Restoration: What Digital Teams Should Do Next
R.02 Hackbat: Revolutionizing Embedded Security with Open-Source Hardware
Digital Healthcare Platforms: The $500B Developer Opportunity in 2026
Sponsored
The dispatch
Working notes from
the studio.
A short letter twice a month — what we shipped, what broke, and the AI tools earning their keep.
Discussion
Join the conversation.
Comments are powered by GitHub Discussions. Sign in with your GitHub account to leave a comment.
Sponsored