In programming, a variable is a container (storage area) to hold data. For example,
number = 10
Here, number is the variable storing the value 10.
As we can see from the above example, we use the assignment operator = to assign a value to a variable.
# assign value to site_name variable
site_name = 'p4n.in'
print(site_name)
# Output: p4n.in
In the above example, we assigned the value 'p4n.in' to the site_name variable. Then, we printed out the value assigned to site_name.
Note: Python is a type-inferred language, so you don't have to explicitly define the variable type. It automatically knows that p4n.in is a string and declares the site_name variable as a string.
site_name = 'p4n.in'
print(site_name)
# assigning a new value to site_name
site_name = 'codeswithpankaj.com'
print(site_name)
Output
p4n.in
codeswithpankaj.com
Here, the value of site_name is changed from 'p4n.in' to 'codeswithpankaj.com'.
a, b, c = 5, 3.2, 'Hello'
print(a) # prints 5
print(b) # prints 3.2
print(c) # prints Hello
If we want to assign the same value to multiple variables at once, we can do this as:
site1 = site2 = 'p4n.in'
print(site1) # prints p4n.in
print(site2) # prints p4n.in
Here, we have assigned the same string value 'p4n.in' to both the variables site1 and site2.
Constant and variable names should have a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_). For example:
snake_case
MACRO_CASE
camelCase
CapWords
Create a name that makes sense. For example, vowel makes more sense than v.
If you want to create a variable name having two words, use underscore to separate them. For example:
my_name
current_salary
Python is case-sensitive. So num and Num are different variables. For example,
var num = 5
var Num = 55
print(num) # 5
print(Num) # 55
Avoid using keywords like if, True, class, etc. as variable names.
A constant is a special type of variable whose value cannot be changed.
In Python, constants are usually declared and assigned in a module (a new file containing variables, functions, etc which is imported to the main file).
Let's see how we declare constants in separate file and use it in the main file,
Create a constant.py:
# declare constants
PI = 3.14
GRAVITY = 9.8
Create a main.py:
# import constant file we created above
import constant
print(constant.PI) # prints 3.14
print(constant.GRAVITY) # prints 9.8
In the above example, we created the constant.py module file. Then, we assigned the constant value to PI and GRAVITY.
After that, we create the main.py file and import the constant module. Finally, we printed the constant value.
Note: In reality, we don't use constants in Python. Naming them in all capital letters is a convention to separate them from variables, however, it does not actually prevent reassignment.
Literals are representations of fixed values in a program. They can be numbers, characters, or strings, etc. For example, 'Hello, World!', 12, 23.0, 'C', etc.
Literals are often used to assign values to variables or constants. For example,
site_name = 'p4n.in'
In the above expression, site_name is a variable, and 'p4n.in' is a literal.
Numeric Literals are immutable (unchangeable). Numeric literals can belong to 3 different numerical types: Integer, Float, and Complex.
There are two boolean literals: True and False.
For example,
result1 = True
Here, True is a boolean literal assigned to result1.
Character literals are unicode characters enclosed in a quote. For example,
some_character = 'S'
Here, S is a character literal assigned to some_character.
Similarly, String literals are sequences of Characters enclosed in quotation marks.
For example,
some_string = 'Python is fun'
Here, 'Python is fun' is a string literal assigned to some_string.
Python contains one special literal None. We use it to specify a null variable. For example,
value = None
print(value)
# Output: None
Here, we get None as an output as the value variable has no value assigned to it.
There are four different literal collections List literals, Tuple literals, Dict literals, and Set literals.
# list literal
fruits = ["apple", "mango", "orange"]
print(fruits)
# tuple literal
numbers = (1, 2, 3)
print(numbers)
# dictionary literal
alphabets = {'a':'apple', 'b':'ball', 'c':'cat'}
print(alphabets)
# set literal
vowels = {'a', 'e', 'i' , 'o', 'u'}
print(vowels)
Output
['apple', 'mango', 'orange']
(1, 2, 3)
{'a': 'apple', 'b': 'ball', 'c': 'cat'}
{'e', 'a', 'o', 'i', 'u'}
In the above example, we created a list of fruits, a tuple of numbers, a dictionary of alphabets having values with keys designated to each value and a set of vowels.