Part - 2 ๐Ÿ“ƒ~ Lists in Python ~๐Ÿ“ƒ

Part - 2 ๐Ÿ“ƒ~ Lists in Python ~๐Ÿ“ƒ

ยท

4 min read

Alright, today we will finish up Lists as well as our basic introduction to Python Programming Language. By now, any beginner who's reading this is advised to start doing very easy coding problems on Leetcode or any platform of your choice. Trying to code yourself and figuring out what your mistakes are, is gonna help more than any blog ever!


Part - 1 covered up until slicing, the things remaining are Adding and removing items from our list, using min-max function and the sum function, which is relatively a moderate topic to understand. So we will take some questions about those topics in this blog and we'll learn how to solve them in a really easy way!


Let's start by creating a new list to refer to in this blog :-

This is a list containing the names of different flowers.

flowers_list = ["pink primrose", "hard-leaved pocket orchid", "canterbury bells", "sweet pea", "english marigold", "tiger lily", "moon orchid", "bird of paradise", "monkshood", "globe thistle"]

Adding Items to List :-

To add items, we use the command "append" as in :

flowers_list.append("snapdragon")

Therefore, we added the name "snapdragon" to our list. And to view the changes, we can then again print the list

print(flowers_list)

["pink primrose", "hard-leaved pocket orchid", "canterbury bells", "sweet pea", "english marigold", "tiger lily", "moon orchid", "bird of paradise", "monkshood", "globe thistle", "snapdragon"]

Removing Items from List :-

Similar to adding, we can use the command "remove" with our list to remove items.

flowers_list.remove("bird of paradise")

# Then we print the list

print(flowers_list)

["pink primrose", "hard-leaved pocket orchid", "canterbury bells", "sweet pea", "english marigold", "tiger lily", "moon orchid", "monkshood", "globe thistle", "snapdragon"]


Min-Max Functions :-

Min-Max functions are used to find out the minimum and maximum value from a list. We are gonna make another fresh list for a store which shows how many books were sold daily in a week, to see how the function works.

hardcover_sales = [139, 128, 172, 139, 191, 168, 170]
print(min(hardcover_sales))
print(max(hardcover_sales))

This is gonna print the minimum and maximum values from the list, but lets make the output a little prettier;

print("Minimum:", min(hardcover_sales))
print("Maximum:", max(hardcover_sales))

Now the output will be :-

Minimum: 128

Maximum: 191


Sum Function :-

Sum function, pretty straight forwardly, adds the items in a list.

print("Total books sold in one week:", sum(hardcover_sales))

Total books sold in one week: 1107

Bonus Functionality :-

We can use the sum function to find the average values from a list, for example, if i wanna see the average books sold in that week, we're gonna do:

print("Average books sold in the week:", sum(hardcover_sales)/7)

Average books sold in first five days: 158.14285


Well, that was it for the basics, let's practice some questions now~!

Question - ๐Ÿ‘‡๐Ÿป

  • Point - 1

To answer the first point, we have to find the average number of customers that visited on the first seven days of the month. We can do that by calling the list from start up until the 7th element, adding all the numbers and then dividing it by 7.

How to do that is :

print("Average number of customers who visited in the first seven days" , sum(num_customers[:7])/7)
  • Point - 2

Similar to the first one but we take the last 7 values, and we do that by taking negative indexing, as in :

print("Average number of customers who visited in the last seven days" , sum(num_customers[-7:])/7)
  • Point - 3

This point wants us to find whats the maximum number of customers that we got on a single day in that month, very simple.

print("Maximum number of Customers that visited in a single day: ", max(num_customers))
  • Point - 4

Same problem as above, we just find the minimum number of customers.

print("Minimum number of Customers that visited in a single day: ", min(num_customers))

And that concludes our Introduction to Python, covering basics of the basics! I was not very consistent with my blog but happy that i finished it :D
Will be happy to see everyone when i start another series! Thank you! and finally....

Consistency is the Key ๐Ÿ—


Peace~๐Ÿ•Š๏ธ

Yash K.

ย