0% found this document useful (0 votes)
15 views

BT NhiemVu 1

Baitap Python coban

Uploaded by

Đạt Võ
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

BT NhiemVu 1

Baitap Python coban

Uploaded by

Đạt Võ
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

TRƯỜNG ĐẠI HỌC GIAO THÔNG VẬN TẢI TP.

HCM
KHOA CÔNG NGHỆ THÔNG TIN

BÀI TẬP

NHIỆM VỤ 1
PYTHON CƠ BẢN
Numbers
Exercises 1_1
1. Declare 5 as num_one and 4 as num_two
a) Add num_one and num_two and assign the value to a variable total
b) Subtract num_two from num_one and assign the value to a variable diff
c) Multiply num_two and num_one and assign the value to a variable
product
d) Divide num_one by num_two and assign the value to a variable division
e) Use modulus division to find num_two divided by num_one and assign
the value to a variable remainder
f) Calculate num_one to the power of num_two and assign the value to a
variable exp
g) Find floor division of num_one by num_two and assign the value to a
variable floor_division
2. The radius of a circle is 30 meters.
a) Calculate the area of a circle and assign the value to a variable name
of area_of_circle
b) Calculate the circumference of a circle and assign the value to a
variable name of circum_of_circle
c) Take radius as user input and calculate the area.
2
Operators
Exercises 1_2
1. Write a program that prompts the user to enter base and height of
the triangle and calculate an area of this triangle (area = 0.5 x b x h).
2. Write a script that prompts the user to enter side a, side b, and side c
of the triangle. Calculate the perimeter of the triangle (perimeter = a
+ b + c).
3. Get length and width of a rectangle using prompt. Calculate its area
(area = length x width) and perimeter (perimeter = 2 x (length +
width))
4. Get radius of a circle using prompt. Calculate the area (area = pi x r x
r) and circumference (c = 2 x pi x r) where pi = 3.14.
5. Calculate the slope, x-intercept and y-intercept of y = 2x -2
6. Slope is (m = y2-y1/x2-x1). Find the slope and Euclidean distance
between point (2, 2) and point (6,10)
7. Compare the slopes in tasks 8 and 9.
8. Calculate the value of y (y = x^2 + 6x + 9). Try to use different x
values and figure out at what x value y is going to be 0.
9. Find the length of 'python' and 'dragon' and make a falsy comparison
statement. 3
Operators
Exercises 1_2_cont
10.Use and operator to check if 'on' is found in both
'python' and 'dragon'
11.I hope this course is not full of jargon. Use in operator to
check if jargon is in the sentence.
12.There is no 'on' in both dragon and python
13.Find the length of the text python and convert the value
to float and convert it to string
14.Even numbers are divisible by 2 and the remainder is
zero. How do you check if a number is even or not using
python?
15.Writs a script that prompts the user to enter hours and
rate per hour. Calculate pay of the person?
16.Write a script that prompts the user to enter number of
years. Calculate the number of seconds a person can
live. Assume a person can live hundred years 4
Python if...else Statement
Exercises 1_3

1. Write a program to check a person is eligible for


voting or not (saccept age from user)
2. Write a program to check whether a number
entered by user is even or odd.
3. Write a program to check whether a number is
divisible by 7 or not.
4. Write a program to check the last digit od a
number (entered by user) is divisible by 3 or not.
5. Write a Python program to guess a number
between 1 and 9.
6. Write a program to accept a number from 1 to 7
and display the name of the day like 1 for Sunday,
2 for Monday and so on.
5
# Prints today's date with help # of datetime library import datetime today = datetime.datetime.today()
(f"{today:%B %d, %Y}")

For loop Statement


Exercises 1_4
1. write a program that prints numbers from 1 to 10
2. Write a program to calculate the sum of numbers in a range from
1 to n (n is entered from the keyboard)
3. Write a program to calculate the sum of even (/odd) numbers in a
range from 1 to n (n is entered from the keyboard)
4. Write a program to check how many vowels are in a string
entered from the keyboard.
5. Write a program to count the number of words in a sentence the
user enters.
6. Write a program that implements a game as the following
description:
7. 1. The computer generates a random number from 1 to 100
8. 2. The user was asked to guess
9. 3. match the user-guessing number to the generated
number
10. The user can only guess five times.
6
Python while Loop
Exercises 1_5
1. Guess The Number Game:
• we will generate a random number with the help of randint()
function from 1 to 100 and ask the user to guess it.
• After every guess, the user will be told if the number is above or
below the randomly generated number.
• The user will win if they guess the number maximum five
attempts.
• Ask the user to stop or continue playing again.
2. Write a game that simulate rolling a pair of dice.
• If the sum of two faces is greater than 5  “Tài”
• Otherwise  “Xỉu”
• User ask for guessing “Tài” or “Xỉu”
• Match the results
• After one turn, ask user for continue playing game.
• **** Extend the game by asking the user to enter an amount of
money, then continue playing until the user runs out of money or
the user stops playing. Statistics of results.
7
Python Set
Exercises 1_6

1. Write a Python program to find the maximum and


minimum values in a set.
2. Write a Python program to check if a given value is
present in a set or not.
3. Write a Python program to check if two given sets
have no elements in common.
4. Write a Python program to find all the unique
words and count the frequency of occurrence from
a given list of strings. Use Python set data type.
5. Given two sets of numbers, write a Python
program to find the missing numbers in the second
set as compared to the first and vice versa. Use
the Python set.
8
Python Dictionary
Exercises 1_7
1. Write python program:
a) Convert two lists into a dictionary
b) Merge two Python dictionaries into one
c) Print the value of key ‘history’ from the below dict
d) Initialize dictionary with default values
e) Create a dictionary by extracting the keys from a given dictionary
f) Delete a list of keys from a dictionary
g) Check if a value exists in a dictionary
h) Rename key of a dictionary
i) Get the key of a minimum value from the following dictionary
j) Change value of a key in a nested dictionary
2. Write a Python program that counts the number of times
characters appear in a text paragraph.
3. Write a program using a dictionary containing keys starting
from 1 and values ​containing prime numbers less than a
value N.
9
Python Dictionary
Exercises 1_7_cont
Restructuring the company data: Suppose you have a dictionary that contains
information about employees at a company. Each employee is identified by an ID
number, and their information includes their name, department, and salary. You want to
create a nested dictionary that groups employees by department so that you can easily
see the names and salaries of all employees in each department.
Write a Python program that when given a dictionary, employees, outputs a nested
dictionary, dept_employees, which groups employees# Resulting dictionary:
by department.
dept_employees = {
"Engineering": {
# Input: 1001: {"name": "Alice", "salary":
employees = { 75000},
1001: {"name": "Alice", "department": 1003: {"name": "Charlie",
"Engineering", "salary": 75000}, "salary": 80000}
1002: {"name": "Bob", "department": },
"Sales", "salary": 50000}, "Sales": {
1003: {"name": "Charlie", "department": 1002: {"name": "Bob", "salary":
"Engineering", "salary": 80000}, 50000},
1004: {"name": "Dave", "department": 1005: {"name": "Eve", "salary":
"Marketing", "salary": 60000}, 55000}
1005: {"name": "Eve", "department": },
"Sales", "salary": 55000} "Marketing": {
} 1004: {"name": "Dave", "salary":
60000}
}
} 10

You might also like