Topics:
1. List
2. List - sort order
3. List - min, max, sum
4. List - Add values
5. List - Remove values
6. List - Modify values
7. List - extend
8. List - copy
9. Task - Selling Lemonade exercise
10. Split Method
11. Join Method
12. Replace Method
13. Task - Fill a List
1. List
>>Return to Menu
List is one of Python's four built-in data types for storing collections of data; the other three are Tuple, Set, and Dictionary, all of which have different properties and applications.
List items are ordered, changeable, and allow duplicate values.
Square brackets are used to write lists.
- Ordered:
Items have a defined order, and that order will not change. If you add new items to a list, the new items will be placed at the end of the list.
- Changeable:
We can change, add, and remove items in a list after it has been created.
- Allow Duplicates:
Lists are indexed, hence they can have items with the same value.
#empty Lists
empty_list = []
empyt_list = list()
people = ['Ben', 'Samuel', 'Mary', 'Bethany', 'Cross']
print(people)
#The person at index=2
print(people[2])
#The last person in the list
print(people[-1])
#The people from index=2 to index=4(excluded)
print(people[2:4])
#Everyone from index=2
print(people[2:])
#Everyone from the first person to index=4(excluded)
print(people[:4])
#Everyone from the first person with step=2
print(people[::2])
#The list in reverse
print(people[::-1])
#The number of people in the list
print(len(people))
#The Index location of person 'Bethany'
print(people.index('Bethany'))
#Number of times 'Mary' is found in the list
print(people.count('Mary'))
2. List - sort order
>>Return to Menu
people = ['Ben', 'Samuel', 'Mary', 'Bethany', 'Cross']
ages = [21, 10, 35, 50, 41]
#Sort list in ascending order
people.sort()
print(people)
ages.sort()
print(ages)
#Sort list in descending order
people.sort(reverse=True)
print(people)
ages.sort(reverse=True)
print(ages)
#Rearrange list in reverse order
people = ['Ben', 'Samuel', 'Mary', 'Bethany', 'Cross']
ages = [21, 10, 35, 50, 41]
people.reverse()
print(people)
ages.reverse()
print(ages)
3. List - min, max, sum
>>Return to Menu
people = ['Ben', 'Samuel', 'Mary', 'Bethany', 'Cross']
ages = [21, 10, 35, 50, 41]
#Find the lowest value in list
print(min(people))
print(min(ages))
#Find the highest value in list
print(max(people))
print(max(ages))
#Find the sum of values in list
print(sum(ages))
4. List - Add values
>>Return to Menu
people = ['Ben', 'Samuel', 'Mary', 'Bethany', 'Cross']
ages = [21, 10, 35, 50, 41]
#Append value to list
people.append('Bob')
print(people)
ages.append(80)
print(ages)
#Insert value at position index=1
people.insert(1,'Charles')
print(people)
ages.insert(1, 12)
print(ages)
5. List - Remove values
>>Return to Menu
people = ['Ben', 'Samuel', 'Mary', 'Bethany', 'Cross']
ages = [21, 10, 35, 50, 41]
#Remove value from list
people.remove('Samuel')
print(people)
ages.remove(10)
print(ages)
#Pop(remove) the index=-1 (last value) from list to reuse
popped_value = people.pop(-1)
print(people)
print(popped_value)
popped_value = ages.pop(-1)
print(ages)
print(popped_value)
#Clear[Empty] the list
people.clear()
print(people)
ages.clear()
print(ages)
#Completely delete the list
del people
del ages
print(people)
6. List - Modify values
>>Return to Menu
people = ['Ben', 'Samuel', 'Mary', 'Bethany', 'Cross']
ages = [21, 10, 35, 50, 41]
#Change[modify] the value at index=2
people[2] = "Shawn"
print(people)
ages[2] = 19
print(ages)
7. List - extend
>>Return to Menu
people = ['Ben', 'Samuel', 'Mary', 'Bethany', 'Cross']
ages = [21, 10, 35, 50, 41]
#Join two lists together
people.extend(ages)
print(people)
8. List - copy
>>Return to Menu
people = ['Ben', 'Samuel', 'Mary', 'Bethany', 'Cross']
ages = [21, 10, 35, 50, 41]
#Copy a list
students = people[:]
print(students)
clients = people.copy()
print(clients)
attendees = list(people)
print(attendees)
9. Task - Selling Lemonade exercise
>>Return to Menu
You sell lemonade in 2 weeks. The lists shows number of lemonades sold per week. The profit for each lemonade sold is $1.5.
sales_w1 = [7,3,42,19,15,35,9]
sales_w2 = [12,4,26,10,7,28]
Add another day to week 2 list by capturing a number as input for how many lemonade were sold on that day. Combine the two lists into a list called "sales". Calculate and print how much you have earned on the best day, worst day, and in total on both best and worst days.
sales_w1 = [7,3,42,19,15,35,9]
sales_w2 = [12,4,26,10,7,28]
sales = []
sales_w2_last = int(input('Week 2 last sale: '))
sales_w2.append(sales_w2_last)
print(sales_w1)
print(sales_w2)
sales = sales_w1.copy()
sales.extend(sales_w2)
print(sales)
print(f'On your best day you earned ${float(max(sales))*1.5}.')
print(f'On your worst day you earned ${float(min(sales))*1.5}.')
print(f'Total of best and worst day earned is ${float(max(sales))*1.5 + float(min(sales))*1.5}.')
Another Method:
sales_w1 = [7,3,42,19,15,35,9]
sales_w2 = [12,4,26,10,7,28]
sales = []
new_day = input('Enter #of lemonades for new day: ')
sales_w2.append(int(new_day))
sales = sales_w1 + sales_w2
sales.sort()
worst_day_prof = sales[0] * 1.5
best_day_prof = sales[-1] * 1.5
print(f'Worst day profit:$ {worst_day_prof}')
print(f'Best day profit:$ {best_day_prof}')
print(f'Combined profit:$ {worst_day_prof + best_day_prof}')
10. Split Method
>>Return to Menu
The split method converts a String to a List.
#The split method converts a String to a List.
msg ='Welcome to Python class'
csv = 'Eric,John,Michael,Terry,Graham'
print(msg.split())
print(msg.split(' '))
print(csv.split(','))
11. Join Method
>>Return to Menu
The join method converts a List to a String.
#The join method converts a List to a String.
friends= ['Eric','John','Michael','Terry','Graham']
print(' '.join(friends))
print(','.join(friends))
print('-'.join(friends))
12. Replace Method
>>Return to Menu
The replace method replaces a word or character with another in a string.
#The replace method replaces a word or character with another in a string.
msg ='Welcome to Python class'
print(msg.replace('Python', 'Javascript'))
13. Task - Fill a List
>>Return to Menu
#From the list "csv", fill a list "friends_list" properly with the names of all the friends, one per slot.
csv = 'Eric,John,Michael,Terry,Graham:TerryG;Brian'
friends_list = ['Exercise: fill me with names']
Method I:
csv = 'Eric,John,Michael,Terry,Graham:TerryG;Brian'
friends_list = ['Exercise: fill me with names']
csv = csv.replace(':', ',')
csv = csv.replace(';', ',')
csv = csv.split(",")
friends_list = csv
print(friends_list)
Method II:
csv = 'Eric,John,Michael,Terry,Graham:TerryG;Brian'
friends_list = ['Exercise: fill me with names']
csv = csv.split(":")
csv = ",".join(csv)
csv = csv.split(";")
csv = ",".join(csv)
csv = csv.split(",")
friends_list = csv
print(friends_list)
#End
Hope you enjoyed this! :) Follow me for more contents...
Get in Touch:
ifeanyiomeata.com
contact@ifeanyiomeata.com
Youtube: youtube.com/c/IfeanyiOmeata
Linkedin: linkedin.com/in/omeatai
Twitter: twitter.com/iomeata
Github: github.com/omeatai
Stackoverflow: stackoverflow.com/users/2689166/omeatai
Hashnode: hashnode.com/@omeatai
Medium: medium.com/@omeatai
© 2022