Thonny has simple infrastructure for extensions. These are some known Thonny plug-ins: thonny-black-format adds a command for formatting current file with Black; thonny-ev3dev allows uploading code to EV3 (and much more) thonny-lahendus allows loading exercises from lahendus.ut.ee and submitting solutions for automatic assessment.
Goals: To learn..
- How python can be used interactively for simple computational tasks.
- How to run Thonny
- Start playing with Turtle graphics
Introduction¶ 'Turtle' is a python feature like a drawing board, which lets you command a turtle to draw all over it! You can use functions like turtle.forward(.) and turtle.left(.) which can move the turtle around. Before you can use turtle, you have to import it. So we can write this code to our clicked function like this: def clicked: res = 'Welcome to ' + txt.get lbl.configure(text= res) If you click the button and there is a text on the entry widget, it will show 'Welcome to' concatenated with the entered text. And this is the complete code.
Installing on Your Machine or on a CS machine?
In this course, you will need a laptop with two pieces of software installed:
- Anaconda Python 3 Distribution
- Thonny
You need to get these installed on your laptop. We will be using these during class times.
During labs, you may work on your own machine or on a CS machine in the Ulab. I recommend you just stick with your own machine.
Installing on your Laptop: Anaconda first
Go first to http://www.continuum.io/downloads.
For windows/Mac, choose the Python 3.6 64-bit Graphical Installer. Download it and follow the instructions to install it. Choose all default options during installation.
Now, Install Thonny
Now, go to http://thonny.org.
Choose your platform (OS X, Linux or Windows), and download and install the software.
Start up Thonny. Once it loads you can create a new file. You may call this whatever you want, but we strongly recommend using a naming scheme that indicates the lab number such as CS106lab0.py
.Ideally, each lab will have its own folder as well.
Also, choose an Interpreter. Click the tools
menu and select options
and click on the Interpreter
tab.You want this to be the version of Python 3.x that you downloaded.
If you want to work on the CS machines in the lab, you will need to proceed to do the steps below. Otherwise, skip to Using the Interpreter as a Fancy Calculator below.
Logging in the first time on a CS machine
Before class, you should have received an email about how to login the first time and set your password (via freeipa1.cs.calvin.edu
). If you have done that already, then you should be able to log in now on a workstation, with your login id / password. Your login id is your userid, e.g., vtn2
or eeg23
.
If you did not receive the email or did not log in to freeipa1
and set your password, then talk to Prof. Norman and he will help you get set up.
In the upper-left corner of the screen, you should see a little 'mouse' icon. This is the 'Start' button on Ubuntu (Linux).
In this course, we will create computer programs using an editor called Thonny
. Start up Thonny
by going to the Start
button, then Development
, then Thonny
.
A dialog box called 'Complete Installation' pops up. Click OK
.
(From now on, you can start thonny from the menu system via Development --> Thonny
.)
Using the Interpreter as a Fancy Calculator
In the upper-left corner you will see your project – myCS106
. Right-click on it and create a new Directory. Call it lab0
.
Go over to the Tools
menu, and choose Python Console…
Use FrostWire's built-in media library to access and play your downloads - all in one simple app. Frostwire ios.
At the bottom of the screen you should see a new window that shows that you are running the Python interpreter now in interactive mode. This is like running a calculator that allows you to evaluate expressions and gives you answers back immediately.
You should see the python prompt:In [2]:
Try adding some values together. The interpreter prints out the result on the following line (when there is a result).
In [3]: 1 + 1
Out[3]: 2
In [4]: (3 + 4) ** 2
Out[4]: 49
In [5]: 2 ** 100
Out[5]: 1267650600228229401496703205376L
Create some variables and make them refer to values: e.g.,
In [6]: a = 17
In [7]: b = 4444
Try some more stuff -- add the variables together; subtract them; store the result in a 3rd variable; divide them, etc. Experiment! Be curious!
Exercises:
Create a new file, in which you will record your answers to the following questions. Do this by selecting lab0
in your file hierarchy on the left-side, right-click, choose New → File
. Call it lab0_answers.txt
.
A.1. Use the python interpreter to find the number of seconds in a year. Record how you did it in lab0_answers.txt
.
A.2. Predict: if you create a variable var1
and put a value in it, then create variable var2
with a different value in it, and then do
In [8]: var1 = var2
In [9]: var2 = var1
What values will be in var1
and var2
when you print them out. (To print out the value in the variable, just type the name of the variable. E.g., var
.)
A.3. Use the python interpreter to create variables that hold the number of seconds in a minute, number of minutes in an hour, number of hours in a day, and number of days in a year. Then, write a statement to compute the number of seconds in a year using these variables. Copy your code into your answer file for submission.
Creating a program in Thonny
Right click on lab0
under your myCS106
directory, and create a new Python File. Call it lab0
(Thonny will append .py
to the end.)
In the edit window, which may say __author__ = 'emc3'
or some such thing at the top, add this:
Pay my chase auto loan by phone. import turtle
win = turtle.Screen()
t = turtle.Turtle()
t.circle(100)
turtle.mainloop()
Note that Thonny will provide intelligent completions on almost every line above. To select the completion, hit Tab
.
Before you run the program, let's look at what it should do:
- The first line imports a 'package' called
turtle
, which allows us to create all kinds of graphics elements – circles, rectangles, etc. - The next line creates a
Screen
, which we will draw on. TheScreen()
function is found in the turtle module, which is why we have to callturtle.Screen()
to use it. - The next line creates a new Turtle object, called
t
. Turtles can be moved around to draw stuff. - The next line tells the turtle
t
to draw a circle with radius 100. - The final line of the program calls the
mainloop()
which allows the user to interact with the Screen. If you forget this line, you can't close the window and go on with life…
Save the file. Run the program by choosing Run
→ Run.. lab0.py
, or you can right-click on the window and choose Run lab0.py
. Do you see the circle?
Start up a web browser. In it, search in google for python turtle.circle
. Investigate other ways you can call circle()
. Try stuff out.
Thonny Turtle Codes
Alter the program in whatever way you want. Perhaps you want to add multiple circles, or you want to create multiple canvases. Maybe you want the circles to be different sizes. See if you can get some of them to be filled in.
Thonny Turtle Colors
Play around a while and see what you can do.
Thonny Turtle Code For Pc
Submitting your files
Turtle Code Glass
Go to Moodle and find Lab0. Upload your files: lab0.py
and lab0_answers.txt
.