Python has relished a steady rise to fame over the last some years and is now jostling for one of the most popular programming languages. Python is favoured for applications ranging from development to scripting and process automation. Moreover, it is quickly becoming the top prefered language among developers for artificial Intelligence (AI), machine learning, and deep learning projects.
If you wish to grow your career in Python, you can join the LSET best python certification course, LSET, the online platform which will clear all your doubts and make you a certified Python developer.
Python Variables
Variable is a name that is generally used to refer to memory location. A Python variable is also called an identifier and is used to hold value. In Python, we do not have to specify the variable type because Python is a infer language and good enough to get variable type.
Variable names can be digits and letters, but they have to begin with a letter or an underscore.
Identifier Naming
Variables are the example of identifiers. It is used to identify the literals used in the program. The criteria to name an identifier are below.
- An underscore ( _ ) or alphabet must be the first character of the variable.
- Except for the first, all the characters may be an alphabet of upper-case (A-Z), lower-case(a-z), a digit (0–9), or underscore.
- Identifier name should not contain any special character (!, @, #, %, ^, &, *) or white-space.
- The name of the Identifier must not be similar to any keyword explained in the language.
- The names of the Identifier are case sensitive; for example, my name and MyNamehas a difference.
- Invalid identifiers examples: 1a, n%4, n 9, etc.
- Examples of valid identifiers: a123, _n, n_8, etc.
Declaring a Variable
Python does not force us to declare a variable before operating it in the application. Instead, it allows us to create a variable at the required time. Thus, we don’t need to declare variables explicitly in Python. Instead, when we assign any value to the variable, that variable is declared automatically.
The equal (=) operator is used to assign a value to a variable.
Object References
It is necessary to acknowledge how the Python interpreter works when we declare a variable. The process of handling variables is somewhat different from many other programming languages.
Python is a highly object-oriented programming language; that’s why every data item belongs to a specific class type.
The multi-word keywords can be generated by the following various methods.
- Camel Case — In this case, each word or abbreviation in the middle begins with a capital letter. There is no intervention of whitespace. For example — valueOfVaraible, nameOfStudent, etc.
- Pascal Case is the same as the Camel Case, but the first word is capital here. For example — NameOfStudent, etc.
- Snake Case — In this case, Words are separated by using underscore, like — name_of_student, etc.
Multiple Assignment
Python permits us to assign a value to multiple variables in a single statement, which is also called multiple assignments.
We can apply multiple assignments in 2 ways: assigning a single value to multiple variables or assigning multiple values to multiple variables.
Python Variable Types
There are 2 types of variables in Python — Local variable and Global variable. First, let’s understand the following variables.
- Local Variable
These are the variables declared inside the function and have scope within the function.
- Global Variables
These variables can be used throughout the program, and its scope is in the whole program. We can use global variables outside or inside the function.
A variable declared outside the function is by default a global variable. Python provides the global keyword to use a global variable inside the function. If we do not use the global keyword, the function treats it as a local variable.
Delete a variable
We can also delete the variable using the del keyword. The syntax is
Syntax –
del <variable_name>