30 C
Jaipur
Friday, October 30, 2020

Python Variables – Linux Hint

Must read

Display Network Information In Linux Using What IP Tool

What IP is a simple graphical application used to display network information in Linux operating systems. Using What IP, anyone can easily find the...

Google Maps Working on Adding Language Changer, Assistant Driving Mode Still in the Works

Google Maps has seen a bunch of updates over the last couple of months, but one of the features that we know the company...

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 ‘[email protected]&’ 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:

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)

Output

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
print(name)

# a webiste variable
website =  “LinuxHint”
# printing the website name
print(website)

# an age variable
age = 25
# printing the age value
print(age)

# a variable for storing weight value
weight = 65.7
# printing the weight value
print(weight)

# a laptop variable
laptop = “HP Folio 9470m”
# printing the laptop name
print(laptop)

# an operating system variable
operating_system = “Ubuntu 20.04”
# printing the laptop name
print(operating_system)

# a list of courses
courses = [“Python”, “Linux Kernel Development”, “MySQL Database”, “C++ Data Structure”]
# printing the list of courses
print(courses)

Output

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”
print(company)

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

Output

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
print(number)

# 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
print(float_number)
# 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
print(complex_number)

# 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”]
print(name_list)

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

Output

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 “+”
print(text+text_web)

Output

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”)

Output

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

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
print(num1)
print(num2)
print(num3)

Output

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(age)
# print roll number value
print(rollNumber)
# print name value
print(name)

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.

Output

Constants

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(student_list)

#print first element of list
print(student_list[0])

#print second element of list
print(student_list[1])

#print third element of list
print(student_list[2])

#print fourth element of list
print(student_list[3])

#print fifth element of list
print(student_list[4])

#print sixth element of the list
print(student_list[5])

Output

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(student_tuple)

# print first element of tuple
print(student_tuple[0])

# print second element of tuple
print(student_tuple[1])

# print element of tuple starting from the 3rd element
print(student_tuple[2:])

# print element of tuple starting from the 2nd  6th element
print(student_tuple[1:5])

# print fifth element of tuple
print(student_tuple[4])

# print sixth element of tuple
print(student_tuple[5])

Output

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
print(student_dict[‘name’])

# Printing the age of student by using the age key
print(student_dict[‘age’])

# Printing the class of student by using the class key
print(student_dict[‘class’])

Output

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
print(name)
# deleting the name variable
del name
# printing the name variable after deleting it
print(name)

Output

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”.

Conclusion

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.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

Display Network Information In Linux Using What IP Tool

What IP is a simple graphical application used to display network information in Linux operating systems. Using What IP, anyone can easily find the...

Google Maps Working on Adding Language Changer, Assistant Driving Mode Still in the Works

Google Maps has seen a bunch of updates over the last couple of months, but one of the features that we know the company...