mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-10 16:54:33 +02:00
Control Flow and Iterables
This commit is contained in:
@@ -297,37 +297,37 @@ dolu_set | diger_set # => {1, 2, 3, 4, 5, 6}
|
|||||||
|
|
||||||
|
|
||||||
####################################################
|
####################################################
|
||||||
## 3. Control Flow and Iterables
|
## 3. Kontrol Akışları ve Temel Soyutlandırma
|
||||||
####################################################
|
####################################################
|
||||||
|
|
||||||
# Let's just make a variable
|
# Bir değişken oluşturalım
|
||||||
some_var = 5
|
bir_degisken = 5
|
||||||
|
|
||||||
# Here is an if statement. Indentation is significant in python!
|
# Burada bir "if" ifadesi var. Girinti(boşluk,tab) python için önemlidir!
|
||||||
# prints "some_var is smaller than 10"
|
# çıktı olarak "bir_degisken 10 dan küçük" yazar
|
||||||
if some_var > 10:
|
if bir_degisken > 10:
|
||||||
print("some_var is totally bigger than 10.")
|
print("bir_degisken 10 dan büyük")
|
||||||
elif some_var < 10: # This elif clause is optional.
|
elif bir_degisken < 10: # Bu 'elif' ifadesi zorunlu değildir.
|
||||||
print("some_var is smaller than 10.")
|
print("bir_degisken 10 dan küçük")
|
||||||
else: # This is optional too.
|
else: # Bu ifade de zorunlu değil.
|
||||||
print("some_var is indeed 10.")
|
print("bir_degisken değeri 10")
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
For loops iterate over lists
|
Döngülerle lsiteleri döngüye alabilirsiniz
|
||||||
prints:
|
çıktı:
|
||||||
dog is a mammal
|
köpek bir memeli hayvandır
|
||||||
cat is a mammal
|
kedi bir memeli hayvandır
|
||||||
mouse is a mammal
|
fare bir memeli hayvandır
|
||||||
"""
|
"""
|
||||||
for animal in ["dog", "cat", "mouse"]:
|
for hayvan in ["köpek", "kedi, "fare"]:
|
||||||
# You can use format() to interpolate formatted strings
|
# format ile kolayca yazıyı biçimlendirelim
|
||||||
print("{} is a mammal".format(animal))
|
print("{} bir memeli hayvandır".format(hayvan))
|
||||||
|
|
||||||
"""
|
"""
|
||||||
"range(number)" returns a list of numbers
|
"range(sayi)" bir sayı listesi döndür
|
||||||
from zero to the given number
|
0'dan belirttiğiniz sayıyıa kadar
|
||||||
prints:
|
çıktı:
|
||||||
0
|
0
|
||||||
1
|
1
|
||||||
2
|
2
|
||||||
@@ -337,8 +337,8 @@ for i in range(4):
|
|||||||
print(i)
|
print(i)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
While loops go until a condition is no longer met.
|
'While' döngüleri koşul çalıştıkça işlemleri gerçekleştirir.
|
||||||
prints:
|
çıktı:
|
||||||
0
|
0
|
||||||
1
|
1
|
||||||
2
|
2
|
||||||
@@ -347,50 +347,49 @@ prints:
|
|||||||
x = 0
|
x = 0
|
||||||
while x < 4:
|
while x < 4:
|
||||||
print(x)
|
print(x)
|
||||||
x += 1 # Shorthand for x = x + 1
|
x += 1 # Uzun hali x = x + 1
|
||||||
|
|
||||||
# Handle exceptions with a try/except block
|
# Hataları kontrol altına almak için try/except bloklarını kullanabilirsiniz
|
||||||
try:
|
try:
|
||||||
# Use "raise" to raise an error
|
# Bir hata oluşturmak için "raise" kullanabilirsiniz
|
||||||
raise IndexError("This is an index error")
|
raise IndexError("Bu bir index hatası")
|
||||||
except IndexError as e:
|
except IndexError as e:
|
||||||
pass # Pass is just a no-op. Usually you would do recovery here.
|
pass # Önemsiz, devam et.
|
||||||
except (TypeError, NameError):
|
except (TypeError, NameError):
|
||||||
pass # Multiple exceptions can be handled together, if required.
|
pass # Çoklu bir şekilde hataları kontrol edebilirsiniz, tabi gerekirse.
|
||||||
else: # Optional clause to the try/except block. Must follow all except blocks
|
else: # İsteğe bağlı bir kısım. Eğer hiçbir hata kontrol mekanizması desteklemiyorsa bu blok çalışacaktır
|
||||||
print("All good!") # Runs only if the code in try raises no exceptions
|
print("Her şey iyi!") # IndexError, TypeError ve NameError harici bir hatada bu blok çalıştı
|
||||||
|
|
||||||
# Python offers a fundamental abstraction called the Iterable.
|
# Temel Soyutlandırma, bir objenin işlenmiş halidir.
|
||||||
# An iterable is an object that can be treated as a sequence.
|
# Aşağıdaki örnekte; Obje, range fonksiyonuna temel soyutlandırma gönderdi.
|
||||||
# The object returned the range function, is an iterable.
|
|
||||||
|
|
||||||
filled_dict = {"one": 1, "two": 2, "three": 3}
|
dolu_sozl = {"bir": 1, "iki": 2, "uc": 3}
|
||||||
our_iterable = filled_dict.keys()
|
temel_soyut = dolu_sozl.keys()
|
||||||
print(our_iterable) #=> range(1,10). This is an object that implements our Iterable interface
|
print(temel_soyut) #=> range(1,10). Bu obje temel soyutlandırma arayüzü ile oluşturuldu
|
||||||
|
|
||||||
# We can loop over it.
|
# Temel Soyutlandırılmış objeyi döngüye sokabiliriz.
|
||||||
for i in our_iterable:
|
for i in temel_soyut:
|
||||||
print(i) # Prints one, two, three
|
print(i) # Çıktısı: bir, iki, uc
|
||||||
|
|
||||||
# However we cannot address elements by index.
|
# Fakat, elementin anahtarına değerine.
|
||||||
our_iterable[1] # Raises a TypeError
|
temel_soyut[1] # TypeError hatası!
|
||||||
|
|
||||||
# An iterable is an object that knows how to create an iterator.
|
# 'iterable' bir objenin nasıl temel soyutlandırıldığıdır.
|
||||||
our_iterator = iter(our_iterable)
|
iterator = iter(temel_soyut)
|
||||||
|
|
||||||
# Our iterator is an object that can remember the state as we traverse through it.
|
# 'iterator' o obje üzerinde yaptığımız değişiklikleri hatırlayacaktır
|
||||||
# We get the next object by calling the __next__ function.
|
# Bir sonraki objeyi almak için __next__ fonksiyonunu kullanabilirsiniz.
|
||||||
our_iterator.__next__() #=> "one"
|
iterator.__next__() #=> "bir"
|
||||||
|
|
||||||
# It maintains state as we call __next__.
|
# Bir önceki __next__ fonksiyonumuzu hatırlayıp bir sonraki kullanımda bu sefer ondan bir sonraki objeyi döndürecektir
|
||||||
our_iterator.__next__() #=> "two"
|
iterator.__next__() #=> "iki"
|
||||||
our_iterator.__next__() #=> "three"
|
iterator.__next__() #=> "uc"
|
||||||
|
|
||||||
# After the iterator has returned all of its data, it gives you a StopIterator Exception
|
# Bütün nesneleri aldıktan sonra bir daha __next__ kullanımınızda, StopIterator hatası oluşturacaktır.
|
||||||
our_iterator.__next__() # Raises StopIteration
|
iterator.__next__() # StopIteration hatası
|
||||||
|
|
||||||
# You can grab all the elements of an iterator by calling list() on it.
|
# iterator'deki tüm nesneleri almak için list() kullanabilirsiniz.
|
||||||
list(filled_dict.keys()) #=> Returns ["one", "two", "three"]
|
list(dolu_sozl.keys()) #=> Returns ["bir", "iki", "uc"]
|
||||||
|
|
||||||
|
|
||||||
####################################################
|
####################################################
|
||||||
|
Reference in New Issue
Block a user