Python Variables, Constants and Literals
This tutorial will focus on the python variables, constants, and literals and their use cases with examples.
Contents
- 1 What are variables in Python?
- 2 How to declare variables in Python?
- 3 How to assign values to variables?
- 4 How to assign multiple values to multiple variables?
- 5 What is constant in Python?
- 6 What are the rules and naming convention for variables and constants?
- 7 Guidelines derived from Guido’s Recommendations
- 8 Literals in Python
What are variables in Python?
Variable is a named location used to store the data in memory while the program is running. These variables have a unique name know as an identifier. The value of the variables can be changed during the program.
How to declare variables in Python?
Unlike in other programming languages where we needed to declare the variable before using it, Python automatically assigns a memory location as we assign the variable with a value.
How to assign values to variables?
You can use the assignment operator “=” to assign the value to a variable. For example, let us assign “John Doe” value to “name” variable. Note that we do not need to declare that it is a string variable as Python automatically typecast the variable according to the value we assign.
name = "John Doe"
Lets explore more variables,
counter = 100 # An integer assignment miles = 1000.0 # A floating point name = "John" # A string
How to assign multiple values to multiple variables?
We can use the assignment operator with a comma separation to assign multiple values to multiple variables.
name, roll, address = "John Doe", 301, "New York" print(name) print(roll) print(address)
If we want to assign the same value to multiple variables at once, we can do this as
a = b = c = "xyz" print(a) print(b) print(c)
What is constant in Python?
By definition, a constant is a type of variable whose value cannot be changed. They are usually declared in a module where a module is a file that can contain variables, functions etc. which is imported to the main file.
Let’s see an example of using constants.
# In a file constant.py, define following constants PI = 3.14 GRAVITY = 9.8 # In another file main.py. import the constant and use import constant print(constant.PI) print(constant.GRAVITY)
What are the rules and naming convention for variables and constants?
- You should not use special symbols like !, @, #, $, %, etc. in a variable name.
- Do not start the variable name with a digit
- Use capital letters where possible to declare a constant. For example PI, GRAVITY etc.
- Constants are put into Python modules and meant not be changed.
- 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 (_).
Guidelines derived from Guido’s Recommendations
Here are the guidelines that is derived from Guido that helps you better obtain uniform programming guidelines for your python projects.
Literals in Python
The data that is provided in the variable are known as literals in Python. Python supports the following literals:-
Python support the following literals:-
1) String Literals
2) Numeric Literals
3) Boolean Literals
4) Literal Collections such as List, Tuples, Dictionary
5) None Literal
1. String Literal
Single quotes or double quotes are used to defined by String literals. There are two kinds of strings supported in Python, single line and multiline string literals. Let us use some examples and ways by which we can define strings
# Single Line Strings name = "John Doe" friend = "Ricky Ponting" # Multiline String by black slash at the end of each line hello = "Hello\ world" # Using triple quotation marks welcome = """welcome to Programming World of Python"""
2. Numeric Literals
A number can be directly assigned to variables in Python. They are immutable. Numeric literals can belong to following four different numerical types.
Int: numbers can be both positive and negative) with no fractional part. e.g. 432 |
Long: Integers of unlimited size followed by lowercase or uppercase L e.g. 1422L |
Float: Real numbers with both integer and fractional part eg: -26.2 |
Complex: In the form of a+bj where a forms the real part and b forms the imaginary part of a complex number. eg: 1+2j |
3. Boolean Literals
A Boolean Literal can have True or False value.
4.Literal Collections
There are 4 different literal collections List Literals, Tuple Literals, Dict Literals, and Set Literals. They represent more complex data and helps to provide extendibility to Python programs.
Let us use an example to see how these Literals function:-
colors = ["red", "green", "yellow"] #list numbers = (101, 202, 304) #tuple student = {'name':'John Doe', 'address':'California', 'email':'[email protected]'} #dictionary vowels = {'a', 'e', 'i' , 'o', 'u'} #set print(colors) print(numbers) print(student) print(vowels)
5. None Literal
Python contains a special type of literal known as None. It specifies the field that is not created. and also can indicate the end of lists in Python.