Python Constants, Literals and Data Types (Beginner Guide)
Python Constants, Literals and Data Types (Beginner Guide)
In Python, data is stored and handled using different types of values and structures. In this article, we will understand constants, literals, and data types in a simple way.
Constants in Python
A constant is a special type of variable whose value cannot be changed during the program.
Although Python does not have built-in constant variables, we usually define constants using a separate file.
Example:
import constant
print(constant.PI) # 3.14
print(constant.GRAVITY) # 9.8
Here, PI and GRAVITY are treated as constant values.
Literals in Python
Literals are fixed values used in a program. They represent data directly.
Types of Literals:
Numeric literals
String literals
Boolean literals
None literal
Bytes and Bytearray
Raw string
Numeric separators
Collection literals
-list, tuple, set, etc
Data Types in Python
Python supports different types of data to store values.1. String (Text Data Type)
A string is a sequence of characters enclosed in single, double, or triple quotes.
Examples:
name = "sidhu"
Examples of strings:
'hello'
"world"
'''hello world'''
2. Numeric Data Types
Python supports three numeric types:
int → whole numbers (e.g., 1, -10, 100)
float → decimal numbers (e.g., 3.14, -2.5)
complex → complex numbers (e.g., 3+2j)
3. Sequence Data Types
List and Tuple Comparison:
| Feature | Tuple | List |
|---|---|---|
| Order | Ordered | Ordered |
| Changeable | Immutable | Mutable |
| Symbol | () | [ ] |
| Example | (1,2,3) | [1,2,3] |
4. Mapping Data Type (Dictionary)
A dictionary stores data in key-value pairs, enclosed in curly brackets
Example:
data = {'name': 'john', 'age': 30}
5. Set Data Type
A set is an unordered collection of unique elements.
Example:
numbers = {1, 2, 3}
6. Boolean Data Type
Boolean represents True or False values. It is used in conditions and logic.
7. None Data Type
None represents a null or empty value in Python.
Final Thoughts
Understanding data types is very important because every Python program uses them. Once you master these basics, you can easily move on to writing real Python programs.


Comments
Post a Comment