Python Variables

Python Variables – Overview

Python Variables are labels (name) of a memory location used to store Variables’ value. You can print the variable (e.g. strings) value contained in the memory location.

On this page, you’ll:

  • Learn what a label is in Python
  • Explore the meaning a Variable
  • Learn the rules of naming a variable
  • Find how to assign a value to a variable

What is Label

The label is a location in the computer’s memory, where the value of the variable is stored. This label name of the variable is associated with this memory location. The memory location can be written to (i.e. changed), thus changing the variable’s value.  The variable can be a number, a string, a list, or another data type. More on the data types in a later chapter.

What is a Variable

In this section, we will explain what Variables are. The computer memory stores billions of bits of data. Your computer program needs to access this data to do the calculations. How does your program find this data?  Let us explain this with an example. Let us say you are 7 years old.  You want to store your age on the computer.  How do we do that?  We define a variable named ‘My_Age’ and store the number 7 in this variable. It is called a variable because the values you store can be changed (varied) by you or by your program. 

But how about if you do not want to store a number; instead you want to store the name of your school? Well, you can do that too. If your school’s name is ‘Bright Elementary School’, you define another variable called ‘school’ and store the words ‘Bright Elementary School’ in it.  Such a variable is called a string variable.  We will explain variables and strings by using examples ‘My_Age’ and string ‘Bright Elementary School’.

Python Variables Declaration?

As mentioned in the introduction above, let us suppose you want to store a value of 7 in a variable name “My_Age” (you can choose any name you like for the variable). The value of 7 is assigned to the “My_Age” by using the equal (=) symbol.

At the prompt sign (>>>) in the Python shell window, you will write the below statement.

>>> My_Age = 7

With the above statement, Python reserves a location in the computer memory with the label “My_Age”, and a number 7 is stored in this memory location.  A program or you (through the keyboard) can change the value stored in this memory location.  Now, can you guess why it is called a variable? If you guessed the reason is that since the value stored in the variable can be changed (varied), it is called a variable, your guess is correct.

Let us now find out what value is contained in the memory location we labeled as variable “My_Age”. We will use the print() function to print the value stored in the variable “My_Age”. The Python program below assigns a value of 7 to the variable “My_Age” and prints the value stored in the memory location labeled ‘My_Age’.

PROGRAM EXAMPLE: DECLARING A VARIABLE
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> My_age = 7
>>> print(My_age)
7
>>>

You can see in the above program that the Python print() statement outputs the value 7. We will discuss the print() function in a later Printing-in-Python chapter.

Now suppose a year goes by and you are now 8. So, the above program you wrote last year needs to be updated. You would do the following change to the program you wrote last year.

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> My_Age = 8
>>> print(My_Age)
8
>>>

You see now that the location in memory with label “My_Age” is now contains 8, not 7.

Rules for Naming Python Variables

A variable (label) name can consist of letters, numbers, and the underscore (_) symbol.  It can be any number of characters from one character to a long sentence. A variable cannot start with a number as its first character.  Also, space is not allowed in the variable name.  Variable names are case-sensitive.  So, the name ‘soccer’ is different from the name ‘Soccer’.

Detailed rules for naming Python variables and identifiers are in the link below”:

<a href=”https://docs.python.org/3/reference/lexical_analysis.html#identifiers“>

Python Variables Types

Python has many different types of data types. Every value in Python has a data type. Different data types in Python are Number, List, Tuple, String, Dictionary, etc.

We will discuss Variable Types in detail on the Python Data Types page, where we introduce the concept of escape characters.

Copyright © 2019 – 2024 softwareprogramming4kids.com

All Rights Reserved


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Verified by MonsterInsights