Jump to content

Python Variables

Linux Hint

Recommended Posts

A Python variable is a location in memory to store the values. The variables are reserved memory locations. A variable is a bag or container that stores the value. We store our data in a Python variable which is subsequently used for multiple purposes i.e. processing, value printing, etc. Every value that is assigned to a variable has a data type.

Following are the different types in Python:

  1. Numbers
  2. Strings
  3. Dictionary
  4. List
  5. Tuple

In Python, the variables can be declared by alphabet or by name. There should not be a special symbol like ‘!@&’ in the variable name. The variable name cannot start with the digit and full stop. According to the coding ethics and rules, the name of the variable should begin with an alphabet or underscore (_). In this article, we will see that

  • How to declare and assign a value to a variable.
  • How to use a variable.
  • Replace or change the variable value.
  • Python strings Concatenation.
  • Assign multiple values to variables
  • Constants.
  • Different standard data type examples.
  • Delete variable.

How to declare and assign a value to a variable

The Python variables are declared and assigned like this:

Price = 100

Here, the price is the variable name, and 100 is the variable value. When the variable is declared space is reserved in memory automatically. The = operator is used to initialize the variable with a value.

The operand on the left side of the = operator is variable and the operand on the right side of the = operator is value.

Let’s declare another variable and calculate the sum of the two numbers. In this case, we will declare two variables and assign them value.

number_1 = 10
number_2 = 20

The number_1 and number_2 are the names of the variables whereas, the 10 and 20 are the values assigned to variables respectively.

In a Python program, we can have multiple types of variables i.e. integer, float, string, and list.

# Assigning string value to the variable
name = "Kamran"

# Assigning integer value to the variable
age = 25

# Assigning float value to the variable
weight = 65.7

# Assigning a list to the variable
courses = ["Python", "Linux Kernel Development", "MySQL Database",
 "C++ Data Structure"]

How to use a variable

The variable is used for various purposes i.e. By using the variable name, we can print the value which is assigned to a particular variable, we can use the variable for performing calculations, and we can also use the variable for processing. Let’s consider the number examples and calculate the sum of two variables. We will declare two numbers of variables and assign value to them. After, that we will declare a sum variable, and store the sum value in this variable. Lastly, we will print the sum variable.

# Declaring a variable number_1 and assigning the value
number_1 = 10

# Declaring a variable number_2 and assigning the value
number_2 = 20

# Declaring a sum variable and taking the sum of two numbers
sum = number_1 + number_2

#printing the sum value
print("The sum value is: ",sum)


The output is displayed on the Python console. The sum value is printed using the “sum” variable.


Let’s see another example of variables.

# a name variable
name = "Kamran"
# printing the name variable

# a webiste variable
website =  "LinuxHint"
# printing the website name

# an age variable
age = 25
# printing the age value

# a variable for storing weight value
weight = 65.7
# printing the weight value

# a laptop variable
laptop = "HP Folio 9470m"
# printing the laptop name

# an operating system variable
operating_system = "Ubuntu 20.04"
# printing the laptop name

# a list of courses
courses = ["Python", "Linux Kernel Development", "MySQL Database", "C++ Data Structure"]
# printing the list of courses


The output is displayed on the Python console.

Replace or change the variable value

The variable is like a bag. It stores the data or variable value. The data in the bag could be replaced at any time. It means that we can easily replace the value of the variable at any time. Let’s see an example of replacing or changing the value of a variable. We declare a ‘company’ variable and assign a value. Later on, we use this variable again and assign another value. So, the first value will be replaced and the second value will be stored in the variable.

# declaring a company variable and assigning the value
company = "Microsoft"

# Assigning a new value to the company variable
company = "Apple Inc."
print("The replaced value is:", company)


The output is displayed on the Python console.


Similarly, we can replace the integer, float, complex number, list, etc. value in this way.

# declaring a number variable and assigning the value
number = 10

# Assigning a new value to the number variable
number = 20
print("The replaced value is:", number)

# declaring a float variable and assigning the value
float_number =  10.1
# Assigning a new value to the float variable
float_number = 20.04
print("The replaced value is:", float_number)

# declaring a complex number variable and assigning the value
complex_number = 2+3j

# Assigning a new value to the complex number variable
complex_number = 5+3j
print("The replaced value is:", complex_number)

# declaring a list variable and assigning the value
name_list = ["Ali", "Kamran", "Talha"]

# Assigning a new value to the list variable
name_list = ["Umer", "Usman", "Assad"]
print("The replaced value is:", name_list)


The output is displayed on the Python console.


Python strings Concatenation

Strings in Python are the set of characters. Strings are represented inside a quotation mark. The string represents a single character, word, or a complete sentence. In Python, we can concatenate or combine the Strings by the “+” operator. Let’s see an example of this. In the given example, we have one variable which contains some string value. Moreover, we have another variable that contains another string value. Lastly, we have concatenated both strings using the “+” operator.

# Declaring one string variable and assigning a value
text = "Hello everyone."
# Declaring second string variable and assigning the value
text_web  = " Welcome to the LinuxHint"

# Concatenating the strings using "+"


The output is displayed on the Python console.


We can do similar work in this way and we will get the same output.

# Declaring one string variable and assigning the value
text = "Hello everyone."

# Concetenating the strings using "+"
print(text+" Weclome to LinuxHint")



Assign multiple values to variables

In Python, multiple values can be assigned to multiple variables simultaneously. We can also assign an atomic value to numerous variables at one time. For example

num1 = num2 = num3 = 5

Here, all three variables (num1, num2, num3) are assigned to a single value 5. If we print all these three variables, you can see that the value of all these three variables is the same.

num1 = num2 = num3 = 5



You can also assign multiple values to multiple variables like this:

# Assigning values to three variables
age,rollNumber,name = 25,12,"Kamran"

#print age value
# print roll number value
# print name value

In the above-given example, we have two integer objects, age and roll number, and one string object. All the variables are created and initialized in a line. In the output, you can see that all the values are successfully assigned to all the variables.




Constants are those variables whose values cannot change. The constant value always remains the same. We use multiple constants in mathematics and physics like the value of PI and Gravity value. In python, the constants are written in the capital letters like this:

# Declaring a constant for PI
PI = 3.14
# Declaring a constant for Foiass
FOIASS= 1.18

Different standard data type examples

As we discussed previously, Python has multiple different data types like numbers, strings, lists, tuple, and dictionaries. We have discussed the numbers and strings in detail. Now, let’s see the examples of lists, tuples, and dictionaries.

Python list

A list in Python contains various comma-separated items. A list can contain the heterogeneous type of items. The list values are accessed by the []. We specify the index number inside the slice operator ([]). The index number starts from zero. For example:

# Declaring a student list
student_list = ["Ali",1, "Kamran",2, "Talha",3]

#printing the student list

#print first element of list

#print second element of list

#print third element of list

#print fourth element of list

#print fifth element of list

#print sixth element of the list


The output is following


Python tuple

Tuples are another type of data type in Python. Tuples are similar to the Python list. The main difference between tuples and the Python list is that the Python tuples are declared inside the parenthesis (). Following is the Python tuple example:

# Declaring a student tuple
student_tuple= ("Ali",1, "Kamran",2, "Talha",3)

# printing the student tuple

# print first element of tuple

# print second element of tuple

# print element of tuple starting from the 3rd element

# print element of tuple starting from the 2nd  6th element

# print fifth element of tuple

# print sixth element of tuple


The output is displayed on the Python console.


Python Dictionary

Python dictionaries work like the hash table. A Python dictionary is alike a key-value pair. A dictionary is declared using the set of curly braces ({}). When we declare the key-value pair, we access the value by using the key. We pass the key as an argument and the value is returned as a result. Let’s create a Python dictionary for a student.

# Declaring a student dictionary
student_dict = {'name': 'Kamran','age':25, 'class': 'MSSE'}

# Printing the name of student by using name key

# Printing the age of student by using the age key

# Printing the class of student by using the class key


The output is displayed on the Python console.


Delete variable

We can delete a variable in Python and remove it from the memory by using the del keyword. So, if we print the variable or use the variable after deleting it, the compiler will return an error.

# Declaring a name variable
name = "Kamran"

# print the name variable
# deleting the name variable
del name
# printing the name variable after deleting it


The output is displayed on the Python console. In the output, you can see that the compiler returns an error “The variable name is not defined”.



The Python variables are like a container or bag that store the values. Variables are declared by names or alphabets. We can easily replace the Python variable values and assign value to multiple variables simultaneously. There are different data types in Python. In this article, we have explained the variables and different data types with several examples. We have used Python 3 syntax throughout this article.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...