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

BT Python

The document contains examples of functions in Python with explanations and test code to find areas of shapes, check if a number is even or odd, calculate averages, solve quadratic equations, calculate discounted prices, exponents, count vowels, calculate products, medians, find odd numbers in a list, calculate percentages, and filter, map and reduce examples.

Uploaded by

Trọng Tuyển
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

BT Python

The document contains examples of functions in Python with explanations and test code to find areas of shapes, check if a number is even or odd, calculate averages, solve quadratic equations, calculate discounted prices, exponents, count vowels, calculate products, medians, find odd numbers in a list, calculate percentages, and filter, map and reduce examples.

Uploaded by

Trọng Tuyển
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Bai Tap FUNCTION

Bai 1:
#dien tich hinh chu nhat
def dientich(dai, rong):
s = dai * rong
return s
dai = float(input('Chieu dai:'))
rong = float(input('Chieu rong:'))
s = dientich(dai, rong)
print(f"Dien tich hinh chu nhat la: {s}")
Bai 2 :
#kiem tra co phai la so chan hay khong
def sochan(n):
return n % 2 == 0
n = int(input('Nhap so: '))
if sochan(n):
print (f"{n} la so chan")
else:
print(f"{n} khong phai la so chan")

Bai 3 :
#trung binh cong cac so trong day
def trungbinh(dayso):
trungbinh = sum(dayso)/len(dayso)
return trungbinh
dayso = [1, 2, 3, 4, 5, 6, 7, 8, 9]
trungbinh = trungbinh(dayso)
print(f"Trung binh cua day so la: {trungbinh}")
Bai 4 :
#Giai phuong trinh bac 2
import math
def timnghiem(a, b, c):
delta = b**2 - 4*a*c
if delta > 0:
x1 = (-b + math.sqrt(delta)) / (2*a)
x2 = (-b - math.sqrt(delta)) / (2*a)
return x1, x2
elif delta == 0:
print("Phuong trinh co nghiem kep ")
x1 = -b / (2*a)
return x1
else:
print("Phuong trinh vo nghiem")
a = float(input("Nhap a: "))
b = float(input("Nhap b: "))
c = float(input("Nhap c: "))
nghiem = timnghiem(a, b, c)
print(f"Nghiem cua phuong trinh la: {nghiem}")

Bai 5 :
# tinh gia tri sau giam gia
def calculate_discounted_price(giagoc, phantramgiamgia):
giagiam = giagoc - ((phantramgiamgia / 100) * giagoc)
return giagiam
giagoc = float(input('Nhap gia goc: '))
phantramgiamgia = float(input('Nhap phan tram giam gia: '))
giagiam = calculate_discounted_price(giagoc, phantramgiamgia)
if giagiam < 0 or giagiam > giagoc:
print("Xem lai phan tram giam gia")
else:
print(f"Gia da giam la: {giagiam}$")
Bai 6:
#Luy thua so mu
def calculate_power(coso, somu):
luythua = coso**somu
return luythua
coso = float(input("Nhap co so: "))
somu = float(input("Nhap so mu: "))
luythua = calculate_power(coso, somu)
print(f"Luy thua cua {coso} mu {somu} la: {luythua}")

Bai 7:
# tim ra so ki tu nguyen am (nguyen am la ue oai)
def count_vowels(input_string):
vowel_count = 0
for char in input_string:
if char in 'aeiou':
vowel_count += 1
return vowel_count
input_string = input('Nhap cau: ')
vowel_count = count_vowels(input_string)
print(f"So nguyen am trong {input_string} la: {vowel_count}")
Bai 8 :
#tinh tich cac so trong day
def calculate_product(*args):
tich = 1.0
for n in args:
tich *= n
return tich
tich = calculate_product(2, 3, 4)
print(f"Tich cua day so la: {tich}")
Bai 9 :
#trung binh cac so trong day
def calculate_median(*args):
tong = 0
for n in args:
tong += n
trungbinh = tong / len(list(args))
return trungbinh
trungbinh = calculate_median(2, 3, 4, 5)
print(f"Trung binh cua day so la: {trungbinh}")
Bai 10 :
#tinh so le trong mang
def find_odd_number(*args):
sole = [n for n in args if n % 2 != 0]
return sole
sole = find_odd_number(1, 2, 3, 4, 5, 6, 7, 8, 9)
print(f"So le trong day so la: {sole}")
Bai 11:
#tinh % gia tri do so voi tong cac gia tri
def calculate_percentages(*args):
total = 0
for n in args:
total += n
percentages = [(value / total) * 100 for value in args]
return percentages
value = (15, 20, 25 ,30)
percentages = calculate_percentages(*value)
print("Percentages relative to the total value:")
for i, value in enumerate(value):
print(f"Value {i+1}: {percentages[i]}%")
BAI TAP FILTER

Bai 1 :
#Tim cac ki tu chia het cho 2 trong day cho truoc
dayso = [1, 2, 3, 4, 5, 6, 7, 8, 9]
loc = list(filter(lambda x: x % 2 == 0, dayso))
print(loc)
Bai2 :

#Tim nhung so khong chia het cho 3


dayso = [1, 2, 3, 4, 5, 6, 7, 8, 9]
loc = list(filter(lambda x: x % 3 != 0, dayso))
print(loc)

Bai 3 :
#loc nhung tu bat dau bang ki tu A
names = ["alice", "awwrwa", "acss", "alex", "eve", "andrew"]
filtered_names = list(filter(lambda name: name.title()[0] != 'A', names))
print(filtered_names)
Bai 4 :
#tim ra nhung phan tu co do dai hon 5 ki tu
names = ["alice", "christian", "david", "jonathan", "eve", "tom"]
filtered_names = list(filter(lambda name: len(name) >= 5, names))
print(filtered_names)

BT MAP
#VIet hoa cac chu cai dau cua cac tu trong chuoi
str_list = ["hello", "my", "name", "is"]
viethoa = list(map(lambda x: x.title(), str_list))
print(viethoa)

Bai 2 :
#chuyen tu do C sang do F
doC = [0, 5, 10, 15, 20]
doF = list(map(lambda x: x * 1.8 + 32, doC))
print(doF)

Bai 3 :
#In ra cac gia tri trong chuoi
str_list = ["1", "2", "3", "4"]
dayso = list(map(int, str_list))
print(dayso)

Bai 4 :
#tim co phai so nguyen to trong day khong
n = [2, 3, 4, 5, 6, 7, 8, 9, 10]
prime = lambda num: num > 1 and all(num % i != 0 for i in range(2,
int(num**0.5) + 1))
x = list(map(prime, n))
print(x)

BT STRING

Bai 1 :
#Lay 1 ki tu tai vi tri bat ki
s = input("Nhap 1 cau: ")
i = int(input("Nhap 1 vi tri: "))
if 0 <= i < len(s):
print(s[i])
else:
print("Xem lai i")
Bai 2 :
#LAY 1 chuoi ki tu
s = input("Nhap 1 cau: ")
x = int(input("Nhap 1 vi tri bat dau: "))
y = int(input("Nhap 1 vi tri ket thuc: "))
if 0 <= x < len(s) and 0 <= y < len(s):
print(s[x:y])
else:
print("Xem lai vi tri bat dau hoac ket thuc")
Bai 3 :
#Dao Nguoc CHUOI
s = input("Nhap 1 cau: ")
print(s[::-1])
Bai 4 :
#So_lan_lap_lai_cua_ki_tu
s = input("Nhap 1 cau: ")
s1 = s.lower()
a = input("Nhap 1 ky tu: ")
dem = 0
for i in s:
if i == a:
dem += 1
print(f"So lan lap lai cua ki tu {a} la {dem}")

Bai 5 :
#THAY_THE_CHUOI_KI_TU
s = input("Nhap 1 cau: ")
s1 = s.lower()
a = input("Nhap 1 ky tu can thay the: ")
b = input("Nhap 1 ky tu de thay the: ")
if a in s:
t = s.replace(a, b)
print(t)
else:
print("Xem lai ky tu can thay the")
Bai 6 :
#Tach 1 chuoi thanh 1 list danh sach
s = input("Nhap 1 cau: ")
i = s.split()
print(f"List of words: {i}")
Bai 7 :
#Noi cac phan tu trong chuoi
s = ["hello", "world"]
i = ' '.join(s)
print(i)
Bai 8 :
#Viet Hoa chu ki dau moi tu
s = input("Nhap 1 cau: ")
i = s.title()
print(i)
Bai 9 :
#Kiem tra tien to chi dinh
s = input("Nhap 1 cau: ")
i = input("Nhap 1 tien to: ")
a = s.find(i)
if a == 0:
print(f"Cau bat dau voi tien to {i}")
else:
print(f"Cau khong bat dau voi tien to {i}")
Bai 10 :
#Kiem tra hau to
s = input("Nhap 1 cau: ")
i = input("Nhap 1 hau to: ")
a = s.find(i)
if s[-len(i):] == i:
print(f"Cau ket thuc voi hau to {i}")
else:
print(f"Cau khong ket thuc voi hau to {i}")
BT DICTIONARY :
#Them cap tu khoa-gia tri
s = {}
key = input("Nhap 1 khoa: ")
value = input("Nhap 1 gia tri: ")
s[key] = value
print(s)
Bai 2 :
#Truy cap gia tri bang tu khoa
s = {'name' : 'Tuyen', 'age' : 21}
i = input("Nhap tu khoa: ")
if i in s:
print(s[i])
else:
print("Xem lai tu khoa")

Bai 3 :
#IN ra cac khoa cung nhu gia tri
s = {'name' : 'tuyen', 'age' : 21, 'job' : 'student', 'country' : 'vietnam'}
for (x, y) in s.items():
print(x + ':' + str(y))
Bai 4:
#Kiem tra su ton tai cua tu khoa
s = {'name' : 'Tuyen', 'age' : 21, 'job' : 'student', 'country' : 'vietnam'}
i = input("Nhap 1 tu khoa: ")
if i in s:
print(f"Co tu khoa {i}")
else:
print(f"Khong co tu khoa {i}")
Bai 5 :
#Xoa 1 tu khoa
s = {'name' : 'Tuyen', 'age' : 21, 'job' : 'student', 'country' : 'vietnam'}
i = input("Nhap 1 tu khoa can xoa: ")
if i in s:
del s[i]
print(s)
else:
print("Xem lai tu khoa can xoa")

Bai 6:
# gop 2 tu dien
a = {'name' : 'Tuyen', 'age' : 21}
b = {'job' : 'student', 'country' : 'vietnam'}
a.update(b)
for x, y in a.items():
print(x, y)
Bai 7:
#tim gia tri lon nhat
s = {'a' : 30, 'b' : 10, 'c' : 20}
max = 0
for x,y in s.items():
if max < y:
max = y
key = x
print(f"Gia tri lon nhat la: {key} = {max}")
Bai 8 :
#sap xep theo thu tu tang dan
s = {'apple': 5, 'banana': 2, 'cherry': 8, 'date': 3}
i = sorted(s.items(), key = lambda x : x[1] )
print(i)

Bai 9 :
#tao ra cac khoa va cung 1 gia tri
key = ['a', 'b', 'c']
value = 1
s = dict.fromkeys(key, value)
print(s)
Bai 10 :
#tim tu khoa chung giua 2 chuoi
s1 = {'a' : 1, 'b' : 2, 'c' : 3}
s2 = {'a' : 4, 'b' : 5, 'd' : 6}
for i in s1.keys():
if i in s2:
common = {i}
print("Tu khoa chung la: ",common)

You might also like