Post thumbnail
PROGRAMMING LANGUAGES

Do You Know How To Create Variables In Python?

By Archana

How to create variables in Python? Give examples?

This is a popular question that we often come across from Python beginners.

image

Python offers a controlled information kind of record as a real language. Firstly, the name “variable” suggests something that can easily be changed. The variable considers a method alluding to the specific memory location used by the PC programs. 

The name of the variable can be emblematic that is specified to any physical area. Therefore, it is noticeable that variables play an important role in Python. Hence, it becomes necessary to know how to create variables in Python.

But before proceeding to the details, let’s take an overview of what are variables in Python. This will help the beginners to understand the variables efficiently. Alternatively, if you would like to explore Python through a Self-paced course, try GUVI’s Python Self-Paced certification course.

Table of contents


    • What are the variables in Python?
    • Create a Set using String
    • How to use variable key names to get the effect of variable variables in Python?
    • Is there any rule for creating variables in Python?
    • How to declare and re-declare the variable?
    • How to assign the new value or name to the previous variable?
    • Can I use the ‘+’ operator for different types?
    • Let’s check your knowledge of what you have understood in this blog!
    • Bottom Line

What are the variables in Python?

A python variable is all about reserving the memory location that can easily store the values. In simple words, a variable in Python programs provides the data to a computer to process it effectively. 

Data types in Python:

Each value present in Python has a specific data type, and each data type can be a List, String, Number, Tuple, Dictionary, and so on. The variable in Python can be declared as a name or an alphabet such as b, bb, abc, and so on.

Example of types of Variable in Python

X = 50

Variable

Type = Integer

Value = 50

Name = X

Name = ‘ABC’

Variable

Type = String

Value = ABC

Name = ‘Name

num = [ 5,9.5, 10 ]

Variable

Type = List

Value = [ 5, 9.5, 10 ]

Name = Num

Data types are the categories and classifications of different data items. And it is used to represent the values that help you know operations that have been performed over specific data. 

As we know, each thing in Python is considered an object, so data types are variables and classes of the specific classes.

Below are built-in or standard data types in Python:

  • Dictionary
  • Numeric
  • Sequence Type
  • Boolean
  • Set

Create a Dictionary using the Integer Keys 

dictionary using integer keys

Output

Dictionary using Integer Keys:  {1: ‘p’, 2: ‘q’, 3: ‘r’}

Numeric

image 2

Output

Number data:  456

Sequence Type 

DmIThI2AP617 3rBSvVJpu2QEJoID2Dn642 d TeR12yj ZAlDDK5lv9K0IxMa3A2ae5oeDyxO18ccS5UXcaWHJREbqTeWAcGj88IAge 2wYvbYWoq5G lOQh agBmyAJs5S vs

Output

A string that has Single Quotes: 

Hello World

Boolean

XHMCJz REe4ZW2nKnpNg1pOH9WbYDc zbhJDrAc8xjBNYIjmHVKxxA6vxwZTn7jmAzLAS CAhaB5o10UvURaM 2kkJW26xVGkNCUdMPsN8sG19IMhsk5Ga4nKfGvLk aH9GePY8

Output

<class ‘bool’>

<class ‘bool’>

MDN

Create a Set using String 

7jF0L vop n6 Hmi9zC4KUmG5KEvImGi2nnhz8JTxMTfxAAoyCXFLQBvypttgWMnLEOrvYJBQjdCVFzF0obsEevB0Vwx6PEojPH9P2gdb5 l8KBEfvL1vakSJCP rVL9FLcVjY

Output

Set using String: 

{‘A’, ‘B’, ‘C’}

NOTE: The list is used to represent the order sequence of the particular object with integer indices. So, a list is more suitable to use where you have some doubt about which variable is created for the particular value. Example:

ra6bn5aYCBZ2Qfi8vA0PcI3qNJVccFZc8U4x26US7I6z93i8nxaNqBg2W7vwa XZISLsOO4wuOCNYcmg8r1bvmnWkWAYUX0uldtF7PcoT72 Drd87NHLqDuH0Hw8N0DwfRlPc2U

Output:

BALL

[‘APPLE’, ‘BALL’, ‘CAT’, ‘DOG’]

It has been seen that list is a more convenient method than a dict for ordered sequences as list support iterations in the index order.

Key points about Variable in Python
A variable is just a name given to a memory location; the variable’s operations will affect the memory location. 
The value that is stored in the variable can be altered at the time of program execution.

How to use variable key names to get the effect of variable variables in Python?

To create the variable variables in Python, the user can use dictionaries to store the values with the keys. Remember:  it is also possible to delete any of the keys: value pairs using the del. 

If you store a value with the help of a key, which is already in use, then the previous value of that key will be replaced with the new value or forgotten. So, let’s check the effect of variables variable in Python.

ZET703ZTHtWK4ij8brgcvoXIVYyL4OlerdXfwjmt6iD1gDqU u T8gzlIrE 2kaJPWEJ o6sg13AR3HxYu05SJizE5BSy27zdGmDnVygehwiamLR1 cw40QHS1piAsaNNy0qNBQ

Output

{‘Enna’: 1234, ‘Jack’: 1478, ‘Tina’: 5678}

LZmivksKdOgIaZ SL1iU3T2mkhF1HWpZF0OLXXF1ODZdzSGGyTKUrjk4PJIEh0bNj8wY7mmyIyE50Lp0aJ7

Output

{‘Enna’: 7531, ‘Tina’: 5678}

nHGicmQy8L6sWSLFmCSmSH S4xke0BQiOv6iQ5zsFIuFlIS Y0V0bPa5CR61VGFdOlreim7pc6op8nd3Z49CayzzUBduY h0PvdLF 7dokdyU85q5iatyYN3Ynfr02bgJut6huA

Output

1234

{‘Enna’:1234, ‘Tina:5678, ‘Jack’:’1478}

EueyaBDPNCr6bqZONid8A CdOlrbjaLPWXawljclK4m6xczsZFATYe5VBQ6qT8OVAhjerpaMVlOg0Ab35b7 EC4r4hogNsd5 NY n00M

Output

{‘Tina’:5678, ‘Jack’:1478}

Is there any rule for creating variables in Python?

Yes, there are certain rules that users need to follow for creating a variable in Python. And the rules are as follows:

Is there any rule for creating variables in Python
  • The variable’s name must start with an underscore character or a letter.
  • A number can not be used at the starting of the variable name.
  • The variable name can be defined using the underscore and alpha-numeric characters, like 0-9, A-z, and _, so on.
  • The reversed keywords (words) are not applicable to define the name of the variable.
  • The names of variables are case-sensitive. It means abc, ABC, Abc are three different variables in Python.

Now, let’s check how to create variables in Python!!!

To know how to create a variable in Python, you need to use any numeric or alphabetical variable and specify a numeric value to it.

Let’s take an example of it (Code in Python 3):

k4bPUdLfNB9B9V3JzCjVr

Output

Jerry

2.5

1000

How to declare and re-declare the variable? 

Now, you know how to create variables in Python. Let’s check an example to declare a variable in Python and print it.

4wjZW5D U52uVoFMRxsiA2GvdvMNTK7tFdvKm3P6NVeEqOV3yyWGbMQvlqiPNdMu6F5DU 4qCTAXQAtklns2qSHyJHxM8PzsIjXy

Output

10

Now, let’s re-declare a variable in Python and print the relevant output.

MKo3wok6I5sgnNpvoRYsBv95bRRULE2 7omM

Output

Before declaration: 10

After re-declaration: 20.5

Is it possible to assign a single or different value to multiple variables?

Yes, it is possible. How?

Using the “=” operator, the user can assign a single value to various variables. For example,

aQFP2YQCz4qAyzmx12wLLYoBOp2rhwo9sIySCNiUg pY6Yaya4gG3YpteHAW6mjCRGpgGRZUjpDwp

Output

5.5

5.5

5.5

Python enables its user to assign a different value to multiple variables using the operator “,” for example

Output

10

15.5

abc

How to assign the new value or name to the previous variable?

If you use a similar name for the variable, it will give the new type and value as an output. For example:

Output 

PQR

Can I use the ‘+’ operator for different types?

No, you can’t!! But it does not mean that you can not use the ‘+’ operator. The below example helps you to understand how to use the ‘+’  operator.

E7vI8NjFLQoHiOhviQ1BknbGu3 jgm5U2mnCWfLnlHWnhZ4AviaolTQLduByun juNotRFk6IcZOWfEJW R1Ex5CkMvOeo r4SWnztsCK0hpjJbOruDYDFEYGJhEQB sYPHwofs

Output

10.0

HelloWorld

Types of variables in Python: What is the difference between Local & Global variables?

When a user uses a single variable for an overall module or program, he/she needs to create the variable as a global variable

But, in the case the user wants to utilize it for a particular function, he/she can use the variable as a local variable when the Python variable declares.

Let’s take an example to understand the difference.

rvblNPXTuUfjdLIhZBZzZy4HxsBzdKE3U6qC1sKinL5gbJabf3Jx1bpQ64yBeImU7IkYpOoFHcrGrDWe5r4UCSl G9Ieg7dwhK1CKAtLKY39Qk472wFo2A0XkFxSQGyfCC q 7U

Output

50

Hello World

50

  1. Firstly, create a variable in the Python “x” as a global variable with the value 50.

2. Again variable x declares within the function with a local scope with the value”Hello World.” This has a different scope as that of global variables declared before.

3. When the variable is called, the local variable x will be eliminated. And it will print the value of “x” that is assigned as a global variable. That is x =50.

In the same vein, when the Python variable is declared with the help of keyword global, the user can declare it within the function.

XMpn8rF KvpZQXbJDY30p2g YcIXQ avz0KzztLzBhMzObSUjcckUWHVrME66O39Tbgh

Output

50

50

Hello World

BONUS:
How to delete the variable in Python?
Users can delete the variable with the help of the “del” command with the variable name.
In the given example, we have deleted the variable x. When you run the program, it will show an error as “name ‘x’ is not defined” that declares that the variable has been deleted from the Python code.
x = 50;print(x)
del x
print(x)
NameError: name ‘x’ is not defined

Let’s check your knowledge of what you have understood in this blog!

  1. Which of the following is the proper method to declare and initialize a variable, a with the value 2?
  • a) int a = 2
  • b) int a a = 2
  • c) declare a = 2
  • d) a = 2

Correct Answer:

Option d) – As there is no need to write int and declare to initialize the variable.

2. Select the one which is not a valid variable name in Python?

  • a) Var_name
  • b) _var
  • c) 11var
  • d) var11

Correct Answer:

Option c)- As you can not define the variable name as 11var in Python.

3. Which of these statements is incorrect about variables in Python?

  • a) The variable name begins with an underscore.
  • b) Variable names in Python do not begin with a number. Still, it may have numbers in another position of a variable name.
  • c) The variable name’s data type need not be declared.
  • d) None of these

Correct Answer:

Option d) – The above statements are true about variables in Python.

4. Which of these statements is incorrect?

  • a) The variable names in Python could be arbitrarily long.
  • b) Variable names in Python can start with any of the numbers.
  • c) Names of variables in Python can contain both numbers and letters.
  • d) The variable name also begins with an underscore.

Correct Answer:

Option b)- The variable name in Python can not be started with any of the numbers. It can begin with an underscore or letter. Therefore, Option 2 has an incorrect statement.

5. Which of these is a valid variable?

  • a) abc_a_c 
  • b) 32var
  • c) var@
  • d) class

Correct Answer

Option a) – As a variable name never be a keyword. And it can not start with any digit, and it must not have a special symbol. 
MDN

Bottom Line

The Python variable is considered to be a symbolic name, which is a pointer or reference to the object. Once you define an object to the variable, you easily call it by the name. Therefore, we can conclude that the variable in Python can help you easily call the value using the name. 

Moreover, it is also necessary that the user must know how to create variables in Python. We are glad that we have provided all the necessary details about it with relevant examples. Hope you understand each example easily. So, keep on practicing and become the master of using variables in Python.

PYTHON with IIT CERTIFICATION” BY GUVI

Immaterial of whether you are a beginner programmer or an advanced professional, GUVI courses will help you discover those hidden stairs to an advanced career, which you really expect from an institute when you step in.

Career transition

Did you enjoy this article?

Schedule 1:1 free counselling

Similar Articles

Loading...
Share logo Copy link
Free Webinar
Free Webinar Icon
Free Webinar
Get the latest notifications! 🔔
close
Table of contents Table of contents
Table of contents Articles
Close button

    • What are the variables in Python?
    • Create a Set using String
    • How to use variable key names to get the effect of variable variables in Python?
    • Is there any rule for creating variables in Python?
    • How to declare and re-declare the variable?
    • How to assign the new value or name to the previous variable?
    • Can I use the ‘+’ operator for different types?
    • Let’s check your knowledge of what you have understood in this blog!
    • Bottom Line