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

Multiplication table

Today I was very bored, and I decided to do programming! I’m going to make a multiplication table! What I’m going to to: Do input and int assigned to a variable Use while loop Use for loop Make it pretty Step 1: First I’m going to make a variable called “num” and them use input to ask the person a question to grab info from. And I’m salso going to use “int” to make the string a integer. ...

November 15, 2023 · 2 min · SourFox

Assignment 5: Elements and Indices

Today I had to do another assignment which is to create a python program which prints the elements of a list followed by their corresponding indices. Each element and its index must be on the same line seperated by a space. And if the list is empty, print “Empty List”. I’m going to make a list called num and put 1 to 4 inside. num = [1, 2, 3, 4] Then I’ll use the if statement to do: ...

November 11, 2023 · 2 min · SourFox

Assignment 4: Checking if a list is empty

I was given an assignment for the fourth time. My goal was to write a python program that checks if a list is empty or not. If the list is empty, then it’ll print empty, if not, it’ll print not empty. I’m going to start with an empty list. emp = [] So that I can start the next step. I’m going to use if statement len() else print() When using if, there must be a condition met. My condition is: ...

October 29, 2023 · 1 min · SourFox