Python Variables

A variable is the name given to a memory location. A value-holding Python variable is also known as an identifier.

Since Python is an infer language that is smart enough to determine the type of a variable, we do not need to specify its type in Python.

Variable names must begin with a letter or an underscore, but they can be a group of both letters and digits.

The name of the variable should be written in lowercase. Both Rahul and rahul are distinct variables.

Identifier Naming

Identifiers are things like variables. An Identifier is utilized to recognize the literals utilized in the program. The standards to name an identifier are given underneath.

  • The variable's first character must be an underscore or alphabet (_).
  • Every one of the characters with the exception of the main person might be a letter set of lower-case(a-z), capitalized (A-Z), highlight, or digit (0-9).
  • White space and special characters (!, @, #, %, etc.) are not allowed in the identifier name. ^, &, *).
  • Identifier name should not be like any watchword characterized in the language.
  • Names of identifiers are case-sensitive; for instance, my name, and MyName isn't something very similar.
  • Examples of valid identifiers: a123, _n, n_9, etc.
  • Examples of invalid identifiers: 1a, n%4, n 9, etc.

Declaring Variable and Assigning Values

  • Python doesn't tie us to pronounce a variable prior to involving it in the application. It permits us to make a variable at the necessary time.
  • In Python, we don't have to explicitly declare variables. The variable is declared automatically whenever a value is added to it.
  • The equal (=) operator is utilized to assign worth to a variable.

Object References

When we declare a variable, it is necessary to comprehend how the Python interpreter works. Compared to a lot of other programming languages, the procedure for dealing with variables is a little different.

Python is the exceptionally object-arranged programming language; Because of this, every data item is a part of a particular class. Think about the accompanying model.

Output:

John

The Python object makes a integer object and shows it to the control center. We have created a string object in the print statement above. Make use of the built-in type() function in Python to determine its type.

Output:

<class 'str'>

In Python, factors are an symbolic name that is a reference or pointer to an item. The factors are utilized to indicate objects by that name.

Let's understand the following example


Python Variables

In the above image, the variable a refers to an integer object.

Suppose we assign the integer value 50 to a new variable b.


Python Variables

The variable b refers to the same object that a points to because Python does not create another object.

Let's assign the new value to b. Now both variables will refer to the different objects.


Python Variables

Python manages memory efficiently if we assign the same variable to two different values.

Object Identity

Every object created in Python has a unique identifier. Python gives the dependable that no two items will have a similar identifier. The object identifier is identified using the built-in id() function. consider about the accompanying model.

Output:

140734982691168
140734982691168
2822056960944

We assigned the b = a, an and b both highlight a similar item. The id() function that we used to check returned the same number. We reassign a to 500; The new object identifier was then mentioned.

Variable Names

The process for declaring the valid variable has already been discussed. Variable names can be any length can have capitalized, lowercase (start to finish, a to z), the digit (0-9), and highlight character(_). Take a look at the names of valid variables in the following example.

Output:

Devansh
20
80.5

Consider the following valid variables name.

Output:

A B C D E D E F G F I

We have declared a few valid variable names in the preceding example, such as name, _name_, and so on. However, this is not recommended because it may cause confusion when we attempt to read code. To make the code easier to read, the name of the variable ought to be descriptive.

The multi-word keywords can be created by the following method.

  • Camel Case - In the camel case, each word or abbreviation in the middle of begins with a capital letter. There is no intervention of whitespace. For example - nameOfStudent, valueOfVaraible, etc.
  • Pascal Case - It is the same as the Camel Case, but here the first word is also capital. For example - NameOfStudent, etc.
  • Snake Case - In the snake case, Words are separated by the underscore. For example - name_of_student, etc.

Multiple Assignment

Multiple assignments, also known as assigning values to multiple variables in a single statement, is a feature of Python.

We can apply different tasks in two ways, either by relegating a solitary worth to various factors or doling out numerous qualities to different factors. Take a look at the following example.

1. Assigning single value to multiple variables

Eg:

Output:

50  
50  
50  

2. Assigning multiple values to multiple variables:

Eg:

Output:

5  
10  
15  

The values will be assigned in the order in which variables appear.

Python Variable Types

There are two types of variables in Python - Local variable and Global variable. Let's understand the following variables.

Local Variable

The variables that are declared within the function and have scope within the function are known as local variables. Let's examine the following illustration.

Example -

Output:

The sum is: 50

Explanation:

We declared the function add() and assigned a few variables to it in the code above. These factors will be alluded to as the neighborhood factors which have scope just inside the capability. We get the error that follows if we attempt to use them outside of the function.

Output:

The sum is: 50
    print(a)
NameError: name 'a' is not defined

We tried to use local variable outside their scope; it threw the NameError.

Global Variables

Global variables can be utilized all through the program, and its extension is in the whole program. Global variables can be used inside or outside the function.

By default, a variable declared outside of the function serves as the global variable. Python gives the worldwide catchphrase to utilize worldwide variable inside the capability. The function treats it as a local variable if we don't use the global keyword. Let's examine the following illustration.

Example -

Output:

101
Welcome To Javatpoint
Welcome To Javatpoint

Explanation:

In the above code, we declare a global variable x and give out a value to it. We then created a function and used the global keyword to access the declared variable within the function. We can now alter its value. After that, we gave the variable x a new string value and then called the function and printed x, which displayed the new value.

=

Delete a variable

We can delete the variable using the del keyword. The syntax is given below.

Syntax -

In the following example, we create a variable x and assign value to it. We deleted variable x, and print it, we get the error "variable x is not defined". The variable x will no longer use in future.

Example -

Output:

6
Traceback (most recent call last):
  File "C:/Users/DEVANSH SHARMA/PycharmProjects/Hello/multiprocessing.py", line 389, in 
    print(x)
NameError: name 'x' is not defined

Maximum Possible Value of an Integer in Python

Python, to the other programming languages, does not support long int or float data types. It uses the int data type to handle all integer values. The query arises here. In Python, what is the maximum value that the variable can hold? Take a look at the following example.

Example -

Output:

<class 'int'>
10000000000000000000000000000000000000000001

As we can find in the above model, we assigned a large whole number worth to variable x and really look at its sort. It printed class <int> not long int. As a result, the number of bits is not limited, and we are free to use all of our memory.

There is no special data type for storing larger numbers in Python.

Print Single and Numerous Factors in Python

We can print numerous factors inside the single print explanation. The examples of single and multiple printing values are provided below.

Example - 1 (Printing Single Variable)

Output:

5
5

Example - 2 (Printing Multiple Variables)

Output:

5 6
1 2 3 4 5 6 7 8

Basic Fundamentals:

This section contains the fundamentals of Python, such as:

i)Tokens and their types.

ii) Comments

a)Tokens:

  • The tokens can be defined as a punctuator mark, reserved words, and each word in a statement.
  • The token is the smallest unit inside the given program.

There are following tokens in Python:

  • Keywords.
  • Identifiers.
  • Literals.
  • Operators.

We will discuss above the tokens in detail next tutorials.






Latest Courses