{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Making Choices\n", "\n", "
\n", "\n", "
\n", "
\n", "

## Learning Objectives

\n", "
\n", "
\n", "
• Explain the similarities and differences between tuples and lists
• \n", "
• Write conditional statements including `if`, `elif`, and `else` branches.
• \n", "
• Correctly evaluate expressions containing `and` and `or`.
• \n", "
\n", "
\n", "\n", "
\n", "
\n", "

## How many paths?

\n", "
\n", "
\n", "
1. A
2. \n", "
3. B
4. \n", "
5. C
6. \n", "
7. B and C
8. \n", "
\n", "\n", "```\n", "if 4 > 5:\n", " print('A')\n", "elif 4 == 5:\n", " print('B')\n", "elif 4 < 5:\n", " print('C')\n", "```\n", "
\n", "\n", "\n", "\n", "
\n", "
\n", "

## What is truth?

\n", "
\n", "\n", "
\n", "\n", "`True` and `False` are special words in Python called `booleans` which represent true and false statements. However, they aren’t the only values in Python that are true and false. In fact, any value can be used in an `if` or `elif`. After reading and running the code below, explain what the rule is for which values are considered true and which are considered false.\n", "\n", "```\n", "if '':\n", " print('empty string is true')\n", "if 'word':\n", " print('word is true')\n", "if []:\n", " print('empty list is true')\n", "if [1, 2, 3]:\n", " print('non-empty list is true')\n", "if 0:\n", " print('zero is true')\n", "if 1:\n", " print('one is true')\n", "```\n", "
\n", "\n", "\n", "
\n", "
\n", "

## Close enough

\n", "
\n", "
\n", "Write some conditions that print `True` if the variable `a` is within 10% of the variable `b` and `False` otherwise. Compare your implementation with your partner’s: do you get the same answer for all possible pairs of numbers?\n", "\n", "
\n", "\n", "
\n", "
\n", "

## In place operators

\n", "
\n", "
\n", "Python (and most other languages in the C family) provides in-place operators that work like this:\n", "\n", "```\n", "x = 1 # original value\n", "x += 1 # add one to x, assigning result back to x\n", "x *= 3 # multiply x by 3\n", "print(x)\n", "```\n", "\n", "Write some code that sums the positive and negative numbers in a list separately, using in-place operators. Do you think the result is more or less readable than writing the same without in-place operators?\n", "\n", "
" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python2", "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.11" } }, "nbformat": 4, "nbformat_minor": 0 }