In this article, I am going to discuss Variables in Python with Examples. All the data which we create in the program will be saved in some memory location on the system. The data can be anything, an integer, a complex number, a set of mixed values, etc.
What is a variable in Python?
Variable is the name that we give to the memory location which holds some data. With a variable, we can store the data, access the data, and also manipulate the data. We can also refer to variables as containers which store some information/data.
Python variables key Points:
- Python has no command for declaring a variable.
- A variable is created the moment you first assign a value to it.
- Variables do not need to be declared with any particular type and can even change type after they have been set.
- If you want to specify the data type of a variable, this can be done with casting.
Creating a variable in Python:
Python is a dynamically typed programming language. There is no need of specifying the data type for a variable. See example below
x = 4 # x is of type int
y = "Code Config" # y is of type string
print(x)
print(y)
Python variable casting:
If you want to specify the data type of a variable, this can be done with casting.
x = str(2) # x will be '2'
y = int(2) # y will be 2
z = float(2) # z will be 2.0
Get the Type of Python Variable:
You can get the data type of a variable with type() unction.
x = 5
y = "Code-Config"
print(type(x))
print(type(y))
Single or Double Quotes?
String variables can be declared either by using single or double quotes, both are same:
x = "CodeConfig"
# is the same as
x = 'CodeConfig'
Case-Sensitive:
Python variable names are case-sensitive.
a = 4
A = "Nitika"
#A will not overwrite a because it is case sensitive
Can not use reserve Keyword in variables:
In Python creating a variable with keyword name is not allowed. like with using if, else or for keyword
if = 10 # 'if' is reserved keyword & cannot be used as variable.
print(if)
Assigning multiple variables in single line:
In Python it is also possible to assigning multiple variables in single line, Check example
a,b,c = 1,2,3
print(a,b,c)
#Output: 1 2 3
Re-initialize the variables in Python:
In Python we can reinitialize the variable values. The old values will be overridden or replaced with the new values.
a = 10
print("At first I have assigned value: ", a)
a = 20
print("On re-initialization of variable, now its value is: ", a)
#Please note value 10 will get overridden with 20
- Python tutorials
- Input and Output in Python
- f-strings in Python
- end and sep in Python
- Python QuickStart
- Variables in Python
- Data Types in Python
- Operators in Python
- Python program to compute net run rate
- Python program to get biggest number
- Python program to count the frequency of every element
- Python program of list and swap its content
- Python program to get max numbers
- Python program to generate patterns
- Python Program to Check ArmstrongÂ
- Python program to print multiplication table
- Python program for domestic electricity billÂ
- Python program to find maximum numberÂ
- Python program to check uppercase or lowercase letterÂ