{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Matematik Operatörleri\n", "Tekrardan hoşgeldiniz.\n", "\n", "Python'da ve diğer programlama dillerinde matematik operatörleri oldukça önemlidir. Bu konumuzda, Pythondaki matematik operatörlerini öğreneceğiz. \n", "\n", "### Toplama İşlemi (+)\n", "\n", "Python'da toplama işlemi şu şekilde yapılabilmektedir.\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "29" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 14\n", "b = 15\n", "a + b" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7.9" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i = 3.1\n", "j = 4.8\n", "i+j" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Çıkarma İşlemi (-)\n", "\n", "Python'da çıkarma işlemi şu şekilde yapılabilmektedir." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-47" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 28\n", "b = 35\n", "c = 40\n", "a-b-c" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-6" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = -1\n", "y = 5\n", "t-y" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-2.6999999999999997" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "k = 3.1\n", "l = 5.8\n", "k -l " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Çarpma İşlemi (*)\n", "Python'da çarpma işlemi şu şekilde yapılabilmektedir.\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 4\n", "b = 5\n", "a *b" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "14.13" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "i = 3.14\n", "j = 4.5\n", "i *j" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "60" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 3\n", "b = 4\n", "c = 5\n", "a *b *c" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9.42" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 3 \n", "b = 3.14\n", "a *b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Bölme İşlemi (/)\n", "Python'da bölme işlemi şu şekilde yapılabilmektedir." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 / 2" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.3333333333333335" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "10 / 3" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.142857142857143" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "22 / 7" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Şimdi, burada matematikten farklı olarak bir şey gerçekleşti. **\"4 / 2\"** işleminin sonucunun **\"2\"** olarak çıkması gerekirken, sonuç **\"2.0\"** olarak, yani ondalıklı olarak(float) çıktı. Bu özellik, Python 3'e işlem sonucunun daha kesin bir şekilde sağlanması için konulmuş bir özellik. Bunları ilerde çok daha iyi bir şekilde anlayacağız.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Peki bir sayının başka bir sayıya bölümünden çıkan bölüm sonucunu nasıl bulacağız ? Bunun için de Python3'te şu şekilde bir operatör mevcut.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tamsayı Bölmesi (//)\n", "Bu operatör, bir sayının başka bir sayıya bölümünden ortaya çıkan bölüm sonucunu vermektedir.Örneklerimize bakalım.\n" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 // 2" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 13'ün 4 ile bölümünden kalan bölüm sonucu 3tür.\n", "13 // 4" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "22 // 7" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "40 // 7" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Kalanı Bulma (%)\n", "\n", "Bu operatör de , bir sayının başka bir sayıya bölümünden kalan sonucunu bulmamızı sağlar." ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 13'ün 4 ile bölümünden kalan 1'dir.\n", "13 % 4" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "14 % 2" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "108" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "330 % 111" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Üs bulma (**)\n", "\n", "Bu operatör bir sayının üssünü bulmamızı sağlar. Örnek olarak operatörün solundaki sayının sağdaki sayıya göre üssünü ekrana basar.\n" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "64" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 4^3\n", "4 ** 3" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "16" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 ** 4" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "30.959144000000002" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3.14 ** 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Peki bir sayının karekökünü nasıl bulacağız ? Matematikten bildiğimiz gibi, bir sayının karekökü , o sayının 1/2 (0.5). üssüdür. O zaman örneğimizle 64 sayısının karekökünü bulmaya çalışalım.\n" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8.0" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 64'ün karekökü \n", "64 ** 0.5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### İşaret değiştirme (-)\n", "Programlarımızda bazen bir sayının işaretini değiştirmek isteyebiliriz.Bunun için de bu operatör işimize yarayabilir.\n", "\n" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-4" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 4\n", "-a" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "13" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = -13\n", "-b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Operatörleri beraber kullanma ve İşlem sırası\n", "\n", "Bütün bu öğrendiğimiz şeyleri beraber nasıl kullanabiliriz? Bunun için matematikteki işlem sırasını biliyorsak çok rahat edeceğiz, çünkü Pythondaki işlem sırası matematiktekiyle birebir aynıdır. Nedir bu işlem sırası ? Kurallar şunlar ;\n", "\n", " 1. Parantez içi her zaman önce yapılır.\n", " 2. Çarpma ve Bölme her zaman Toplama ve Çıkarma işlemine göre önce yapılır.\n", " 3. İşlemler soldan sağa değerlendirilir.\n", " \n", "Ancak, buradaki işlemleri ezberlememize hiç gerek yok. Kafamızın karıştığı yerler de işlemleri parantez içine almak, hayat kurtarır :) Şimdi örneklere bakalım." ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-4.0" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "8 + 4 * 3 /2 - 18" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-4.0" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Kafamız karışıyorsa paranteze alalım.\n", "\n", "8 + ((4 * 3) /2) - 18" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "İşte matematik operatörleri tamamıyla bu şekilde !\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Şimdi de diğer bir veri tipimiz olan Karakter Dizilerini (**string**) öğrenelim." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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 }