Skip to content

Commit bae2240

Browse files
#3 - Python
1 parent 2287fe2 commit bae2240

File tree

1 file changed

+180
-0
lines changed

1 file changed

+180
-0
lines changed
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
### Python Data Structures ###
2+
3+
#! List
4+
5+
my_list = [1, 6, 3, 10, 5]
6+
print(my_list)
7+
8+
my_str = 'hello'
9+
print(list(my_str))
10+
11+
# Append
12+
my_list.append(8)
13+
print(my_list)
14+
15+
# Remove
16+
my_list.remove(8)
17+
print(my_list)
18+
19+
# Insert
20+
my_list.insert(2, 50)
21+
print(my_list)
22+
23+
# Sort
24+
my_list.sort()
25+
print(my_list)
26+
27+
#! Dictionary
28+
29+
my_dict = { 'name': 'John', 'lastname': 'Doe', 'age': 44}
30+
print(my_dict)
31+
32+
# Add
33+
my_dict['country'] = 'Norway'
34+
print(my_dict)
35+
36+
# Remove
37+
my_dict.pop('name')
38+
print(my_dict)
39+
40+
# Update
41+
my_dict['lastname'] = 'new lastname'
42+
print(my_dict)
43+
44+
# Order
45+
print(sorted(my_dict.keys()))
46+
47+
#! Tuple
48+
49+
my_tuple = (1, 2, 3, 8, 7, 4)
50+
print(my_tuple)
51+
52+
#* Since tuples are immutable you can't add or remove items.
53+
54+
# Order
55+
print(sorted(my_tuple))
56+
57+
#! Set
58+
my_set = {1, 2, 3, 5, 8, 12, 3, 3, 4, 5} # The 3 will be only one time since sets don't allow duplicated items.
59+
print(my_set)
60+
61+
# Add
62+
my_set.add(50)
63+
print(my_set)
64+
65+
# Remove
66+
my_set.remove(4)
67+
print(my_set)
68+
69+
# Update
70+
my_second_set = {100, 200, 500}
71+
my_set.update(my_second_set)
72+
print(my_set)
73+
74+
# Order
75+
print(sorted(my_set))
76+
77+
#! Optional Challenge
78+
print('---Optional Challenge---')
79+
80+
# Start the program
81+
# Give the user a set of option, to insert, search, update, or remove a contact
82+
# Ask the user for the data corresponding to the operation that was selected
83+
# Execute the selected operation
84+
# Verify that telephone number data type is numeric and contains no more than 11 digits
85+
# Notify the user if the operation was successful or not
86+
# Ask the user to add another contact or to end the programm
87+
88+
contacts_list = []
89+
id = 1
90+
91+
user_start = input('Please type start to run the program: ')
92+
run_program = False
93+
94+
if user_start == 'start':
95+
run_program = True
96+
97+
def search_contact(contact_name, list):
98+
for contact in list:
99+
if contact_name == contact['contact_name'].lower():
100+
return contact
101+
102+
def update_contact(contact_name, list):
103+
for contact in list:
104+
index = list.index(contact)
105+
if contact_name == list[index]['contact_name'].lower():
106+
print(list[index]['contact_name'])
107+
new_name = input('Type the new name for your contact: ').lower()
108+
new_phone = input('Type the new phone for your contact: ').lower()
109+
list[index]['contact_name'] = new_name
110+
list[index]['contact_phone'] = new_phone
111+
print(f'Contact updated: {list[index]['contact_name']} - {list[index]['contact_phone']}')
112+
113+
def delete_contact(contact_name, list):
114+
for contact in list:
115+
index = list.index(contact)
116+
if contact_name == list[index]['contact_name'].lower():
117+
print(list[index]['contact_name'])
118+
del list[index]
119+
print('Contact deleted')
120+
121+
def show_contacts(list):
122+
if len(list) == 0:
123+
print('Your contact list is empty')
124+
125+
for contact in list:
126+
print(f'{contact['contact_name']} - {contact['contact_phone']}')
127+
128+
129+
while run_program == True:
130+
print('----Choices----')
131+
user_choice = input('a: Add contact\nb: Search contact\nc: Update contact\nd: Remove contact\ne: Show all contacts\nType the corresponding letter to perform an action or type "exit" to end the program: ').lower()
132+
133+
if user_choice== 'exit':
134+
run_program = False
135+
136+
# Add Contact
137+
138+
if user_choice == 'a':
139+
print('----New Contact----')
140+
contact_name: str = input('Enter your contact name: ').title()
141+
contact_phone: str = input('Enter your contact phone number: ')
142+
143+
if not contact_phone.isnumeric():
144+
print('Phone number should contain only numbers!')
145+
146+
if len(contact_phone) > 11:
147+
print('Phone number should contain only 11 digits or less')
148+
else:
149+
new_contact = {'id': id, 'contact_name': contact_name, 'contact_phone': contact_phone }
150+
id += 1
151+
contacts_list.append(new_contact)
152+
153+
# Search Contact
154+
155+
if user_choice == 'b':
156+
print('----Search Contact----')
157+
contact_name: str = input('Type the name of the contact you want to search: ').lower()
158+
contact = search_contact(contact_name, contacts_list)
159+
print(f'Contact name: {contact['contact_name'].title()}\nContact phone number: {contact['contact_phone']}')
160+
161+
# Update Contact
162+
163+
if user_choice == 'c':
164+
print('----Search Update----')
165+
contact_name: str = input('Type the name of the contact you want to update: ').lower()
166+
update_contact(contact_name, contacts_list)
167+
168+
# Remove Contact
169+
170+
if user_choice == 'd':
171+
print('----Remove Contact----')
172+
contact_name: str = input('Type the name of the user you want to remove: ').lower()
173+
delete_contact(contact_name, contacts_list)
174+
175+
# Show Contacts
176+
177+
if user_choice == 'e':
178+
print('----Contacts----')
179+
show_contacts(contacts_list)
180+

0 commit comments

Comments
 (0)