0% found this document useful (0 votes)
7 views25 pages

python btbt

Uploaded by

Lê Minh
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 views25 pages

python btbt

Uploaded by

Lê Minh
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/ 25

If statement y=b

1 if a<=c and a<=b:

a=int (input('nhapso : ')) y=a

b=int (input('nhapso : ')) if c<=a and c<=b:

c=int (input('nhapso : ')) y=c

if a <= b and c <= b: print ('smallest:',y)

x=b print('hieu cua 2 so la :',x-y)

if a>=c and a>=b: 3.

x=a a=int (input('nhap nam : '))

if c>=a and c>=b: print('namnhuan'if a % 4== 0 and a % 100!


=0 or a % 400==0 else 'khong la nam
x=c
nhuan')

print (x)
4

2
a=input('gioi tinh : ')

a=int (input('nhapso : '))


b=int(input('tuoi : '))

b=int (input('nhapso : '))


#print ('chua nghi huu' if a =='nam' and b <
c=int (input('nhapso : ')) 65 else 'nghi huu')

if a <= b and c <= b: #print ('chua nghi huu' if a =='nu'and b <


60 else 'nghi huu')
x=b
if a=='nam':
if a>=c and a>=b:
if b<65:
x=a
print ('chua nghi huu')
if c>=a and c>=b:
else:
x=c
print('nghi huu')
print ('largest:',x)
if a == 'nu':
if a >= b and c >= b:
if b < 60: if n==1:

print('chua nghi huu') print (' khong phai la so palindrome


nguyen to')
else:

print('nghi huu')

5. import math

a = int (input('nhap so : '))

n=0

x=0

# str(a) == str(a)[::-1]:

#chuyen a thành chuoi và dịch ngược lại


chuỗi a

#for i in range(2, int(math.sqrt(a)) + 1):

#for i in range(2, int(a**0.5) + 1):

for i in range(1, a-1):

if a % i == 0:

x=1

if x==1:

n=1

else:

x=2

if a==2 or a==3 :

x=2

if x==2 and str(a) == str(a)[::-1] and n==0


:

print(' là so palindrome nguyen to')


FORLOOP max=0

1. for i in range(1,n+1,1):

n = int(input('nhap so : ')) a = int(input())

a=0 if i==1:## xet th so nhap vao am

for i in range(1,n,1): max=a

if i % 2 == 0: if a>=max:

print(i) max=a

a=a+i else:

print ("tong cua cac so tren la : %s" %a) max=max

2.

n = int(input('nhap so : ')) print(max)

a=0 4.

b=0 n = int(input('nhap so : '))

for i in range(1,n,1): a=0

if i % 3 == 0: b=0

a=a+i for i in range(1,n,1):

if i%5==0: a=a+n%10

b=b+i n=n//10

print ("tong cua cac so chia het cho 3 : %s" if n == 0: break


%a)
print ("tong cua cac so chia het cho 3 : %s"
print ("tong cua cac so chia het cho 5 : %s" %a)
%b)
5.
3.
n = int(input('nhap so : '))
n = int(input('nhap so so : '))
a=1
c=0
b=n b=1

for i in range(1,n,1): else:

a=a*n a=2

n=n-1 if b==1 :

if n == 0: break print('khong la so nguyen to')

print ("luy thua cua %s la : %s" %(b,a)) if a==2 and b==0:

6. print(' la so nguyen to')

n = int(input('nhap so : ')) if n==2 or n==3:

a=1 print(' la so nguyen to')

b=n 7. n = int (input('nhap so : '))

for i in range(1,n,1): #a = str(n)[::-1]

a=a*n #print(a)

n=n-1 a=0

if n == 0: break b=0

print ("luy thua cua %s la : %s" %(b,a)) for i in range(1,n,1):

n = int(input('nhap so : ')) a=n%10

a=0 b=b*10+a

b=0 n=n//10

for i in range(2, n-1): if n == 0: break

if n==2 or n==3: print ("so dao nguoc la : %s" %b)

print(' la so nguyen to')

if n % i == 0:

a=1

if a==1:
WHILELOOP

1. a=1

while True: b=int(n)

str = input('password: ') for i in range(1,b,1):

if str=='21CDT': a=a*b

print('correct') n=n-1

break if n == 0: break

else: print ("luy thua cua %s la : %s" %(b,a))

print('try again') 4.

2. a=0

n=0 b=0

a=0 while True:

while n<=100: n = float(input('nhap so: '))

a=a+n if n==int(n):

n=n+2 print("%s la so nguyen" %n)

print(a) m = int(n)

3. i=1

while True: while i < m-1:

n = float(input('nhap so: ')) i=i+1

if n==int(n): if m % i == 0:

print("%s la so nguyen" %n) a=1

break else:

else: b=1

print('nhap lai 1 so nguyen khac') break


else: b=b+1## xet th so am

print('nhap lai 1 so nguyen khac') if n==0:

if a==1: print("so lon nhat la %s " %max)

print("%s khong la so nguyen to" % m) break

if b==1 and a==0: else:

print("%s la so nguyen to" % m) if b==1: ## xet th so am

5. max=n

tb=0 if n>=max:

i=0 max=n

while True: else:

n = float(input('nhap so: ')) max=max

if n==0: 7.

print("trung binh cua day so la: %s " def bangcuuchuong(n):


%tb)
print('bang cuu chuong %s'%n)
break
for i in range (1,10):
else:
print('%s*i=%s'%(n, n*i))
tb=(tb*i+n)/(i+1)
n=5
i=i+1
while n<=9:
6.
bangcuuchuong(n)
b=0
n=n+1
c=0
8.
max=0
tong=0
while True:
while True:
n = float(input('nhap so: '))
n = float(input('nhap so: '))
if n==0: if i%2==0:

print("tong cac so chia het cho 3 la: tong=i+tong


%s " %tong)
i=i+1
break
print(tong)
else:

if n%3==0:

tong=tong+n

9.

import random

print('Day so ngau nhien duoc sinh ra nhu


duoi day:')

while True:

i = random.randint(1, 100) # Generate a


random integer between 1 and 100

print(i) # Print the generated random


integer

if i % 7 == 0: # Check if the number is


divisible by 7

break

10.

tong=0

n = float(input('nhap so: '))

i=1

while i<=n:
FUNTION

1. m=int(input('nhap so phan tu cua danh


def dientich(a,b): sach: '))

s=a*b for a in range (1,m+1):

print("dien tich la %s" %s ) b=float(input('nhap so: '))

trungbinh(b)

m= float(input('chieu dai : '))

n = float(input('chieu rong : ')) print('trung binh cua danh sach la %s' %


tb)
dientich(m,n)
4.
2.
def giai_pt_bac_2(a, b, c):
def ktsochan(n):
delta = b ** 2 - 4 * a * c
if n%2==0:
if delta > 0:
print('%s la so chan '%n)
x1 = (-b + delta**-0.5) / (2 * a)
else:
x2 = (-b - delta**-0.5) / (2 * a)
print('%s khong la so chan ' % n)
return (x1, x2)
m=int(input('nhap so: '))
elif delta == 0:
ktsochan(m)
x = -b / (2 * a)
3.
return (x)
tb=0
else:
i=0
return None
def trungbinh(n):
a=float(input('nhap so: '))
global tb,i
b=float(input('nhap so: '))
tb = (tb * i + n) / (i + 1)
c=float(input('nhap so: '))
i=i+1
nghiem = giai_pt_bac_2(a, b, c) for char in s:

if nghiem: if char in vowels:

print("Nghiệm của phương trình là:", i+= 1


nghiem)
return i
else:

print("Phương trình vô nghiệm")


a=str(input('nhập chuỗi: '))
5.
print("Số lượng nguyên âm trong chuỗi
def calculator_discounted_price(a,b): là:", count_vowels(a))

x=a-a*b/100 8.

print('gia sau chiet khau la: %s'%x) def caculator_product(*args):

a=float(input('nhap giá: ')) s=1

b=float(input('nhap phan tram chiet khau: for num in args:


'))
s=s*num
calculator_discounted_price(a,b)
return s
6.
#print(caculator_product(1,3,4,5,6))
def calculator_power(a,b):
m= int(input('so bien nhan vao : '))
x=a**b
numbers = []
print('%s mũ %s la: %s'%(a,b,x))
for i in range(m):
a=float(input('cơ số: '))
num = int(input(f'Nhập số thứ {i+1}: '))
b=float(input('mũ: '))
numbers.append(num)
calculator_power(a,b)
a = caculator_product(*numbers)
7.
print("%s"%a)
def count_vowels(s):
9.
vowels = "aeiouAEIOU"
def caculate_median(*args):
i=0
sapxep = sorted(args)#hàm sắp xếp if total == 0:

n = len(sapxep)#hàm tìm so phần tử return [0] * len(args) # Tránh chia


nhập vào cho 0

percentages = [(num / total) * 100 for


num in args]
mid = n // 2
return percentages

if n % 2 == 0:

return (sapxep[mid - 1] +
sapxep[mid]) / 2 numbers = [10, 20, 30, 40]

else: percentages =
percentage_of_total(*numbers)
return sapxep[mid]
for num, perc in zip(numbers,
print(caculate_median(1, 3, 3,10,5, 6, 7, 8,
percentages):
9))
print(f"Số {num} chiếm {perc:.2f}%
của tổng.")

10. def find_odd_number(*args):

mang_so_le = [a for a in args if a % 2 !=


0]

for a in mang_so_le:

print(a)

find_odd_number(11,2,4,7,3,5,6,8,4,6,3,2,
9,8,7)

11.

def percentage_of_total(*args):

total = sum(args)
for i in range (n):

MAP b=float(input('nhap so: '))

FILTER a=a+[b]

MAP1 b=list(map(lambda x:int (x),a))

def viethoa(strings): print(b)

viethoa = map(str.capitalize, strings) MAP4

for s in viethoa: def ktsonguyento(n):

print(s) a=0

b=0

strings = ["hello", "world", "python", for i in range(2, n-1):


"programming"]
if n % i == 0:
viethoa(strings)
a=1
MAP 2
if a==1:
a=[]
b=1
n = int(input('nhap so phan tu cua danh
else:
sach : '))
a=2
for i in range (n):

if b==1 :
b=int(input('nhap so: '))

return'khong la so nguyen to'


a=a+[b]

b=list(map(lambda x:x*1.8+32,a))

if a==2 and b==0:


print(b)

return' la so nguyen to'


MAP3

if n == 2 or n == 3:
a=[]

return' la so nguyen to'


n = int(input('nhap so phan tu cua danh
sach : '))
for i in range (n):

a=[] b=float(input('nhap so: '))

n = int(input('nhap so phan tu cua danh a=a+[b]


sach : '))
b=list(filter(lambda x:x%2==1,a))
for i in range (n):
print(b)
b=int(input('nhap so: '))
FILTER 2
a=a+[b]
a=[]
b=list(map(ktsonguyento,a))
n = int(input('nhap so phan tu cua danh
print(b) sach : '))

## Kiem tra so nguyen to (Prime) dung for i in range (n):


Lambda va ham map()
b=float(input('nhap so: '))
#N = int(input('Nhap vao so nguyen bat
a=a+[b]
ky: '))

b=list(filter(lambda x:x%3==0,a))
#kiemtraNT = lambda x: 1 if x > 1 and
all(x % i != 0 for i in range(2, int(x**0.5) print(b)
+ 1)) else 0
FILTER 3
#result = list(map(kiemtraNT, [N]))
def bat_dau_bang_a(ten):

return ten[0].lower() == 'a'


#if result == [1]:
a=[]
# print(N, 'la so nguyen to')
n = int(input('nhap so phan tu cua danh
#elif result == [0]: sach : '))

# print(N, 'khong phai la so nguyen to') for i in range (n):

FILTER 1 b=(input('nhap ten: '))

a=[] a=a+[b]

n = int(input('nhap so phan tu cua danh b=list(filter(bat_dau_bang_a,a))


sach : '))
print(b)
FILTER 4 SONGHUYEN TO

a=[] # Kiem tra so nguyen to (Prime) dung


Lambda va ham map()
n = int(input('nhap so phan tu cua danh
sach : ')) N = int(input('Nhap vao so nguyen bat ky:
'))
for i in range (n):
kiemtraNT = lambda x: 1 if x > 1 and all(x
b=str(input('nhap chuoi: '))
% i != 0 for i in range(2, int(x**0.5) + 1))

a=a+[b] else 0

result = list(filter(lambda s: len(s) > 5, a)) result = list(map(kiemtraNT, [N]))

print('Cac chuoi co do dai lon hon 5 trong


day cac chuoi cho truoc la:', result)
if result == [1]:

print(N, 'la so nguyen to')

elif result == [0]:

print(N, 'khong phai la so nguyen to')


m=0

STRING for i in range(len(n)):

1 if n[i]==a:

n=str(input('nhap chuoi : ')) m=m+1

m=int(input('nhap vi tri lay ky tu : ')) print("so ky tu %s la : %s"%(a,m))

if m>len(n): 5.

print('nhap lai vi tri') n=str(input('nhap chuoi : '))

else: a=str(input('nhap vao ky tu muon thay the :


'))
print(n[m])
b=str(input('nhap vao ky tu thay the : '))
2.
m=''
n=str(input('nhap chuoi : '))
for i in range(len(n)):
a=int(input('nhap vi tri dau lay ky tu : '))
if n[i]==a:
b=int(input('nhap vi tri cuoi lay ky tu : '))
m=m+b
if b>len(n):
else:
print('nhap lai vi tri')
m=m+n[i]
else:
print("chuoi thay the băng ky tu %s la :
print(n[a:b])
%s"%(b,m))

3.
#chuoi_moi = chuoi.replace(ky_tu_cu,

n=str(input('nhap chuoi : ')) ky_tu_moi)

6.

print(n[::-1]) chuoi = input('Nhập chuỗi: ')

4. danh_sach_tu = chuoi.split()

n=str(input('nhap chuoi : ')) print('Danh sách các từ trong chuỗi là:',


danh_sach_tu)
a=str(input('nhap vao ky tu kiem tra : '))
7. print('Chuỗi không bắt đầu bằng tiền
tố.')
danh_sach_tu = input('Nhập danh sách các
từ, cách nhau bởi dấu cách: ').split() 10.

chuoi = ','.join(danh_sach_tu) chuoi = input('Nhập chuỗi: ')

print('Chuỗi sau khi nối:', chuoi) hau_to = input('Nhập hậu tố: ')

chuoi = input('Nhập chuỗi: ') ket_thuc_bang_hau_to = True

chuoi_moi = chuoi.title() for i in range(1, len(hau_to) + 1):

print('Chuỗi sau khi viết hoa mỗi từ:', if i > len(chuoi) or chuoi[-i] != hau_to[-
chuoi_moi) i]:

9. ket_thuc_bang_hau_to = False

chuoi = input('Nhập chuỗi: ') break

tien_to = input('Nhập tiền tố: ')

if ket_thuc_bang_hau_to:

bat_dau_bang_tien_to = True print('Chuỗi kết thúc bằng hậu tố.')

for i in range(len(tien_to)): else:

if i >= len(chuoi) or chuoi[i] != print('Chuỗi không kết thúc bằng hậu


tien_to[i]: tố.')

bat_dau_bang_tien_to = False

break

if bat_dau_bang_tien_to:

print('Chuỗi bắt đầu bằng tiền tố.')

else:
DIXTIONARY m = int(input('nhap so cap key-gia tri: '))

1 for i in range(0,m):

my_dict={} a=input('nhap key: ')

m = int(input('nhap so cap key-gia tri: ')) b=input('nhap gia tri ')

for i in range(0,m): my_dict[a] = b

a=input('nhap key: ') print(my_dict)

b=input('nhap gia tri ') print(list(my_dict.items()))

my_dict[a] = b 4.

print(my_dict) my_dict={}

2, m = int(input('nhap so cap key-gia tri: '))

my_dict={} for i in range(0,m):

m = int(input('nhap so cap key-gia tri: ')) a=input('nhap key: ')

for i in range(0,m): b=input('nhap gia tri ')

a=input('nhap key: ') my_dict[a] = b

b=input('nhap gia tri ') print(my_dict)

my_dict[a] = b

print(my_dict) n= input("Nhập khóa cần kiểm tra: ")

#in ra gia tri thong qua key

n = int(input('nhap so key: ')) if n in my_dict.keys():## gia tri thi thay


bawng values
for i in range(0,n):
print(f"Khóa '{n}' tồn tại trong từ điển.")
c=input('nhap key: ')
else:
print(my_dict [c])
print(f"Khóa '{n}' không tồn tại trong từ
3,
điển.")

my_dict={}
5.
my_dict={} print(my_dict2)

m = int(input('nhap so cap key-gia tri: ')) my_dict1.update(my_dict2)

for i in range(0,m): print(my_dict1)

a=input('nhap key: ') 7. my_dict={}

b=input('nhap gia tri ') m = int(input('nhap so cap key-gia tri: '))

my_dict[a] = b for i in range(0,m):

print(my_dict) a=input('nhap key: ')

n= input("Nhập khóa cần xoa: ") b=input('nhap gia tri ')

del my_dict[n] my_dict[a] = b

print(my_dict) print(my_dict)

6. max_value = max(my_dict.values())

my_dict1={} print(f"Giá trị lớn nhất trong từ điển là:


{max_value}")
m = int(input('nhap so cap key-gia tri: '))
8.
for i in range(0,m):
my_dict={}
a=input('nhap key: ')
m = int(input('nhap so cap key-gia tri: '))
b=input('nhap gia tri ')
for i in range(0,m):
my_dict1[a] = b
a=input('nhap key: ')
print(my_dict1)
b=input('nhap gia tri ')
my_dict2={}
my_dict[a] = b
m = int(input('nhap so cap key-gia tri: '))
print(my_dict)
for i in range(0,m):
sapxep = dict(sorted(my_dict.items(),
a=input('nhap key: ')
key=lambda item: item[1]))#giam dan -
b=input('nhap gia tri ') item[1]

my_dict2[a] = b
print(sapxep) print(my_dict2)

9. x = my_dict1.keys() & my_dict2.keys()

my_dict={}

m = int(input('nhap so cap key-gia tri: ')) print(f"Các khóa chung giữa hai từ điển là:
{x}")
b = input('nhap gia tri ')

for i in range(0,m):

a=input('nhap key: ')

my_dict[a] = b

print(my_dict)

10

my_dict1={}

m = int(input('nhap so cap key-gia tri: '))

for i in range(0,m):

a=input('nhap key: ')

b=input('nhap gia tri ')

my_dict1[a] = b

print(my_dict1)

my_dict2={}

m = int(input('nhap so cap key-gia tri: '))

for i in range(0,m):

a=input('nhap key: ')

b=input('nhap gia tri ')

my_dict2[a] = b
SET # Chuyển đổi tập hợp sang danh sách

1 list_a = list(tap_a)

tap_a = set()

m=int(input('nhap so phan tu cua tap hop: # Đảo ngược thứ tự danh sách
'))
list_a.reverse()
for i in range(0,m):
# Chuyển đổi lại thành tập hợp (sẽ mất thứ
n=input('nhap gt: ') tự, nhưng danh sách đảo ngược có thể sử
dụng)
tap_a.add(n)
reversed_set = set(list_a)
print(tap_a)
print("Danh sách đảo ngược:", list_a)
tap_b = set()
3.
m=int(input('nhap so phan tu cua tap hop:
')) tap_a = set()

for i in range(0,m): m=int(input('nhap so phan tu cua tap hop:


'))
n=input('nhap gt: ')
for i in range(0,m):
tap_b.add(n)
n=input('nhap gt: ')
print(tap_b)
tap_a.add(n)
print(tap_a&tap_b)
print(tap_a)
2.
tap_b = set()
tap_a = set()
m=int(input('nhap so phan tu cua tap hop:
m=int(input('nhap so phan tu cua tap hop:
'))
'))
for i in range(0,m):
for i in range(0,m):
n=input('nhap gt: ')
n=input('nhap gt: ')
tap_b.add(n)
tap_a.add(n)
print(tap_b)
print(tap_a)
print(tap_a-tap_b) 7.

4. tap_a = set()

print(tap_a|tap_b) m=int(input('nhap so phan tu cua tap hop:


'))
5.
for i in range(0,m):
print((tap_a-tap_b)|(tap_b-tap_a))
n=input('nhap gt: ')
6
tap_a.add(n)
from itertools import chain, combinations
print(tap_a)

tap_b = set()
def all_subsets(input_set, max_length):
m=int(input('nhap so phan tu cua tap hop:
"""Trả về tập hợp chứa tất cả các tập
'))
hợp con có độ dài <= max_length"""
for i in range(0,m):
return
set(chain.from_iterable(combinations(inpu n=input('nhap gt: ')
t_set, r) for r in range(max_length + 1)))
tap_b.add(n)

print(tap_b)
tap_a = set()
def is_subset(set1, set2):
m=int(input('nhap so phan tu cua tap hop:
return set1.issubset(set2) or
'))
set2.issubset(set1)
for i in range(0,m):
result = is_subset(tap_a, tap_b)
n=input('nhap gt: ')
print(result)
tap_a.add(n)
8.
print(tap_a)
tap_a = set()
max_length = int(input('nhap so : '))
m=int(input('nhap so phan tu cua tap hop:
subsets = all_subsets(tap_a, max_length) '))

print(subsets) for i in range(0,m):


n=int(input('nhap gt: ')) result = isdisjoint(tap_a, tap_b)

tap_a.add(n) print(result)

print(tap_a)

def kt_so_chan(input_set):

return all(x % 2 == 0 for x in input_set)

result_a = kt_so_chan(tap_a)

print(result_a)

tap_a = set()

m=int(input('nhap so phan tu cua tap hop:


'))

for i in range(0,m):

n=input('nhap gt: ')

tap_a.add(n)

print(tap_a)

tap_b = set()

m=int(input('nhap so phan tu cua tap hop:


'))

for i in range(0,m):

n=input('nhap gt: ')

tap_b.add(n)

print(tap_b)

def isdisjoint(set1, set2):

return set1.isdisjoint(set2) or
set2.isdisjoint(set1)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy