Introduction:
Let’s start by revisiting variables—one of the most important concepts in programming. Think of a variable like a storage box with a label on it. This box can hold different types of data, like numbers, text, or even True
and False
values. As your program runs, the data inside the box (the variable) can change, just like you could replace what’s in a physical storage box. For example, you might store a person’s age in a variable, and that number could change over time.
Example:
age = 15 age = 16 # Now the value has changed
But what if we have data that shouldn’t change while the program is running? This is where constants come in. Constants are like permanent markers—you write the value down once, and it stays the same. For example, in many programming languages, you might have a constant for something like the value of pi (π), which doesn't change. While Python doesn’t have a strict way to declare constants, we follow a convention by writing them in ALL CAPS to show that they shouldn’t change.
Example:
PI = 3.14159 # This is a constant, by convention we write it in uppercase
Data Types
Variables and constants are used to store data, and this data can come in different types. For example, the data could be a number, a word, or even a True
or False
value. It’s important for Python to know what type of data is being stored because it affects what can be done with it.
By default, Python often treats data as text (a string), but sometimes we need to perform calculations or comparisons on the data. If the data is a number and we want to add, subtract, or perform other mathematical operations, we need to tell Python that the data is a number—either an integer or a float (a decimal number). Without specifying this, Python might get confused or won’t know how to handle the operation.
For instance, if we want to add two numbers stored in variables, Python must know that the data in those variables is numeric, not text. If Python thinks it’s working with text, it won’t perform mathematical operations but will instead combine the pieces of text (concatenation).
Example:
# If we want to perform a calculation: age = "10" # Python assumes this is a string print(age + 5) # This will give an error because age is treated as text! # We need to convert it to a number (integer): age = int("10") # Now age is an integer print(age + 5) # This will give the correct result: 15
This is why data types are so important in Python. They tell Python how to treat the data in the variable. Is it a number you want to add? Is it a string of text? Is it a true or false value for decision-making?
Here are some common data types in Python:
- Integers: Whole numbers like
42
or-7
. - Floats: Decimal numbers like
3.14
or-2.5
. - Strings: Text, like
"Hello, World!"
or"123"
(which is still considered text until we convert it). - Booleans:
True
orFalse
values, used for decision-making in your programs.
When you want to perform certain actions with your data—like calculations or comparisons—you must make sure that Python knows what type of data it’s working with. You can also convert between data types using functions like int()
, float()
, and str()
depending on what you want to do with the data.
Understanding data types helps Python handle your variables correctly and allows your program to work as expected.
Converting Between Data Types
As we just learnt, sometimes, we need to change the type of data we’re working with so that Python can perform the correct operations on the data it is working with. Imagine needing to change the type of data that is in your variable—maybe you want to turn a string into a number, or a number into a string. Python allows you to do this with built-in functions like int()
, float()
, and str()
.
Example:
num_str = "10" # This is a string num = int(num_str) # Now it's converted into an integer value = 3.14 value_str = str(value) # Converts a float to a string
In summary:
- Variables are like boxes where you store data, and the contents can change.
- Constants are like boxes that don’t change once set, but in Python, we simply capitalise the name to show it shouldn’t change.
- Data types help the program know how to handle different kinds of data (numbers, text, true/false values).
- Python allows you to convert between these data types when needed and this is called casting.
Now that you have a clear understanding of variables, constants, and data types, let’s explore them in action!
Predict:
Look at the following code. Can you predict what it will output? Try to think about what each line does.
print(int(3.9)) # Converts float to integer print(str(10) + " dogs") # Concatenates a string and a number print(5.0 / 2) # Division resulting in a float print(True and False) # Boolean logic
What do you think will be printed when this code is run? Try to predict the output before moving on.
Run:
Now, run the following code in the embedded IDE and observe how Python handles different data types and conversions.
Investigate:
Let’s investigate how Python handles data type conversions and operations. Run the following code and observe what happens.
Notice how we use a constant (PI
) and perform conversions between strings, integers, and Booleans. Try modifying some values and observe the changes.
Modify:
Here’s a program with an error. Can you fix it? The goal is to correctly calculate the area of a circle. The code has an issue when trying to convert the string input into a float for calculations.
Hint: You need to convert the string radius
to a float before performing the calculation.