problem solution google python

Now it’s your turn to try! Give this a go yourself.

Use Python to calculate (((1+2)3)/4)5(((1+2)*3)/4)^5

Tip: remember that you can use a**b to calculate a to the power of b. 

 

x = (((1+2)*3)/4)
b = x ** 5
print(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?

 

1 / 1 point
Correct

Right on! Being able to write such programs is a super useful skill that you'll acquire through this course.

 

 

 

 

 

 

 

 

 

 

Correct

You got it! By replacing a manual step with an automatic one we create automation that helps us reduce unnecessary manual work.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Correct

You nailed it! Creating reports based on data are a great example of things that can be automated.

Correct

Spot on! The process of setting up the account for a new employee is usually repetitive and most of it can be automated.

This should not be selected

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.

Correct

Nice job! Automatically populating a website based on data is a great example of a task that can be automated.

 

 






Correct

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.

Correct

You nailed it! We write our code using Python's syntax and semantics, and the interpreter transforms that into instructions that our computer executes.

Correct

Awesome! We can practice writing Python code with many different tools available to us, both online and offline.













Correct

Woohoo! Each language has its pros and cons. The best language to choose will depend on the problem you are trying to solve.

 

 

 

 

 

 

Question 10

Most 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.


 

disk_size = 16*1024*1024*1024
sector_size = 512
sector_amount = disk_size / sector_size

print(sector_amount)





Learning function from google.



def hey(name, department):
print("welcome, " + name)
print("you are part of " + department)


hey("amy","it support")

hey("Sayed Amy","Software developer")

hey("Sah", "coder")





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.


def print_seconds(hours, minutes, seconds):
print(seconds)

print_seconds(1,2,3)




 

 

 

 

 

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:

def get_seconds(hours, minutes, seconds):
return 3600*hours + 60*minutes + seconds

amount_a = get_seconds(2,30,0)
amount_b = get_seconds(0,45,15)
result = amount_a + amount_b
print(result)



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: 

# REPLACE THIS STARTER CODE WITH YOUR FUNCTION
def month_days(month, days):
june_days = 30
print("June has " + str(june_days) + " days.")
july_days = 31
print("July has " + str(july_days) + " days.")
 
month_days(30,1)


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.

 

def f1(x, y):
z = x*y # the area is base*height
print("The area is " + str(z))


Solution:

def rectangle_area(base,height):
area = base*height # the area is base*height
print("The area is " + str(area))
rectangle_area(5,6)


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?


Correct

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.

 

def lucky_number(name):
number = len(name) * 9
print = "Hello " + name + ". Your lucky number is " + str(number)
pass
print(lucky_number("Kay"))
print(lucky_number("Cameron"))


Sol:



 

 

Question: 5

What are the values passed into functions as input called?

 

Variables


 

 

Question: 6

This function compares two numbers and returns them in increasing order.

  1. 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:

# This function compares two numbers and returns them
# in increasing order.
def order_numbers(number1, number2):
if number2 > number1:
return number1, number2
else:
return number2, number1

# 1) Fill in the blanks so the print statement displays the result
# of the function call
smaller,bigger = order_numbers(100, 99)
print(smaller, bigger)

output:

99 100


Correct
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:

def is_positive(number):
if number > 0:
return True
else:
return False
 
 
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?


def number_group(number):
if ___:
return "Positive"
elif ___:
return ___
else:
___

print(number_group(10)) #Should be Positive
print(number_group(0)) #Should be Zero
print(number_group(-5)) #Should be Negative


 



Solution:

def number_group(number):
if number > 0:
return "Positive"
elif number < 0:
return "Negative"
else:
return "Zero"

print(number_group(10)) #Should be Positive
print(number_group(0)) #Should be Zero
print(number_group(-5)) #Should be Negative


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:

 

if condition1:
if-block
elif condition2:
elif-block
else:
else-block
 
 
 
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.

 
 
 
 
Question 1

What's the value of this Python expression: (2**2) == 4?


4

False


 
 Question 2
Complete 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".
 
 
def greeting(name):
if ___ == "Taylor":
return "Welcome back Taylor!"
___:
return "Hello there, " + name

print(greeting("Taylor"))
print(greeting("John"))
 
 
 
Question 3

What’s the output of this code if number equals 10?

if number > 11:
print(0)
elif number != 10:
print(1)
elif number >= 20 or number < 12:
print(2)
else:
print(3)
 
enter output: 





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.


print("A dog" > "A mouse" )
print(9999+8888 > 100*100)


Select the right 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



 


إرسال تعليق (0)
أحدث أقدم