{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Bu dersteki bütün IPython Notebook'lar adresinde https://github.com/sinanonur/Python-Lectures mevcuttur ve [bu adres](https://github.com/rajathkumarmp/Python-Lectures)teki içerik Türkçe'ye çevirilerek hazırlanmıştır. Bazı kısımlarda değişiklik yapılmıştır.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Akış Kontrol ifadeleri" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## If" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "if koşul:\n", " \n", " algoritma" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "merhaba\n" ] } ], "source": [ "x = 12\n", "if x >10:\n", " print \"merhaba\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## If-else" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "if bir_koşul:\n", " \n", " algoritma\n", " \n", "else:\n", " \n", " algoritma" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "merhaba\n" ] } ], "source": [ "x = 12\n", "if x > 10:\n", " print \"merhaba\"\n", "else:\n", " print \"dünya\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## if-elif" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "if bir_koşul:\n", " \n", " algoritma\n", " \n", "elif bir_koşul:\n", "\n", " algoritma\n", " \n", "else:\n", " \n", " algoritma" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x<y\n" ] } ], "source": [ "x = 10\n", "y = 12\n", "if x > y:\n", " print \"x>y\"\n", "elif x < y:\n", " print \"x<y\"\n", "else:\n", " print \"x=y\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bir if ifadesinin içinde başka bir if ifadesi yer alıyorsa buna iç-içe girmiş if (nested if)ifadesi denir." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x<y\n", "x=10\n" ] } ], "source": [ "x = 10\n", "y = 12\n", "if x > y:\n", " print \"x>y\"\n", "elif x < y:\n", " print \"x<y\"\n", " if x==10:\n", " print \"x=10\"\n", " else:\n", " print \"gecersiz\"\n", "else:\n", " print \"x=y\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Döngüler (Loop)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### For" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "for değişken in bir_şeyler:\n", " \n", " algoritma" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for i in range(5):\n", " print i" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Yukarıdaki örnekte i 0,1,2,3,4 üzerinden tekrarlanır. Her değeri aldığında döngünün içerisindeki algoritmayı çalıştırır. İç içe geçmiş (nested) listelerin üzerinden değer almak da aşşağıdaki gibi mümkündür." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3]\n", "[4, 5, 6]\n", "[7, 8, 9]\n" ] } ], "source": [ "list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n", "for list1 in list_of_lists:\n", " print list1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "İç içe girmiş for döngüsü için örnek bir kullanım aşşağıdaki gibi olabilir." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n", "for list1 in list_of_lists:\n", " for x in list1:\n", " print x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### While" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "while koşul:\n", " \n", " algoritma" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "4\n", "Bye\n" ] } ], "source": [ "i = 1\n", "while i < 3:\n", " print(i ** 2)\n", " i = i+1\n", "print('Bye')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bir döngüyü çalıştırırken döngüden çıkmaya yarar." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n" ] } ], "source": [ "for i in range(100):\n", " print i\n", " if i>=7:\n", " break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Burada if koşulu sağlandığında break çalışarak döngüyü sonlandırmıştır." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Continue" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Döngünün geri kalanını çalıştırmaya devam eder. O adımın geri kalan kısmı çalıştırılmaz.\n", "\n", "Bazen bir koşul sağlandığında döngünün sonlanma ihtimali vardır. bu continue kullanılarak önlenebilir." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-0.2\n", "-0.25\n", "-0.333333333333\n", "-0.5\n", "-1.0\n" ] }, { "ename": "ZeroDivisionError", "evalue": "float division by zero", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-17-cf5579d0f432>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0;36m1.0\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m: float division by zero" ] } ], "source": [ "for i in range(10):\n", " print 1.0/(i-5)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-0.2\n", "-0.25\n", "-0.333333333333\n", "-0.5\n", "-1.0\n", "1.0\n", "0.5\n", "0.333333333333\n", "0.25\n" ] } ], "source": [ "for i in range(10):\n", " if(i == 5):\n", " continue\n", " print 1.0/(i-5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Liste oluşturma (List comprehensions)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pyhon gerekli bir listeyi oluşturmayı list comprehensionlar kullanarak tek satırda çok kolay hale getirmiştir. Örneğin 27nin katlarına ihtiyacımız olduğunda aşşağıdaki kodu yazabiliriz." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]\n" ] } ], "source": [ "res = []\n", "for i in range(1,11):\n", " x = 27*i\n", " res.append(x)\n", "print res" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Yepyeni bir liste oluşturduğumuz için list comprehension daha etkili bir yöntemdir." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[27*x for x in range(1,11)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bu kadar! Sadece köşeli parantezi kapatmayı unutmayın." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Kodu anlamak için, kodun ilk kısmı her zaman çalışan algoritmadır. sonra bir boşluk bırakılarak gerekli döngü yazılır.\n", "\n", "Peki iç içe geçmiş listelerle list comprehention kullanabilir miyiz? Tabii ki evet." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[135, 270, 405]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[27*x for x in range(1,20) if x%5==0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bir döngü daha ekleyelim:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[27*z for i in range(50) if i==27 for z in range(1,11)]" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [default]", "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": 0 }