Anuj Shukla
Posted on March 24, 2021
print(array, end=" ")
Everytime you print something in python it automatically creates a new line. It is very useful unless you want to print something in the same line for that you use end=" " . print("whatever you want to", end="")set()
This is very helpful in removing duplicate items, it is used especially when comparing values. Let's say you want to get the lowest and highest scores from a class usually you want to just make a list and sort it and you will have scores in ascending order, now set() is really helpful when you have to find things like second highest or second lowest since more than one person can have same highest/lowest score So, second_highest = print(sorted(set(list(score)))[-2]) Second_lowest = print(set(sorted(list(score)))[1]) Here [1] is basically index 1 of score since in python the counting start from 0 so index 1 will give the second position value from an ascending order of scores
While reading a list backwards (i.e., from right to left) index starts at -1 so [-2] will give the value of second highest
list(score), will make the list of scores
set(list(score)), will remove the duplicates of scores
sorted(set(list(score))), it will give the scores without duplicates in ascending order
Posted on March 24, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 1, 2024
October 6, 2024