{ "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", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Print ifadesi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**print** ifadesi aşşağıdaki şekillerde kullanılabilir :\n", "\n", " - print \"Merhaba Dünya\"\n", " - print \"Merhaba\", <String içeren değişken>\n", " - print \"Merhaba\" + <String içeren değişken>\n", " - print \"Merhaba %s\" % <String içeren değişken>" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello World\n" ] } ], "source": [ "print \"Merhaba Dünya\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python'da tekli çiftli ve üçlü tırnaklar bir string belirtmek için kullanılır.\n", "Tek bir karakteri belirtmek için çoğu zaman tek tırnak kullanılır.\n", "Çift tırnaklar bir satırı belirtmek için,\n", "Üç tırnak ise paragraf ya da çok sayıda satırı belirtmek için kullanılır." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hey\n" ] } ], "source": [ "print 'Hey'" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My name is Rajath Kumar M.P.\n", "\n", "I love Python.\n" ] } ], "source": [ "print \"\"\"My name is Rajath Kumar M.P.\n", "\n", "I love Python.\"\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Stringler _string1_, _string2_ gibi değişkenlere atanıp print ile çağırılabilir." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Merhaba Dünya\n", "Merhaba Dünya !\n" ] } ], "source": [ "string1 = 'Dünya'\n", "print 'Merhaba', string1\n", "\n", "string2 = '!'\n", "print 'Merhaba', string1, string2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "String birleştirme(concatenation) iki stringin toplamıdır. Birleştirirken arada boşluk olmadığına dikkat edin." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "MerhabaDünya!\n" ] } ], "source": [ "print 'Merhaba' + string1 + string2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**%s** string içeren bir değişkeni belirtmek için kullanılır." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Merhaba Dünya\n" ] } ], "source": [ "print \"Merhaba %s\" % string1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Benzer şekilde diğer veri türleri\n", "\n", " - %s -> string \n", " - %d -> Integer (tamsayı)\n", " - %f -> Float (ondalık sayı)\n", " - %o -> Octal (sekizli)\n", " - %x -> Hexadecimal (onaltılı)\n", " - %e -> exponential (üstel)\n", " \n", "Bu şekilde print ifadelerinin ve stringlerin içine yerleştirilebilirler." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sayının kendisi = 42\n", "Sayının ondalığı = 42.000000\n", "Sayının sekizli karşılığı = 52\n", "Sayının On altılı karşılığı = 12\n", "Sayının üstel karşılığı = 1.800000e+01\n" ] } ], "source": [ "print \"Sayının kendisi = %d\" % 42\n", "print \"Sayının ondalığı = %f\" % 42\n", "print \"Sayının sekizli karşılığı = %o\" % 42\n", "print \"Sayının On altılı karşılığı = %x\" %18\n", "print \"Sayının üstel karşılığı = %e\" %18" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Birden fazla değişkeni ifade ederken parantez kullanılır." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Merhaba Dünya !\n" ] } ], "source": [ "print \"Merhaba %s %s\" %(string1,string2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Diğer Örnekler" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Örneklerde print ifadesinin diğer örnek kullanımları yer almaktadır." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "buraya %d gelsin istiyorum\n" ] } ], "source": [ "print \"%s %%d gelsin istiyorum\" %'Buraya'" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "_A_A_A_A_A_A_A_A_A_A\n" ] } ], "source": [ "print '_A'*10" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Jan\n", "Feb\n", "Mar\n", "Apr\n", "May\n", "Jun\n", "Jul\n", "Aug\n" ] } ], "source": [ "print \"Jan\\nFeb\\nMar\\nApr\\nMay\\nJun\\nJul\\nAug\"" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\\n yazdırmak istiyorum .\n" ] } ], "source": [ "print \"\\\\n yazdırmak istiyorum .\"" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Rutin:\n", "\t- Ye\n", "\t- İç\n", "\t- Tekrarla\n", "\n" ] } ], "source": [ "print \"\"\"\n", "Rutin:\n", "\\t- Ye\n", "\\t- İç\\n\\t- Tekrarla\n", "\"\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# PrecisionWidth ve FieldWidth (Hassasiyet)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Fieldwidth bütün sayının genişliğidir. Precision ise sağa doğru olan genişliktir. Bu değerler gereksinimlere göre ayarlanabilir.\n", "\n", "Precision Width için varsayılan değer 6 dır." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'3.121312'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"%f\" % 3.121312312312" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "6 tane ondalık basamak dikkate alınmıştır. Bir ondalık sayıyı ifade etmek için '%(fieldwidth).(precisionwidth)f' kullanılaibilir." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'3.12131'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"%.5f\" % 3.121312312312" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Eğer genişlik (field width) gerekenden fazla verişmişse verilen değerlere uymak için kendini hizalar." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "' 3.12131'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"%9.5f\" % 3.121312312312" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "0 ile tamamlama genişlik değerinin başına 0 koyarak yapılır." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'00000000000003.12131'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"%020.5f\" % 3.121312312312" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Düzgün hizalama için bir boşluk bırakılabilir. BÖylece eğer negatif bir sayı kullanılrsa hiza bozulmaz." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 3.121312\n", "-3.121312\n" ] } ], "source": [ "print \"% 9f\" % 3.121312312312\n", "print \"% 9f\" % -3.121312312312" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "sayının pozitif olduğunu özellikle belirtmek için başına '+' işareti eklenebilir." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "+3.121312\n", "-3.121312\n" ] } ], "source": [ "print \"%+9f\" % 3.121312312312\n", "print \"%+9f\" % -3.121312312312" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Yukarıda belirtildiği gibi veri kendini genişliğe göre ayarlar. Negatif bir sayı kullanarak boşlukların sonda olması sağlanabilir." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'3.121 '" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"%-9.3f\" % 3.121312312312" ] } ], "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 }