Data Types

Dark-themed laptop setup with a red glowing keyboard and code on screen, ideal for tech enthusiasts.

Python has various data types to represent different types of values. Some of the most commonly used data types are:
int (integers) – positive or negative whole numbers,
e.g. 10, -20, 0

float (floating-point numbers) – numbers with decimal points,
e.g. 1.0, 3.14, -0.5

complex (complex numbers) – numbers with real and imaginary parts,
e.g. 1 + 2j, 3.2 + 1j


bool (Booleans) – True or False values

str (strings) – sequence of characters,
e.g. “hello”, “world”

list – ordered, mutable sequence of elements, enclosed within [ ]
e.g. [1, 2, 3, 4]


tuple – ordered, immutable sequence of elements, enclosed within ( )
e.g. (1, 2, 3, 4)

dict (dictionaries) – unordered collection of key-value pairs, enclosed within { }
e.g. {“name”: “John”, “age”: 30}


set – unordered collection of unique elements, enclosed within { }

e.g. {1, 2, 3, 4}

Here, x is an integer, y is a floating-point number, z is a string, a is a list, b is a tuple, c is a dictionary, and d is a set.

Type Conversions in Python
Type conversion refers to converting one data type into another. Python supports both implicit type conversion and explicit type conversion (also known as type casting).

Implicit Type Conversion
  • Performed automatically by Python.
  • Occurs when Python converts a smaller data type to a larger data type to prevent data loss.
Explicit Type Conversion
  • Performed manually by using functions like int(), float(), str(), etc.
  • The programmer explicitly converts one type into another.
Type Conversion Functions in Python
Common Explicit Type Conversion Functions
Function Description Example
int() Converts to an integer int(3.7)3
float() Converts to a floating-point number float(5)5.0
str() Converts to a string str(25)'25'
list() Converts to a list list("hello")['h', 'e', 'l', 'l', 'o']
tuple() Converts to a tuple tuple([1, 2, 3])(1, 2, 3)
set() Converts to a set set([1, 1, 2]){1, 2}
dict() Converts to a dictionary (from key-value pairs) dict([(1, 'a')]){1: 'a'}

Examples of Explicit Type Conversion

To buy Python books click the below links
Python Programming
Python Programming 2E
Handbook of Python Programming

Leave a Comment

Your email address will not be published. Required fields are marked *