Merhaba. Burada product yerine neden listeyi ekrana yazdırmış?
Bu mantık gerçekten kafamı karıştırıyor ya. Mentör nereden ayarlayabilirim kendime?
class Product:
def __init__(self, id, name, price, amount, date, is_active):
self.Id = id
self.Name = name
self.Price = price
self.Amount = amount
self.Date = date
self.Status = is_active
product_list = [
Product(1, "iPhone 13", 1500, 20, "2024-04-22", False),
Product(2, "iPhone 14", 1500, 20, "2024-04-22", True),
Product(3, "iPhone 14 Pro", 1500, 20, "2024-04-22", False),
Product(4, "iPhone 15", 1500, 20, "2024-04-22", True),
Product(5, "iPhone 15 Pro Plus", 1500, 20, "2024-04-22", False)
]
def showProducts(product_list):
active_products = []
for product in product_list:
if product.Status:
active_products.append(product)
if not active_products:
print('There is no result.')
else:
for product in active_products:
print(product.Name)
showProducts(product_list)
Bende Pythonla ilgileniyorum hobi olarak ve basit halk dilinde yazayim.
Bu Python nesne tabanli bir programlamadir.
Class bir kalip gibidir, ne dokersek onu verir.
Altin dokersen Altin kulce verir.
Kalip degismez ama ornekleri degisir.
Iste degisen bu orneklere (Altin, Bakir, Demir) biz bunlara Object (NESNE) diyoruz.
__init__ fonksiyonu nedir?
Biz tanimlamadigimiz surece, otomatik calisip yok olan bir fonksiyon!
Class'tan obje turettigimiz anda calisir. Kac obje uretildiyse o kadar calisir.
product_list listesi icinde nesneler uretilecek fonksyon var ama su an obje uretmiyor. Zaten bu product_list listesi showProducts icerisinde cagirilacak.
Gelelim showProducts fonksiyonuna:
active_products isimli bir bos liste olusturulmus.
product_list listesi icerisinde for dongusu ile gezerken iste bu noktada nesneler (object) uretilmeye basliyor.
Ayni zamanda her nesne uretildiginde if product.Status: statusu varmi diye kontrol ediliyor. Burada True olanlar listeye eklenecektir.
5 obje uretiliyor ve sadece 4 numarali nesne active_products listesine ekleniyor.
Islem bitiyor.
Daha sonra if not active_products:
Burada yaptigi su active_products icerisinde bisey yoksa sonuc yok yazdiriyor. Ancak bizde 4 numarali nesne vardi;
else durumunda ne istiyor bizden bir bakalim.
for product in active_products:
print(product.Name)
for dongusuyle gez ve product.Name yazdirmasini istiyor. Dolayisiyla iPhone 15 yazdiracaktir.
Onemli:
def showProducts(product_list):
showProducts(product_list)
Fonksiyonlara sayi yani integer veya string gonderebileceginiz gibi listede gonderebilirsiniz. Burda ne yapmaya calistigini soyle anlatayim.
liste = [1,2,3,4,5,6]
def hesaplama(product_list):
for i in product_list:
print(i*2)
hesaplama(liste)
burada once listeyi okuyor ve liste gonderiyor.
sundan farki yok
def hesaplama([1,2,3,4,5,6]):
for i in product_list:
print(i*2)
hesaplama()
buda calisir
Yani bu kodlamada adam liste gondermis. direk liste gondermis gonksiyona.
Listeyide def hesaplama([1,2,3,4,5,6]): yazmak istememis. Cunku sizin ornekte liste cok uzun o yuzden once once
product_list = [
Product(1, "iPhone 13", 1500, 20, "2024-04-22", False),
Product(2, "iPhone 14", 1500, 20, "2024-04-22", True),
Product(3, "iPhone 14 Pro", 1500, 20, "2024-04-22", False),
Product(4, "iPhone 15", 1500, 20, "2024-04-22", True),
Product(5, "iPhone 15 Pro Plus", 1500, 20, "2024-04-22", False)
okutmus hafizaya almis
sonra product_list olarak liste gondermis
Bende 1 senedir python bakmiyorum.