{ "cells": [ { "cell_type": "markdown", "id": "b8d103fa", "metadata": {}, "source": [ "# `if-elif-else` Statements\n", "\n", "In the world of programming, decision-making is a fundamental concept, and the `if-elif-else` statement in Python is a powerful tool for precisely that purpose. These statements allow you to create dynamic, branching logic within your programs. With them, you can instruct your code to take different actions based on specific conditions, making your programs more intelligent and responsive.\n", "\n", "## What is an if-elif-else Statement?\n", "\n", "An `if-elif-else` statement is a control structure in Python that enables your program to make choices. It starts with an `if` statement, which checks a particular condition. If that condition is true, a specific block of code is executed. However, if the condition is not met, the program can continue to evaluate other conditions using `elif` (short for \"else if\") statements. These \"elif\" clauses allow for multiple conditions to be checked sequentially. Finally, if none of the preceding conditions are true, the `else` block of code is executed.\n", "\n", "Why is it Used?\n", "\n", "The primary purpose of `if-elif-else` statements is to introduce decision-making capabilities into your code. They allow your program to adapt and respond to varying situations. For example, you might use `if-elif-else` statements to implement logic like:\n", "\n", "- **Game Over Conditions**: Determine if a player's score has reached a winning threshold. If yes, declare a winner; otherwise, continue the game." ] }, { "cell_type": "code", "execution_count": 4, "id": "4d997146-4d85-41d5-b0e8-b584dd8b6e61", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "You need 53 more points to win!\n" ] } ], "source": [ "player_1_score = 47\n", "\n", "if player_1_score >= 100:\n", " print(\"YOU WIN!\")\n", "else:\n", " print(f\"You need {100 - player_1_score} more points to win!\")" ] }, { "cell_type": "markdown", "id": "14c4f5d5-6638-4854-a622-5ceffae5c0a1", "metadata": {}, "source": [ "- **User Authentication**: Check if a user's credentials are correct. If they are, grant access; otherwise, deny access." ] }, { "cell_type": "code", "execution_count": 5, "id": "42b482a3-8ef5-44cb-abae-f8241669a66c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Access granted!\n" ] } ], "source": [ "user_input_username = \"john_doe\"\n", "user_input_password = \"secure_password\"\n", "\n", "correct_username = \"john_doe\"\n", "correct_password = \"secure_password\"\n", "\n", "if user_input_username == correct_username and user_input_password == correct_password:\n", " print(\"Access granted!\")\n", "else:\n", " print(\"Access denied. Please check your username and password.\")" ] }, { "cell_type": "markdown", "id": "10e9c540-715b-4ce1-8b7c-d16d4a677a1c", "metadata": {}, "source": [ "- **Grade Evaluation**: Assign grades based on a student's score." ] }, { "cell_type": "code", "execution_count": 6, "id": "9b999ab1-2772-4df3-80f7-463495e0b457", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "B\n" ] } ], "source": [ "student_score = 85\n", "\n", "if student_score >= 90:\n", " print(\"A\")\n", "elif student_score >= 80:\n", " print(\"B\")\n", "elif student_score >= 70:\n", " print(\"C\")\n", "elif student_score >= 60:\n", " print(\"D\")\n", "else:\n", " print(\"F\")" ] }, { "cell_type": "markdown", "id": "cd2b89c2-a7ec-4eef-a5a2-ac2117bc5d44", "metadata": {}, "source": [ "In these examples, \"if-elif-else\" statements allow the program to take different actions depending on specific conditions. They provide the flexibility to handle a wide range of scenarios and are an essential tool for building responsive and intelligent software." ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "formats": "ipynb,md:myst", "main_language": "python", "notebook_metadata_filter": "-all" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.4" } }, "nbformat": 4, "nbformat_minor": 5 }