Variables and Data Types in Python
Like you learnt last lesson, variables are like boxes that can hold different types of data. Let's explore some of the most common ones.
Data Types: Different Flavours of Information
Just like ice cream comes in different flavours, data in Python comes in different types:
| Data types | Meaning | Example |
|---|---|---|
| Strings | Alphanumeric characters - text wrapped in quotes | "Hello, Python!" |
| Integers | Whole numbers | 42 or -10 |
| Floats | Numbers with decimal points | 3.14 or -0.5 |
| Boolean | True or False values | True or False |
Input: Getting Information from Users
Remember the input() function? It's super helpful,
but it has a secret: it always gives us a string, even if the
user has entered a number!
age = input("How old are you? ")
For example, if the user enters 12, the variable age will hold the
string "12", not the integer 12.
Casting: Changing Data Types
Sometimes we need to change the type of our data e.g. from a string to an integer. We can do this with casting.
age = int(input("How old are you? "))
Now if the user enters 12, the variable age will hold
the integer 12, which we can use for example in maths.
If we want to output the value, print() expects age to be a string. This means we need to use
str(age) to cast 12back to the string "12" for us to output it.
print("You are " + str(age) + " years old.")
print("Next year you will be " + str(age + 1) + " years old.")
Mathematical Operators: Doing Maths with Python
Python is great at maths. Here are some operators (symbols) you can use:
| Operator | Function |
|---|---|
+ |
addition |
- |
subtraction |
* |
multiplication |
/ |
division |
Updating Variables: Changing Values
Variables are not only for storing values - we can also change them as our program runs. They can vary. This is super useful for keeping track of data that changes, like scores in a game.
Here's how we can update a variable:
num = 4
# num is 4
num = num + 2
# now num is 6
In this example, num starts as 4. Then we add 2 to
it and store the result back in num. So
num becomes 6!
We can do this with any mathematical operation. Here are a few more examples:
num = num * 3 # Multiplies num by 3
num = num - 1 # Subtracts 1 from num
num = num / 2 # Divides num by 2
This is really powerful because it lets our variables change as our program runs. Try using them in your next program!
Putting It All Together
Let's use everything we've learned to make a program:
days = int(input("Give me a number of days: "))
hours = days * 24
print("There are", hours, "hours in", days, "days")
print("There are " + str(hours) + " hours in " + str(days) + " days")
This program:
- Asks the user to input a number of days,
- Casts the response to an integer,
- Calculates the number of hours,
- Outputs a final message with the numbers concatenated in.
Now it's your turn! Can you make a program that asks for someone's age and then calculates how many months they've been alive? (Hint: multiply their age by 12!) Press inspire me! for more ideas of what to code.
Write a program to...