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?
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:
Here, my_variable
is a box that first holds a message. It was then cleared and held a number.
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:
