{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Listeler" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Şimdi de yeni bir veritipimiz olan **listeleri** öğrenmeye çalışalım. Listeler yapıları gereği stringlere oldukça benzerler ve kullanıldıkları yerler de çok yararlı olan bir veritipidir. Tıpkı stringler gibi ,indekslenirler,parçalanırlar ve üzerinde değişik işlemler yapabildiğimiz metodlar bulunur. Ancak listelerin stringlerden önemli farkları da bulunmaktadır. Stringler konusundan bildiğimiz kadarıyla stringler değiştirilemez bir veri tipidir. Ancak, listelerimiz değiştirilebilir bir veritipidir.\n", "\n", "Bir listede her veritipinden elemanı saklayabiliriz. Bu anlamda sıralı bir diziye benzer. Peki bu konuda ne öğreneceğiz? \n", "\n", " 1.Liste oluşturma\n", " 2.Indeksleme ve Parçalama\n", " 3.Temel Liste Metodları ve İşlemleri\n", " 4.İç içe Listeler \n", " \n", " \n", "### Liste Oluşturma\n", "\n", "Listeler bir *[]* köşeli parantez içine veriler veya değerler konarak oluşturulabilir.\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 4, 5, 6, 'Elma', 3.14, 5.324]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# liste değişkeni. Değişik veri tiplerinden değerleri saklayabiliyoruz.\n", "liste = [3,4,5,6,\"Elma\",3.14,5.324]\n", "liste" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste2 = [3,4,5,6,7,8,9]\n", "liste2" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Boş liste\n", "bos_liste = []\n", "bos_liste" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Boş liste . list() fonksiyonuyla da oluşturulabilir.\n", "bos_liste = list()\n", "bos_liste" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# len fonksiyonu listeler üzerinde de kullanılabilir.\n", "liste3 = [3,4,5,6,6,7,8,9,0,0,0]\n", "len(liste3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bir string *list()* fonksiyonu yardımıyla listeye dönüştürülebilir.\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['M', 'e', 'r', 'h', 'a', 'b', 'a']" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = \"Merhaba\"\n", "lst = list(s)\n", "lst" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Listeleri Indeksleme ve Parçalama\n", "\n", "Listeleri indeksleme ve parçalama stringlerle birebir olarak aynıdır.\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 4, 5, 6, 7, 8, 9, 10]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste = [3,4,5,6,7,8,9,10]\n", "liste" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 0. eleman \n", "liste[0]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 4. eleman \n", "liste[4]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Sonuncu Eleman\n", "liste[len(liste)-1]" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Sonuncu Eleman\n", "liste[-1]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# İlk Eleman\n", "liste[-len(liste)]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 4, 5, 6]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Baştan 4. indekse kadar (dahil değil)\n", "liste[:4]" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, 5, 6, 7]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 1.indeksten 5.indekse kadar\n", "liste[1:5]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[8, 9, 10]" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste[5:]" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 5, 7, 9]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste[::2]" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[10, 9, 8, 7, 6, 5, 4, 3]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste[::-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Temel Liste Metodları ve İşlemleri\n", "\n", "Bu kısımda da listelerde yapabileceğimiz temel işlemleri ve bazı temel metodları öğreneceğiz. Listelerin daha bir çok metodunu kursun ileriki kısımlarında görüyor olacağız.\n", "\n", "Bir listeyi birbirine toplama işlemini stringlerdeki gibi yapabiliriz.\n" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste1 = [1,2,3,4,5]\n", "liste2 = [6,7,8,9,10]\n", "liste1 + liste2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bir listeye bir tane eleman eklemek içinse aşağıdaki işlemi uygulayabiliriz.\n" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "liste = [1,2,3,4]\n", "liste = liste + [\"Murat\"]" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 'Murat']" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "# Listeler direk olarak değiştirilebilirler.\n", "liste[0] = 10" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[10, 2, 3, 4, 'Murat']" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "# Şöyle bir kullanım da mümkündür.\n", "liste[:2] = [40,50]" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[40, 50, 3, 4, 'Murat']" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bir listeyi bir tamsayıyla da çarpabiliriz.\n" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste = [1,2,3,4,5]\n", "liste * 3" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste # Ama gördüğümüz gibi listemiz değişmiyor." ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "liste = liste * 3 " ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste # Eşitleme yaptığımız için liste değişti." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Şimdi de isterseniz temel bazı liste metodlarına bakalım. Listeler, diğer programlama dillerindeki **arraylere** göre oldukça esnektir. Belli bir boyutları yoktur ve ekleme, çıkarma yapmak oldukça kolaydır.\n", "\n", "#### append metodu\n", "append metodu, verdiğimiz değeri listeye eklememizi sağlar." ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 4, 5, 6, 7]" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste = [3,4,5,6]\n", "liste.append(7)\n", "liste" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "liste.append(\"Murat\")" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[3, 4, 5, 6, 7, 'Murat']" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### pop metodu\n", "Bu metod, içine değer vermezsek listenin son indeksindeki elemanı, değer verirsek verdiğimiz değere karşılık gelen indeksteki elemanı listeden atar ve attığı elemanı ekrana basar.\n", "\n" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste = [1,2,3,4,5]\n", "liste.pop()" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "eleman = liste.pop(2)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eleman" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 4]" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 4, 'Murat']" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste.append(\"Murat\")\n", "liste" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Murat'" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste.pop()" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 4]" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Aslında zamanı gelmişken söylemekte fayda var. Liste elemanlarına ulaşırken eğer olmayan bir indeksi verirsek *Python* bizlere hata verecektir.\n" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "liste = [12,54,67,67]" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "list index out of range", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mliste\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m50\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: list index out of range" ] } ], "source": [ "liste[50]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### sort metodu\n", "sort metodu listenin elemanlarını sıralamamızı sağlar. Hemen kullanımına bakalım.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "liste = [34,1,56,334,23,2,3,19]\n", "liste.sort() # Küçükten büyüğe sıralar.\n", "liste" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "liste.sort(reverse = True) # Büyükten küçüğe sıralar.\n", "liste" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "liste = [\"Elma\",\"Armut\",\"Muz\",\"Kiraz\"]\n", "liste.sort() # Alfabetic olarak küçükten büyüğe\n", "liste" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "liste = [\"Elma\",\"Armut\",\"Muz\",\"Kiraz\"]\n", "liste.sort(reverse = True) # Alfabetic olarak büyükten küçüğe \n", "liste" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### İç içe Listeler\n", "Bir listenin içinde başka bir liste bulundurmak mümkündür. Bunlara Pythonda içiçe listeler denmektedir. Bu tip bir özellik, Pythonda ağaç yapılarında veya matris yapılarında oldukça kullanışlı olmaktadır.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 3 Tane liste oluşturalım.\n", "\n", "liste1 = [1,2,3]\n", "liste2 = [4,5,6]\n", "liste3 = [7,8,9]\n", "\n", "yeniliste = [liste1,liste2,liste3]\n", "yeniliste" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Şimdi, 2. listenin ilk elemanına nasıl ulaşacağımıza bakalım.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 1.elemanın 0.elemanı\n", "yeniliste[1][0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pythonda listeler şimdilik bu kadar! Listelerin geri kalan özelliklerini ileride göreceğiz." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 2 }