Masterschool Live Coding Challenges 14th August, 2023 Explained.
If you are finding it difficult to get through learning python programming, this would very well bring you up to speed, as I have drafted the "5 year old approach to learning python"
Question 1:
Use the
print
function in Python to print the following sentence: "Hello, World!"Next, use the
print
function to print another sentence: "Python is fun!"Finally, print one last sentence: "Let's code!"
Solution:
Use the
print
function in Python to print the following sentence: "Hello, World!"
To get started, I would like to explain what the print function is in the simplest form. Forget about the word print for a minute, and think of something more active like an instruction given to someone. For instance, “John, sit down”, we all naturally expect John to do one thing right? to sit. The "print" function in Python serves the purpose of displaying output. It visualizes whatever is embedded inside of it to the user. Now go back and read the question, this time, read it in this manner.
Use the "print" function in Python to display the following sentence to the user:
"Hello, World"
To solve this, all you need to do is write in the code area
print("Hello, World!")
Output: “Here’s what we would see in the console when we hit run”
Hello, World!
Next, use the
print
function to print another sentence: "Python is fun!"
This is similar to question 1, the only difference is the content of the print statement. We need to use the print function to display "Python is fun" in a more straightforward way. I’ll go ahead and fix this for you below.
print("Python is fun!")
Output: “Here’s what we would see in the console when we hit run”
Python is fun!
Question 2:
Print
the message: "You've found Python's hidden treasure!".Add a comment above this print statement saying, "This is where the treasure is revealed".
At the end of the print statement line, add a comment saying "Treasure revealed here!".
Solution:
Before we dive into the solution of question 2, let’s discuss what a comment is. Have you ever had an opinion about someone, and you prefer to tell it to someone else other than the concerned person in question? Yeah! that’s gossip! No better way to put it. But the crucial point I want to emphasize is that
“you would rather disclose this information to a third party rather than directly to the individual involved.”
When we write programs, we are in fact doing it solely for the end user. However, we sometimes have to work in teams or improve our code base to better serve the user in the future, hence the reason you see upgrades in software. More so, the best way to keep track of what you have done in your code is to write a ‘comment’. This section of the code is the "Gossip" that is exclusively shared among yourself or fellow programmers involved in maintaining or enhancing the codebase.
# In Python, a comment is written with an hash(#) character in front of it
Sometimes, you might have a portion of the code you don’t want to execute at a specific time. In this case, you can always use a comment as well to stop Python from reading and executing that code block or line. I have successfully highlighted the two main uses of a comment. If at this phase you cannot remember it, we can safely say you have commented it out in your mind, quickly go back a couple of lines and read them again, but this time execute them.
Write these lines in your code to answer question 2
# This is where the treasure is revealed
print("You've found Python's hidden treasure!")
# Treasure revealed here!
Output: Here is what we see in the console.
You've found Python's hidden treasure!
Notice how the first and third lines were not executed? That is the power of comment.
Question 3:
Create a function named
variable_type
which receives a single variable as input and uses thetype
function to return the following string:Variable type is: X
where X is the given variable type (<class 'str'>
,<class 'int'>
, …)Create a function named
sum_2
which receives two numbers as input and returns their sum.
Explanation:
First, we need to understand the question to properly answer it with zero or minimal errors. We were asked to “create a function” and name the function “variable_type”
Creating a function is easy, we need to define what our function will do. Do we even know what a function is? Absolutely! If I may simplify it, a function can be described as a set of instructions within a code block that can be reused whenever necessary by simply calling the function by its name.
Let’s think of the phrase “take_a_bath” as a function. I bet you immediately can tell what the function does when called right? Let’s get practical.
def take_a_bath():
pour the soap in the bath
scrub your body mildly
rinse your body
dry up with your towel
For the observant ones, you can notice that everything within the function was written with a different indentation. The reason is simple, everything in the code block defines what the function does. They use the word local and global to explain these terms, but that may be a little confusing at this phase for you.
I have successfully defined what happens when a function called take_a_bath is called. Now how do we call this function? We simply go out of the code base and call the function. I’ll show you in a jiffy.
def take_a_bath():
pour the soap in the bath
scrub your body mildly
rinse your body
dry up with your towel
take_a_bath()
If you notice, I didn’t indent the last line because it is outside the code block of the function. That is exactly how to call a function.
N.B The sample function above does not work, as it is just a mere illustration of a function.
Solution:
def variable_type(var):
return f"Variable type is: {type(var)}"
print(variable_type(12))
What this code does is, it takes in the variable called “var” and returns the type of data the var is. I used formatted strings to solve this question as it is the most readable manner of writing codes.
To find a data type in Python, we use the word “type” and add a parenthesis with the data inside it. Which means to find the type of data 2 is, we can simply do this
print(type(2)
This will execute and resolve to this:
<class 'int'>
I need you to do something simple, find the data type of the following:
print(type("John Doe"))
print(type(4.5))
print(type(30))
If you have read this far, then you must have been enjoying the content. Kindly subscribe to get notified first when I post a new content.
-Create a function named sum_2
which receives two numbers as input and returns their sum.
def sum_2(num1, num2):
return num1 + num2
This function arguments num1 and num2, it then returns the addition of both numbers.
Question 4:
Create a function named
multiple_of_4
that receives a single number as input and returns0
if the number is a multiple of 4, otherwise it should return the number reminder (do not useif
statement)Create a function named
multiple_of_6
that receives a single number as input and returns0
if the number is a multiple of 6, otherwise it should return the number reminder (do not useif
statement)Create a function named
multiple_of_6_and_4
that receives a single number and usesmultiple_of_4
andmultiple_of_6
and returns0
if the number is a multiple of both of them and return the sum of their reminders if it isn’t (remember, you can’t useif
statement)
def multiple_of_4(num):
return num % 4 or 0
def multiple_of_6(num):
return num % 6 or 0
def multiple_of_6_and_4(num):
return (not multiple_of_4(num) and not multiple_of_6(num)) * 0 or (multiple_of_4(num) + multiple_of_6(num))
This code defines three functions for checking if a given number is a multiple of 4, a multiple of 6, or both.
The function "multiple_of_4" receives a number as input and outputs 0 if the number is a multiple of 4. Otherwise, it returns the remainder of the number when it is divided by 4.
The function named "multiple_of_6" accepts a single number as its input. If the number is divisible by 6, the function will output 0. Alternatively, if the number is not divisible by 6, the function will output the remainder obtained when the number is divided by 6.
The function multiple_of_6_and_4 receives a single number as an input and employs the functions multiple_of_4 and multiple_of_6 to ascertain whether the number is a multiple of both 4 and 6. If it is, it returns 0. If it isn't, it returns the sum of the remainders when the number is divided by 4 and 6.
This code employs boolean logic and the * operator to precisely determine whether the input number is a multiple of both 4 and 6. Depending on the result, it will either return the sum of remainders or 0.
Question 5:
Create a function named
ones_digit
that receives a number and return its rightmost digitCreate a function named
tens_digit
that receives a number between 100-999 (only) and return its ten digit (the middle digit)Create a function named
hundreds_digit
that receives a number between 100-999 (only) and return its hundreds digitCreate a function named
digit_separator
that receives a number between 100-999 (only) and return the following string:number = (hundreds * 100) + (tens * 10) + (ones * 1)
wherehundreds
,tens
&ones
are replaces with the correct amount. (Hint: use the functions you already wrote to insidedigit_separator
)
def ones_digit(num):
return num % 10
def tens_digit(num):
return (num // 10) % 10
def hundreds_digit(num):
return num // 100
def digit_seperator(num):
ones = ones_digit(num)
tens = tens_digit(num)
hundreds = hundreds_digit(num)
result = f"number = ({hundreds} * 100) + ({tens} * 10) + ({ones} * 1)"
return result
print(digit_seperator(678))
This code defines four functions for separating a three-digit number into its individual digits.
The function ones_digit is designed to take a single number as an input and efficiently determine the remainder of that number when divided by 10. This smartly allows us to identify and extract the ones digit of the original number.
The tens_digit function takes a single number as input and returns the tens digit of the number by first dividing it by 10 to remove the ones digit, then taking the remainder of that result when divided by 10.
The function "hundreds_digit" receives a number as input and returns its hundreds digit. This is achieved by dividing the number by 100 and rounding down to the nearest integer.
The digit_seperator function takes a single number as input and uses the previous three functions to separate it into its individual digits. It then returns a string that shows how the original number can be expressed as a sum of its digits, using their respective place values.
For example, if we call digit_seperator(678), it will return the string "number = (6 * 100) + (7 * 10) + (8 * 1)", which shows that the number 678 can be expressed as 6 hundreds + 7 tens + 8 ones.
Question 6:
Create a function named
greet
that takes a single parameter:name
.Inside the function, return a greeting message using an f-string that says
Hello, {name}! Welcome to the program.
Call the
greet
function with three different names from people in your Breakout Room to display personalized greeting messages
def greet(name):
return f"Hello, {name}! Welcome to the program"
print(greet("Chinedum"))
print(greet("Raymond"))
print(greet("Oscar"))
The function greet takes the arguments “Chinedum”, “Raymond, “Oscar” and returns the string below:
Hello, Chinedum! Welcome to the program
Hello, Raymond! Welcome to the program
Hello, Oscar! Welcome to the program
If you observe closely, the names have been placed into the sentence.
Question 7:
Define a function named
birthday_greeting
that takes two parameters:name
andage
.Inside the function, use an f-string to return a personalized birthday message that includes the person's name and age. The message should follow this format:
Happy {age}th birthday, {name}! Have a fantastic day!
Call the
birthday_greeting
function with the same names as questions #6. Ask them how old they are, so you can display their personalized birthday message.
def birthday_greeting(name, age):
return f"Happy {age}th birthday, {name}! Have a fantastic day"
print(birthday_greeting("Ray", 26))
This code will print
Happy 26th birthday, Ray! Have a fantastic day
Kindly remember to share the content
Question 8:
Without using
if
statement or any built-in Python functions besidesint
function, write a function namedround_up
which receives a number (float) and round it up only if needed.
def round_up(num):
decimal_part = num - int(num)
round_up = decimal_part > 0
return int(num) + round_up
print(round_up(3.1415))
print(round_up(20.0))
This sets a boolean variable “round_up” to True if the decimal part is greater than zero, or False otherwise. It then adds the value of “round_up” (which is either 0 or 1) to the integer part of the input number using the + operator, which has the effect of rounding up if “round_up” is True.
I hope this piece has been very educative. Feel free to reach out to me on my social media platforms for corrections where necessary.
The trick to become a great programmer is to keep coding. Don’t forget that.