Page 95 - Programming With Python 3
P. 95

An Introduction to STEM Programming with Python — 2019-09-03a                               Page 82
            Chapter 7 — In, If, Random, and While



                    "b" in {'a':9, 'b':3} — is true ('b' is a key)
             Free
                    "x" in {'a':9, 'b':3} — is false ('x' is not a key)
                    9 in {'a':9, 'b':3} — is false (9 is not a key, it is a value)




             value in sequence                                                                       Operator
             eBook
             Returns true if the value is found in a sequence or other iterable object. For
             dictionaries tests if the value is a key.

             https://docs.python.org/3/reference/expressions.html#membership-test-operations
             http://bit.ly/2Qv9q4E

             Edition


            Try this code with several state abbreviations that you know.

               1|  states = {"AL":"ALABAMA", "AK":"ALASKA", "AZ":"ARIZONA",
                       "AR":"ARKANSAS", "CA":"CALIFORNIA ", "CO":"COLORADO",
            Please support this work at
                       "CT":"CONNECTICUT" }
               2|  code = input("Enter State Code").upper()
               3|  print(code, "exists in dictionary:", code in states)
                                  http://syw2l.org
            Random Numbers and Shuffle

                                                                               Free
            To play an game or do a simulation we need a source of random numbers. A random number is a
            number that is selected from a range or group of numbers that has no apparent correlation to the
                                                        2
            previous numbers or future numbers selected.   You can think of a random number generated by Python
            to be like throwing a coin or dice, you just don't know what will happen next.

                                                                   eBook
            Importing the Random Module


            To use random numbers in your programs you will need to include the following line of code BEFORE
            you want to use a random number method. It is recommended that your import statements, of which we
            will show a few more in this introduction, be included in the beginning of your program.
                                                                Edition
               1|  import random





            2   http://mathworld.wolfram.com/RandomNumber.html
            Copyright 2019 — James M. Reneau Ph.D. — http://www.syw2l.org — This work is licensed
            under a Creative Commons Attribution-ShareAlike 4.0 International License.
   90   91   92   93   94   95   96   97   98   99   100