{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# For Döngüleri\n", "\n", "Bu konuda Pythondaki **for** döngülerinin yapısını ve for döngülerinin kullanım alanlarını öğreneceğiz. Ancak ondan önce , Pythondaki in operatörünü öğrenmeye çalışalım." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### in Operatörü" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pythondaki *in* operatörü , bir elemanın başka bir listede,demette veya stringte (karakter dizileri) bulunup bulunmadığını kontrol eder. Kullanımı şu şekildedir;\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"a\" in \"merhaba\"" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"mer\" in \"merhaba\"" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"t\" in \"merhaba\"" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 in [1,2,3,4]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 in [1,2,3,4]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 in (1,2,3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### for Döngüsü\n", "\n", "for Döngüsü , listelerin ,demetlerin, stringlerin ve hatta sözlüklerin üzerinde dolaşmamızı sağlayan bir döngü türüdür. Yapısı şu şekildedir.\n", "\n", "\n", " for eleman in veri_yapısı(liste,demet vs):\n", " Yapılacak İşlemler\n", "Bu yapı bize şunu söyler;\n", "\n", " eleman değişkeni her döngünün başında listenin,demetin vs. her bir elemanına eşit olacak ve her döngüde \n", " bu elemanla işlem yapılacak.\n", "for döngüsünü daha iyi anlamak için örneklerimize bakalım." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### Listeler Üzerinde Gezinmek" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Eleman 1\n", "Eleman 2\n", "Eleman 3\n", "Eleman 4\n", "Eleman 5\n", "Eleman 6\n", "Eleman 7\n" ] } ], "source": [ "liste = [1,2,3,4,5,6,7]\n", "\n", "for eleman in liste:\n", " print(\"Eleman\",eleman)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Toplam 28\n" ] } ], "source": [ "# Liste elemanlarını toplama\n", "liste = [1,2,3,4,5,6,7]\n", "toplam = 0\n", "for eleman in liste:\n", " toplam += eleman\n", "print(\"Toplam\",toplam)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "4\n", "6\n", "8\n" ] } ], "source": [ "# Çift elemanları bastırma\n", "liste = [1,2,3,4,5,6,7,8,9]\n", "\n", "for eleman in liste:\n", " if eleman % 2 == 0:\n", " print(eleman)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Karakter Dizileri Üzerinde Gezinmek (Stringler)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "P\n", "y\n", "t\n", "h\n", "o\n", "n\n" ] } ], "source": [ "s = \"Python\"\n", "for karakter in s:\n", " print(karakter)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "PPP\n", "yyy\n", "ttt\n", "hhh\n", "ooo\n", "nnn\n" ] } ], "source": [ "# Her bir karakterleri 3 ile çarpma\n", "s = \"Python\"\n", "\n", "for karakter in s:\n", " print(karakter * 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Demetler üzerinde gezinmek (Demetler)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n" ] } ], "source": [ "# Listelerle aynı mantık\n", "demet = (1,2,3,4,5,6,7)\n", "\n", "for eleman in demet:\n", " print(eleman)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Demetlerin üzerinde for döngüsü uygularken aslında çok pratik bir yöntem bulunuyor. Aşağıdaki örneğe bakalım.\n", "\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 2)\n", "(3, 4)\n", "(5, 6)\n", "(7, 8)\n" ] } ], "source": [ "# Listelerin için 2 boyutlu demetler\n", "\n", "liste = [(1,2),(3,4),(5,6),(7,8)]\n", "\n", "for eleman in liste:\n", " print(eleman) # Herbir elemanın demet olduğu görebiliyoruz." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2\n", "3 4\n", "5 6\n", "7 8\n" ] } ], "source": [ "# Demet içindeki herbir elemanı almak için pratik yöntem\n", "liste = [(1,2),(3,4),(5,6),(7,8)]\n", "\n", "for (i,j) in liste:\n", " print(i,j)\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6\n", "120\n", "504\n", "1320\n" ] } ], "source": [ "# Demet içindeki elemanları çarpalım.\n", "liste = [(1,2,3),(4,5,6),(7,8,9),(10,11,12)]\n", "for (i,j,k) in liste:\n", " print(i * j * k)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sözlükler üzerinde gezinmek (Dictionary)\n", "\n", "Hatırlarsanız, sözlükler konusunda 3 adet metod görmüştük. (*keys(),values(),*items()*). İsterseniz bunları burada hatırlayalım.\n" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['bir', 'iki', 'üç', 'dört'])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sözlük = {\"bir\":1,\"iki\":2,\"üç\":3,\"dört\":4}\n", "\n", "sözlük.keys()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_values([1, 2, 3, 4])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sözlük.values()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_items([('bir', 1), ('iki', 2), ('üç', 3), ('dört', 4)])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sözlük.items()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python sonuçları *dict_keys,dict_values,dict_items* olarak vermesine rağmen , bunlar üzerinde liste veya demet üzerinde gezinir gibi *for* döngüsüyle gezinebiliriz." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bir\n", "iki\n", "üç\n", "dört\n" ] } ], "source": [ "# Metodları kullanmadan sözlük üzerinde gezinmek - Sadece anahtarları alabiliyoruz.\n", "sözlük = {\"bir\":1,\"iki\":2,\"üç\":3,\"dört\":4}\n", "\n", "for eleman in sözlük:\n", " print(eleman)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bir\n", "iki\n", "üç\n", "dört\n" ] } ], "source": [ "# keys() - Aynı şey\n", "sözlük = {\"bir\":1,\"iki\":2,\"üç\":3,\"dört\":4}\n", "\n", "for eleman in sözlük.keys():\n", " print(eleman)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "# values() \n", "sözlük = {\"bir\":1,\"iki\":2,\"üç\":3,\"dört\":4}\n", "\n", "for eleman in sözlük.values():\n", " print(eleman)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Anahtar: bir Değer: 1\n", "Anahtar: iki Değer: 2\n", "Anahtar: üç Değer: 3\n", "Anahtar: dört Değer: 4\n" ] } ], "source": [ "# items() \n", "sözlük = {\"bir\":1,\"iki\":2,\"üç\":3,\"dört\":4}\n", "\n", "for (i,j) in sözlük.items():\n", " print(\"Anahtar:\",i,\"Değer:\",j)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "İşte *for* döngüsü bu kadar! Bir sonraki konuda *while* döngülerini anlamaya çalışacağız.\n" ] }, { "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 }