Python variables are names that refer to a memory location containing data. These variables can hold different types of values, such as numbers, strings, or data structures. As Python is dynamically typed, variable types are inferred automatically, eliminating the need for explicit type declarations. Variable names should follow certain conventions, such as starting with a letter or underscore, and can consist of a combination of letters and digits.
A variable is a name given to a memory location. In Python, a value-holding variable is also referred to as an identifier. Since Python is an interpreted language that can determine the type of a variable, there’s no need to explicitly specify its type. Variable names must start with a letter or an underscore, but can be a combination of letters and digits. It is recommended to use lowercase letters for variable names. Both “Book” and “book” are considered distinct variables.
Identifier Naming
Identifiers, such as variables, are used to label the literals in a program. The rules for naming an identifier are as follows:
- The first character of the variable must be an underscore or an alphabet (_).
- Subsequent characters can be letters (both lowercase and uppercase), underscores, or digits (0-9).
- Identifiers cannot contain any whitespace or special characters like (!, @, #, %, ^, &, *).
- The identifier name should not be a keyword reserved by the programming language.
- Identifier names are case-sensitive, meaning “myname” and “MyName” are not considered the same.
Examples of valid identifiers include a123, _n, n_9, etc., while examples of invalid identifiers include 1a, n%4, and n 9.
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 declaring a variable in Python, it is essential to understand the workings of the Python interpreter. Unlike many other programming languages, the handling of variables in Python is slightly different.
Python is a highly object-oriented programming language, meaning that every data item is an instance of a specific class. Consider the following example:
Output:
In Python, an integer object is generated, and it is displayed in the console. In the given print statement, we have created a string object. To determine its type, we can use the built-in type() function in Python.
Output:
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
Object Identity
Each object created in Python possesses a unique identifier. Python ensures that no two objects will share the same identifier. The built-in id() function is used to determine the object’s identifier. Consider the following example:
After assigning b = a, both a and b point to the same object. The id() function confirms this by returning the same number for both. When we reassign a to 500, the identifier of the new object is then displayed.
Variable Names
The process for declaring valid variables has been discussed earlier. Variable names can be of any length and can include uppercase and lowercase letters (a to z), digits (0-9), and underscore characters (_). Here are examples of valid variable names:
Output:
Consider the following valid variables name.
Output:
In the previous example, we declared some valid variable names such as name, name, and so on. However, it is recommended to use descriptive variable names to enhance code readability. To create multi-word variable names, the following conventions are commonly used:
- Camel Case: Each word or abbreviation within the variable begins with a capital letter, without any spaces. For example, nameOfStudent, valueOfVariable, etc.
- Pascal Case: Similar to Camel Case, but the first word is also capitalized. For example, NameOfStudent, etc.
- Snake Case: Words are separated by underscores. For example, name_of_student, etc.
Do yoy know about Python Applications
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:
n this case, the value 10
is assigned to all three variables x
, y
, and z
simultaneously.
Another way is to do unpacking when you have multiple values:
2. Assigning multiple values to multiple variables:
Eg:
In this example, 10
is assigned to x
, 20
to y
, and 30
to z
in a single line.
Another way to assign multiple values to multiple variables is by using unpacking with a list or tuple:
Here, the values from the list [40, 50, 60]
are assigned to a
, b
, and c
respectively.
This concept is prevalent in many programming languages and can often be achieved using arrays, tuples, or other data structures. The syntax might differ, but the principle remains the same: assigning multiple values to multiple variables in a single statement.
Python Variable Types:
Local Variable:
Local variables are the ones defined within a function. They have a limited scope and are only accessible within that specific function. Once the function finishes executing, the local variable’s scope is lost, and the variable cannot be accessed from outside the function.
Here’s an example:
In this case, x
is a local variable within the my_function()
function. Trying to access x
outside the function will result in an error because its scope is limited to that function.
Global Variables: Global variables, on the other hand, are defined outside of any function and can be accessed throughout the code, including within functions. However, if a function wants to modify a global variable, it needs to explicitly declare the variable as global using the global
keyword.
Here’s an example:
In this example, y
is a global variable, accessible both inside and outside the function another_function()
. However, to modify y
within the function, we need to declare it as global using the global
keyword.
Understanding these variable scopes is crucial in Python programming, as it affects how variables are accessed and modified within functions and the broader program.
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.
In Python, global variables are accessible throughout the program and their scope extends across the entire code. If a variable is declared outside a function, it is considered a global variable by default. To access and modify a global variable within a function, the global
keyword is used. If the global
keyword isn’t used inside the function, it treats the variable as local to that function.
Let’s examine an example:
Output:
In this example, x
is a global variable initially set to 101
. Inside the mainFunction()
, global x
is used to explicitly reference the global variable x
. This allows the function to both access and modify the global variable. As a result, after the function call, the value of x
is changed to 'Welcome To Python'
, and when printed outside the function, it retains this modified value.
Delete a variable:
We can delete a variable using the del
keyword followed by the variable name you wish to delete.
Here’s the syntax:
This del
keyword removes the variable variable_name
from the program’s memory, making it inaccessible afterward. Trying to access the variable after deletion will result in an error because the variable no longer exists.
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.
Once the del x
statement is executed, the variable x
is removed from the program’s memory. Any attempt to access or use the variable x
after it’s been deleted will result in a NameError
because x
no longer exists in the program. Therefore, it can’t be used further.
Maximum Possible Value of an Integer in Python
Python, the int
data type can store integers of virtually unlimited size, limited only by the available memory on the system.
Here’s an example showcasing this:
- # A Python program to display that we can store
- # large numbers in Python
Output:
In this example, a
is initially assigned a very large integer value. When a
is incremented by 1 (a = a + 1
), Python handles this increase without any explicit issue.
Python’s int
type automatically adjusts to accommodate larger integer values, using as much memory as necessary. This property allows you to work with very large integers without worrying about overflow errors typically encountered in some other programming languages.