Finding the GCD of two numbers

In this assignment, it’ll be finding the GCD(Greatest Common Divisor). This means I need to write a program to find the GCD of two numbers. Below, I have written the program and next to the hashtags are just a sentence that’ll explain to you what each part of the code is doing. #The two variables 'num' and 'ber' will be where the inputs will be stored in. When it starts it will show '(num)a: ' and you'll have to put a number in. This goes the same for '(ber)b: ' num = int(input("a: ")) ber = int(input("b: ")) #Then once the variable has gotten their information, they will go through this: def gcd(num, ber): if ber == 0: return num else: return gcd(ber, num % ber) #And last, it'll gather their answer and print it for you as a sentence. print(f"The GCD {num}, {ber} is {gcd(num, ber)}.") That’s all today. Bye!

July 12, 2024 · 1 min · SourFox

Check if a year is a leap year

Today, I just got back to python after a few months. So when coming back, I got an assignment. My assignment was to check if a year is a leap year. In the assignment, it also gave hints. These were the hints: A leap year is a calendar year that contains an additional day (February 29th) compared to a common year. It occurs once every four year. s This is how you can determine if a year is a leap year or not: ...

May 14, 2024 · 3 min · SourFox

Make a Frequency Dictionary From the Elements Of a List

Halo! Today I have to make a python program that meets these conditions: Write a Python program that creates and print a dictionary that maps each element in a list to its corresponding frequency (how many times it occurs in the list). The test should be case-sensitive. Therefore, “A” should not be considered the same element as “a”. I think for today, I’m just going to make it short. Yea… ...

February 25, 2024 · 1 min · SourFox

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

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