Personal Pages - Denison University



Discovering CS: Getting Python up and RunningStep 1: Download Anaconda using the link Scroll down and find the green Download button. Select Python 3.7.On a mac, this will download a package (.pkg file). On a PC, this will download an executable (.exe). The website detects if you have a PC or a mac.Either way, double click this file once the download is finished, and follow the prompts. You may need to type in the password you use to log in to your computer.You do not need VSCode (you might be asked this question at the end of installation)Step 2: Open Anaconda Navigator by double clicking the icon Step 1 created.Find where it says Spyder and click “Launch”File New FileType in the following sample program:print(“Hello world!”)File SaveType in a file name, e.g. hello.pyIt is essential that you include the “.py” in the filename above. There should be colors, e.g. the Python keyword print is purple and “Hello world!” is green.Step 3: Run your programClick the green triangle above where it says Editor. On a mac, you can also use the Run menu from the top of the page, or press F5 on your keyboard.If a dialogue box pops up, just click Ok. In the lower right window is the result of running your code.Step 4: Make your program say something else, by modifying the code, saving, and running. For instance, you can print other sentences, or you can make it do math for you, e.g. print(3+4)Step 5: How to stop something from running. Create a new file called infinite loop.py. Then type the following code:i = 0while(True): i = i + 1 print(i)Notice how, after the colon, when you pressed the return key, Spyder automatically indented the next line (i = i + 1). You’ll appreciate that automatic indentation later!When you run this (see Step 3 above), the output will print numbers forever. To escape from this process, click the X in the upper left of the window where the output is being printed. Do Consoles Open a new IPython Console. This replaces what you just closed.It’s also possible to write code that does not compile, and consequently does not run. For example, create a file with the following bad code. Give your file a descriptive name, and not something like “error.py”. In general, do not name your python program the same thing as an existing module. For instance, don't name it math.py, random.py. code.py, turtle.py, etc. If you’re unsure, you can google the filename you’re thinking of, and if there’s already a python module online with that name, then don’t use that name.Now, type the following code:print(oops)When you run the code (see Step 3), an error is created in the space where you usually see output. It should say NameError: name 'oops' is not definedThis is not a big deal. You don’t need to click the x or start a new console. You can just fix the error, e.g. type print(“oops”) instead, and then run the code again. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download