[ Index | | Exercise 1.2 ]
Objectives:
- Make sure Python is installed correctly on your machine
- Start the interactive interpreter
- Edit and run a small program
Files Created: art.py
Start Python3 on your machine. Make sure you can type simple statements such as the "hello world" program:
>>> print('Hello World')
Hello World
>>>
In much of this course, you'll want to make sure you can work from the interactive REPL like this. If you're working from a different environment such as IPython or Jupyter Notebooks, that's fine.
Create the following program and put it in a file called art.py
:
# art.py
import sys
import random
chars = '\|/'
def draw(rows, columns):
for r in rows:
print(''.join(random.choice(chars) for _ in range(columns)))
if __name__ == '__main__':
if len(sys.argv) != 3:
raise SystemExit("Usage: art.py rows columns")
draw(int(sys.argv[1]), int(sys.argv[2]))
Make sure you can run this program from the command line or a terminal.
bash % python3 art.py 10 20
If you run the above command, you'll get a crash and traceback message. Go fix the problem and run the program again. You should get output like this:
bash % python3 art.py 10 20
||||/\||//\//\|||\|\
///||\/||\//|\\|\\/\
|\////|//|||\//|/\||
|//\||\/|\///|\|\|/|
|/|//|/|/|\\/\/\||//
|\/\|\//\\//\|\||\\/
|||\\\\/\\\|/||||\/|
\\||\\\|\||||////\\|
//\//|/|\\|\//\|||\/
\\\|/\\|/|\\\|/|/\/|
bash %
It is absolutely essential that you are able to edit, run, and debug ordinary Python programs for the rest of this course. The choice of editor, IDE, or operating system doesn't matter as long as you are able to experiment interactively and create normal Python source files that can execute from the command line.
[ Solution | Index | Exercise 1.2 ]
>>>
Advanced Python Mastery
...
A course by dabeaz
...
Copyright 2007-2023
. This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License