Python Assignment
Standard Data Types
By
[Link] vel
22SUIT034
Introduction to Standard Data Types in Python:
In Python, data types are the different kinds of data that can be stored and
manipulated. Python has various built-in data types that allow you to work with
different types of values such as numbers, text, and more. Understanding these data
types is important because they define how data is stored, accessed, and used in a
program.
The main categories of standard data types in Python are:
Numeric Types: These store numbers (integers and floating-point numbers).
Sequence Types: These are ordered collections of items (like strings, lists, and
tuples).
Set Types: These store unique, unordered elements.
Mapping Type: This represents key-value pairs (like dictionaries).
Boolean Type: These represent True or False values.
Binary Types: These are used to store raw binary data (like bytes).
In this assignment, we will discuss these data types in detail with simple examples.
Numeric Data Types:
Python provides several numeric types to work with numbers:
1. Integer (int):
An integer is a whole number (positive, negative, or zero) without a decimal point. In Python,
integers can be very large.
Example:
age = 25
height = -180
age is an integer with value 25.
height is an integer with value -180.
2. Floating-Point (float):
A float is a number that has a decimal point. Floats are used to represent real numbers.
Example:
temperature = 37.5
pi = 3.14159
temperature is a floating-point number with value 37.5.
pi is a floating-point number with value 3.14159.
3. Complex Number (complex):
A complex number has two parts: a real part and an imaginary part. The imaginary part is
represented by a "j" or "J".
Example:
x = 2 + 3j
y = 1 - 4j
x is a complex number with a real part 2 and imaginary part 3.
Sequence Types
Sequence types are used to store ordered collections of items. The most common sequence
types in Python are strings, lists, and tuples.
4. String (str):
A string is a sequence of characters enclosed in quotes (single or double). Strings are used to
store text.
Example:
name = "John"
greeting = 'Hello, World!'
name is a string with value "John".
greeting is a string with value "Hello, World!".
Strings are immutable, meaning once created, they cannot be changed.
5. List (list):
A list is an ordered, mutable collection of items. Lists can store different types of data
(integers, strings, etc.).
Example:
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4]
fruits is a list containing three strings.
numbers is a list containing four integers.
Lists are mutable, meaning you can change, add, or remove items.
6. Tuple (tuple):
A tuple is similar to a list but is immutable. Once a tuple is created, its values cannot be
changed.
Example:
coordinates = (10, 20)
point = (5, 7, 12)
coordinates is a tuple containing two numbers.
point is a tuple containing three numbers.
Other Data Types and Conclusion
7. Set (set):
A set is an unordered collection of unique elements. Sets are useful when you need to store
distinct items without duplicates.
Example:
numbers = {1, 2, 3, 4, 5}
fruits = {"apple", "banana", "cherry"}
numbers is a set containing unique numbers.
fruits is a set containing unique fruits.
8. Dictionary (dict):
A dictionary is a collection of key-value pairs. Each key in a dictionary is unique, and each
key is used to access its corresponding value.
Example:
student = {"name": "John", "age": 25, "major": "Computer Science"}
student is a dictionary with three key-value pairs: "name", "age", and "major".
9. Boolean (bool):
A Boolean represents one of two values: True or False. Booleans are used in conditions and
comparisons.
Example:
is_active = True
is_sunny = False
is_active is a Boolean with value True.
is_sunny is a Boolean with value False.
10. Binary Types:
Binary types are used to store raw binary data, such as images or files. Python provides three
binary types: bytes, bytearray, and memoryview.
Example:
binary_data = b"Hello"
byte_array = bytearray([1, 2, 3, 4])
binary_data is a bytes object.
byte_array is a bytearray object.
Conclusion:
Understanding the standard data types in Python is crucial for effective programming.
Each data type has specific features that make it suitable for different tasks. Numeric
types like integers and floats are used for mathematical calculations, while sequence
types like strings, lists, and tuples store ordered data. Sets and dictionaries are perfect
for managing unique elements and key-value pairs, respectively. Booleans are used
for decision-making, and binary types handle raw data.
By mastering these data types, you can write more efficient and flexible Python
programs. They form the foundation of working with data in Python, and as you
advance, you'll encounter more complex data types and structures.