{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Remember to import any libraries that you need!\n", "from datascience import *\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The condition is true!\n", "Math still works\n" ] } ], "source": [ "# This is an \"if statement,\" or \"conditional statement,\" and the logic is only evaluated if the condition is true\n", "some_condition = True\n", "if some_condition:\n", " print(\"The condition is true!\")\n", " \n", "# You can also test the inverse of the condition using the \"not\" keyword\n", "if not some_condition:\n", " print(\"The condition is false!\")\n", " \n", "# There's no need to assign the condition to a variable! Python will evaluate your expressions for you\n", "# Remember how '==' returns a boolean value?\n", "if 1 == 1:\n", " print(\"Math still works\")\n", "if 1 == 2:\n", " print(\"Math is broken\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "One condition is true!\n", "One condition is true!\n" ] } ], "source": [ "# You can chain conditions together in an if statement with \"and\" and \"or\"\n", "some_condition = True\n", "some_other_condition = False\n", "\n", "# This tests that 'both' conditions are true\n", "if some_condition and some_other_condition:\n", " print(\"Both conditions are true!\")\n", "\n", "# This tests that 'at least' one condition is true, but also succeeds if 'both' values are true\n", "if some_condition or some_other_condition:\n", " print(\"One condition is true!\")\n", "\n", "\n", " \n", " \n", "# Instead of writing a brand new \"if statement\" every time we have a new condition, we can\n", "# chain them together using \"if, elif, else\". The rules are that for each \"conditional\" statement:\n", "# You must have exactly 1 \"if\" case\n", "# You may have 0 to infinite \"elif\" case\n", "# You may have 0 or one \"else\" cases\n", "\n", "# An \"if\" is your basic use case. If the condition is true, the logic will be evaluated.\n", "# An \"elif\" (short for else if) will only be tested if the base \"if\" case was false and if each of the preceeding\n", "# \"elif\"s were false\n", "# An \"else\" statement contains logic for \"if all other cases fail.\"\n", "if some_condition and some_other_condition:\n", " print(\"Both conditions are true!\")\n", "elif some_condition or some_other_condition:\n", " print(\"One condition is true!\")\n", "else:\n", " print(\"Neither condition is true!\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4] evaluates to True!\n", "[] evaluates to False!\n", "0 evaluates to False!\n", "1 evaluates to True!\n", "Some string value evaluates to True!\n", " evaluates to False!\n", "[0] evaluates to True!\n" ] } ], "source": [ "# Python is a little bit weird, and everything has either a True or False value assigned to it.\n", "# These refer to the \"truthiness\" or \"falsiness\" of the value. Test a few of them out!\n", "\n", "truthiness_test_vals = [[1,2,3,4],[],0,1, \"Some string value\", \"\", [0]]\n", "\n", "for value in truthiness_test_vals:\n", " if value:\n", " print(f'{str(value)} evaluates to True!')\n", " else:\n", " print(f'{str(value)} evaluates to False!')\n", " \n", "# As a general rule, if a variable contains a non-zero or non-empty value, it is true." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([8, 7, 9, 0, 2, 5, 3, 0, 4, 6])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# From your reading, you'll see that this generates 10 random integers between 0 (inclusive) and 10 (exclusive)\n", "random_ints = np.random.choice(10,10)\n", "random_ints" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0.39315903, 0.86328183, 0.65969407, 0.97453189, 0.92070023,\n", " 0.36266504, 0.49156882, 0.20067649, 0.33757991, 0.76892529])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Similarly, this generates random floats between 0 and 1. \n", "# The parameter dictates the number of random floats you get back\n", "random_floats = np.random.random(10)\n", "random_floats" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "44\n", "Some high, some low\n" ] } ], "source": [ "print(sum(random_ints))\n", "if sum(random_ints) < 30:\n", " print(\"Mostly low numbers\")\n", "elif sum(random_ints) >= 30 and sum(random_ints) <60:\n", " print(\"Some high, some low\")\n", "else:\n", " print(\"Mostly high numbers\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Question 1:\n", "Create two variables, (first_num and second_num), and assign them random values (ints or floats -- it doesn't matter). Use if statements to compare the values and output a message that indicates the largest value.\n", "\n", "I.e. if first_num == 10 and second_num = 1, you should get a message like \"first number is greater!\"" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "first number: 65 is greater than second number: -85\n" ] } ], "source": [ "# create a first and second number variable, let it be within range of -100 to 99.\n", "first_num = np.random.choice(np.arange(-100, 100))\n", "second_num = np.random.choice(np.arange(-100, 100))\n", "# using if statements and formatted strings, check to see which number is greate\n", "if first_num > second_num:\n", " print(f\"first number: {first_num} is greater than second number: {second_num}\")\n", "elif second_num > first_num:\n", " print(f\"second number: {second_num} is greater than first number: {first_num}\")\n", "else:\n", " print(f\"Both numbers {first_num} and {second_num} are equal!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Question 2:\n", "\n", "Consider the following code. Even though \"num\" is less than 2, we're still getting the message \"less than 10\" every time. Explain why." ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Less than 10\n" ] } ], "source": [ "num = 1\n", "if num < 10:\n", " print(\"Less than 10\")\n", "elif num < 5:\n", " print(\"Less than 5\")\n", "elif num < 2:\n", " print(\"Less than 2\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Whenever any value is entered that is less than 10, only the first if statement executes. It would never get to the 5 or 2 checks, and only display what is in the first if statement. If you reverse the order of the if statements, this would work as intended." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Question 3:\n", "In your last mini homework (and in this week's reading) we saw a \"for loop,\" which is used to iterate through any iterable item (like a list, string, etc.). Create a list of random floats, then use a for loop to print each value individually" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "value 1 : 0.0409743281636753\n", "value 2 : 0.6943827044075414\n", "value 3 : 0.9193827692519987\n", "value 4 : 0.34529162046253525\n", "value 5 : 0.5680070954504679\n", "value 6 : 0.333482737370685\n", "value 7 : 0.24474460886039917\n", "value 8 : 0.9479221591217194\n", "value 9 : 0.9241714541116084\n", "value 10 : 0.17094329526544638\n", "value 11 : 0.5130324935270965\n", "value 12 : 0.920872760969936\n", "value 13 : 0.15156731317904037\n", "value 14 : 0.5419109732740256\n", "value 15 : 0.043521794935426406\n" ] } ], "source": [ "# create 15 random float values and store in printed_values\n", "printed_values = np.random.random(15)\n", "# using a for loop and formatted strings, list position of value and what is stored there.\n", "for index in range(len(printed_values)):\n", " print(f\"value {index+1} : {printed_values[index]}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Question 4:\n", "Perform a simulation in which you roll a die 10 times. Store the output in an array called \"rolls\"" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "# Replace the ellipses with your code\n", "# rolls a six-sided die, 10 times.\n", "# modify the range to get a different die,\n", "# modify the second parameter of the random choice function to get any number of rolls.\n", "rolls = np.random.choice(np.arange(1, 7), 10)" ] } ], "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.9.12" } }, "nbformat": 4, "nbformat_minor": 2 }