Python Variables

     When we write a program in any programming language, we need names given to entities used in the program. For example, if we use an employee name in our program, we need the name “emp_name” to store the name of an employee, or if we want to store marks of a student, we need names like ‘computer_marks’, ‘physics_marks’.

        We also need to name functions, classes, or any other entity that we use in our program. These names given to functions, classes or variables are called identifiers. Identifiers help to identify and distinguish entities used in the program.

 

     In this lesson, we will study one such identifier, Variable.

Variable in Python

     In Python variables are names that are used to store values like, emp_name, computer_marks, physics_marks, etc. Once we assign values to these variables, we can refer to these values using variable names. You can change the value assigned to the variable and therefore they are called variables. (This means its value can vary.)

Creating Variable.

     Python has no command for declaring a variable. A variable is created the moment you first assign a value to it.

For example.

student_name = ‘Piyu’

total_marks = 590

In Python, variables do not need to be declared with any type. In other programming languages like Java programming, you need to declare the type of variable you used in the program.

For example,

String student_name = “Piyu”;

int total_marks = 590;

Python keeps track of the kind of type of data your program uses when it runs. The concept is called Dynamic Typing. You can change the type of a value stored in one variable.

Example,

student_name = 100

total_marks = param

So, to create a variable in Python you need the assignment operator ‘=’. When you assign value to some variable you can use that variable throughout your program.

Python also allows chain assignment. If you want to assign the same value to several variables simultaneously. You can do this as

>>> p=q=r = 100

In Python, you also assign multiple values to multiple variables at once. You just need to separate the variables and values by comma.

Example,

Roll_number, name = 100, ‘Piyu’

 

If the number values on the left does not match, the number of values on the right, python will prompt ValueError.

Variable naming rules.

Variable names or identifiers in the python cab be as long and as short as you like, but there are some rules that you must follow.

  1. Identifiers can be a combination of letters in lower case (a-z) or upper case (A-Z) digits (0-9) or underscore.

 

  • student_name
  • StudentName  
  • emp_Salary
  •  EmpSalary

 

  1. An identifier cannot start with a digit.

 

  • 1student_name
  • 1class

 

  1. Keywords cannot be used as an identifier.

 

  1. One can does not use special symbols like, @, #, $, &, etc to name variables or identifiers.

 

Python is a case-sensitive language, which means.

Student_name and  student_name is different.

(I will explain Python naming conventions in a separate article.)

While naming variables, you should always use names that make sense. For example,

c=10

If ‘c’ is used for the count of students, name it ‘count’ instead of ‘c’. you can even make it descriptive as ‘student_count’. To make variable names descriptive you can use multiple words usually two, or three words. In a large program, descriptive words help you to figure out what it represents.

error: Content is protected !!