Lesson 2

Mastering Python Variables: Dive into Variables Definition, Naming, Usage, and Display

Lesson Introduction and Overview

Welcome to the exciting world of Python programming! In today's session, we aim to understand a key concept in coding: Python variables. Consider a variable as a handy box that can hold different types of goodies. These "goodies" could be numbers, text, or even more complex data types like lists or dictionaries, that we will cover in the next courses. Our plan for this lesson is to first establish a firm understanding of what variables are. We'll then delve into defining variables, assigning values (or goodies!) to them, and understanding what constitutes a name in Python. Finally, we'll display our accomplishments by learning how to print the value held by a variable. Isn't that exciting?

Understanding Python Variables

Consider a box. You could use this box to hold different items - perhaps books, your favorite snacks, or perhaps even some adorable little figurines you collect. Variables in Python function just like this box. They're used to store different types of values. The best part is that much like how you can change what you keep in the box at any time, the same applies to variables - the type of value they hold can change. Let's see this concept in action with some code:

Python
1my_variable = "Hello, world!" 2print(my_variable) # This will print "Hello, world!" 3 4my_variable = 123 5print(my_variable) # This will print 123

Here, my_variable is a box that first holds a message. It was then cleared and held a number.

Defining and Naming Variables in Python

In Python, we create a variable and assign it a value in one line using the assignment operator (=). It's the same process as putting something in a box and labeling it to remember what's inside. When naming these variable boxes in Python, we must follow some rules: they must start with a letter (a-z, A-Z) or underscore (_), and they can include any combination of letters, digits (0-9), or underscores thereafter. For example:

Python
1x = 7 # valid: starts with a letter 2myVariable = 7 # valid: starts with a letter 3_myVariable = 7 # valid: starts with an underscore 47x = 7 # invalid - starts with a digit 5x!y = 7 # invalid - contains the prohibited character '!'

In the code block above, x, myVariable, and _myVariable are all valid names, whereas 7x and x!y are not.

Printing Variables in Python

Now that we understand what our variable box contains and have even created new values with it, it's time to show off our treasures. In Python, we can use the print() function to print the value our variable contains. We can also combine multiple expressions within the same print() statement, joining them with the comma! It's like opening our box and revealing what's inside:

Python
1name = "John" 2print("The name is", name) # Prints "The name is John" 3 4age = 30 5print("The person is", age, "years old") # Prints "The person is 30 years old"

We find that using the print() function displays our variable's value for everyone to see. You can also mention that when using multiple expressions in the print() statement, they are automatically joined and separated by a single whitespace.

Wrapping up

Congratulations! You've just mastered Python variables! You learned how to define variables, give them names, and print their stored values. These are fundamental steps toward becoming a Python programmer.

Are you ready to apply your newfound knowledge? We have some exciting practice exercises coming up next. These exercises will help you consolidate what you learned today and apply it practically. Stay tuned as we reinforce our understanding of Python variables.

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.