Variables in Python
Identifiers: Identifiers are used to identify an entity uniquely in a program at the time of execution.
Examples: class names, function names, variable names etc.
Variable:Variable is a name given to a memory location that is used to hold a value.
Note: All variables are identifiers whereas, vice versa, is not true.
In many of the programming languages, variables are statically typed, i.e a variable is initially declared to have a specific data type, and any value assigned to it during its lifetime must always have that predefined type only.
But variables in python are not restricted to this. In python, a variable is initially assigned one type and later can change its datatype.
As we observed in the example, we didn’t specify the datatype of variable v and also changed its datatype from int to string after assigning.
Rules for variable names :
A variable name must start with the underscore character or a letter
- Ex1. var (Accepted)
- Ex2. _var(Accepted)
- Ex3. 1_var(Not Accepted)
A variable name cannot start with a number or any special character like @, #etc.
- Ex1. #python (Not Accepted)
- Ex2. 9py (Not Accepted)
- Ex3. py (Accepted)
A variable name can only contain alphanumeric characters and underscores
- All letters (A to Z and a to z)
- Digits (0 to 9)
Python reserved keywords cannot be used naming the variable
- Ex: If, elif, else, def etc.
FOR MULTI_WORD VARIABLE:
The most commonly used methods of constructing a multi-word variable name are the last three examples:
Camel Case: Second and subsequent words are capitalized, to make word boundaries easier to see.
Ex : classNameVariable
Pascal Case: Identical to Camel Case, except the first word is also capitalized.
Snake Case: Words are separated by underscores.
Python is an object-oriented language. That means virtually every item of data in a Python program is an object of a specific type or class.
A Python variable is a symbolic name that is a reference to an object. Once an object is assigned to a variable, you can refer to the object by that name. But the data itself is still contained within the object.
This assignment creates an integer object with the value 100 and assigns the variable m to point to that object.
Multiple variables with same value:
Single object having multiple reference
If we change any one of the value, then the reference of one variable will be changed
In Python, every object that is created is given a number that uniquely identifies it. It is guaranteed that no two objects will have the same identifier during any period in which their lifetimes overlap. Once an object’s reference count drops to zero, and it is garbage collected.
The built-in Python function id() returns an object’s integer identifier. Using the id() function, you can verify that two variables indeed point to the same object.
When the number of references to an object drops to zero, it is no longer accessible. At that point, its lifetime is over. Python will eventually notice that it is inaccessible and reclaim the allocated memory, so it can be used for something else. In computer lingo, this process is referred to as garbage collection.
KEYWORDS IN PYTHON
Keyword:Keywords are some predefined and reserved words in python that have special meanings. Keywords are used to define the syntax of the coding.
The keyword cannot be used as an identifier, function, and variable name.
There are 33 keywords in the python programming language.
Python keywords are the fundamental building blocks of any Python program. Understanding their proper use is key to improving your skills and knowledge of Python.
|Author : Teja|
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates
KTExperts is always active on social media platforms.
Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform