Sample - Mid - Exam - S1 - 24-25.ipynb - Colab
Sample - Mid - Exam - S1 - 24-25.ipynb - Colab
ipynb - Colab
Bắt đầu lập trình hoặc tạo mã bằng trí tuệ nhân tạo (AI).
Question 1. In the tutorial, you defined several variables to calculate the total number of seconds
in a year. Run the next code cell to do the calculation here.
# Create variables
num_years = 4
days_per_year = 365
hours_per_day = 24
mins_per_hour = 60
secs_per_min = 60
126144000
Define a variable births_per_min and set it to 250. (There are on average 250 babies born
each minute.)
Define a variable births_per_day that contains the average number of babies born each day.
(To set the value of this variable, you should use births_per_min and some of the variables
from the previous code cell.)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-2-5d0ea602c7be> in <cell line: 5>()
3
4 # TODO: Set the value of the births_per_day variable
----> 5 births_per_day = births_per_min*min_per_hour*hour_per_day
https://colab.research.google.com/drive/1mOHuxrxC7zRgWKcGJzbbdgT12rFdYuSV?usp=sharing#scrollTo=s3W4Xtawea2L&printMode=true 1/4
10/30/24, 11:44 AM Sample_Mid_Exam_S1_24-25.ipynb - Colab
Question 2. Write a function called calculate_area that takes the length and width of a
rectangle as input and returns its area. Fill in the blanks
Args:
length: The length of the rectangle.
width: The width of the rectangle.
Returns:
The area of the rectangle.
"""
# TODO: Calculate the area and return it
area = length*width
return area
print(area)
Question 3.
Write a lambda function that takes two numbers as input and returns their sum.
Question 4.
Create a list of numbers from 1 to 10. Then, use a list comprehension to create a new list
containing only the even numbers from the original list.
Question 5.
Write a function that takes an integer as input and returns its factorial.
numbers = [1,2,3,4,5,6,7,8,9,10]
even_numbers = [2,4,8,6,10]
https://colab.research.google.com/drive/1mOHuxrxC7zRgWKcGJzbbdgT12rFdYuSV?usp=sharing#scrollTo=s3W4Xtawea2L&printMode=true 2/4
10/30/24, 11:44 AM Sample_Mid_Exam_S1_24-25.ipynb - Colab
print(numbers)
print(even_numbers)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[2, 4, 8, 6, 10]
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
print(factorial(5))
120
Question 6.
Create a dictionary called student_grades where the keys are student names (strings) and
the values are their corresponding grades (integers).
Add at least three students and their grades to the dictionary.
Print the dictionary.
Fill in the blanks
print(student_grades)
Question 7.
Create a set called colors containing the following colors: "red", "green", "blue".
Add the color "yellow" to the set.
Remove the color "green" from the set.
Print the set.
Fill in the blanks
Question 9.
Create a dictionary and use a loop to print each key-value pair in the dictionary.
a 1
b 2
c 3
https://colab.research.google.com/drive/1mOHuxrxC7zRgWKcGJzbbdgT12rFdYuSV?usp=sharing#scrollTo=s3W4Xtawea2L&printMode=true 4/4