Understanding Variables in Python: A Comprehensive Guide
Table of Content:
Variable is a name which is used to refer memory location. Variable also known as identifier and used to hold value.
In Python, we don't need to specify the type of variable because Python is a type infer language and smart enough to get variable type.
Variable names can be a group of both letters and digits, but they have to begin with a letter or an underscore.
It is recomended to use lowercase letters for variable name. Rahul and rahul both are two different variables.
Identifier Naming
Variables are the example of identifiers. An Identifier is used to identify the literals used in the program. The rules to name an identifier are given below.
- The first character of the variable must be an alphabet or underscore ( _ ).
- All the characters except the first character may be an alphabet of lower-case(a-z), upper-case (A-Z), underscore or digit (0-9).
- Identifier name must not contain any white-space, or special character (!, @, #, %, ^, &, *).
- Identifier name must not be similar to any keyword defined in the language.
- Identifier names are case sensitive for example my name, and MyName is not the same.
- Examples of valid identifiers : a123, _n, n_9, etc.
- Examples of invalid identifiers: 1a, n%4, n 9, etc.
Variable types:
Text Type: | str |
Numeric Types: | int , float , complex |
Sequence Types: | list , tuple , range |
Mapping Type: | dict |
Set Types: | set , frozenset |
Boolean Type: | bool |
Binary Types: | bytes , bytearray , memoryview |
In Python, the data type is set when you assign a value to a variable:
Example | Data Type |
---|---|
x = "Hello World" | str |
x = 20 | int |
x = 20.5 | float |
x = 1j | complex |
x = ["male", "female", "others"] | list |
x = ("apple", "banana", "cherry") | tuple |
x = range(6) | range |
x = {"name" : "Rambo", "age" : 23} | dict |
x = {"apple", "banana", "cherry"} | set |
x = frozenset({"apple", "banana", "cherry"}) | frozenset |
x = True | bool |
x = b"Hello" | bytes |
x = bytearray(5) | bytearray |
x = memoryview(bytes(5)) | memoryview |
Setting the Specific Data Type
If you want to specify the data type, you can use the following constructor functions:
Example | Data Type |
---|---|
x = str("Hello World") | str |
x = int(20) | int |
x = float(20.5) | float |
x = complex(1j) | complex |
x = list(("male", "female", "others")) | list |
x = tuple(("apple", "banana", "cherry")) | tuple |
x = range(6) | range |
x = dict(name="Rambo", age=21) | dict |
x = set(("apple", "banana", "cherry")) | set |
x = frozenset(("apple", "banana", "cherry")) | frozenset |
x = bool(5) | bool |
x = bytes(5) | bytes |
x = bytearray(5) | bytearray |
x = memoryview(bytes(5)) | memoryview |
1. Assigning single value to multiple variables
Code:
x=y=z=50 print iple print y print z
Output:
The above code will produce the following result-
50 50 50
2.Assigning multiple values to multiple variables:
Code:
a,b,c=5,10,15 print a print b print c
Output:
The above code will produce the following result-
5 10 15
The values will be assigned in the order in which variables appears.
What are the Local and Global variables in Python?
Global variables are defined outside of any module and function. These variables can be accessed by any function in the program. Local variables are defined inside the function where it is used.