Variables

Think of a variable as a box in which you will store something to use later. On the box you put a label with a name that describes the contents. The name on the label is the variable name.

A variable name can be a combination of uppercase letters, lowercase letters, underscores, and digits (0-9). Hence, the following are valid identifiers: myClass, my_variable, var_1, and print_hello_world.

Special characters such as %, @, and $ are not allowed within variable names.

A variable name should not begin with a number. Hence, 2variable is not valid, but variable2 is acceptable.

Python is a case-sensitive language and this behaviour extends to identifiers. Thus, Students and students are two distinct variables in Python.

You cannot use Python keywords as variable names.

You can use underscores to separate multiple words in your variable’s name.

A variable name should be chosen to reflect what it stores. This makes the code easier to read.


Good names

counter

first_name

total

Bad names

x

a

my_variable


So why use variables? Image if you had to print the same sentence in 50 different places within your program. That’s 50 places you could make a typo or 50 places you need to update if you are told to change the text shown. By storing it in a variable you now only need to change it in a single place.

Report a Glow concern
Cookie policy  Privacy policy

Glow Blogs uses cookies to enhance your experience on our service. By using this service or closing this message you consent to our use of those cookies. Please read our Cookie Policy.