{ "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.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Stringler" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Stringler sıralı metin tabanlı verilerdir ve tırnak içine alınarak ifade edilirler." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "String0 = 'Just a flesh wound.'\n", "String1 = \"Just a flesh wound.\"\n", "String2 = '''Just\n", "a\n", "flesh\n", "wound.'''" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Just a flesh wound. <type 'str'>\n", "Just a flesh wound. <type 'str'>\n", "Just\n", "a\n", "flesh\n", "wound. <type 'str'>\n" ] } ], "source": [ "print String0 , type(String0)\n", "print String1, type(String1)\n", "print String2, type(String2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Stringlerin indislenmesi (indexing) ve kesilmesi(slicing) daha önceden listelerde blelirtildiği gibidir." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \n", " a flesh wound.\n" ] } ], "source": [ "print String0[4]\n", "print String0[4:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Gömülü fonksiyonlar" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**find( )** fonksiyonu verilen verinin bulunduğu indis değerini döndürür. Eğer bulunamazsa **-1** döndürür. Döndürülen değeri ters indisleme değeriyle karıştırmamaya dikkat edin." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7\n", "-1\n" ] } ], "source": [ "print String0.find('fl')\n", "print String0.find('Ni')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dönen indis değeri girilen verinin ilk elemanıdır." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "f\n" ] } ], "source": [ "print String0[7]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**find( )** fonksiyonu aynı zamanda indis değerleri arasında arama yapmak için de kullanılabilir." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "-1\n" ] } ], "source": [ "print String0.find('t',1)\n", "print String0.find('t',1,3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**capitalize( )** stringin ilk elemanını büyük harfe çevirmek için kullanılır." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Bu cümledeki ilk harfe dikkat edin.\n" ] } ], "source": [ "String3 = 'bu cümledeki ilk harfe dikkat edin.'\n", "print String3.capitalize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**center( )** bir alan genişliği(field width) belirterek stringi ortalamak için kullanılır." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "' Just a flesh wound. '" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "String0.center(70)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Boşluklar başka karakterle de doldurulabilir." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'-------------------------Just a flesh wound.--------------------------'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "String0.center(70,'-')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**zfill( )** bir alan genişliği belirterek sıfır ile tamamlamak için kullanılır." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'00000000000Just a flesh wound.'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "String0.zfill(30)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**expandtabs( )** tab karakterinin boşluğunu değiştirme imkanı sağlar. '\\t' için varsayılan değer 8 boşluktur." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "h\te\tl\tl\to\n", "h e l l o\n", "h e l l o\n" ] } ], "source": [ "s = 'h\\te\\tl\\tl\\to'\n", "print s\n", "print s.expandtabs(1)\n", "print s.expandtabs()" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "**index( )**, **find( )** fonksiyonu gibi çalışır. Farkı **find( )** fonksiyonunun -1 döndürmesi ama **index( )** fonksiyonunun ValueError fırlatmasıdır." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "5\n" ] }, { "ename": "ValueError", "evalue": "substring not found", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-17-1b7765e2f54f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Just'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'a'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'flesh'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: substring not found" ] } ], "source": [ "print String0.index('Just')\n", "print String0.index('a',0)\n", "print String0.index('flesh',10,20)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "**endswith( )** fonksiyonu bir stringin diğer bir string ile bitip bitmediğine bakmak için kullanılır." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print String0.endswith('d')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Başlangış ve bitiş indisleri de beirtilebilir." ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "True\n" ] } ], "source": [ "print String0.endswith('.',0)\n", "print String0.endswith('t',0,4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**count( )** fonksiyonu verilen stringdeki karakterleri sayar. Başlangıç ve bitiş indisleri girilebilir ya da boş bırakılabilir." ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "0\n" ] } ], "source": [ "print String0.count('u',0)\n", "print String0.count('u',5,10)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "**join( )** fonksiyonu verilen girdinin elemanları arasına bir karakter koymak için kullanılır." ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'*a_a-'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'a'.join('*_-')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "'*_-' girdi stringi dir. 'a' karakteri bütün karakterler arasına koyulmuştur." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**join( )** aynı zamanda bir listeyi stringe dönüştürmek için de kullanılaiblir." ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['J', 'u', 's', 't', ' ', 'a', ' ', 'f', 'l', 'e', 's', 'h', ' ', 'w', 'o', 'u', 'n', 'd', '.']\n", "Just a flesh wound.\n" ] } ], "source": [ "a = list(String0)\n", "print a\n", "b = ''.join(a)\n", "print b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Stringe dönüştürmeden önce **join( )** fonksiyonu liste elemanlarının arasına herhangi bir karakter koymak için kullanılabilir." ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "e/s/h/ /w/o/u/n/d/.\n" ] } ], "source": [ "c = '/'.join(a)[18:]\n", "print c" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "**split( )** fonksiyonu stringi tekrar listeye çevirmek için kullanılır. **join()** fonksiyonunun tersidir." ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['e', 's', 'h', ' ', 'w', 'o', 'u', 'n', 'd', '.']\n" ] } ], "source": [ "d = c.split('/')\n", "print d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**split( )** fonksiyonunda aynı zamanda listenin kaç defa bölüneceği da belirtilebilir. Listenin eleman sayısı belirtilen sayının bir fazlasıdır. Çünkü sayı bölünme sayısını belirtir." ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['e', 's', 'h', ' /w/o/u/n/d/.']\n", "4\n" ] } ], "source": [ "e = c.split('/',3)\n", "print e\n", "print len(e)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**lower( )** büyük harfleri küçük harfe dönüştürür." ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Just a flesh wound.\n", "just a flesh wound.\n" ] } ], "source": [ "print String0\n", "print String0.lower()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**upper( )** küçük harfleri büyük harfe dönüştürür." ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'JUST A FLESH WOUND.'" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "String0.upper()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**replace( )** fonksiyonu bir elemanı diğeriyle değiştirir." ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'Yet another a flesh wound.'" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "String0.replace('Just','Yet another')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**strip( )** sağ ve sol uçtan gereksiz elemanları silmek için kullanılır." ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": true }, "outputs": [], "source": [ "f = ' merhaba '" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Eğer bir karakter belirtilmemişse sağ ve soldaki boşlukları siler." ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'merhaba'" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.strip()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**strip( )** fonksiyonu eğer bir karakter belirtilmişse, baştan ve sonran o karakterleri siler." ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": true }, "outputs": [], "source": [ "f = ' ***----merhaba---******* '" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "' ***----merhaba---******* '" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f.strip('*')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Yıldızın silinmesi gerekiyor gibiydi ama silinmedi. Çünkü sağında ve slunda boşluklar yer almakta. strip fonksiyonunda karakterler sunulan sırada verilmelidir." ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "----merhaba---\n", "merhaba\n" ] } ], "source": [ "print f.strip(' *')\n", "print f.strip(' *-')" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "**lstrip( )** ve **rstrip( )** strip ile aynı özelliğe sahiptir. tek farkı **lstrip( )** sadece sola doğru**rstrip( )** ise sadece sağa doğru siler." ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "----merhaba---******* \n", " ***----merhaba---\n" ] } ], "source": [ "print f.lstrip(' *')\n", "print f.rstrip(' *')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sözlükler?? (Dictionaries)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dictionlaryler daha çok veri tabanı gibi kullanılır çünkü berilen bir şey önceden belirlenmiş bir string(ya da başka bir değer) kullanılarak indislenebilir." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bir dictionary tanımlamak için bir değişkeni { } ya da dict()'a eşitleyin." ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "<type 'dict'> <type 'dict'>\n" ] } ], "source": [ "d0 = {}\n", "d1 = dict()\n", "print type(d0), type(d1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dictionary liste gibi çalışır fakat kendi indisleme tarzı vardır." ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Bir': 1, 'BirIki': 12, 'OneTwo': 12, 'One': 1}\n" ] } ], "source": [ "d0['Bir'] = 1\n", "d0['BirIki'] = 12 \n", "print d0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Böylece 'Bir' kullanarak 1 değerine erişebiliriz." ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "print d0['Bir']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "İlgili iki liste bir dictionary oluşturmak için birleştirilebilir." ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": false }, "outputs": [], "source": [ "names = ['Bir', 'Iki', 'Uc', 'Dort', 'Bes']\n", "numbers = [1, 2, 3, 4, 5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**zip( )** fonksiyonu iki listeyi birleştirmek için kullanılır." ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[('Bir', 1), ('Iki', 2), ('Uc', 3), ('Dort', 4), ('Bes', 5)]\n" ] } ], "source": [ "d2 = zip(names,numbers)\n", "print d2" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "İki liste birleştirilmiştir ve her eleman eşleşen diğer listeden eşleşen elemanlardan bir tuple şeklindedir. Tuple kullanılmasının sebebi atanan değerlerin değiştirilemez oluşudur.\n", "\n", "Daha sonra yukarıdakini dictionary'e çevirmek için **dict( )** fonksiyonu kullanılır." ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Dort': 4, 'Iki': 2, 'Bir': 1, 'Uc': 3, 'Bes': 5}\n" ] } ], "source": [ "a1 = dict(d2)\n", "print a1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Gömülü fonksiyonlar" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**clear( )** fonksiyonu yaratılan veri tabanını siler." ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{}\n" ] } ], "source": [ "a1.clear()\n", "print a1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dictionary föngü kullanılarak da oluşturulabilir." ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Dort': 4, 'Iki': 2, 'Bir': 1, 'Uc': 3, 'Bes': 5}\n" ] } ], "source": [ "for i in range(len(names)):\n", " a1[names[i]] = numbers[i]\n", "print a1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**values( )** fonksiyonu dictionary içindeki bütün değerleri içerek bir liste döndürür." ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[4, 2, 1, 3, 5]" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a1.values()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**keys( )** fonksiyonu bütün indisleri ya da anahtarları (key) içeren bir liste döndürür." ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['Dort', 'Iki', 'Bir', 'Uc', 'Bes']" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a1.keys()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**items( )** dictionarydeki her elemanı bir tuple şeklinde içeren bir liste döndürür. bu zip fonksiyonu kullanıldıktan sonraki şekli döndürür." ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[('Dort', 4), ('Iki', 2), ('Bir', 1), ('Uc', 3), ('Bes', 5)]" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a1.items()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**pop( )** fonksiyonu belirli bir elemanı çıkartmak için kullanılabilir ve çıkartılan değer bir değişkene atanabilir. fakat sadece değer(value) saklanır." ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'Iki': 2, 'Bir': 1, 'Uc': 3, 'Bes': 5}\n", "4\n" ] } ], "source": [ "a2 = a1.pop('Dort')\n", "print a1\n", "print a2" ] } ], "metadata": { "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 }