Introduction:
Imagine a variable in Python like a storage box. This box is labelled with a name (the variable’s name), and inside it, you can place one item of data, such as a number, a word, or even the result of a calculation. Just like how you can open and change the contents of a real storage box, in Python, you can change what’s stored in a variable at any time.
For example, let’s say you have a box labelled age. You can place the number 15 in it:
age = 15
Later on, if you want to update the value, you can put a new number in the same box:
age = 16
But here’s what’s really happening behind the scenes: when you create a variable in Python, you’re actually creating a location in your computer’s memory. This memory location is named (just like your storage box) and holds the data. While the variable’s name stays the same, the value inside (the data) can change as your program runs.
So, a variable is like a labelled storage box that can hold different pieces of data at different times as your program runs.
In Python, assigning a value to a variable is simple: use the equals sign =
.
For example:
name = "Alice" age = 15 total = 10 + 5
In the example above, the string "Alice"
is stored in the variable name
, the number 15
is stored in age
, and the result of 10 + 5
(which is 15
) is stored in total
.
Valid Variable Names
When naming variables, there are a few rules you need to follow:
- Variable names must start with a letter or an underscore (
_
), but they cannot start with a number. - Variable names can contain letters, numbers, and underscores, but no spaces or special characters.
- Variable names are case-sensitive, meaning
name
andName
are two different variables.
For example, the following are valid variable names:
student_name
age
total_marks
But these are invalid:
2nd_name
(can't start with a number)student name
(can't contain spaces)score!
(can't use special characters like!
)
Real-world application: Imagine creating a program for a school's student database. You might store a student's name, age, and grades in variables. This information can be used throughout your program to calculate averages or print reports.
Predict:
Before we start writing any code, take a look at this example and predict what it will do.
name = "Jake" score = 90 print("Student:", name) print("Score:", score)
What do you think the output will be? Have a think about it, then move on to the next section.
Run:
Now, let’s run a piece of code that assigns values to variables and uses them to display information. Try running this code which stores the name and age of a student in variables and prints them.
Run it to see what happens!
Concatenation: Combining Text and Numbers
Now that you understand how variables work, let’s talk about concatenation—how we can join different pieces of information (like text and numbers) together when using the print()
function.
In Python, you can concatenate using commas or plus signs (+
), but they work slightly differently.
Using Commas
- You can use commas in the
print()
function to combine both strings and numbers, and Python will automatically add spaces between the items.
Example:
name = "Alice" age = 15 print("Name:", name, ", Age:", age)
This will print:
Name: Alice, Age: 15
Using Plus Signs (+
)
- You can use the plus sign to join only strings. If you want to combine strings and numbers, you must first convert the numbers to strings using the
str()
function. - The plus sign does not add any spaces between items, so you have to include spaces yourself if needed.
Example:
name = "Alice" age = 15 print("Name:" + name + ", Age:" + str(age))
This will also print:
Name:Alice, Age:15
However, the a space will not be added during the concatenation (joining) and if you don’t convert the number age
to a string, you will get an error.
Investigate:
Now, let’s investigate how using commas and plus signs affects the way text and numbers are printed. Run the code below and observe the difference between the two methods of concatenation.
Instructions: Run the code and observe how the output differs when using commas versus plus signs. Try removing the str()
function and see what happens. Why does Python give an error when you try to concatenate a string and a number using +
?
Modify:
Here’s a piece of code that has an error. Your task is to modify the code so it correctly joins the strings and numbers using either commas or plus signs.
Hint: You’ll need to either use a comma in the print()
statement or convert the number age
to a string using str()
to fix this.