20
Variables in Python
As a beginner, have you ever wondered what variables are or why we even need them??
Variables act as a container to store different types of values. They are one of the most important concepts in any programming language and are used to store information that can be used in our programs.
the example below shows how to create a variable:
name = "john"
Every variable is connected to a value and we can access the value by calling the variable.
In the example above, "john"(the value) is assign to "name"(the variable).
In the example above, "john"(the value) is assign to "name"(the variable).
Anytime we need to print "john" we just call "name" as in the example below:
print(name)
# --- output ---
>>> john
you can change the value to be anything you want.
Need a number ??
age = 10
print(age)
# --- output ---
>>> 10
When you create*(declare)* a variable, there are a few rules you must follow.
Breaking some of these rules will cause errors in your programs.
Breaking some of these rules will cause errors in your programs.
two_words
)here are some examples of bad variable declaration
2schools = "One and two" # cannot start with a number
my name = "John" # cannot have spaces (replace with "my_name")
pass = 10 # cannot use a keyword
Variables may not seem interesting now, but they are very useful and you will use them frequently.
20