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:

  1. Numeric literals

  2. String literals

  3. Boolean literals

  4. None literal

  5. Bytes and Bytearray

  6. Raw string

  7. Numeric separators

  8. 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:


FeatureTupleList
   OrderOrdered   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

Popular posts from this blog

What is python? A Beginner's Guide to Coding

How to Start Learning Python: A Beginner's Roadmap

Top 5 Things you can Build With Python