Skip to content

Commit b77058e

Browse files
Add files via upload
1 parent f31f391 commit b77058e

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed

week_8/00-Data types.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Built-in Data Types
2+
3+
In programming, a **data type** is an important concept. Variables can store data of different types, and different types can do different things.
4+
5+
Python has the following built-in data types by default, categorized as follows:
6+
7+
- **Text Type:** `str`
8+
- **Numeric Types:** `int`, `float`, `complex`
9+
- **Sequence Types:** `list`, `tuple`, `range`
10+
- **Mapping Type:** `dict`
11+
- **Set Types:** `set`, `frozenset`
12+
- **Boolean Type:** `bool`
13+
- **Binary Types:** `bytes`, `bytearray`, `memoryview`
14+
- **None Type:** `NoneType`

week_8/01-List.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
## Python Data Types: List, Set, Tuple, and Dictionary
2+
3+
### 1. List
4+
Lists are used to store multiple items in a single variable.List items are ordered, changeable, and allow duplicate values.
5+
Lists are created using square brackets []
6+
7+
```python
8+
# Creating a list
9+
fruits = ["apple", "banana", "cherry"]
10+
print(type(fruits)) #Class List
11+
print(lent(fruits)) #2
12+
13+
# Accessing list items
14+
print(fruits[0]) # Output: apple
15+
16+
# Adding an item to the list
17+
fruits.append("orange")
18+
print(fruits) # Output: ['apple', 'banana', 'cherry', 'orange']
19+
```
20+
21+
## Here are some practice questions for Python lists:
22+
23+
*Practice Questions*
24+
## Basic Operations:
25+
26+
Create a list of your favorite fruits. Add a new fruit to the list and then remove one fruit from the list. Print the updated list.
27+
Write a Python program to find the length of a list without using the built-in len() function.
28+
29+
## Accessing Elements:
30+
31+
Given the list numbers = [10, 20, 30, 40, 50], write a Python program to access the third element.
32+
Write a Python program to access the last element of a list using negative indexing.
33+
34+
## Slicing and Modifying Lists:
35+
36+
Write a Python program to reverse a list using slicing.
37+
Given a list colors = ["red", "green", "blue", "yellow", "purple"], write a program to replace "blue" with "orange".
38+
39+
## List Methods:
40+
41+
Write a Python program to count the occurrences of a specific element in a list.
42+
Create a list of numbers and write a Python program to sort the list in descending order.
43+
44+
## Comprehensions:
45+
46+
Write a list comprehension that creates a list of the squares of all even numbers from 0 to 20.
47+
Given a list of strings, write a list comprehension to create a new list with only the strings that have more than 3 characters.
48+
49+
## Nested Lists:
50+
51+
Create a nested list (a list inside another list) and write a program to access an element from the inner list.
52+
Write a Python program to flatten a nested list (convert it into a single list with no nested lists).
53+
54+
## Advanced Operations:
55+
56+
Write a Python program to remove all occurrences of a specific value from a list without using a loop.
57+
Given two lists, write a Python program to merge them into a dictionary where elements from the first list are keys and elements from the second list are values.
58+
59+
These questions cover various aspects of working with lists in Python and should provide a good range of practice.
60+

week_8/02-Set.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Sets are used to store multiple items in a single variable.
2+
3+
A **set** is one of the 4 built-in data types in Python used to store collections of data. The other 3 are **List**, **Tuple**, and **Dictionary**, each with different qualities and usage.
4+
5+
A **set** is a collection that is:
6+
- **Unordered**: The items have no defined order.
7+
- **Unchangeable***: The items cannot be modified after the set is created.
8+
- **Unindexed**: The items do not have an index.
9+
10+
*Note: Although the items themselves cannot be changed, new items can be added or removed from the set.
11+
```Python
12+
myset={10,20,"Ali","Zain"}
13+
print(myste)
14+
print(types(set))
15+
16+
```
17+
### Practice Questions for Sets
18+
**Basic Operations:**
19+
20+
Create a set of your favorite colors. Add a new color to the set and then remove one color from the set. Print the updated set.
21+
Write a Python program to find the number of items in a set using the built-in len() function.
22+
23+
## Set Methods:
24+
25+
Given two sets A = {1, 2, 3, 4} and B = {3, 4, 5, 6}, write a Python program to find their union and intersection.
26+
Write a Python program to check if one set is a subset of another set.
27+
28+
## Manipulating Sets:
29+
30+
Write a Python program to remove an element from a set using the discard() method. What happens if the element does not exist in the set?
31+
Create two sets and write a Python program to find their symmetric difference.
32+
33+
## Set Comprehension:
34+
35+
Write a set comprehension that creates a set of the squares of all odd numbers from 0 to 10.
36+
Given a set of strings, write a set comprehension to create a new set with only the strings that start with the letter 'a'.
37+
38+
## Advanced Operations:
39+
40+
Write a Python program to remove all elements from a set.
41+
Given a set of numbers, write a Python program to find the maximum and minimum values in the set.
42+
43+
## Using Sets to Solve Problems:
44+
45+
Write a Python program that takes two lists and returns a set of all the elements that are present in both lists.
46+
Given a list of words, write a Python program to find all unique words using a set.
47+
48+
## Converting Between Data Types:
49+
50+
Write a Python program to convert a list to a set and then back to a list. Explain why this might be useful.
51+
Given a string, write a Python program to convert it into a set of characters.

week_8/list.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Creating a list
2+
fruits = ["apple", "banana", "cherry"]
3+
4+
# Accessing list items
5+
print(fruits[0]) # Output: apple
6+
7+
# Adding an item
8+
fruits.append("Hello")
9+
print(fruits)

week_8/set.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Creating a set
2+
unique_numbers = {1, 2, 3, 4, 5}
3+
4+
# Adding an item to the set
5+
unique_numbers.add(6)
6+
print(unique_numbers) # Output: {1, 2, 3, 4, 5, 6}
7+
8+
# Sets do not allow duplicate values
9+
unique_numbers.add(3)
10+
print(unique_numbers) # Output: {1, 2, 3, 4, 5, 6}

0 commit comments

Comments
 (0)