{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Demetler (Tuplelar)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Demetler veya İngilizce ismiyle tuplelar listelere oldukça benzer ancak farkları demetlerin değiştirilemez oluşudur. Bu yüzden programlarımızda değiştirilmesini istemediğimiz değerleri bir demet içinde depolayabiliriz. İsterseniz konumuza demetlerin oluşturulmasıyla başlayalım.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Demet Oluşturma" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Demet elemanları parantez içine alınarak demet oluşturulabilir.\n", "demet = (1,2,3,4,5,6,7,8,9)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, 3, 4, 5, 6, 7, 8, 9)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "demet" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# type() fonksiyonu yardımıyla türünü öğrenelim.\n", "type(demet)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Peki tek elemanlı bir demet nasıl tanımlanır ?" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Tek elemanlı demet bu şekilde tanımlanabilir.\n", "demet = (1,)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1,)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "demet" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(demet)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "demet = (1,2,3,4,5,6,7)\n", "# 0. indekse ulaşma\n", "demet[0]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 4. indekse ulaşma\n", "demet[4]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "demet[-1]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 4, 5, 6, 7)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "demet[2:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Demetlerin Temel Metodları\n", "\n", "**index** metoduyla içine verdiğimiz elemanın hangi indekste olduğunu bulabiliriz. \n", "\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Demeti oluşturalım.\n", "demet = (1,2,3,\"Mustafa\",\"Murat\",\"Coşkun\")\n", "# \"Mustafa\" elemanının indeksini buluyoruz.\n", "demet.index(\"Mustafa\")" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "demet.index(1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**count** metoduyla içine verdiğimiz değerin demette kaç defa geçtiğini bulabiliriz.\n" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "demet = (1,23,34,34,2,1,4,5,1,1,34)\n", "demet.count(1)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "demet.count(34)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Değiştirilmeme Özelliği\n", "\n", "Demetlerin değiştirilemez olduğunu artık biliyoruz. İsterseniz bir deneme yapalım.\n", "\n" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'tuple' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mdemet\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m\"Elma\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"Armut\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"Muz\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mdemet\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"Kiraz\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ], "source": [ "# Demet oluşturalım.\n", "\n", "demet = (\"Elma\",\"Armut\",\"Muz\")\n", "demet[0] = \"Kiraz\"" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "'tuple' object has no attribute 'remove'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mAttributeError\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[0mdemet\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Elma\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'remove'" ] } ], "source": [ "demet.remove(\"Elma\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Demetleri Ne Zaman Kullanalım ? \n", "\n", "Aslında Python programcıları demetlerden ziyade listeleri daha çok kullanır. Ancak eğer programınızda değiştirilmesini istemediğiniz bilgiler varsa (Android uygulama sabitleri gibi) bunları demet içinde depolayabilirsiniz. Aynı zamanda, Read Only(Sadece Okuma) bir veritipi olduğu için listelere göre biraz daha hızlı çalışırlar.\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 }