Let’s learn about Variables in Python

Helenjoy
3 min readMay 24, 2023

--

A variable is similar to a memory box in a computer that can hold a single value. You can save the outcome of an evaluated expression within a variable to use it later in your programme.

Variable Assignment

With the use of an assignment statement, you’ll save values in variables. A variable name, an equal sign (also known as the assignment operator), and the value to be stored make up an assignment statement. If you type the assignment statement var1 = 42, an integer value 42 will be put in the var1 variable.

Memory is like a box and value 42 is placed in the box ie memory with label(variable name) var1

When a value is first stored in a variable, it is initialized (or created). Following that, you can incorporate it into expressions with other variables and values.

Naming a variable

A good variable name should describe the information it holds. Consider moving to a new home and designating all of your moving boxes as “Stuff.” Nothing would ever be discovered!. so when var1 is assigned with a string hello it will be kept inside the var1-labeled box, When the print command comes it search in var1 box and takes out and print hello. When we reassign var1 to bye bye it takes out hello and replaces it with bye bye. In next print command, it shows bye bye. understand that it takes out hello and keeps bye bye.However, in your programs, a name that is descriptive will help make your code easier to read.

Python does have some naming constraints, even though you can give your variables practically any name. Legal variable names are shown in Table 1–3. Any name for a variable is acceptable as long as it complies with the following three rules:

It can be only one word with no spaces.

• It can use only letters, numbers, and the underscore (_) character.

• It can’t begin with a number.

Variable names are case-sensitive, meaning that spam, SPAM, Spam, and sPaM are four different variables. Though Spam is a valid variable you can use in a program, it is a Python convention to start your variables with a lowercase letter.

Happy learning…:)

--

--

Helenjoy

Research aspirant in deep learning based video compression