What is variable in Python ?
Variables are like containers for storing values. A variable in python is a name given to a memory location in a program. Python Variable
# declaring the variable
name = "Fullstackadda.com"
# displaying the output
print(name)
Fullstackadda.com
means name has now assigned to Fullstackadda.com. we can change the value of variables if we want.
Rules to know before you create a variable:
- Variables are not written inside double quotes like strings.
- A variable name must start with a letter or the underscore character. (A-z, 0-9, and _ ).
- A variable cannot have space between it’s letters. (person ✅, per son ❌ )
- Some special symbols are allowed.
- A variable never starts with a number or a symbol.
Python is as easy as you type english in your texts.
- The basic type would look like
a=10
b=20
c=a+b
print(c)
if you are taking input from the user then the code would be like:
a=input()
b=input()
c=a+b
print(c)
If you want to save some steps there and don’t want to take another variable to store the result, then code will be like
a=int(input("enter"))
b=int(input())
print(a+b)
Different Examples:
Assigning multiple variables:
a, b, c = 1, 2, 3
print(a)
print(b)
print(c)
1
2
3
Using + operator :
a = 5
b = 5
print(a+b)
a = "Fullstack"
b = "adda"
print(a+b)
10
Fullstackadda
a = 1
b = "two"
print(a+b)
Output:
- Variables are like containers for storing values.
- Variable in python is a name given to a memory location in a program.