When we write programs in Python we need to work with different types of values like numbers, text, or booleans. Python provides us various data types to handle these kinds of data/values. so what are data types?
Data type is a classification of the data items it specifies what type of data a variable holds
Table of Contents
1. Numeric
Any representation of data which has a numeric value is called numeric data type. Numeric data types are used to store numbers.
int
(Integer): Represents whole numbers, both positive and negative, without any decimal point. For example:6
,-12
,500
float
(Floating Point Number): Represents real numbers, including those with decimal points. For example:2.14
where2
is integer and.14
is decimal,-3.6
,36.0
complex
(Complex Number): Represents complex numbers, which consist of a real part and an imaginary part. The imaginary part is denoted with aj
and not withi
like in mathematics. For example:2 + 8j
where2
is real and8j
is imaginary.
2. Boolean
Boolean data types are used to make decisions in your program, represented by these 2 keywords: True
& False
. Notice one thing: True and False always start with a capital letter.
3. Sequence
Sequence data types store an ordered collection of items. The main types are:
3.1 String
A string (str
) is a ordered collection or sequence of one or more characters enclosed in either single quotes ('
), double quotes ("
), or even triple quotes (”’ or “””) for multiline text. It is used to store text or string values. String is immutable, which means you cannot change the string once it is created. For example: "Guys"
, 'Hello'
, '''Hello'''
, """World"""
.
3.2 List
A list (list
) in Python is an ordered collection data type that can hold multiple items in a single variable. You can create a list using square brackets []
and separate items with commas. Lists are mutable, meaning you can change their content. for example: [1, 2, 3, 4, 5]
, ["apple", "banana", "mango"]
, [1, "hello", 3.5, True]
, [[1, 2], [3, 4]]
3.3 Tuple
A tuple
in Python is also an ordered collection data type similar to a list, but with one key difference: tuples are immutable. This means that once a tuple is created, you cannot change, add, or remove its elements. Tuples are great for storing data that should not be modified. Tuples are written with parentheses ()
. For example: for example: (1, 2, 3, 4, 5)
, ("apple", "banana", "mango")
, (1, "hello", 3.5, True)
, ([1, 2], (3, 4))
4. Collection
A collection data type is used to store multiple items together. These data types allow you to group related data and manipulate it easily. Collections can hold data of different types, making them versatile.
4.1 Dictionary
A dictionary (dict
) is a collection of key-value pairs and is similar to a real-life dictionary where you have a word (key) and its definition (value). Each key in a Python dictionary must be unique, but the values can be repeated. You can create a dictionary using curly braces {}
and colons :
. For example: {'name': 'Inkita', 'age': 22, 'city': 'Indore'}
4.2 Set
A set in Python is used to store unique and unordered items, similar to a mathematical set. It automatically removes duplicate values and maintains no specific order of elements. Sets are mutable, so you can add or remove elements, but the items inside a set must be immutable, such as numbers, strings, or tuples. You can create a set using curly braces {}
. for example {"apple", "banana", "cherry"}
.
5. None
None
is a special data type and is used to represent the absence of a value or a null value. It is similar to null
in other programming languages like JavaScript or NULL
in SQL.
Conclusion
Learning Python data types is important because they help you understand how to store and work with different kinds of data in your programs. Mastering them allows you to write cleaner, more efficient code, and solve problems more effectively. With a solid grasp of data types, you can easily manipulate information and build powerful Python applications.