{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# suppress automatic output\n", "from IPython.core.interactiveshell import InteractiveShell\n", "InteractiveShell.ast_node_interactivity = \"none\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Conditional Statements\n", "\n", "

\n", " Conditional statements tell the program when to run a particular block of code. Often, we only want something to happen when a certain condition is met.\n", "

\n", " \n", "

\n", " For example, we might want a robot to stop if it is a particular distance from a wall.\n", "

\n", " \n", "

\n", " In Python, we can use comparison operators along with the keywords if, else, and elif to write conditional statements.\n", "

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Comparing Numbers\n", "\n", "

\n", " We often need to know whether one number is larger than, less than, or equal to another. The following operators allow us to do this:\n", "

\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Check if two numbers (either int or float) are equal.\n", "
Remember to print the output!!!!\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Start your code here\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Check if one number is less than another.\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Start your code here\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Check if one number is greater than or equal to another.\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Start your code here\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Compare 2 and 2 using >. Is the result what you expected?\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Start your code here\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Compare 2 and 2 using >=. Is the result what you expected?\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Start your code here\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Compare 2 and 2.0 using ==. \n", "
Before running it, what do you expect the result to be? \n", "
What does the result tell us about how == compares integers and floats?\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Start your code here\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Compare 2 and 2.0 using is. \n", "
Before running it, do you expect the result to be? \n", "
What does the result tell us about how is compares integers and floats?\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Start your code here\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Comparing Booleans\n", "

\n", " Booleans can be compared using the ==, !=, is, and is not operators.\n", "

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Check if True is equal to False using ==.\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Start your code here\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Check if False is equal to 0 using ==. \n", "
Before running it, do you expect the result to be? \n", "
What does the result tell us about how == compares booleans and zero?\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Start your code here\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Check if False is identical to 0 using is. \n", "
Before running it, do you expect the result to be? \n", "
What does the result tell us about how is compares booleans and zero?\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Start your code here\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Comparing Strings\n", "\n", "

\n", " We can compare strings using the ==, !=, is, and is not operators.\n", "
For strings, == and is generally do the same thing.\n", "

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Check if two strings are equal using ==.\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Start your code here\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Check if two strings are not identical using is not.\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Start your code here\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Logical Operators\n", "

\n", " We can also use the logical operators not, and, and or on booleans to make more complicated logical statements:\n", "

\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Run not False below. Is the result what you expected?\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Start your code here\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Run True and False below. Is the result what you expected?\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Start your code here\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Run True or True below. Is the result what you expected?\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Start your code here\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Like mathematical operators, logical operators also have an order of operations: \n", "

    \n", "
  1. ()
  2. \n", "
  3. not
  4. \n", "
  5. and
  6. \n", "
  7. or
  8. \n", "
\n", "

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Based on this order, run not False or True and True below. \n", "
Is the result what you expected?\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Start your code here\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " We can also use parenthesis ( ). Whatever is inside the parenthesis is evaluated first.\n", "

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Based on this order, run not (False or True) and True below. \n", "
Is the result what you expected?\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Start your code here\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Combining Comparisons\n", "

\n", " As we have seen in previous exercises, comparison statements evaluate to booleans. This means that we can combine multiple comparisons using logical operators!\n", "

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Run 1 > 3 or 4 == 4.0 below. Is the result what you expected?\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Start your code here\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Run 1 == 1 and 1 > -1 below. Is the result what you expected?\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Start your code here\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Try making your own statement using comparison and logical operators. Do you expect your statement to evaluate to True or False? Why?\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Start your code here\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# --- STOP HERE --- \n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Conditional Statements\n", "

\n", " We can use if, else, and elif in order to tell the program whether to run a particular indented block of code based on a boolean.\n", "

\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Let's start with an example of a simple if statement. What do you expect to be printed for different values of n? Why? Try choosing different numbers and see what happens.\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Choose n\n", "n = int(raw_input(\"Input n: \"))\n", "\n", "if n > 10:\n", " print('n is greater than 10')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " If n is less than or equal to 10, nothing is printed since the condition is false (remember, > means strictly greater than).\n", "

\n", "

\n", " We can improve our code by adding an elif statement.\n", "

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Try running the code again for several numbers and think about what is happening.\n", "
Remember, an elif statement only runs the code block if all previous conditions were False.\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Choose n\n", "n = int(raw_input(\"Input n: \"))\n", "\n", "if n > 10:\n", " print('n is greater than 10')\n", "elif n < 10:\n", " print('n is less than 10')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Our code is better, but we still get no feedback if n is exactly 10. An else statement will run if all previous conditions were False.\n", "

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " Try running the following code for different values of n, and make sure to try 10.\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Choose n\n", "n = int(raw_input(\"Input n: \"))\n", "\n", "if n > 10:\n", " print('n is greater than 10')\n", "elif n < 10:\n", " print('n is less than 10')\n", "else:\n", " print('n is 10')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

\n", " We can also nest if statements. Examine the following code and think about what will happen for different values of n. (Remember, an if statement is just a block of code that can be within another if statement!)\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Choose n\n", "n = int(raw_input(\"Input n: \"))\n", "\n", "if n > 0:\n", " if n > 10:\n", " print('n is greater than 10')\n", " elif n < 10:\n", " print('n is less than 10')\n", " else:\n", " print('n is 10')\n", "else:\n", " print('n is not positive!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# The Candy Party\n", "

\n", " When beavers get together for a party, they like to have candy. A beaver party is successful when the number of candies is between 40 and 60, inclusive. Unless it is the weekend, in which case there is no upper bound on the number of candies.\n", "

\n", "

\n", " Print True if the party with the given values is successful, or False otherwise.\n", "

\n", "

\n", " Try your code for different values of candies and is_weekend to test it.\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Change these to test your code!\n", "candies = 0\n", "is_weekend = True\n", "\n", "# Start your code here\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Tennis Game\n", "\n", "

\n", " You are playing doubles in a tennis game. The variable you is your tennis skill level, from 0 to 10. tm is your teammate's tennis skill level, from 0 to 10. Your chances of winning are 0 = no, 1 = maybe, and 2 = yes. Your chances are based on the following conditions:\n", "

\n", "\n", "

\n", " Based on you and your teammate's skill levels, print your chances of winning.\n", "

\n", " \n", "

\n", " Check your answer by running the code several times and manually checking the result.\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# This code generates and prints the skill levels.\n", "# (Don't worry about understanding it.)\n", "from random import randint # import the randint function\n", "you = randint(0, 10) # pick a random integer between 0 and 10\n", "tm = randint(0, 10) # pick a random integer between 0 and 10\n", "print(\"You: \" + str(you)) # print your skill level\n", "print(\"Teammate: \" + str(tm)) # print teammate's skill level\n", "\n", "# Start your code here\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Lone Sum\n", "

\n", " Given three random integers a, b, and c, print their sum. However, if two numbers have the same value, neither value counts towards the sum. For example:\n", "

\n", "

\n", "Check your answer by running the code several times and manually checking the result.\n", "

" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# This code generates and prints the random numbers.\n", "# (Don't worry about understanding it for now.)\n", "from random import randint # import the randint function\n", "a = randint(0, 5) # pick a random integer between 0 and 5\n", "b = randint(0, 5) # pick a random integer between 0 and 5\n", "c = randint(0, 5) # pick a random integer between 0 and 5\n", "print('a: ' + str(a)) # print a\n", "print('b: ' + str(b)) # print b\n", "print('c: ' + str(c)) # print c\n", "\n", "# Start your code here\n", "\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.16" } }, "nbformat": 4, "nbformat_minor": 2 }