Back to: Python Programming
What is a variable?
A variable is a reusable container for storing a value, a variable behaves as if it were the value it contains (a bit like Algebra where x + 1 = 3, x is a container holding a value, in this case 2).
When we create a variable we would like to give a descriptive and unique name of what that variable contains – suppose I’m working with a users ‘Age’, I could declare a variable named age – age equals some value, let’s say 16. To print a variable you can place it within a print statement: (paste the following code into your IDE and see how it works)
# variable = a reusable container for storing a value
# a variable behaves as if it were the value it contains
age = 16
print(age) # note: no parenthesis (quotes)
When printing a variable, you do not use quotes, using quotes tells python to literally print what is within those quotes, which would print the word age and not the variable 16.
String Concatenation
To display a variable along with some text, like “You are 16 years old” where we pull in the value of the variable ‘age’, in some programming languages you’d write:
age = 16
print("You are" + age + "years old")
but here we get an error: TypeError: can only concatenate str (not “int”) to str .
In Python you can NOT add a ‘string’ to an ‘integer’, we need to ‘type cast’ (convert) the ‘integer’ value to a ‘string’ first by preceding it with ‘str’.
age = 16
print("You are " + str(age) + " years old")
Separate Arguments
Another way to display a variable with some text is to separate the text and variables into separate arguments.
age = 16
print("You are", age, "years old")
F-Strings
Another popular way to achieve this is to use f-strings, by preceding the string with an ‘f’.
age = 16
print(f"You are {age} years old")
f-strings are becoming much more popular for output and we will be using them in most instances.
Data Types
There are four basic data types in Python that we’ll be utilising:
- Integer
- Float
- String
- Boolean
Integer
Consists of data which is a ‘whole number’.
minimum_lenth = 27
bag_limit = 10
print("The minimum legal size for catching a")
print(f"King George Whiting is {minimum_lenth} cm")
print(f"and you can catch up to {bag_limit} per day.")
Float
A float is a number that contains a decimal portion, like a distance or price.
distance = 4.5
price = 2.25
print(f"To travel {distance}km will cost ${price} in 2024.")
String
A variable holding a piece of text. Note that numbers wil be treated as strings if encapsulated in quotes.
year = "2024"
school = "Norwood Secondary College"
teacher = "Mr Dickinson"
print(f"In {year} I attended {school} and")
print(f"my favourite teacher was {teacher}.")
Boolean
A Boolean is either ‘true’ or ‘false’, it is binary, like a light switch, it’s either on or off. We don’t usually print the value of a Boolean, they are mostly used internally to with conditional logic.
public_holiday = True
# unlikely to be used this way
print(f"Today is a public holiday: {public_holiday}")
# more likely to be used this way
if public_holiday:
print("We are closed due to the public holiday!")
else:
print("We are open!")
Multiple Assignment
You can declare multiple variable on a single line, which can save some time.
| Instead of: | Try: | Also |
|---|---|---|
| x = 1 y = 2 z = 3 print(x) print(y) print(z) | x, y, z = 1, 2, 3 print(x) print(y) print(z) | x = y = z = 20 print(x) print(y) print(z) |
Python Variables & Data Types – The Building Blocks of Code – (8m:11s)
The Rules of Naming Variables in Python – (3m:17s)