Now it’s your turn to try! Give this a go yourself.
Use Python to calculate
Tip: remember that you can use a**b to calculate a to the power of b.
Arithmetic operators
Python can operate with numbers using the usual mathematical operators, and some special operators, too. These are all of them (we'll explore the last two in later videos).
a + b = Adds a and b
a - b = Subtracts b from a
a * b = Multiplies a and b
a / b = Divides a by b
a ** b = Elevates a to the power of b. For non integer values of b, this becomes a root (i.e. a**(1/2) is the square root of a)
a // b = The integer part of the integer division of a by b
a % b = The remainder part of the integer division of a by b
Ques: 1
What is a computer program?
Right on! Being able to write such programs is a super useful skill that you'll acquire through this course.
You got it! By replacing a manual step with an automatic one we create automation that helps us reduce unnecessary manual work.
You nailed it! Creating reports based on data are a great example of things that can be automated.
Spot on! The process of setting up the account for a new employee is usually repetitive and most of it can be automated.
Not quite. While there might be some automation available for taking the picture and the right time and post-processing them, taking pictures is still a creative process. Consider reviewing our videos on automation.
Nice job! Automatically populating a website based on data is a great example of a task that can be automated.
Right on! Because the syntax used by Python is similar to the one used by the English language, Python programs are easy to write and understand.
You nailed it! We write our code using Python's syntax and semantics, and the interpreter transforms that into instructions that our computer executes.
Awesome! We can practice writing Python code with many different tools available to us, both online and offline.
Woohoo! Each language has its pros and cons. The best language to choose will depend on the problem you are trying to solve.
10.
Question 10Most hard drives are divided into sectors of 512 bytes each. Our disk has a size of 16 GB. Fill in the blank to calculate how many sectors the disk has.
Note: Your result should be in the format of just a number, not a sentence.
Learning function from google.
Solve the question:
Do you think you can flesh out your own function? I think you can! Let’s give it a go.
Flesh out the body of the print_seconds function so that it prints the total amount of seconds given the hours, minutes, and seconds function parameters. Remember that there are 3600 seconds in an hour and 60 seconds in a minute.
This is not valid answer. This is a demo.
Solve the problem:
Ques: 1
Use the get_seconds function to work out the amount of seconds in 2 hours and 30 minutes, then add this number to the amount of seconds in 45 minutes and 15 seconds. Then print the result.
Solution:
Ques:2
Ready to try it yourself? See if you can reduce the code duplication in this script.
In this code, identify the repeated pattern and replace it with a function called month_days, that receives the name of the month and the number of days in that month as parameters. Adapt the rest of the code so that the result is the same. Confirm your results by making a function call with the correct parameters for both months listed.
Sol:
Here is your output:
June has 30 days.
July has 31 days.
Nice work! You're getting acquainted with some interesting
coding practices to reduce code duplication.
Question: 3
This function to calculate the area of a rectangle is not very readable. Can you refactor it, and then call the function to calculate the area with base of 5 and height of 6? Tip: a function that calculates the area of a rectangle should probably be called rectangle_area, and if it's receiving base and height, that's what the parameters should be called.
Solution:
Here is your output:
The area is 30
Wonderful! You're learning how to write self-documenting
code, that will be easier to read & reuse.Question: 4
What is the purpose of the def keyword?
Awesome! When defining a new function, we must use the def keyword followed by the function name and properly indented body.
Question: 5
Let's revisit our lucky_number function. We want to change it, so that instead of printing the message, it returns the message. This way, the calling line can print the message, or do something else with it if needed. Fill in the blanks to complete the code to make it work.
Sol:
Question: 5
What are the values passed into functions as input called?
Variables
Variables
Question: 6
This function compares two numbers and returns them in increasing order.
Fill in the blanks, so the print statement displays the result of the function call in order.
Hint: if a function returns multiple values, don't forget to store these values in multiple variables
Sol:
output:
99 100
Nice! You remembered how to accept multiple return values from a function. You’re ready for the next lesson!
Question: 7
The is_positive function should return True if the number received is positive and False if it isn't. Can you fill in the gaps to make that happen?
solution:
is_positive(-5) returned False
is_positive(0) returned False
is_positive(13) returned True
Well done, you! You're starting to
master conditional expressions!
Question: 8
The number_group function should return "Positive" if the number received is positive, "Negative" if it's negative, and "Zero" if it's 0. Can you fill in the gaps to make that happen?
Solution:
Here is your output:
Positive
Zero
Negative
Well done! You're progressing so fast! Conditionals Cheat Sheet
Comparison operators
a == b: a is equal to b
a != b: a is different than b
a < b: a is smaller than b
a <= b: a is smaller or equal to b
a > b: a is bigger than b
a >= b: a is bigger or equal to b
Logical operators
a and b: True if both a and b are True. False otherwise.
a or b: True if either a or b or both are True. False if both are False.
not a: True if a is False, False if a is True.
Branching blocks
In Python, we branch our code using if, else and elif. This is the branching syntax:
Remember: The if-block will be executed if condition1 is True. The elif-block will be executed if condition1 is False and condition2 is True. The else block will be executed when all the specified conditions are false.
What's the value of this Python expression: (2**2) == 4?
4
False
Question 2Complete the script by filling in the missing parts. The function
receives a name, then returns a greeting based on whether or not that
name is "Taylor". Question 3What’s the output of this code if number equals 10?
Question 4
Is "A dog" smaller or larger than "A mouse"? Is 9999+8888 smaller or larger than 100*100? Replace the plus sign in the following code to let Python check it for you and then answer.
"A dog" is larger than "A mouse" and 9999+8888 is larger than 100*100
"A dog" is smaller than "A mouse" and 9999+8888 is smaller than 100*100