Python Variables and Data Types
Learning to program is a bit like learning a new language. One of the first things you’ll need to know is how to store and use information. In Python, we…
Learning to program is a bit like learning a new language. One of the first things you’ll need to know is how to store and use information. In Python, we do this with variables. Understanding variables and data types is your first big step toward writing real, useful programs. Let’s dive in!
What Are Variables in Python?
Think of a variable as a container or a box where you can store information. This information might be a number, a word, or something else entirely. When you want to use that information later in your program, you just refer to the variable’s name.
For example, if you want to remember someone’s age, you can store it in a variable:
age = 25
Here, age is the variable. It holds the value 25.
Why are variables important?
- They help us keep track of data in our programs.
- We can reuse and update the information as needed.
- Variables make our code easier to read and manage.
Whenever you hear "python variables," just remember: they’re the building blocks for storing and working with data in your code.
How to Name Variables
Before you start using variables, you need to give them names. Good variable names make your code easier to read and understand.
Here are some simple rules for naming variables in Python:
- Use letters, numbers, and underscores (
_). - Variable names cannot start with a number.
- Names are case-sensitive (
scoreandScoreare different). - Avoid using spaces or special characters.
- Don’t use Python’s reserved words (like
for,if,while).
Examples of good variable names:
user_name = "Alice"
total_score = 100
is_logged_in = True
Try to choose names that describe what the variable is storing. This will help 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 is important because different types of data are used in different ways.
For example, you can add numbers together, but you can’t add numbers to words (at least, not without special instructions).
Why do data types matter?
- They help Python know how to handle your data.
- They prevent mistakes and errors in your code.
- They allow you to use the right operations for the right kind of data.
Common Data Types: Numbers and Text
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 kinds of 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.
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 in variable names. 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.