{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Conditionals\n", "\n", "## Types:\n", "\n", "- booleans: `True` and `False`\n", "\n", "## Comparison operators:\n", "\n", "### Equality" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = 10\n", "y = 3\n", "y == x" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 == 4.0" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3.0 == 3.5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### \"Disequality\" (not equal)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4.0 != 4" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 != 7" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Less than" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 < 6" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4.0 < 3.9" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 < 6 < 7" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Less than or equal" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "32 <= 32" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "32 <= 32.8" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "7.5 <= 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Greater than" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 > 6" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "8 > 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Greater than or equal" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4.0 >= 4" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4.5 >= 3" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4.5 >= 9" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Boolean operators:\n", "\n", "### And\n", "\n", "| a | b | a and b |\n", "|:---:|:---:|:---:|\n", "|True |True |True |\n", "|True |False|False|\n", "|False|True |False|\n", "|False|False|False|\n" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and False" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 > 0 and 4 < 3" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 > 0 and 5 < 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Or\n", "\n", "| a | b | a or b |\n", "|:---:|:---:|:---:|\n", "|True |True |True |\n", "|True |False|True |\n", "|False|True |True |\n", "|False|False|False|" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True or False" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 < 0 or 3 > 2" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 < 0 or 3 < 0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Not\n", "\n", "| a | not a |\n", "|:---:|:---:|\n", "|True |False|\n", "|False|True|" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not 4 < 0" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not 0 < 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### If conditions \n", "\n", "if-else" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def myAbs(x):\n", " if x < 0:\n", " x = -x\n", " # ... other commands may appear here, for example, another assignment or another if!\n", " else:\n", " x = x\n", " # else case cannot be empty\n", " return x\n", "\n", "myAbs(-3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "if (without else)\n", "\n", "If an `else` case \"does nothing\", we can get rid of it." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "32" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def myAbs(x):\n", " if x < 0:\n", " x = -x\n", " return x\n", "\n", "myAbs(-32)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "if-elif" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def sign(n):\n", " s = 0\n", " if n < 0:\n", " s = -1\n", " elif n > 0:\n", " s = 1\n", " #else:\n", " # s = 0\n", " \n", " return s\n", " \n", "sign(-43)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 1: middle number\n", "\n", "Implement the function `middle(a, b, c)` that takes three numbers as input, and returns the middle among them.\n", "\n", "Hint: Draw a flowchart to avoid an explosion on the number of cases." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "def middle(a, b, c):\n", " return m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 2: card game\n", "\n", "\n", "Tri-du is a card game inspired in the popular game of Truco. The game uses a normal deck of 52 cards, with 13 cards of each suit, but suits are ignored. What is used is the value of the cards, considered as integers between 1 to 13.\n", "\n", "In the game, each player gets three cards. The rules are simple:\n", "\n", "1. A Three of a Kind (three cards of the same value) wins over a Pair (two cards of the same value).\n", "2. A Three of a Kind formed by cards of a larger value wins over a Three of a Kind formed by cards of a smaller value.\n", "3. A Pair formed by cards of a larger value wins over a Pair formed by cards of a smaller value.\n", "\n", "Note that the game may not have a winner in many situations; in those cases, the cards are returned to the deck, which is re-shuffled and a new game starts.\n", "\n", "A player received already two of the three cards, and knows their values. Your task is to write a program to determine the value of the third card that maximizes the probability of that player winning the game.\n", "\n", "Implement the function `bestCard(c1, c2)` that takes the values `c1` and `c2` of the two cards at hand, and returns the value of that card that will result on the best hand.\n" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "def bestCard(c1, c2):\n", " return 42" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 3: rounding numbers\n", "\n", "Humans like round numbers to the closest integer. That is why 99.99 becomes 100, and 2.01 becomes 2.\n", "\n", "Implement the function `round(x)` that rounds `x` to the closest integer. That means that if `x` ends with .5 or greater, you should round the number up. Otherwise, round the number down." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "import math\n", "\n", "def round(x):\n", " return 42" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 4: older person\n", "\n", "Given the birthdays of two people, it is possible to decide whether one is older than another one.\n", "\n", "Implement the function `isOlder(d1, m1, y1, d2, m2, y2)`, where `d1, m1, y1` is the day, month and year of birth of person 1, and `d2, m2, y2` is the day, month and year of birth of person 2, and returns `True` if person 1 is older. Otherwise, it returns false." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "def isOlder(d1, m1, y1, d2, m2, y2): \n", " return False" ] } ], "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": 2 }