lab06.pdf - lab06 November 26, 2020 1 Lab 6: Randomization...

This preview shows page 1 - 4 out of 10 pages.

lab06November 26, 20201Lab 6: RandomizationWelcome to Lab 1 of Data 8.2x!We will go overiteration and simulations, as well as introduce the concept ofrandomness.First, set up the tests and imports by running the cell below.[1]:importnumpyasnpfromdatascienceimport*importottergrader=otter.Notebook()1.11. Nachos and ConditionalsIn Python, Boolean values can either beTrueorFalse.We get Boolean values when usingcomparison operators such as<(less than),>(greater than), and==(equal to). A list of commoncomparison operators can be found below![2]:3 > 1 + 1[2]:TrueWe can even assign the result of a comparison operation to a variable.[3]:result= 10 / 2 == 5result[3]:TrueArrays are compatible with comparison operators. The output is an array of boolean values.[4]:make_array(1,5,7,8,3,-1)> 3[4]:array([False,True,True,True, False, False])Waiting on the dining table just for you is a hot bowl of nachos! Let’s say that whenever you takea nacho, it will have cheese, salsa, both, or neither (just a plain tortilla chip).1
Using the function callnp.random.choice(array_name), let’s simulate taking nachos from thebowl at random. Start by running the cell below several times, and observe how the results change.
Question 1.1Assume we took ten nachos at random, and stored the results in an array calledten_nachosas done below. Find the number of nachos with only cheese using code (do not hardcodethe answer).Hint:Our solution involves a comparison operator and thenp.count_nonzeromethod.[6]:ten_nachos=make_array('neither','cheese','both','both','cheese','salsa',,'both','neither','cheese','both')number_cheese=np.count_nonzero(ten_nachos=='cheese')number_cheese[6]:3[7]:grader.check("q1_1")[7]:All tests passed!Conditional StatementsA conditional statement is made up of many lines that allow Python to choose from differentalternatives based on whether some condition is true.Here is a basic example.def sign(x):if x > 0:return 'Positive'How the function works is if the inputxis greater than0, we get the string'Positive'back.If we want to test multiple conditions at once, we use the following general format.if <if expression>:<if body>elif <elif expression 0>:<elif body 0>elif <elif expression 1>:<elif body 1>...else:<else body>2
Only one of the bodies will ever be executed.Eachifandelifexpression is evaluated andconsidered in order, starting at the top. As soon as a true value is found, the corresponding bodyis executed, and the rest of the expression is skipped. If none of theiforelifexpressions aretrue, then theelse bodyis executed. For more examples and explanation, refer to

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 10 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Spring
Professor
Florian
Tags
Pride and Prejudice, Nachos, elif nacho

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture