An in-depth analysis  on Python Variables. All you need to know.

An in-depth analysis on Python Variables. All you need to know.

In this article, you're going to learn everything you need to know about Python variables. This includes how to declare, initialize, and use them in your programs.

By the end of this article, you'll be well on your way to using variables like a pro!

Mathematical idea of a variable

Mathematically, a variable is a symbol and placeholder for any mathematical object. In particular, a variable may represent a number, a vector, a matrix, a function, the argument of a function, a set, or an element of a set. In algebra, a symbol (usually a letter) standing in for an unknown numerical value in an equation or an algebraic expression. In simple words, a variable is a quantity that can be changed from time to time.

What a python variable is

When a variable is incorporated into python code, it is used to store information. The information being stored can be a line of text, name of a person, number, etc. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.

Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals or characters in these variables. Most often, variables are viewed as assignment statements, returning no results with values being assigned to it.

X= 2
Y= 5

X and Y are the most common variables used in mathematics. In python, variable names should mean something or say something about what the variable represents.

Example

on the terminal create a variable called age which will store the age of an individual.

>>> age= 35
>>> print(“my age is”, age, “years”)

Output


my age is 35 years

Declaring python variables

Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign is used to assign values to variables. The operand to the left of the equal sign operator is the name of the variable and the operand to the right of the equal sign operator is the value stored in the variable

How to create a variable

To create a variable, pick a name e.g Height followed by the equal sign(=) and then the value you wish to assign the variable

>>> Height = 190

From the above, we have a variable Height that keeps the value 190. Now to use the variable, call it by name by passing it through the print function.

>>> print(“my height is”,
          Height, “cm”)

Output

my height is 190 cm

Rules for assigning variable names

  1. Variable names can only contain letters, numbers and underscores.
Special characters like $,", ’ , ?, /, -, @. #*, ! are not allowed as well as spaces.

  2. Variable names can not start with numbers.
"Rich2" is a valid variable name. "2Rich" is an invalid variable name.

  3. Variable names are case sensitive.
"rich" is not the same as"Rich”.

  4. Reserved words can not be used as variable names. 
"print" is an invalid variable name. "Print" is a valid variable name but it is not recommended. “print” can not be used as a variable name because it’s a name of a function and that it is reserved. “Print” can be used because print is not the same as Print.
More reserved words: if, elif, else, from, global, not, return, and, or, try, finally, lamda, while, for. You can search on google for python reserved words to avoid using them in your codes.

NB

The value of a variable is dynamic and can be changed at anytime.

>>> Height = 190
>>> Height =150

Since python codes are executed line by line,

>>> print(Height)

Output

150

What Are the Different Types of Variables in Python?

Variables are an important part of any programming language, and Python is no exception. In this article, we're going to take a closer look at variables in Python and what makes them so special.

There are three main types of variables in Python:

  1. Local variables: These are variables that are specific to a certain function or block of code. They're created when that function or code is executed, and they disappear when the function or code is finished.

  2. Class variables: These are shared by all instances of a class. As the name suggests, they're usually used to store data that is common to all objects of a class.

  3. Instance variables: These are specific to a certain object instance. They're created when the object is instantiated, and they disappear when the object is destroyed.

How to Delete Variables in Python?

Deleting a variable in Python is a pretty simple process. You use the del keyword to delete a variable, like this:

del Height

This will delete the Height variable. If you want to delete a list, you can use the del keyword with the list's name, like this:

del mylist

This will delete the mylist list.

Cont’d

Python variables are a fundamental part of the language. In this article, you learned the basics of variables and how to use them in your code. You should now be able to create variables, assign values to them, and use them in your programs.

Variables are important for two reasons: They make your code more readable, and they help you avoid repeating yourself. By using variables, you can store data in a single location and access it from anywhere in your program.

Variables are an essential part of any programming language, and Python is no exception. In this article, you learned the basics of Python variables and how to use them in your code.