Variables in Python
A variable in Python is a location reserved in memory for storing and holding a specific value. In other words, a variable in python gives data to the computer for processing. Variables in Python do not need to be declared before using them or specifying their type. The variable is created the moment we assign a value to it. The value stored in a variable can also be changed during program execution.
Simplify variable definition in Python
Think of a number. Any number. Now, before you forget this number, let's save it for later. When you think of a number, you keep that value in your head. If you want to remember it later and your memory is as poor as mine, write it down on a piece of paper. And if it's important, you'll put it somewhere safe. In computer science, this safe place is a variable. They are called (variables) because they are able to vary or change. You can name a variable (almost) anything you want and can instantly change the value while the program is executing.
Example:
I thought of the number 5, so in the Python prompt, I'll create a variable with the clever name "number", and enter its value:
number = 5
Variable naming rules in Python
- The variable name must begin with a letter or an underscore.
- The variable name cannot start with a number.
- A variable name can only contain letters, numbers, and underscores (Az, 0-9, and _).
- Variable names must begin with an alphabetic character or an underscore ( _ )
- Variable names are case sensitive (ahmed, Ahmed, and AHMED are three different variables).
- can not useundefined Reserved words (programming language keywords) for naming a variable.
Rules for creating variables in Python
In mathematics, variables are generally single letters such as x, y, and z, or Greek symbols such as π or θ. Mathematicians often use variables that they don't know a specific value for but that they work to find. It's different in Python. You must assign a value to a variable before you can use it, even if that value is zero or null. For example, if you call the counter variable before assigning a value to it:
counter
We will get the following error:
Traceback (most recent call last): File "", line 1, in
NameError: name 'counter' is not defined
Variable names can start with a letter or an underscore, like:
- _underscore
- underscore_
The rest of the variable name may consist of letters, numbers, and underscores.
- password1
- n00b
- underscores
Forms are acceptable in variants
counter = ""
And also
counter = False
Unacceptable forms in variables
A variable cannot be named with a number, nor can it be named with a quotation mark, for example
Example:
'' = counter
0 = counter
We will get the following error:
File "", line 1
SyntaxError: can't assign to literal
Also, reserved words (programming language keywords) cannot be used to name a variable. For example, False is a reserved word in Python that denotes a Boolean value, and you cannot use it as a name and identifier for a variable.
Example:
False = counter
We will get the following error:
File "<stdin>", line 1
SyntaxError: assignment to keyword</stdin>
Assigning values to variables in Python
As we can see in the example below, we use the assignment operator ( = ) to assign a value to a variable.
# assign value to site_name variable
site_name = 'ahmed obaid'
The operator to the left of the assignment operator (=) is the name of the variable and the operator to the right of the = operator is the value stored in the variable. for exampleundefined :
counter = 10 # An integer assignment
miles = 100.0 # A floating point
name = "ahmed" # A string
Assign one value to several variables
Python allows you to assign a single value to several variables at once. for example :
x = y = z = 10
Assigning multiple objects to multiple variables
You can also assign multiple objects to multiple variables. for example:
x = y = z = 10
Data types that are used in variables
The data stored in memory can be of several types. For example, a person's age is stored as a numeric value and their address is stored as alphanumeric characters. Python has different standard data types that are used to define the possible operations on it and the storage method for each.
There are five standard types of data that are used in variables:
The data types that are used in variables are explained in a separate article at this link
more information:
You may wish to refer to the following resources for additional information on this topic
If you have any questions? Leave it in the comments
Tags
Python python variables
Share page
About author
Ahmed Obaid
Hello, I am Ahmed Obaid, an Egyptian Arabic programmer. I would like to put my experiences learning Python on this site So that it will be a reference for you and me as well.
Sign up to leave a comment