{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# İleri Seviye Listeler\n", "\n", "Bu konuda listelerin üzerinde en çok kullanılan metodları öğreneceğiz. \n", "\n", "\n", "### append() metodu\n", "\n", "***append*** metodu listenin en sonuna eleman eklememizi sağlar.\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste = [1,2,3,4,5,6,7]\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste.append(34)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 34]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste.append(\"Python\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 34, 'Python']" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### extend() metodu\n", "\n", "***extend()*** metodu bir listeye başka bir listenin elemanları eklememizi sağlar." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste = [1,2,3,4,5,6,7]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste.extend([10,11,12])" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 10, 11, 12]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### insert() metodu\n", "\n", "***insert()*** metodu listenin belli bir indeksine bir eleman eklememizi sağlar." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste = [1,2,3,4,5,6,7,8,9]" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste.insert(2,\"Python\") # 2.indekse \"Python\" değerini ekliyoruz." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 'Python', 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### pop() metodu\n", "\n", "***pop() metodu*** içine hiçbir değer vermezsek listenin son elemanını silerek ekrana basar. İçine belli bir indeks değeri verirsek o indeksi siler ve ekrana basar." ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste = [1,2,3,4,5,6,7]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste.pop() # Son eleman siliniyor." ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste.pop(2) # 2.indeksteki eleman siliniyor." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### remove() metodu\n", "\n", "***remove() metodu*** verdiğimiz değeri listeden çıkarmamızı sağlar." ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste = [\"Python\",\"Php\",\"Java\",\"C\"]" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste.remove(\"Python\") # Python'ı siliyoruz." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Php', 'Java', 'C']" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "list.remove(x): x not in list", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mliste\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Javascript\"\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# Listede yok hata verir.\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list" ] } ], "source": [ "liste.remove(\"Javascript\") # Listede yok hata verir." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### index() metodu\n", "\n", "***index() metodu*** verilen bir değerin **baştan başlayarak** hangi indekste olduğunu söyler. Değer listede yoksa hata döner. Eğer ekstra index değeri belirtilirse, ***index metodu()*** değeri bu indeksten itibaren aramaya çalışır." ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste = [1,2,3,4,3,3,5,6,7,8,9]" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste.index(3) # 3 elemanı baştan başlayarak 2.indekste" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste.index(3,3) # 3 elemanı 3.indekten itibaren arandığından 4.indekste " ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "'Python' is not in list", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mliste\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Python\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mValueError\u001b[0m: 'Python' is not in list" ] } ], "source": [ "liste.index(\"Python\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### count() metodu\n", "\n", "***count() metodu*** verilen bir değerin listede kaç defa geçtiğini sayar." ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste = [1,2,3,4,5,6,1,1,1,1,1,1,1,1,8]" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste.count(1)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste.count(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### sort() metodu\n", "\n", "***sort()*** metodu bir listenin elemanlarını sayıysa küçükten büyüğe , string ise alfabetik olarak sıralar. Eğer özellikle içine ***reverse = True*** değeri verilirse elemanları büyükten küçüğe sıralar." ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste = [12,-2,3,1,34,100]" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste.sort()" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-2, 1, 3, 12, 34, 100]" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste2 = [\"Python\",\"Php\",\"C\",\"Java\"]" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste2.sort()" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['C', 'Java', 'Php', 'Python']" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste2" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste = [12,-2,3,1,34,100]" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste.sort(reverse = True)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[100, 34, 12, 3, 1, -2]" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste2 = [\"Python\",\"Php\",\"C\",\"Java\"]" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste2.sort(reverse = True)" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Python', 'Php', 'Java', 'C']" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }