Skip to content

Commit ad7a00d

Browse files
Add files via upload
1 parent 276d333 commit ad7a00d

File tree

4 files changed

+181
-0
lines changed

4 files changed

+181
-0
lines changed

week_9/01-Tuple.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
## Tuple
2+
3+
Tuples are used to store multiple items in a single variable.
4+
5+
Tuple is one of the four built-in data types in Python used to store collections of data. The other three are List, Set, and Dictionary, each with different qualities and usage.
6+
7+
### Ordered
8+
9+
When we say that tuples are ordered, it means that the items have a defined order, and that order will not change.
10+
11+
### Unchangeable
12+
13+
Tuples are unchangeable, meaning that we cannot change, add, or remove items after the tuple has been created.
14+
15+
### Allow Duplicates
16+
17+
A tuple is a collection that is ordered and unchangeable. Tuples can contain duplicate items.
18+
19+
Tuples are written with round brackets `()`.
20+
21+
```python
22+
thistuple = ("apple", "banana", "cherry")
23+
print(thistuple)
24+
print(type(thistyple))
25+
```
26+
## Modifying Tuples
27+
28+
Tuples in Python are immutable, which means you **cannot** directly add or remove items from them once they are created. However, there are some workarounds to modify tuples:
29+
30+
### Adding Items to a Tuple
31+
32+
To add an item to a tuple, you can convert the tuple to a list, add the item, and then convert it back to a tuple.
33+
34+
```python
35+
thistuple = ("apple", "banana", "cherry")
36+
temp_list = list(thistuple) # Convert tuple to list
37+
temp_list.append("orange") # Add item to the list
38+
thistuple = tuple(temp_list) # Convert list back to tuple
39+
print(thistuple) # Output: ('apple', 'banana', 'cherry', 'orange')
40+
```
41+
42+
### Tuple Practice Questions
43+
44+
## Creating Tuples:
45+
Create a tuple named fruits that contains the items "apple", "banana", and "cherry".
46+
47+
## Accessing Elements:
48+
Given a tuple fruits = ("apple", "banana", "cherry"), access and print the second item in the tuple.
49+
50+
## Negative Indexing:
51+
Using the tuple fruits = ("apple", "banana", "cherry"), print the last item using negative indexing.
52+
53+
## Tuple Length:
54+
Create a tuple named colors with the values "red", "green", "blue", and "yellow". Write a program to find and print the length of the tuple.
55+
56+
## Tuple Slicing:
57+
Given the tuple numbers = (10, 20, 30, 40, 50, 60), slice the tuple to get a new tuple with the elements 20, 30, and 40.
58+
59+
## Concatenating Tuples:
60+
Create two tuples, tuple1 with values (1, 2, 3) and tuple2 with values (4, 5, 6). Concatenate these two tuples into a new tuple.
61+
62+
## Finding Elements:
63+
Write a program to check if the element "python" exists in the tuple languages = ("java", "python", "c++", "ruby").
64+
65+
## Tuple Unpacking:
66+
Given a tuple person = ("John", 25, "Engineer"), unpack the values into separate variables and print them.
67+
68+
## Immutable Nature:
69+
Write a program to try to change the first item of the tuple animals = ("cat", "dog", "rabbit") to "bird". What happens?
70+
71+
## Nested Tuples:
72+
Create a tuple named nested_tuple with values (1, (2, 3), 4). Access and print the value 3 from the nested tuple.
73+
74+
## Converting List to Tuple:
75+
Convert the list weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"] to a tuple and print it.
76+
77+
## Iterating Over a Tuple:
78+
Write a program to iterate through the tuple prime_numbers = (2, 3, 5, 7, 11) and print each element.
79+
80+
## Tuple Methods:
81+
Use the tuple method count() to find out how many times the value 4 appears in the tuple numbers = (1, 2, 3, 4, 4, 4, 5).
82+
83+
## Tuple Index:
84+
Write a program to find the index of the value 100 in the tuple data = (10, 20, 30, 100, 50, 100).
85+
86+
## Deleting a Tuple:
87+
Create a tuple months = ("January", "February", "March"). Delete this tuple and try printing it afterward. What happens?

week_9/02-Dictionaries.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
## Dictionary
2+
3+
Dictionaries are used to store data values in **key: value** pairs.
4+
5+
A dictionary is a collection that is ordered*, changeable, and does not allow duplicates. Dictionaries are written with curly brackets `{}`, and each key is associated with a specific value.
6+
7+
### Key Characteristics of Dictionaries
8+
9+
- **Ordered**: As of Python 3.7, dictionaries maintain the order of items based on the insertion order.
10+
- **Changeable**: You can add, modify, and remove items in a dictionary after it has been created.
11+
- **No Duplicates**: Dictionaries do not allow duplicate keys. If a duplicate key is entered, the last value associated with the key will overwrite the previous one.
12+
13+
### Basic Example of a Dictionary
14+
15+
```python
16+
# Creating a dictionary
17+
thisdict = {
18+
"brand": "Ford",
19+
"model": "Mustang",
20+
"year": 1964
21+
}
22+
23+
# Accessing a value
24+
print(thisdict["model"]) # Output: Mustang
25+
26+
# Adding a new key-value pair
27+
thisdict["color"] = "red"
28+
print(thisdict) # Output: {'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'red'}
29+
30+
# Modifying an existing value
31+
thisdict["year"] = 2020
32+
print(thisdict) # Output: {'brand': 'Ford', 'model': 'Mustang', 'year': 2020, 'color': 'red'}
33+
34+
# Removing an item
35+
thisdict.pop("model")
36+
print(thisdict) # Output: {'brand': 'Ford', 'year': 2020, 'color': 'red'}
37+
38+
# Checking if a key exists
39+
if "brand" in thisdict:
40+
print("Yes, 'brand' is a key in the dictionary") # Output: Yes, 'brand' is a key in the dictionary
41+
```
42+
43+
### Dictionary Practice Questions
44+
45+
## Creating a Dictionary:
46+
Create a dictionary named student with the following key-value pairs: "name": "Alice", "age": 22, "major": "Computer Science". Print the dictionary.
47+
48+
## Accessing Dictionary Values:
49+
Given the dictionary car = {"brand": "Toyota", "model": "Corolla", "year": 2021}, write a code snippet to print the value associated with the key "model".
50+
51+
## Modifying Dictionary Values:
52+
Modify the value of the key "year" in the dictionary car to 2022 and print the updated dictionary.
53+
54+
## Adding a New Key-Value Pair:
55+
Add a new key-value pair "color": "blue" to the dictionary car and print the updated dictionary.
56+
57+
## Removing a Key-Value Pair:
58+
Remove the key-value pair with the key "major" from the dictionary student and print the updated dictionary.

week_9/Dictionaries.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Creating a dictionary
2+
thisdict = {
3+
"brand": "Ford",
4+
"model": "Mustang",
5+
"year": 1964
6+
}
7+
8+
# Accessing a value
9+
print(thisdict["model"]) # Output: Mustang
10+
11+
# Adding a new key-value pair
12+
thisdict["color"] = "red"
13+
print(thisdict) # Output: {'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'red'}
14+
15+
# Modifying an existing value
16+
thisdict["year"] = 2020
17+
print(thisdict) # Output: {'brand': 'Ford', 'model': 'Mustang', 'year': 2020, 'color': 'red'}
18+
19+
# Removing an item
20+
thisdict.pop("model")
21+
print(thisdict) # Output: {'brand': 'Ford', 'year': 2020, 'color': 'red'}
22+
23+
# Checking if a key exists
24+
if "brand" in thisdict:
25+
print("Yes, 'brand' is a key in the dictionary")

week_9/Tuple.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tuple1 = ("apple", "banana", "cherry")
2+
print(tuple1)
3+
print(type(tuple1))
4+
print(len(tuple1))
5+
6+
7+
tuple1 = ("apple", "banana", "cherry")
8+
temp_list = list(tuple1) # Convert tuple to list
9+
temp_list.append("orange") # Add item to the list
10+
thistuple = tuple(temp_list) # Convert list back to tuple
11+
print(thistuple) # Output: ('apple', 'banana', 'cherry', 'orange')

0 commit comments

Comments
 (0)