{ "cells": [ { "metadata": {}, "cell_type": "markdown", "source": "# 1. if statement" }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "x = 20\ny = 23\n\nif x == y:\n print(\"x equals y\")\nelif x < y:\n print(\"x is less than y\")\nelse:\n print(\"x is greater than y\")", "execution_count": 2, "outputs": [ { "output_type": "stream", "text": "x is less than y\n", "name": "stdout" } ] }, { "metadata": {}, "cell_type": "markdown", "source": "# 2. while statement" }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "i = 1\nx = 2\n# print the mathematical power of x raise to i \nwhile i <= 10:\n print(x, \"^\", i, \" = \", x**i)\n i += 1", "execution_count": 3, "outputs": [ { "output_type": "stream", "text": "2 ^ 1 = 2\n2 ^ 2 = 4\n2 ^ 3 = 8\n2 ^ 4 = 16\n2 ^ 5 = 32\n2 ^ 6 = 64\n2 ^ 7 = 128\n2 ^ 8 = 256\n2 ^ 9 = 512\n2 ^ 10 = 1024\n", "name": "stdout" } ] }, { "metadata": {}, "cell_type": "markdown", "source": "# 3. for statement" }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "arr = [3, 7, 8, 8, 9, 1]\n\nfor i in arr:\n print(i)", "execution_count": 4, "outputs": [ { "output_type": "stream", "text": "3\n7\n8\n8\n9\n1\n", "name": "stdout" } ] }, { "metadata": {}, "cell_type": "markdown", "source": "## 3.1 for with range function" }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "for i in range(1, 10):\n print(i)", "execution_count": 5, "outputs": [ { "output_type": "stream", "text": "1\n2\n3\n4\n5\n6\n7\n8\n9\n", "name": "stdout" } ] }, { "metadata": {}, "cell_type": "markdown", "source": "## 3.2 for with range function with step" }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "for i in range(1, 10, 2):\n print(i)", "execution_count": 6, "outputs": [ { "output_type": "stream", "text": "1\n3\n5\n7\n9\n", "name": "stdout" } ] }, { "metadata": {}, "cell_type": "markdown", "source": "# 4. break and continue statement" }, { "metadata": {}, "cell_type": "markdown", "source": "## 4.1 Find first negative number" }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "arr = [3, 6, 10, 334, -5, 33, 48, -11, 20]\n\nfor i in arr:\n if i < 0:\n print(\"First n-ve number : \", i)\n break", "execution_count": 7, "outputs": [ { "output_type": "stream", "text": "First n-ve number : -5\n", "name": "stdout" } ] }, { "metadata": {}, "cell_type": "markdown", "source": "## 4.2 print all positive numbers" }, { "metadata": { "trusted": true }, "cell_type": "code", "source": "for i in arr:\n if i < 0:\n continue\n print(i)", "execution_count": 8, "outputs": [ { "output_type": "stream", "text": "3\n6\n10\n334\n33\n48\n20\n", "name": "stdout" } ] } ], "metadata": { "kernelspec": { "name": "python36", "display_name": "Python 3.6", "language": "python" }, "language_info": { "mimetype": "text/x-python", "nbconvert_exporter": "python", "name": "python", "pygments_lexer": "ipython3", "version": "3.6.3", "file_extension": ".py", "codemirror_mode": { "version": 3, "name": "ipython" } } }, "nbformat": 4, "nbformat_minor": 2 }