Data Types in Python Programming Language
In this lesson, we will learn about the Data Types in the Python programming language along with a quiz on it.
What is Data Type
In Python, a data type classifies the kind of value stored in a variable. Python is dynamically typed, meaning the type of a variable is determined at runtime based on the assigned value. Memory management is handled automatically through reference counting and garbage collection. Python supports the following data types:
Integer
The integer data type (represented as int) is used to represent whole numbers, which can be positive, negative, or zero. Unlike some other programming languages, Python's integer type has no size limit, meaning it can handle very large values, as long as you have enough memory.
Example: 56, 8, 91, 436, etc.
Float
The float data type (represented as float) is used to represent floating-point numbers, which are numbers that contain a decimal point. Floats can represent both positive and negative numbers, including fractions and very small or large values.
Example: 2.3, 18.12, 294.0 etc.
Complex Number
A complex number data type (represented as complex) is a number that has both a real part and an imaginary part. Python requires j (lowercase) for imaginary parts. Complex numbers are commonly used in fields like engineering, physics, and computer science, particularly in applications involving signal processing, control systems, and quantum mechanics.
Example: 9 + 21j, -3 - 9j, -72 + 81j, etc.
Boolean
The boolean data type (represented as bool) is used to represent logical values, which can be either True or False. This data type is essential for making decisions in programming, allowing you to control the flow of your code based on certain conditions.
String
The string data type (represented as str) is used to represent sequences of characters. Strings can include letters, numbers, symbols, and whitespace. They are one of the most commonly used data types for handling textual data. Strings can be defined using single quotes ('), double quotes ("), or triple quotes (''' or """) for multi-line strings.
Example: 'hello', "Python", "4586", "56$", " Programming ", etc.
List
The list data type is used to store an ordered collection of items. Lists can hold items of different data types, including other lists, and are mutable, meaning their contents can be changed after creation. Lists are defined using square brackets [ ]
Example: ["apple", "banana", "cherry"], [1, 2, 3, 'apple', 4.5].
Tuple
The tuple data type is used to store an ordered collection of items. Unlike lists, tuples are immutable, meaning once they are created, their contents cannot be changed. This property makes tuples useful for representing fixed collections of items. Tuples are defined using parenthesis ( )
Example: (15, "Albert", 18.20)
Dictionary
A dictionary is an unordered collection of key-value pairs, where each key is unique and is used to access its corresponding value. Dictionaries are defined using curly braces { } and allow for fast retrieval of values based on their keys.
Example: {"name" : "frankie" , "age" : 18}
Set
A set is an unordered collection of unique elements, meaning it cannot contain duplicate items.
Example: {"a", "b", "c"}
In the upcoming lessons, we will explore all the data types in detail and discuss their uses.
Test Your Knowledge
Attempt the questions in the exercise to check whether you have understood the lesson clearly.