{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Funksiyalar\n", "\n", "* ushbu darsda chuquroq funksiyalarni o'rganamiz\n", "* funksiya nima\n", "* sodda namunalar" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def my_function():\n", " print(\"Hello from a function\")\n", " print(\"Salom olam\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello from a function\n", "Salom olam\n" ] } ], "source": [ "my_function()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Argumentlar" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Emil Refsnes\n", "Tobias Refsnes\n", "Linus Refsnes\n" ] } ], "source": [ "def my_function(fname):\n", " print(fname + \" Refsnes\")\n", "\n", "my_function(\"Emil\")\n", "my_function(\"Tobias\")\n", "my_function(\"Linus\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Emil Refsnes\n" ] } ], "source": [ "def my_function(fname, lname):\n", " print(fname + \" \" + lname)\n", "\n", "my_function(\"Emil\", \"Refsnes\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sum: 14\n" ] } ], "source": [ "# function with two arguments\n", "def add_numbers(num1, num2, num3):\n", " sum = num1 + num2 + num3\n", " print(\"Sum: \", sum)\n", "\n", "# function call with two values\n", "add_numbers(5, 4, 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `return` kalit so'zi" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Square: 9\n" ] } ], "source": [ "# function definition\n", "def find_square(num):\n", " result = num * num\n", " return result\n", "\n", "# function call\n", "square = find_square(3)\n", "\n", "print('Square:', square)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False True\n" ] } ], "source": [ "# some more functions\n", "def is_prime(n):\n", " if n in [2, 3]:\n", " return True\n", " if (n == 1) or (n % 2 == 0):\n", " return False\n", " r = 3\n", " while r * r <= n:\n", " if n % r == 0:\n", " return False\n", " r += 2\n", " return True\n", "print(is_prime(78), is_prime(79))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bittadan ortiq qiymat qaytarish" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Yuza: 12\n", "Peremeter: 14\n" ] } ], "source": [ "# function definition\n", "def find_area_per(a, b):\n", " area = a * b\n", " p = 2 * (a + b)\n", " return (area, p)\n", "\n", "# function call\n", "yuza, peremeter = find_area_per(3, 4)\n", "\n", "print('Yuza: ', yuza)\n", "print('Peremeter: ', peremeter)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `pass` kalit so'zi" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "def future_function():\n", " pass\n", "\n", "# this will execute without any action or error\n", "future_function() " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Kelishuv qiymatlari" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x: 10\n", "y: 50\n", "x: 10\n", "y: 100\n" ] } ], "source": [ "# Python program to demonstrate\n", "# default arguments\n", "def myFun(x, y=50):\n", " print(\"x: \", x)\n", " print(\"y: \", y)\n", "\n", "\n", "# Driver code (We call myFun() with only\n", "# argument)\n", "myFun(10)\n", "myFun(10, 100)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "I am from Sweden\n", "I am from India\n", "I am from Norway\n", "I am from Brazil\n" ] } ], "source": [ "def my_function(country = \"Norway\"):\n", " print(\"I am from \" + country)\n", "\n", "my_function(\"Sweden\")\n", "my_function(\"India\")\n", "my_function()\n", "my_function(\"Brazil\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Kalit so'zli qiymatlar" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Geeks Practice\n", "Geeks Practice\n" ] } ], "source": [ "# Python program to demonstrate Keyword Arguments\n", "def student(firstname, lastname):\n", " print(firstname, lastname)\n", "\n", "\n", "# Keyword arguments\n", "student(firstname='Geeks', lastname='Practice')\n", "student(lastname='Practice', firstname='Geeks')\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Majburiy qiymatlar" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Case-1:\n", "Hi, I am Suraj\n", "My age is 27\n", "\n", "Case-2:\n", "Hi, I am 27\n", "My age is Suraj\n" ] } ], "source": [ "def nameAge(name, age):\n", " print(\"Hi, I am\", name)\n", " print(\"My age is \", age)\n", "\n", "\n", "# You will get correct output because \n", "# argument is given in order\n", "print(\"Case-1:\")\n", "nameAge(\"Suraj\", 27)\n", "# You will get incorrect output because\n", "# argument is not in order\n", "print(\"\\nCase-2:\")\n", "nameAge(27, \"Suraj\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `*args` argumenti" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "The youngest child is Linus\n" ] } ], "source": [ "def my_function(*kids):\n", " print(type(kids)) # tuple\n", " # kids = (\"Emil\", \"Tobias\", \"Linus\")\n", " print(\"The youngest child is \" + kids[2])\n", "\n", "my_function(\"Emil\", \"Tobias\", \"Linus\")" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello\n", "Welcome\n", "to\n", "GeeksforGeeks\n", "sd\n", "43\n", "43\n" ] } ], "source": [ "# Python program to illustrate\n", "# *args for variable number of arguments\n", "def myFun(*argv):\n", " for arg in argv:\n", " print(arg)\n", "\n", "\n", "myFun('Hello', 'Welcome', 'to', 'GeeksforGeeks', 'sd', 43, 43)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "3\n", "6\n", "10\n" ] } ], "source": [ "def my_sum(*values):\n", " s = 0\n", " for val in values:\n", " s += val\n", " return s\n", "\n", "a = 1\n", "b = 2\n", "c = 3\n", "d = 4\n", "\n", "print(my_sum(a))\n", "print(my_sum(a, b))\n", "print(my_sum(a, b, c))\n", "print(my_sum(a, b, c, d))" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "first Geeks\n", "mid for\n", "last Geeks\n", "age 25\n" ] } ], "source": [ "# Python program to illustrate\n", "# *kwargs for variable number of keyword arguments\n", "\n", "\n", "def myFun(**kwargs):\n", " for key, value in kwargs.items():\n", " print(key, value)\n", "\n", "\n", "# Driver code\n", "myFun(first='Geeks', mid='for', last='Geeks', age=25)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Hujjat qatori" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Function to check if the number is even or odd\n", " \n", " x bu yerda butun son\n", " \n" ] } ], "source": [ "# A simple Python function to check\n", "# whether x is even or odd\n", "# docstr\n", "\n", "def evenOdd(x):\n", " \"\"\"Function to check if the number is even or odd\n", " \n", " x bu yerda butun son\n", " \"\"\"\n", " \n", " if (x % 2 == 0):\n", " print(\"even\")\n", " else:\n", " print(\"odd\")\n", "\n", "\n", "# Driver code to call the function\n", "print(evenOdd.__doc__)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "evenOdd()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Kalit so'zli argumentlar" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The youngest child is Linus\n" ] } ], "source": [ "def my_function(child3, child2, child1):\n", " print(\"The youngest child is \" + child3)\n", "\n", "my_function(child1 = \"Emil\", child2 = \"Tobias\", child3 = \"Linus\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `**kwargs` argumenti" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "His last name is Refsnes\n" ] } ], "source": [ "def my_function(**kid):\n", " print(\"His last name is \" + kid[\"lname\"])\n", "\n", "my_function(fname = \"Tobias\", lname = \"Refsnes\")" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "my_function() got some positional-only arguments passed as keyword arguments: 'a'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[46], line 4\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mmy_function\u001b[39m(a\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m2\u001b[39m, b\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m/\u001b[39m, c\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m5\u001b[39m):\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(a, b, c)\n\u001b[0;32m----> 4\u001b[0m \u001b[43mmy_function\u001b[49m\u001b[43m(\u001b[49m\u001b[43ma\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mc\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m5\u001b[39;49m\u001b[43m)\u001b[49m\n", "\u001b[0;31mTypeError\u001b[0m: my_function() got some positional-only arguments passed as keyword arguments: 'a'" ] } ], "source": [ "def my_function(a=2, b=3, /, c=5):\n", " print(a, b, c)\n", "\n", "my_function(a=3, c=5)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3 6 5\n" ] } ], "source": [ "def my_function(a, b, *, c=5):\n", " print(a, b, c)\n", "\n", "my_function(3, 6, c=5)" ] } ], "metadata": { "kernelspec": { "display_name": "ai-courses", "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.10.14" } }, "nbformat": 4, "nbformat_minor": 2 }