Finding the Second Largest Value in a List

Halooo! Today I’ll be teaching you how to find the second largest value in a list. The conditions - Write a Python program that prints the second largest value in a list. If the list is empty or only has one element, print None.SSS To do this, we’ll have to make a list and then sort it and then we use the if statement and else. Input: lista = [1, 4, 8, 9, 100, 200, 30] lista.sort() if len(lista) > 1: print(lista[-2]) else: print("None") Output - ...

February 10, 2024 · 1 min · SourFox

Finding elements not in list

Haloo people! How long has it been since I’ve done a blog post? Welp, I’ve finished my long break and now it’s time to get back to work. Today, I’ll be writing a python program that prints the elements of one list (a) that are not in the other list (b) as a list. My conditions are: If the lists have the same elements, it will print an empty list. If the first list (a) is an empty list, it’ll print an empty list. An example - ...

February 4, 2024 · 2 min · SourFox

Commas replaced by dots

It’s been a long time since i’ve done a blog post. I wonder why…. Welp, today I’ve got an assignment, today’s one is to write a python program that prints a version of the strings with all commas replaced by dots. Like this. Example - Input Output "Hello, World!" "Hello. World!" "725,000" "725.000" First, I’m going to do the 1st one. I’m going to assign it to a variable called string. ...

December 26, 2023 · 1 min · SourFox

Bonus assignment: Challenge Add a Key-Value Pair Only if the Key is Not in the Dictionary

Today I was very bored so I checked my bonus assignments and I got this. Write a Python program that adds a new key-value pair to a dictionary only if the key doesn’t exist already. If the key-value pair exists in the dictionary, do not update the existing value. The dictionary should not be modified in this case. Store the new key in the new_key variable and the new value in the new_value variable. Print the final value of the dictionary. ...

November 18, 2023 · 1 min · SourFox

Check if a Key Is in a Dictionary

The next one I’ll actually explain thoroughly. So I had enough time to make another program. This time I had to meet the condtitions: Write a Python program that checks if a key exists in a dictionary or not. If the key exists in the dictionary, print True. Else, print False. The key should be stored in the variable key. Here is how I did it: Input: dict = {"toy": "dino", "sport": "soccer", "food": "ramen"} key = "toy" if key in dict: print(True) else: print(False) Output: ...

November 15, 2023 · 1 min · SourFox