Python Demetler

Hüseyin

Hüseyin

Üye
Top Poster Of Month
Katılım
21 Ocak 2024
Mesajlar
78
Tepkime puanı
27
Puanları
18
Tuples

Tuple, sıralı ve değiştirilemez (immutable) olan farklı veri tiplerinin bir koleksiyonudur. Tuple'lar yuvarlak parantez () ile yazılır. Bir tuple oluşturulduktan sonra değerlerini değiştiremeyiz. Bir tuple'da add, insert, remove yöntemlerini kullanamayız çünkü değiştirilemez (mutable). Listenin aksine tuple'ın az sayıda metodu vardır. Tuple'lar ile ilgili metotlar:

tuple(): boş bir tuple oluşturmak için
count(): bir tuple içindeki belirli bir öğenin sayısını saymak için
index(): bir tuple içinde belirtilen bir öğenin indeksini bulmak için
operator: iki veya daha fazla tuple'ı birleştirmek ve yeni bir tuple oluşturmak için

Tuple Oluşturma

Boş tuple: Boş bir tuple oluşturma
Python:
# syntax
empty_tuple = ()
# veya tuple yapıcısını kullanarak
empty_tuple = tuple()

Başlangıç değerlerine sahip ikili
Python:
# syntax
tpl = ('item1', 'item2','item3')

Python:
fruits = ('banana', 'orange', 'mango', 'lemon')

Tuple uzunluğu

Bir tuple'ın uzunluğunu elde etmek için len() yöntemini kullanırız.

Python:
# syntax
tpl = ('item1', 'item2', 'item3')
len(tpl)

Tuple Öğelerine Erişme

Pozitif İndeksleme Liste veri tipine benzer şekilde, tuple öğelerine erişmek için pozitif veya negatif indeksleme kullanırız.
Python:
# Syntax
tpl = ('item1', 'item2', 'item3')
first_item = tpl[0]
second_item = tpl[1]

Python:
fruits = ('banana', 'orange', 'mango', 'lemon')
first_fruit = fruits[0]
second_fruit = fruits[1]
last_index =len(fruits) - 1
last_fruit = fruits[las_index]

Negatif indeksleme Negatif indeksleme, sondan başlayarak -1'in son öğeyi, -2'nin ikinci son öğeyi ve liste/çift uzunluğunun negatifinin ilk öğeyi ifade ettiği anlamına gelir.
Python:
# Syntax
tpl = ('item1', 'item2', 'item3','item4')
first_item = tpl[-4]
second_item = tpl[-3]

Python:
fruits = ('banana', 'orange', 'mango', 'lemon')
first_fruit = fruits[-4]
second_fruit = fruits[-3]
last_fruit = fruits[-1]

Tuple'ları dilimleme

Bir alt tuple'ı, tuple'da nereden başlayıp nerede biteceğini belirten bir indeks aralığı belirterek dilimleyebiliriz, dönüş değeri belirtilen öğeleri içeren yeni bir tuple olacaktır.

Pozitif İndeksler Aralığı
Python:
# Syntax
tpl = ('item1', 'item2', 'item3','item4')
all_items = tpl[0:4]         # tüm itemler
all_items = tpl[0:]         # tüm itemler
middle_two_items = tpl[1:3]  # dizin 3'teki öğeyi içermez

Python:
fruits = ('banana', 'orange', 'mango', 'lemon')
all_fruits = fruits[0:4]    # tüm itemler
all_fruits= fruits[0:]      # tüm itemler
orange_mango = fruits[1:3]  # dizin 3'teki öğeyi içermez
orange_to_the_rest = fruits[1:]

Negatif Endeksler Aralığı

Python:
# Syntax
tpl = ('item1', 'item2', 'item3','item4')
all_items = tpl[-4:]         # all items
middle_two_items = tpl[-3:-1]  # dizin 3'teki öğeyi içermez (-1)

Python:
fruits = ('banana', 'orange', 'mango', 'lemon')
all_fruits = fruits[-4:]    # tüm itemler
orange_mango = fruits[-3:-1]  # dizin 3'teki öğeyi içermez
orange_to_the_rest = fruits[-3:]

Tuple'ları Listelere Dönüştürme

Tuple'ları listelere ve listeleri tuple'lara dönüştürebiliriz. Tuple değişmezdir, eğer bir tuple'ı değiştirmek istiyorsak onu bir listeye dönüştürmeliyiz.

Python:
# Syntax
tpl = ('item1', 'item2', 'item3','item4')
lst = list(tpl)

Python:
fruits = ('banana', 'orange', 'mango', 'lemon')
fruits = list(fruits)
fruits[0] = 'apple'
print(fruits)     # ['apple', 'orange', 'mango', 'lemon']
fruits = tuple(fruits)
print(fruits)     # ('apple', 'orange', 'mango', 'lemon')

Tuple'daki Bir Öğeyi Kontrol Etme

Bir öğenin bir tuple'da var olup olmadığını in kullanarak kontrol edebiliriz, bir boolean döndürür.

Python:
# Syntax
tpl = ('item1', 'item2', 'item3','item4')
'item2' in tpl # True

Python:
fruits = ('banana', 'orange', 'mango', 'lemon')
print('orange' in fruits) # True
print('apple' in fruits) # False
fruits[0] = 'apple' # TypeError: 'tuple' object does not support item assignment

Tuple'ları Birleştirme

İki veya daha fazla tuple'ı + operatörünü kullanarak birleştirebiliriz

Python:
# syntax
tpl1 = ('item1', 'item2', 'item3')
tpl2 = ('item4', 'item5','item6')
tpl3 = tpl1 + tpl2

Python:
fruits = ('banana', 'orange', 'mango', 'lemon')
vegetables = ('Tomato', 'Potato', 'Cabbage','Onion', 'Carrot')
fruits_and_vegetables = fruits + vegetables

Tuple'ları Silme

Bir tuple içindeki tek bir öğeyi kaldırmak mümkün değildir, ancak del kullanarak tuple'ın kendisini silmek mümkündür.

Python:
# syntax
tpl1 = ('item1', 'item2', 'item3')
del tpl1

Python:
fruits = ('banana', 'orange', 'mango', 'lemon')
del fruits
 
Geri
Üst