Uploaded by boburvalixonov045

raximov

advertisement
MUHAMMAD AL-XORAZMIY NOMIDAGI
TOSHKENT AXBOROT
TEXNOLOGIYALARI UNIVERSITETI FARG`ONA
FILIALI
DASTURIY INJINIRING VA RAQAMLI IQTISODIYOT
FAKULTETI
Dasturiy injiniring yo`nalishi 653-21-guruh talabasi
Raximov Jaxongirning
Dasturlash uslublari va paradigmalar fanidan tayyoragan
Amaliy ish
Bajardi:
Qabul qildi:
Raximov.J
Kodirov.A.A
MySQL-connector yordamida LIMIT bandi bilan SELECT so'rovini bajarish uchun
Python dasturini tuzing.
import mysql.connector
# MySQL serveriga ulanish ma'lumotlari
db_config = {
'host': 'localhost',
'user': 'username',
# MySQL server manzili
# MySQL foydalanuvchi nomi
'password': 'password', # MySQL foydalanuvchi paroli
'database': 'mydatabase' # Ma'lumotlar bazasi nomi
}
# MySQL serveriga ulanish
db_connection = mysql.connector.connect(**db_config)
# Cursor yaratish
cursor = db_connection.cursor()
# SELECT so'rovi bilan LIMIT bandi
select_query = "SELECT * FROM mytable LIMIT 5" # mytable jadvalidan 5 ta
yozuvni tanlash
# So'rovni bajargan natijani olish
cursor.execute(select_query)
records = cursor.fetchall()
# Natijani ekranga chiqarish
for record in records:
print(record)
# Ulanishni yopish
cursor.close()
db_connection.close()
Foydalanuvchidan fayl nomini kiritishini so'rang. Bunday nomdagi faylni yozish
rejimida ochishga harakat qiling. Fayl yozish uchun ochilmasa, PermissionError
bilan ishlang.
file_name = input("Iltimos, fayl nomini kiriting: ")
try:
# Fayl yozish rejimida ochish
with open(file_name, 'w') as file:
file.write("Bu faylga yozish uchun ochilgan.")
print(f"{file_name} nomli fayl yaratildi va yozish rejimida ochildi.")
except PermissionError as e:
print(f"Xatolik: {e}")
Sana klassi: Sana klassini kun, oy va yil kabi atributlar bilan amalga oshiring. Turli sana
formatlarini ko'rsatish metodlarini yozing, sana asosida yoshni hisoblang va kabisa yillarini
tekshiring
import datetime
class Sana:
def
init
(self, kun, oy, yil):
self.kun = kun
self.oy = oy
self.yil = yil
def korsatish(self, format="dd-mm-yyyy"):
if format == "mm-dd-yyyy":
return f"{self.oy:02d}-{self.kun:02d}-{self.yil}"
elif format == "yyyy-mm-dd":
return f"{self.yil}-{self.oy:02d}-{self.kun:02d}"
else:
return f"{self.kun:02d}-{self.oy:02d}-{self.yil}"
def yosh_hisoblash(self):
bugungi_sana = datetime.date.today()
yosh = bugungi_sana.year - self.yil - ((bugungi_sana.month,
bugungi_sana.day) < (self.oy, self.kun))
return yosh
def kabisa_yilni_tekshirish(self):
return (self.yil % 4 == 0 and self.yil % 100 != 0) or (self.yil % 400 == 0)
# Test qilish
bugun = Sana(6, 1, 1990)
print(bugun.korsatish()) # Kiritilgan sana formatida ko'rsatib beradi
print(bugun.yosh_hisoblash()) # Hisoblangan yosh
print(bugun.kabisa_yilni_tekshirish()) # Kabisa yilini tekshiradi
Download