| EX | Exercises |
Introduction
This unit covers Python’s data structures, including lists and data frames (using pandas). You will work through various tasks to build familiarity with these concepts.
🧪 Task 1: Working with Lists
📋 Description:
In this task, you will practice using Python lists. You’ll define lists, access elements, and apply key list methods such as append(), remove(), and extend().
️ Instructions:
- Define two lists:
list1 = [19, 20, 3]
list2 = [48, 5, 6]
- Perform the following operations:
- Concatenate
list1andlist2to create a new list calledmy_list. - Calculate the sum of
list1(use Accessing elements by index, no loops). - Find the length of
list1using thelen()function. - Print the second element of
list2.
- Concatenate
- Modify
my_list:- Append a new element
'apple'usingappend(). - Remove the value
19from the list usingremove(). - Extend the list with another list
['tree', 'leave', 'root']usingextend(). - Finally, print the updated length of
my_list.
- Append a new element
💡 Reminder:
append()adds a single item.extend()adds each element from another iterable.- Lists are mutable, so you can modify them in place.
🧪 Task 2: Compare Array Elements
📋 Description:
You will create a NumPy array and use an if-else statement to compare specific elements. No loops are required.
️ Instructions:
- Import the NumPy library.
- Create an array called
scoreswith the values[88, 92, 75, 91]. - Compare the first and last elements of the array using an
if-elsestatement:- If the first element is greater than the last, print:
"First score is higher." - Otherwise, print:
"Last score is higher or equal."
- If the first element is greater than the last, print:
Task 3: Data Frames
Description:
Learn how to create and manipulate data frames using pandas.
Instructions:
- Create a data frame using pandas with the following data:
Name = ['Alice', 'Bob', 'Charlie']Age = [25, 30, 35]Salary = [50000, 60000, 70000]
- Perform the following operations:
- Display the first two rows of the data frame.
- Add a new column called
Departmentwith values['Human Resources', 'Engineering', 'Marketing']. - Select the
NameandSalarycolumns. - Filter the data frame to only show rows where
Age > 28.
Task 3: Matrices
Description:
Learn how to create and manipulate matrices using numpy.
Instructions:
- Create a 2x3 matrix with the following values:
[[1, 2, 3], [4, 5, 6]] - Perform the following operations:
- Transpose the matrix.
- Calculate the sum of all elements.
- Multiply each element of the matrix by 2.
- Access the element at row 1, column 2.
Happy coding!