Python Variables and Data Types
Learning Python starts with understanding how to store and work with information. In Python, we use variables to hold data, and each piece of data has a…
Learning Python starts with understanding how to store and work with information. In Python, we use variables to hold data, and each piece of data has a type. Mastering these basics is your first step toward writing real programs. Let’s get started!
What Are Variables in Python?
A variable is like a labeled box where you can store information. That information might be a number, some text, or another kind of value. When you want to use that information later, you just use the variable’s name.
For example, to store someone’s age:
age = 25
Here, age is the variable, and it holds the value 25.
Why use variables?
- They help you keep track of information in your programs.
- You can update or reuse the data easily.
- Variables make your code easier to read and manage.
Whenever you hear about "python variables," think of them as the building blocks for storing and working with data in your code.
How to Name Variables
Before using a variable, you need to give it a name. Good names make your code easier to understand.
Rules for naming variables in Python:
- Use letters, numbers, and underscores (
_). - Variable names cannot start with a number.
- Names are case-sensitive (
scoreandScoreare different). - No spaces or special characters.
- Avoid using Python’s reserved words (like
for,if,while).
Examples of good variable names:
user_name = "Alice"
total_score = 100
is_logged_in = True
Choose names that describe what the variable stores. This helps you and others understand your code later.
Quiz Question 1
Question: Which of the following is a valid variable name in Python?
- A) user_name
- B) 2score
- C) total score
- D) user-name
Introduction to Data Types
Every variable in Python has a data type. A data type tells Python what kind of information is being stored. This matters because different types of data are used in different ways.
For example, you can add numbers together, but you can’t add a number to a word without converting one of them first.
Why do data types matter?
- They help Python know how to handle your data.
- They help prevent mistakes in your code.
- They let you use the right operations for each kind of data.
Common Data Types: Numbers and Text
Variables in Python act as labeled boxes that can store different types of data—whole numbers (int), decimal numbers (float), and text (str). This diagram helps you visualize how each variable holds a specific data type.
Let’s look at the most common data types you’ll use as a beginner: numbers and text.
Integers (int)
An integer (or int) is a whole number. It can be positive, negative, or zero.
age = 30
score = -7
Floating Point Numbers (float)
A float is a number with a decimal point.
temperature = 22.5
height = -1.75
Strings (str)
A string (or str) is a sequence of characters, used for text. Strings are always surrounded by quotes (single ' or double ").
name = "Alice"
greeting = 'Hello, world!'
Quick recap:
intfor whole numbersfloatfor numbers with decimalsstrfor text
Quiz Question 2
Question: What is the data type of the variable price in the following code?
price = "19.99"
- A) int
- B) float
- C) str
- D) bool
Why Data Types Matter
Choosing the right data type is important because it affects what you can do with your variables.
- You can add two integers or two floats.
- You can join (concatenate) two strings.
- But if you try to add a number and a string, Python will give you an error.
Understanding data types helps you avoid these mistakes and write code that works as expected.
Changing Data Types (Type Conversion)
Sometimes, you’ll need to change a variable from one data type to another. This is called type conversion or casting.
Here are some simple ways to convert between common data types:
- Turn a number into a string:
age = 25 age_str = str(age) # "25" - Turn a string into an integer (if the string contains a valid number):
year = "2024" year_int = int(year) # 2024 - Turn a string into a float:
price = "19.99" price_float = float(price) # 19.99
Be careful: if you try to convert a string that doesn’t look like a number, Python will give you an error.
More Data Types to Explore
As you continue learning Python, you’ll encounter other useful data types:
- Boolean (
bool): RepresentsTrueorFalsevalues, often used for decisions. - List (
list): Stores a collection of items in a specific order. - Dictionary (
dict): Stores key-value pairs for fast lookups.
For now, focus on mastering int, float, and str. These are the foundation of most beginner Python programs.
Practice: Identifying Variables and Data Types
Let’s put your new knowledge to the test! Look at the following code and see if you can identify the variables and their data types:
city = "London"
population = 9000000
average_temp = 15.5
Questions:
- What is the data type of
city? - What about
population? - And
average_temp?
Try this: Think about what data type you would use for these pieces of information:
- A user’s email address
- The number of items in a shopping cart
- The price of a product
Frequently Asked Questions
Can I use spaces in variable names?
No, spaces are not allowed. Use underscores (_) instead.
What happens if I try to add a string and an integer together?
Python will give you an error. You need to convert one to match the other’s type first.
How do I know which data type to use for my variable?
Think about what kind of information you’re storing: numbers use int or float, and text uses str.
Is there a way to change a variable from a string to a number?
Yes! Use int() or float() to convert strings that look like numbers.
Are variable names like 'Age' and 'age' considered the same in Python?
No, Python is case-sensitive. Age and age are different variables.
Can variable names start with a number?
No, variable names cannot start with a number.
What is the difference between an int and a float?
An int is a whole number. A float is a number with a decimal point.
Why do I get an error when I try to convert some strings to numbers?
If the string doesn’t look like a number (for example, "hello"), Python can’t convert it.
Do I have to declare the data type of a variable before using it in Python?
No, Python figures out the data type automatically based on the value you assign.
Can I use special characters like @ or $ in variable names?
No, only letters, numbers, and underscores are allowed.
Conclusion
Variables and data types are the foundation of every Python program. By understanding how to use python variables and choosing the right python data types (like int, float, and str), you’re building the skills you’ll need for more advanced programming.
Keep practicing! Try creating your own variables and experimenting with different data types. As you continue your Python journey, these basics will help you tackle bigger and more exciting projects.
Ready for the next step? Soon, you’ll learn how to use your variables to make decisions, repeat actions, and store collections of data. You’re off to a great start!
Quiz Answer Key
Question 1
Correct answer: A) user_name
Explanation: Variable names can use letters, numbers, and underscores, but cannot start with a number or include spaces or special characters.
Question 2
Correct answer: C) str
Explanation: The value is surrounded by quotes, so it is a string (str), even though it looks like a number.
Want a more structured Python path?
Use the Python Starter Pack to turn scattered tutorials into a focused practice path.
Python Starter Pack
A compact LearnPyFast PDF pack covering what Python is, installation, your first program, running Python code, and Python versions.
- 5 curated chapters
- Enhanced PDF edition with bundle-only learning guidance
- Offline-friendly format for focused review
- Source article links for future online updates
Coming soon