Week 1: Python GRPA Assignments
Welcome to the Week 1 Python Graded Assignments. Below are the tasks for the week, each with its respective Python code and detailed explanation. Feel free to follow along and test the code with different inputs!
GRPA 1
Code:
a = 5
b = 6
price, discount_percent = 80, 5.75
total_mins = 470
output1 = a + b
output2 = 2 * (a + b)
output3 = abs(a - b)
output4 = abs((a + b) - (a * b))
discounted_price = price - (price * (discount_percent / 100))
rounded_discounted_price = round(discounted_price)
hrs = total_mins // 60
mins = total_mins % 60
Explanation:
- Sum of a and b: The code computes the sum of two numbers,
aandb. - Twice the sum: The second output is simply twice the sum of
aandb. - Absolute difference: The third output is the absolute difference between
aandb. - Absolute difference between sum and product: The fourth output is the absolute difference between the sum and the product of
aandb. - Discounted price: The fifth output calculates the discounted price using the price and discount percentage, and rounds the result.
- Hours and minutes from total minutes: The program calculates how many hours and minutes fit into the given total minutes (470 in this case).
GRPA 2
Code:
a = 5
price1, discount1 = 50, 4
price2, discount2 = 60, 8
output1 = a >= 5
output2 = a % 5 == 0
output3 = a % 2 != 0 and a < 10
output4 = a % 2 != 0 and -10 <= a <= 10
output5 = len(str(abs(a))) % 2 == 0 and len(str(abs(a))) <= 10
is_offer1_cheaper = discount2 < discount1
Explanation:
- Comparisons and conditions: The code contains several conditions that check if
ameets certain criteria such as being greater than or equal to 5, divisible by 5, being an odd number, and others. - Comparing offers: The program also compares two discount offers to determine if the second offer is cheaper than the first.
GRPA 3
Code:
s = "hello pyhton"
course_code = "24t2cs1002"
output1 = s[2]
output2 = s[-4]
output3 = s[:3]
output4 = s[::2]
output5 = s[-3:]
output6 = s[::-1]
course_term = int(course_code[3])
course_year = int(course_code[:2])
Explanation:
- String slicing: The code performs several string operations such as getting specific characters (e.g., the third character, the fourth last character), and slicing to get parts of the string.
- Reversing and even indexing: The code retrieves every second character from the string and also reverses the string entirely.
- Extracting course info: The program extracts the term and year from the
course_codestring.
GRPA 4
Code:
word1 = "Wingardium"
word2 = "Leviyosa"
word3 = "Silver"
sentence = "Learning python is fun"
n1 = 6
n2 = 4
output1 = " ".join([word1, word2])
output2 = word1[:4] + "-" + word2[-4:]
output3 = " ".join([word3, str(n1)])
output4 = "-" * 50
output5 = "-" * n2
output6 = str(n1) * n2
are_all_words_equal = word1 == word2 == word3
is_word1_comes_before_other_two = word1 < word2 and word1 < word3
has_h = "h" in word1
ends_with_a = word1.endswith('a') or word1.endswith('A')
has_the_word_python = "python" in sentence.lower()
Explanation:
- String operations: The code uses string joining, slicing, and repeating operations. For instance,
join()is used to combine words with a space, and the hyphen "-" is used to join parts of words. - Boolean conditions: The program checks if all three words are equal, if a word comes before another in lexicographical order, and if certain letters exist in the words.
- Word search: The code also checks if the word "python" exists in the given sentence.
GRPA 5
Code:
age = int(input())
dob = input()
day, month, year = map(int, dob.split('/'))
fifth_birthday = f"{day}/{month}/{year + 5}"
last_birthday = f"{day}/{month}/{year + age}"
new_month = month + 10
new_year = year
if new_month > 12:
new_month -= 12
new_year += 1
tenth_month = f"{day}/{new_month}/{new_year}"
print(tenth_month, fifth_birthday, last_birthday, sep=", ")
weight = float(input())
kg = int(weight)
grams = round((weight - kg) * 1000)
weight_readable = f"{kg} kg {grams} grams"
print(weight_readable)
Explanation:
- Reading user input: The program reads the user's age and date of birth as input, processes the date to extract day, month, and year, and calculates the fifth birthday, last birthday, and a date 10 months later.
- Weight conversion: The program reads the user's weight, splits it into kilograms and grams, and prints the weight in a readable format such as "55 kg 250 grams".
Additional Notes:
- This post covers all the five assignments of Week 1. Make sure to practice and test the code with different inputs to solidify your understanding of Python concepts.
- Continue with the subsequent weeks and assignments to enhance your programming skills!
Feel free to copy and modify the code as needed. Happy coding!