{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# For Else Operation" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['hello', 'My', 'Name', 'is', 'Nishant', 'Baheti']" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = \"hello My Name is Nishant Baheti\".split(\" \")\n", "l" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n", "My\n", "Name\n", "is\n", "Nishant\n", "Baheti\n", "this loop ran successfully\n" ] } ], "source": [ "for item in l:\n", " print(item)\n", "else:\n", " print(\"this loop ran successfully\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- if there is a break then else will not run" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n" ] } ], "source": [ "for item in l:\n", " print(item)\n", " break\n", "else:\n", " print(\"this loop ran successfully\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Nishant found\n" ] } ], "source": [ "for item in l:\n", " if item == \"Nishant\":\n", " print(\"Nishant found\")\n", " break\n", "else:\n", " print(\"Nishant was not found\")" ] } ], "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.8.3" } }, "nbformat": 4, "nbformat_minor": 4 }