Python – Exploring variables

Hello friends,
Today in this article we are going to discuss about variables in Python
What is variable?
A variable is like a container in which we can store value.
A variable is only a name given to a memory location, all the operations done on the variable effects that memory location.
Rules to create variables –
- A variable name must start with a letter or the underscore character.
- A variable name cannot start with a number.
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
- Variable names are case-sensitive (name, Name and NAME are three different variables).
Lets create a simple program using variable

Output
30
110011.1
Jack
Declare the Variable:
Number = 500
print(Number)
-------------------------
In[1] runfile (...)
500
Assigning a single value to multiple variables:
a = b = c = "Hello"
print(a)
print(b)
print(c)
-----------------------------
In[1] runfile (...)
Hello
Hello
Hello
Assigning different values to multiple variables:
Python allows adding different values in a single line with “,”operators.
a, b, c = 10, 200.9, "Knowledgejunction"
print(a)
print(b)
print(c)
---------------------------
In[1] runfile (...)
10
200.9
Knowledgejunction
Using the same name for different types
a = 100
a = "Knowledgejunction"
print(a)
--------------------------------
In[1] runfile (...)
Knowledgejunction
How does + operator work with variables?
a = 50
b = 20
print(a+b)
a = "Knowledge"
b = "junction"
print(a+b)
---------------------------------
In[1] runfile (...)
70
Knowledgejunction
Can we use + for different types also?
No using for different types would produce error.
a = 100
b = "Knowledge"
print(a+b)
In[1] runfile (...)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
We have very good series of Python articles, please have a look –
Thank you
Have nice day 🙂
You must be logged in to post a comment.