Data Types:
What Are Data Types?
Data types classify or categorize data items.
They determine what operations can be performed on specific data.
Python’s Data Types:
Numeric Types:
Integer (whole numbers)
Complex (imaginary numbers)
Float (decimal numbers)
Sequential Types:
String (text)
Lists (ordered collection of data)
Tuples (immutable collection of Python objects)
Boolean Type:
- Represents True or False values
Set Type:
- Unordered collection of unique elements
Dictionaries:
- Unordered key-value pairs (used for optimization)
Example:
Imagine a toolbox:
Numeric tools (measuring tape, calculator)
Sequential tools (to-do list, shopping list)
Boolean tool (light switch: on/off)
Set tool (deck of cards)
Dictionary tool (phone book)
Data Structures:
Organizing Data Efficiently:
Data structures help organize data for efficient access.
They form the foundation of any program.
Examples:
Lists: Ordered collection of data (items can be of different types).
Tuples: Immutable collection of Python objects (similar to lists but unchangeable).
Dictionaries: Unordered key-value pairs (used for optimization).
Let's use this knowledge in our own practice.
Task 1: Exploring Numeric Types
Objective: Understand numeric data types and perform basic operations.
Create a Python Script:
Open your favorite code editor (e.g., Visual Studio Code).
Create a new Python script (e.g.,
numeric_types.py
).
Explore Numeric Types:
In your script, define variables for different numeric types:
my_integer = 42 my_float = 3.14 my_complex = 2 + 3j
Perform Operations:
- Add, subtract, multiply, and divide these numeric values.
Run Your Script:
Task 2: Working with Strings
Objective: Explore string data type and its methods.
Create a New Script:
- In the same directory, create another Python script (e.g.,
string_operations.py
).
- In the same directory, create another Python script (e.g.,
Define a String:
In your script, define a string variable:
my_string = "Hello, Python!"
String Methods:
Use string methods to:
Convert to uppercase.
Replace “Python” with “World.”
Check if it starts with “Hello.”
Print the Modified String:
- Print the modified string after applying the methods.
Run Your Script:
Task 3: Lists and List Operations
Objective:
Learn about lists and perform list operations.
Create a New Script:
- Create a Python script (e.g.,
list_operations.py
).
- Create a Python script (e.g.,
Define a List:
In your script, define a list of your favorite fruits:Python
my_fruits = ["apple", "banana", "orange"]
List Operations:
Append “grape” to the list.
Print the length of the list.
Access the second fruit (“banana”).
Sort the list alphabetically.
Print the Modified List:
- Print the list after performing the operations.
Run Your Script:
Task 4: Tuples and Immutability
Objective:
Explore tuples and understand their immutability.
Create a New Script:
- Create a Python script (e.g.,
tuple_immutable.py
).
- Create a Python script (e.g.,
Define a Tuple:
In your script, define a tuple of your favorite colors:
my_colors = ("red", "green", "blue")
Access Tuple Elements:
- Print the second color (“green”).
Try to Modify the Tuple:
- Tries to modify the tuple (e.g.,
my_colors[0] = "yellow"
).
- Tries to modify the tuple (e.g.,
Run Your Script:
Task 5: Dictionaries and Key-Value Pairs
Objective: Learn about dictionaries and their key-value pairs.
Create a New Script:
- Create a Python script (e.g.,
dictionary_key_value.py
).
- Create a Python script (e.g.,
Define a Dictionary:
In your script, define a dictionary of book genres:
book_genres = { "fantasy": "Harry Potter", "mystery": "Sherlock Holmes", "science fiction": "Dune" }
Access Dictionary Values:
- Print the book title for the “fantasy” genre.
Run Your Script:
Thank you for reading😉.