{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Conditionals" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Testing truth value" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `bool`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f\"type of True and False: {type(True)}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f\"0: {bool(0)}, 1: {bool(1)}\")\n", "print(f\"empty list: {bool([])}, list with values: {bool(['woop'])}\")\n", "print(f\"empty dict: {bool({})}, dict with values: {bool({'Python': 'cool'})}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `==, !=, >, <, >=, <=`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f\"{1 == 0}\")\n", "print(f\"{1 != 0}\")\n", "print(f\"{1 > 0}\")\n", "print(f\"{1 > 1}\")\n", "print(f\"{1 < 0}\")\n", "print(f\"{1 < 1}\")\n", "print(f\"{1 >= 0}\")\n", "print(f\"{1 >= 1}\")\n", "print(f\"{1 <= 0}\")\n", "print(f\"{1 <= 1}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can combine these:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f\"{1 <= 2 <= 3}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `and, or, not`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "python_is_cool = True\n", "java_is_cool = False\n", "empty_list = []\n", "secret_value = 3.14" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f\"Python and java are both cool: {python_is_cool and java_is_cool}\")\n", "print(f\"secret_value and python_is_cool: {secret_value and python_is_cool}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f\"Python or java is cool: {python_is_cool or java_is_cool}\")\n", "print(f\"{1 >= 1.1 or 2 < 1.4}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f\"Java is not cool: {not java_is_cool}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can combine multiple statements, execution order is from left to right. You can control the execution order by using brackets." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(bool(not java_is_cool or secret_value and python_is_cool or empty_list))\n", "print(bool(not (java_is_cool or secret_value and python_is_cool or empty_list)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `if`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "statement = True\n", "if statement:\n", " print(\"statement is True\")\n", "\n", "if not statement:\n", " print(\"statement is not True\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "empty_list = []\n", "# With if and elif, conversion to `bool` is implicit\n", "if empty_list:\n", " print(\"empty list will not evaluate to True\") # this won't be executed" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "val = 3\n", "if 0 <= val < 1 or val == 3:\n", " print(\"Value is positive and less than one or value is three\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `if-else`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_dict = {}\n", "if my_dict:\n", " print(\"there is something in my dict\")\n", "else:\n", " print(\"my dict is empty :(\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `if-elif-else`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "val = 88\n", "if val >= 100:\n", " print(\"value is equal or greater than 100\")\n", "elif val > 10:\n", " print(\"value is greater than 10 but less than 100\")\n", "else:\n", " print(\"value is equal or less than 10\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can have as many `elif` statements as you need. In addition, `else` at the end is not mandatory." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "greeting = \"Hello fellow Pythonista!\"\n", "language = \"Italian\"\n", "\n", "if language == \"Swedish\":\n", " greeting = \"Hejsan!\"\n", "elif language == \"Finnish\":\n", " greeting = \"Latua perkele!\"\n", "elif language == \"Spanish\":\n", " greeting = \"Hola!\"\n", "elif language == \"German\":\n", " greeting = \"Guten Tag!\"\n", "\n", "print(greeting)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For more detailed overview about conditionals, check this [tutorial from Real Python](https://realpython.com/python-conditional-statements/)." ] } ], "metadata": { "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.0" } }, "nbformat": 4, "nbformat_minor": 1 }