Last login: Sun Feb 28 17:49:25 on ttys001 The default interactive shell is now zsh. To update your account to use zsh, please run `chsh -s /bin/zsh`. For more details, please visit https://support.apple.com/kb/HT208050. (base) MAC:Mon Mar 01:09:08:~> B (base) MAC:Mon Mar 01:09:08:4240> cd ch4 (base) MAC:Mon Mar 01:09:08:ch4> mkdir 0301B (base) MAC:Mon Mar 01:09:08:ch4> cd 0301B (base) MAC:Mon Mar 01:09:08:0301B> ls (base) MAC:Mon Mar 01:09:08:0301B> python Python 3.8.5 (default, Sep 4 2020, 02:22:02) [Clang 10.0.0 ] :: Anaconda, Inc. on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import random >>> dir(random) ['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', '_Sequence', '_Set', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_accumulate', '_acos', '_bisect', '_ceil', '_cos', '_e', '_exp', '_inst', '_log', '_os', '_pi', '_random', '_repeat', '_sha512', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate'] >>> print(random.randint.__doc__) Return random integer in range [a, b], including both end points. >>> random.randint(1,6) 2 >>> random.randint(1,6) 1 >>> random.randint(1,6) 4 >>> random.randint(1,6) 6 >>> random.randint(1,6) 2 >>> (random.randint(1,6),random.randint(1,6)) (2, 5) >>> (random.randint(1,6),random.randint(1,6)) (3, 3) >>> x = [1,2,3,4,5,6] >>> random.shuffle(x) >>> x [1, 4, 6, 2, 5, 3] >>> x [1, 4, 6, 2, 5, 3] >>> random.shuffle(x) >>> x [6, 4, 3, 1, 2, 5] >>> x [6, 4, 3, 1, 2, 5] >>> random.choice(x) 6 >>> random.choice(x) 1 >>> random.choice(x) 5 >>> random.sample(x, 3) [3, 2, 4] >>> random.sample(x, 3) [5, 4, 1] >>> random.sample(x, 3) [3, 6, 5] >>> (base) MAC:Mon Mar 01:09:32:0301B> vi eightball.py (base) MAC:Mon Mar 01:09:36:0301B> python eightball.py Enter your question: Is is raining outside? The answer to your question Is is raining outside? is {random.choice(replies)} (base) MAC:Mon Mar 01:09:36:0301B> !vi vi eightball.py (base) MAC:Mon Mar 01:09:36:0301B> !p python eightball.py Enter your question: Is is raining outside? The answer to your question Is is raining outside? is Don't count on it. (base) MAC:Mon Mar 01:09:36:0301B> python Python 3.8.5 (default, Sep 4 2020, 02:22:02) [Clang 10.0.0 ] :: Anaconda, Inc. on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import random >>> random.random() 0.13212661879466792 >>> random.random() 0.8637168910820039 >>> random.random() 0.982909339902225 >>> random.uniform(0,10) 8.579159479118163 >>> random.uniform(0,10) 3.8694437072194834 >>> random.uniform(0,10) 6.754578729500322 >>> random.uniform(0,10) 1.6187927982936734 >>> random.uniform(0,10) 9.929038622798107 >>> (base) MAC:Mon Mar 01:09:42:0301B> pythoh -bash: pythoh: command not found (base) MAC:Mon Mar 01:09:44:0301B> python Python 3.8.5 (default, Sep 4 2020, 02:22:02) [Clang 10.0.0 ] :: Anaconda, Inc. on darwin Type "help", "copyright", "credits" or "license" for more information. >>> s = "cowabunga" >>> for k in s: ... print(k) ... c o w a b u n g a >>> for k in s: ... print(k.upper(), end = "") ... COWABUNGA>>> for k in s: ... KeyboardInterrupt >>> for k in s: ... if k <= "M File "<stdin>", line 2 if k <= "M ^ SyntaxError: EOL while scanning string literal >>> for k in s: ... if k <= "m": ... print(k) ... c a b g a >>> xmas = ["Charcoal", "Horsefeathers", "Nasty bugs"] >>> for k in xmas: ... print(k) ... Charcoal Horsefeathers Nasty bugs >>> tuple = (1,2,3,4,True) >>> for in tuple: File "<stdin>", line 1 for in tuple: ^ SyntaxError: invalid syntax >>> for k in tuple: ... print(k) ... 1 2 3 4 True >>> s = {"shampoo", "toothpaste", "deodorant", "shaving cream"} >>> for k in s: ... print(k) ... shampoo shaving cream deodorant toothpaste >>> words = { ... ... KeyboardInterrupt >>> words = {"cat":"feline", "sheep", "ovine", "pig": "porcine", "horse":"equine"} File "<stdin>", line 1 words = {"cat":"feline", "sheep", "ovine", "pig": "porcine", "horse":"equine"} ^ SyntaxError: invalid syntax >>> words = {"cat":"feline", "sheep": "ovine", "pig": "porcine", "horse":"equine"} >>> words {'cat': 'feline', 'sheep': 'ovine', 'pig': 'porcine', 'horse': 'equine'} >>> for k in words: ... print(k) ... cat sheep pig horse >>> for k in words: ... print(words[k]) ... feline ovine porcine equine >>> for k in words.values(): ... print(k) ... feline ovine porcine equine >>> type(words.values) <class 'builtin_function_or_method'> >>> type(words.values()) <class 'dict_values'> >>> for k in dictionary.keys(): ... print(k) ... Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'dictionary' is not defined >>> for k in words.keys(): ... print(k) ... cat sheep pig horse >>> for k in range(5): ... print(k) ... 0 1 2 3 4 >>> for k in range(1,5): ... print(k) ... 1 2 3 4 >>> for k in range(1,5,2): ... print(k) ... 1 3 >>> for k in range(1,6,2): ... print(k) ... 1 3 5 >>> for k in range(0, 1, .1): ... print(k) ... Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'float' object cannot be interpreted as an integer >>> (base) MAC:Mon Mar 01:09:59:0301B> ls eightball.py (base) MAC:Mon Mar 01:09:59:0301B> vi show.py (base) MAC:Mon Mar 01:10:02:0301B> python show.py eightball.py import random question = input("Enter your question: ") replies = ["As I see it, yes.", "Ask again later.", "Better not tell you now.", "Cannot predict now.", "Concentrate and ask again.", "Don't count on it.", "It is certain.", "It is decidedly so."] print("The answer to your question") print(question) print (f"is {random.choice(replies)}") (base) MAC:Mon Mar 01:10:02:0301B> !vi vi show.py (base) MAC:Mon Mar 01:10:03:0301B> python show.py eightball.py import random question = input("Enter your question: ") replies = ["As I see it, yes.", "Ask again later.", "Better not tell you now.", "Cannot predict now.", "Concentrate and ask again.", "Don't count on it.", "It is certain.", "It is decidedly so."] print("The answer to your question") print(question) print (f"is {random.choice(replies)}") (base) MAC:Mon Mar 01:10:03:0301B> vi eight_times.py (base) MAC:Mon Mar 01:10:04:0301B> python eight_times.py repeat repeat repeat repeat repeat repeat repeat repeat (base) MAC:Mon Mar 01:10:04:0301B> vi this.txt (base) MAC:Mon Mar 01:10:06:0301B> vi make_this.py (base) MAC:Mon Mar 01:10:09:0301B> python make_this.py * ** *** **** ***** ****** ******* (base) MAC:Mon Mar 01:10:09:0301B> !vi vi make_this.py (base) MAC:Mon Mar 01:10:17:0301B>