[go: up one dir, main page]

0% found this document useful (0 votes)
28 views4 pages

Sample - Mid - Exam - S1 - 24-25.ipynb - Colab

Uploaded by

vthieunhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views4 pages

Sample - Mid - Exam - S1 - 24-25.ipynb - Colab

Uploaded by

vthieunhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

10/30/24, 11:44 AM Sample_Mid_Exam_S1_24-25.

ipynb - Colab

Bắt đầu lập trình hoặc tạo mã bằng trí tuệ nhân tạo (AI).

keyboard_arrow_down Introduction to Python


Sample of Midterm Exam

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

# Calculate number of seconds in four years


total_secs = num_years*days_per_year*hours_per_day*mins_per_hour*secs_per_min
print(total_secs)

126144000

Use the next code cell to:

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

# TODO: Set the value of the births_per_min variable


births_per_min = 250

# TODO: Set the value of the births_per_day variable


births_per_day = births_per_min*min_per_hour*hour_per_day

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

NameError: name 'min_per_hour' is not defined

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

Các bước tiếp theo: Giải thích lỗi

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

def caculate_area(length, width):


length = 60
width = 50
"""Calculates the area of a rectangle.

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.

Fill in the blanks

# your code here


sum_lambda = lambda x,y: x+y
# print sum_lambda of 3 and 4
print(sum_lambda(3,4))

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.

Fill in the blanks**

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

student_grades = {"Alice","Bob", "Charlie"}

print(student_grades)

{'Bob', 'Alice', 'Charlie'}

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

set_colors = {"red","green", "blue"}


print(set_colors)

{'blue', 'green', 'red'}

Question 9.

Create a dictionary and use a loop to print each key-value pair in the dictionary.

my_dict = {"a": 1, "b": 2, "c": 3}


https://colab.research.google.com/drive/1mOHuxrxC7zRgWKcGJzbbdgT12rFdYuSV?usp=sharing#scrollTo=s3W4Xtawea2L&printMode=true 3/4
10/30/24, 11:44 AM Sample_Mid_Exam_S1_24-25.ipynb - Colab
for key, value in my_dict.items():
print(key,value)

a 1
b 2
c 3

https://colab.research.google.com/drive/1mOHuxrxC7zRgWKcGJzbbdgT12rFdYuSV?usp=sharing#scrollTo=s3W4Xtawea2L&printMode=true 4/4

You might also like