{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lambda İfadeleri\n", "\n", "Bu konuda **lambda ifadelerini(expression)** öğrenmeye çalışacağız. **lambda ifadeleri** fonksiyonlarımızı oluşturmak için Pythonda bulunan pratik bir yöntemdir ve gerektiği yerlerde bu ifadeleri kullanabiliriz. Biliyorsunuz listelerimizi oluşturmak için **List Comprehension** yöntemini kullanabiliyorduk. İsterseniz **List Comprehension** yöntemini hatırlayalım.\n", "\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 4, 6, 8, 10]\n" ] } ], "source": [ "liste1 = [1,2,3,4,5] \n", "liste2 = list()\n", "for i in liste1: # Bu klasik liste oluşturma yöntemi\n", " liste2.append(i*2)\n", "print(liste2)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2, 4, 6, 8, 10]\n" ] } ], "source": [ "liste3 = [1,2,3,4,5]\n", "liste4 = [i * 2 for i in liste3] # List Comprehension\n", "print(liste4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Aynı buradaki gibi bir fonksiyonu da tek satır halinde **lambda ifadeleriyle** oluşturabiliriz. İlk önce yapısına bakalım sonra örneklerimize geçelim.\n", "\n", " etiket = lambda parametre1,parametre2.... : İşlem\n", " \n", "Bu yapıdan henüz bir şey anlamamış olabiliriz. İsterseniz örneklerimizle ***lambda ifadelerini*** anlamaya çalışalım. Bir tane iki ile çarpma görevini yerine getiren fonksiyon yazalım.\n", " " ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def ikiyleçarp(x): # Klasik fonksiyon tanımlama\n", " return x * 2\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "print(ikiyleçarp(2))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Şimdi de bu fonksiyonu lambda ifadelerini kullanarak tek satırda yazalım.\n", "\n", "ikiyleçarp = lambda x : x * 2 # x parametre x* 2 return ifadesi ve ikiyleçarp değeri de bir etikettir(değişken gibi düşünelim)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ikiyleçarp(3) # Buradaki 3 argümanı lambda ifadesindeki x'in yerine geçiyor." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def toplama(a,b,c):\n", " return a + b + c" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "12\n" ] } ], "source": [ "print(toplama(3,4,5))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "topla = lambda x,y,z : x + y + z" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "12\n" ] } ], "source": [ "print(topla(3,4,5))" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Stringi ters çevirme\n", "def terscevir(s):\n", " return s[::-1]\n" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "amalmargorP nohtyP\n" ] } ], "source": [ "print(terscevir(\"Python Programlama\"))" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": true }, "outputs": [], "source": [ "ters = lambda s : s[::-1]" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "amalmargorP nohtyP\n" ] } ], "source": [ "print(ters(\"Python Programlama\"))" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# çift mi \n", "\n", "def çiftmi(sayı):\n", " return ( sayı % 2 == 0 )\n", " " ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print(çiftmi(12))" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print(çiftmi(13))" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": true }, "outputs": [], "source": [ "çifttek = lambda sayı : sayı % 2 == 0 " ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "çifttek(34)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "çifttek(13)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "İşte lambda ifadesini bu şekilde küçük fonksiyonlar için kullanabiliriz. **lambda ifadelerini** özellikle kısa bir fonksiyonu **def** ifadesiyle yazmanın zahmetli olduğu zamanlarda kullanılabilir.\n" ] } ], "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 }