100%(4)4 out of 4 people found this document helpful
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.: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!:3 > 1 + 1:TrueWe can even assign the result of a comparison operation to a variable.:result= 10 / 2 == 5result:TrueArrays are compatible with comparison operators. The output is an array of boolean values.:make_array(1,5,7,8,3,-1)> 3: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.
Get answer to your question and much more
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.:ten_nachos=make_array('neither','cheese','both','both','cheese','salsa',␣,→'both','neither','cheese','both')number_cheese=np.count_nonzero(ten_nachos=='cheese')number_cheese:3:grader.check("q1_1"):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