{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# `for` takrorlash operatori\n", "\n", "* oldingi darslarimizda `while` operatori bilan ishladik\n", "* `for` eng ko'p ishlatiladi\n", "* asosan ketma-ketlikdan qiymatlarni birma-bir olib berish orqali ishlaydi\n", "* bir necha xil sintaksisga ega\n", "\n", "## Sodda namunalar" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "apple\n", "banana\n", "cherry\n" ] } ], "source": [ "fruits = [\"apple\", \"banana\", \"cherry\"]\n", "for x in fruits:\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "b\n", "a\n", "n\n", "a\n", "n\n", "a\n" ] } ], "source": [ "for x in \"banana\":\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Dictionary Iteration\n", "xyz 123\n", "abc 345\n" ] } ], "source": [ "# Iterating over dictionary\n", "print(\"Dictionary Iteration\")\n", "\n", "d = dict()\n", "\n", "d['xyz'] = 123\n", "d['abc'] = 345\n", "for i in d:\n", " print(\"% s % d\" % (i, d[i]))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2\n", "3 4\n", "5 6\n" ] } ], "source": [ "t = ((1, 2), (3, 4), (5, 6))\n", "for a, b in t:\n", " print(a, b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `range()` funksiyasi\n", "\n", "* 3 ta parameterdan iborat:\n", "* `start` - qiymatning boshlanishi\n", "* `stop` - qiymatning tugashi \n", "* `step` - qadam\n", "\n", "### Namunalar" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n" ] } ], "source": [ "for x in range(6):\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "3\n", "4\n", "5\n" ] } ], "source": [ "for x in range(2, 6):\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "5\n", "8\n", "11\n", "14\n", "17\n", "20\n", "23\n", "26\n", "29\n" ] } ], "source": [ "for x in range(2, 30, 3):\n", " print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `for-else` operatori" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "Finally finished!\n" ] } ], "source": [ "for x in range(6):\n", " print(x)\n", "else:\n", " print(\"Finally finished!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ichma-ich `for` operatori" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "red apple\n", "red banana\n", "red cherry\n", "big apple\n", "big banana\n", "big cherry\n", "tasty apple\n", "tasty banana\n", "tasty cherry\n" ] } ], "source": [ "adj = [\"red\", \"big\", \"tasty\"]\n", "fruits = [\"apple\", \"banana\", \"cherry\"]\n", "\n", "for x in adj:\n", " for y in fruits:\n", " print(x, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `break` operatori" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "apple\n", "banana\n" ] } ], "source": [ "fruits = [\"apple\", \"banana\", \"cherry\"]\n", "for x in fruits:\n", " print(x)\n", " if x == \"banana\":\n", " break" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "apple\n" ] } ], "source": [ "fruits = [\"apple\", \"banana\", \"cherry\"]\n", "for x in fruits:\n", " if x == \"banana\":\n", " break\n", " print(x)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ] } ], "source": [ "for x in range(6):\n", " if x == 3: break\n", " print(x)\n", "else:\n", " print(\"Finally finished!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `continue` operatori" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "apple\n", "cherry\n" ] } ], "source": [ "fruits = [\"apple\", \"banana\", \"cherry\"]\n", "for x in fruits:\n", " if x == \"banana\":\n", " continue\n", " print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `pass` operatori" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9\n" ] } ], "source": [ "for x in range(10):\n", " pass\n", "\n", "print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `enumerate()` funksiyasi\n", "\n", "* `iterable` - ketma-ketlik\n", "* `start` - boshlanish indeksi" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Return type: \n", "[(0, 'eat'), (1, 'sleep'), (2, 'repeat')]\n", "[(2, 'g'), (3, 'e'), (4, 'e'), (5, 'k')]\n" ] } ], "source": [ "l1 = [\"eat\", \"sleep\", \"repeat\"]\n", "s1 = \"geek\"\n", "\n", "# creating enumerate objects\n", "obj1 = enumerate(l1)\n", "obj2 = enumerate(s1)\n", "\n", "print (\"Return type:\", type(obj1))\n", "print (list(enumerate(l1)))\n", "\n", "# changing start index to 2 from 0\n", "print (list(enumerate(s1, 2)))" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(0, 'eat')\n", "(1, 'sleep')\n", "(2, 'repeat')\n", "100 eat\n", "101 sleep\n", "102 repeat\n", "0\n", "eat\n", "1\n", "sleep\n", "2\n", "repeat\n" ] } ], "source": [ "l1 = [\"eat\", \"sleep\", \"repeat\"]\n", "\n", "# printing the tuples in object directly\n", "for ele in enumerate(l1):\n", " print (ele)\n", "\n", "# changing index and printing separately\n", "for count, ele in enumerate(l1, 100):\n", " print (count, ele)\n", "\n", "# getting desired output from tuple\n", "for count, ele in enumerate(l1):\n", " print(count)\n", " print(ele)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Next Element: (0, 'apple')\n" ] } ], "source": [ "fruits = ['apple', 'banana', 'cherry']\n", "enum_fruits = enumerate(fruits)\n", "\n", "next_element = next(enum_fruits)\n", "print(f\"Next Element: {next_element}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `zip()` funksiyasi" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "apple is red\n", "banana is yellow\n", "cherry is green\n" ] } ], "source": [ "fruits = [\"apple\", \"banana\", \"cherry\"]\n", "colors = [\"red\", \"yellow\", \"green\"]\n", "for fruit, color in zip(fruits, colors):\n", " print(fruit, \"is\", color)" ] } ], "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 }