{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Modül Kullanımı - math Modülü\n", "\n", "Bu derste Pythonda modüller nasıl kullanılır öğrenmeye çalışacağız. Ayrıca bir modülü içeri aktarmanın değişik yöntemlerini göreceğiz. \n", "\n", "İsterseniz hazır bir modül olan **math** modülünü kullanmaya başlayalım." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Yöntem1 - import modül_adı\n", "\n", "Bir modülü içeri aktarmak yani programımıza dahil etmek için **import modül_adı** yazabiliriz. İsterseniz bunun için **math** modülünü içeri aktaralım." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import math # Modülü içeri aktarıyoruz. Artık bu modülün tüm fonksiyonlarını kullanabiliriz." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__doc__',\n", " '__loader__',\n", " '__name__',\n", " '__package__',\n", " '__spec__',\n", " 'acos',\n", " 'acosh',\n", " 'asin',\n", " 'asinh',\n", " 'atan',\n", " 'atan2',\n", " 'atanh',\n", " 'ceil',\n", " 'copysign',\n", " 'cos',\n", " 'cosh',\n", " 'degrees',\n", " 'e',\n", " 'erf',\n", " 'erfc',\n", " 'exp',\n", " 'expm1',\n", " 'fabs',\n", " 'factorial',\n", " 'floor',\n", " 'fmod',\n", " 'frexp',\n", " 'fsum',\n", " 'gamma',\n", " 'gcd',\n", " 'hypot',\n", " 'inf',\n", " 'isclose',\n", " 'isfinite',\n", " 'isinf',\n", " 'isnan',\n", " 'ldexp',\n", " 'lgamma',\n", " 'log',\n", " 'log10',\n", " 'log1p',\n", " 'log2',\n", " 'modf',\n", " 'nan',\n", " 'pi',\n", " 'pow',\n", " 'radians',\n", " 'sin',\n", " 'sinh',\n", " 'sqrt',\n", " 'tan',\n", " 'tanh',\n", " 'tau',\n", " 'trunc']" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(math) # Math modülünün içindekileri görmek için dir fonksiyonunu kullanabiliriz." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in module math:\n", "\n", "NAME\n", " math\n", "\n", "DESCRIPTION\n", " This module is always available. It provides access to the\n", " mathematical functions defined by the C standard.\n", "\n", "FUNCTIONS\n", " acos(...)\n", " acos(x)\n", " \n", " Return the arc cosine (measured in radians) of x.\n", " \n", " acosh(...)\n", " acosh(x)\n", " \n", " Return the inverse hyperbolic cosine of x.\n", " \n", " asin(...)\n", " asin(x)\n", " \n", " Return the arc sine (measured in radians) of x.\n", " \n", " asinh(...)\n", " asinh(x)\n", " \n", " Return the inverse hyperbolic sine of x.\n", " \n", " atan(...)\n", " atan(x)\n", " \n", " Return the arc tangent (measured in radians) of x.\n", " \n", " atan2(...)\n", " atan2(y, x)\n", " \n", " Return the arc tangent (measured in radians) of y/x.\n", " Unlike atan(y/x), the signs of both x and y are considered.\n", " \n", " atanh(...)\n", " atanh(x)\n", " \n", " Return the inverse hyperbolic tangent of x.\n", " \n", " ceil(...)\n", " ceil(x)\n", " \n", " Return the ceiling of x as an Integral.\n", " This is the smallest integer >= x.\n", " \n", " copysign(...)\n", " copysign(x, y)\n", " \n", " Return a float with the magnitude (absolute value) of x but the sign \n", " of y. On platforms that support signed zeros, copysign(1.0, -0.0) \n", " returns -1.0.\n", " \n", " cos(...)\n", " cos(x)\n", " \n", " Return the cosine of x (measured in radians).\n", " \n", " cosh(...)\n", " cosh(x)\n", " \n", " Return the hyperbolic cosine of x.\n", " \n", " degrees(...)\n", " degrees(x)\n", " \n", " Convert angle x from radians to degrees.\n", " \n", " erf(...)\n", " erf(x)\n", " \n", " Error function at x.\n", " \n", " erfc(...)\n", " erfc(x)\n", " \n", " Complementary error function at x.\n", " \n", " exp(...)\n", " exp(x)\n", " \n", " Return e raised to the power of x.\n", " \n", " expm1(...)\n", " expm1(x)\n", " \n", " Return exp(x)-1.\n", " This function avoids the loss of precision involved in the direct evaluation of exp(x)-1 for small x.\n", " \n", " fabs(...)\n", " fabs(x)\n", " \n", " Return the absolute value of the float x.\n", " \n", " factorial(...)\n", " factorial(x) -> Integral\n", " \n", " Find x!. Raise a ValueError if x is negative or non-integral.\n", " \n", " floor(...)\n", " floor(x)\n", " \n", " Return the floor of x as an Integral.\n", " This is the largest integer <= x.\n", " \n", " fmod(...)\n", " fmod(x, y)\n", " \n", " Return fmod(x, y), according to platform C. x % y may differ.\n", " \n", " frexp(...)\n", " frexp(x)\n", " \n", " Return the mantissa and exponent of x, as pair (m, e).\n", " m is a float and e is an int, such that x = m * 2.**e.\n", " If x is 0, m and e are both 0. Else 0.5 <= abs(m) < 1.0.\n", " \n", " fsum(...)\n", " fsum(iterable)\n", " \n", " Return an accurate floating point sum of values in the iterable.\n", " Assumes IEEE-754 floating point arithmetic.\n", " \n", " gamma(...)\n", " gamma(x)\n", " \n", " Gamma function at x.\n", " \n", " gcd(...)\n", " gcd(x, y) -> int\n", " greatest common divisor of x and y\n", " \n", " hypot(...)\n", " hypot(x, y)\n", " \n", " Return the Euclidean distance, sqrt(x*x + y*y).\n", " \n", " isclose(...)\n", " isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0) -> bool\n", " \n", " Determine whether two floating point numbers are close in value.\n", " \n", " rel_tol\n", " maximum difference for being considered \"close\", relative to the\n", " magnitude of the input values\n", " abs_tol\n", " maximum difference for being considered \"close\", regardless of the\n", " magnitude of the input values\n", " \n", " Return True if a is close in value to b, and False otherwise.\n", " \n", " For the values to be considered close, the difference between them\n", " must be smaller than at least one of the tolerances.\n", " \n", " -inf, inf and NaN behave similarly to the IEEE 754 Standard. That\n", " is, NaN is not close to anything, even itself. inf and -inf are\n", " only close to themselves.\n", " \n", " isfinite(...)\n", " isfinite(x) -> bool\n", " \n", " Return True if x is neither an infinity nor a NaN, and False otherwise.\n", " \n", " isinf(...)\n", " isinf(x) -> bool\n", " \n", " Return True if x is a positive or negative infinity, and False otherwise.\n", " \n", " isnan(...)\n", " isnan(x) -> bool\n", " \n", " Return True if x is a NaN (not a number), and False otherwise.\n", " \n", " ldexp(...)\n", " ldexp(x, i)\n", " \n", " Return x * (2**i).\n", " \n", " lgamma(...)\n", " lgamma(x)\n", " \n", " Natural logarithm of absolute value of Gamma function at x.\n", " \n", " log(...)\n", " log(x[, base])\n", " \n", " Return the logarithm of x to the given base.\n", " If the base not specified, returns the natural logarithm (base e) of x.\n", " \n", " log10(...)\n", " log10(x)\n", " \n", " Return the base 10 logarithm of x.\n", " \n", " log1p(...)\n", " log1p(x)\n", " \n", " Return the natural logarithm of 1+x (base e).\n", " The result is computed in a way which is accurate for x near zero.\n", " \n", " log2(...)\n", " log2(x)\n", " \n", " Return the base 2 logarithm of x.\n", " \n", " modf(...)\n", " modf(x)\n", " \n", " Return the fractional and integer parts of x. Both results carry the sign\n", " of x and are floats.\n", " \n", " pow(...)\n", " pow(x, y)\n", " \n", " Return x**y (x to the power of y).\n", " \n", " radians(...)\n", " radians(x)\n", " \n", " Convert angle x from degrees to radians.\n", " \n", " sin(...)\n", " sin(x)\n", " \n", " Return the sine of x (measured in radians).\n", " \n", " sinh(...)\n", " sinh(x)\n", " \n", " Return the hyperbolic sine of x.\n", " \n", " sqrt(...)\n", " sqrt(x)\n", " \n", " Return the square root of x.\n", " \n", " tan(...)\n", " tan(x)\n", " \n", " Return the tangent of x (measured in radians).\n", " \n", " tanh(...)\n", " tanh(x)\n", " \n", " Return the hyperbolic tangent of x.\n", " \n", " trunc(...)\n", " trunc(x:Real) -> Integral\n", " \n", " Truncates x to the nearest Integral toward 0. Uses the __trunc__ magic method.\n", "\n", "DATA\n", " e = 2.718281828459045\n", " inf = inf\n", " nan = nan\n", " pi = 3.141592653589793\n", " tau = 6.283185307179586\n", "\n", "FILE\n", " (built-in)\n", "\n", "\n" ] } ], "source": [ "help(math) # Fonksiyonların görevlerini görebilmek için help fonksiyonunu kullanabiliriz." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Peki bu içeri aktarma yöntemiyle **math** modülünün herhangi bir fonksiyonunu nasıl kullanacağız ? \n", "\n", "**----------------------------------------------------------------------------------------------------------------------------**\n", "\n", " modül_adı.fonksiyonadı()\n", "\n", "**----------------------------------------------------------------------------------------------------------------------------**\n", "\n", "Örneğin ilk olarak **math** modülünün içindeki **factorial** fonksiyonu ne iş yapıyor bakalım." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function factorial in module math:\n", "\n", "factorial(...)\n", " factorial(x) -> Integral\n", " \n", " Find x!. Raise a ValueError if x is negative or non-integral.\n", "\n" ] } ], "source": [ "help(math.factorial)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "120" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.factorial(5)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3628800" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.factorial(10)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function floor in module math:\n", "\n", "floor(...)\n", " floor(x)\n", " \n", " Return the floor of x as an Integral.\n", " This is the largest integer <= x.\n", "\n" ] } ], "source": [ "help(math.floor)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.floor(5.4)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.floor(3.5)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function ceil in module math:\n", "\n", "ceil(...)\n", " ceil(x)\n", " \n", " Return the ceiling of x as an Integral.\n", " This is the smallest integer >= x.\n", "\n" ] } ], "source": [ "help(math.ceil)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.ceil(5.4)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.ceil(3.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Peki biz bir modülü kendi belirlediğimiz isimle nasıl kullanıyoruz ? Bunun için de şu şekilde bir şey yapacağız." ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import math as matematik" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "720" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "matematik.factorial(6) # Modülü artık matematik ismiyle kullanabiliriz." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "matematik.factorial(0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Yöntem2 - from modül_adı import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bir modülü programımıza **dahil etmek** için bu yöntemi de kullanabiliriz. İsterseniz **math** modülünü bu yöntem içeri aktaralım." ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from math import * # Yıldızın anlamı math modülünün içindeki bütün fonksiyonları almak istediğimizi belirtiyor." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__doc__',\n", " '__loader__',\n", " '__name__',\n", " '__package__',\n", " '__spec__',\n", " 'acos',\n", " 'acosh',\n", " 'asin',\n", " 'asinh',\n", " 'atan',\n", " 'atan2',\n", " 'atanh',\n", " 'ceil',\n", " 'copysign',\n", " 'cos',\n", " 'cosh',\n", " 'degrees',\n", " 'e',\n", " 'erf',\n", " 'erfc',\n", " 'exp',\n", " 'expm1',\n", " 'fabs',\n", " 'factorial',\n", " 'floor',\n", " 'fmod',\n", " 'frexp',\n", " 'fsum',\n", " 'gamma',\n", " 'gcd',\n", " 'hypot',\n", " 'inf',\n", " 'isclose',\n", " 'isfinite',\n", " 'isinf',\n", " 'isnan',\n", " 'ldexp',\n", " 'lgamma',\n", " 'log',\n", " 'log10',\n", " 'log1p',\n", " 'log2',\n", " 'modf',\n", " 'nan',\n", " 'pi',\n", " 'pow',\n", " 'radians',\n", " 'sin',\n", " 'sinh',\n", " 'sqrt',\n", " 'tan',\n", " 'tanh',\n", " 'tau',\n", " 'trunc']" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(math)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Peki böyle bir durumda **math** modülünün içindeki fonksiyonları nasıl kullanacağız. Bunun için modül ismini yazmamıza gerek kalmamaktadır.\n", "\n", "**---------------------------------------------------------------------------------------------------------------------------**\n", " fonksiyon_adı()\n", "**---------------------------------------------------------------------------------------------------------------------------**" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "120" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "factorial(5)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "720" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "factorial(6)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "floor(4.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Peki bir modül içindeki fonksiyonların belli bir kısmını almak için ne yapacağız ? Bunun için hangi fonksiyonları alacağımızı özellikle belirtmemiz gerekiyor.\n", "\n", "**Öncelikle kernel kısmından programımı restart yapalım. Böylelikle Jupyter sıfırlanacaktır.**" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from math import factorial,floor # Sadece 2 tanesini dahil ettik." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "120" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "factorial(5)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "floor(3.5)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'ceil' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\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[0mceil\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3.4\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# Dahil etmediğimiz için hata verdi.\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mNameError\u001b[0m: name 'ceil' is not defined" ] } ], "source": [ "ceil(3.4) # Dahil etmediğimiz için hata verdi." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Peki bu yöntemlerin birbirinden farkı ne ? 2.yöntemi kullandığımızda bildiğimiz gibi sadece fonksiyon isimlerini kullanıyoruz. Ancak eğer programa birden fazla modül dahil edersek veya dahil ettiğimiz modülün içindeki fonksiyon kendi tanımladığımız fonksiyon ismiyle aynıysa Python son gördüğü fonksiyonu çalıştıracaktır.\n", "\n", "** Tekrar Kernel kısmından Jupyter'i sıfırlayalım. **" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from math import *" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def factorial(sayı):\n", " print(\"Kendi Factorial fonksiyonum.\")\n", " faktoriyel = 1\n", " if (sayı == 0 or sayı == 1):\n", " return 1\n", " while (sayı >= 1):\n", " faktoriyel *= sayı\n", " sayı -= 1\n", " return faktoriyel" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Kendi Factorial fonksiyonum.\n" ] }, { "data": { "text/plain": [ "120" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "factorial(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "İşte modülleri programımıza dahil etmek bu şekilde yapılabiliyor. Bir sonraki konuda kendi modüllerimizi nasıl yazacağımızı ve kullanacağımızı öğreneceğiz." ] }, { "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 }