What is Python?
Python is a programming language. It is called a high level language because it is easier to read than many other languages ( assembly is a low level language).
Python is a text based language. This means you can only display text or get some text from the user. |
Who uses Python?
Python is not just for professional programmers! It is considered very versatile ( it can be used in lots of ways), Often when we want to automate a simple process, or work out what a lot of data actually means.
Here are some of the main areas:
Here are some of the main areas:
- Making business decisions – analysing data.
- Machine Learning and AI - programming robots.
- Web applications.
- Automations – running scripts.
- Financial Analysis.
- Science Research and Engineering.
- E-commerce.
Python SandBox
Python Sandbox was started in early 2016 in order to provide students a place to learn and practice programming with the Python language. Click here to open sandbox. You should see some example code. Press the play button. The code will run in the canvas on the right hand side. You can save your code by pressing the disk. You should also copy and paste it to a text file to ensure that it doesn't get lost.
Saving your Python Code
Python Turtle Commands - Getting Started
Python turtle is a simple way to begin coding. Instruct the turtle to move around the canvas with these commands. You need these two lines of code first. This sets up your turtle.
import turtle
t = turtle.Turtle()
import turtle
t = turtle.Turtle()
Commands for Movement
Drawing Commands |
Show and Hide |
Clearing the Screen |
Put the pen on the canvas:
t.pendown() Lift the pen off of the canvas: t.penup() Change the pen color to red: t.pencolor("red") |
Show the turtle:
t.showturtle() t.shape("turtle") Hide the turtle: t.hideturtle() |
Clear the canvas to start again:
t.clear() Pause the ScreenTurtle.done( );
Changing the Background Colourturtle.Screen().bgcolor("black")
|
Filling a shape with Colour
Advanced - repeating code
If you want to repeat the same lines of code you can use a for loop. Don't worry about what this code means. All you need to know is that the number is the number of times to repeat. So the example code below makes a square by turning left then going forward four times.
Note: the code has to be exactly as it is here. After the semi-colon press enter. The next lines of code should automatically indent.
for i in range(4):
t.left(90)
t.forward(150)
Note: the code has to be exactly as it is here. After the semi-colon press enter. The next lines of code should automatically indent.
for i in range(4):
t.left(90)
t.forward(150)
The example code below creates a blue square.
|
The example here creates a yellow star.
|
Here is the code for both shapes together.
|
..and the result
|
Iteration - While Loops
While loops will continue until a condition is satisfied
The following code will repeat 10 times and make 10 small circles. Each circle will be a little bigger than the last. count = 1; while count <=10: t.circle(count) count = count + 1 |
This code will make 10 circles. Each will be noticeably bigger than the last:
count = 10; while count <=100: t.circle(count) count = count + 10 |
Variables
Using Variables as RGB values
If you want to change your colour you can specify the amount of red, green and blue as variables.
Using Random to Change a Variable
The following pattern sets the red, green and blue with a random number between 0 and 255:
User Inputs
To get data from the user you will need the input command. The following code will ask the user for a colour. The colour will be stored in the variable called Colour:
When you run your program, you will see the dialog shown here.
You can then use the chosen data. In code below the Colour variable is being used as the fill colour:
Here is an example where an input is being used to fill circles with a colour.
Functions
Functions allow you to group code together and use it more than once. You have to:
1. Create the function
2. Call ( use) the function.
1. Create the function
2. Call ( use) the function.
Step 1: Create the Function |
Step 2: Call the function |
Use the keyword def and give the function a name. This should include a verb and summarise its purpose. In the example below the function draws a square, so it is called DrawSquare. The function below will draw a square but only when it is called - see step 2.
|
Call the function to make it do something.
|
Example: Create a function and call it twice.
The following code shows the function and it being called twice to make two squares.
Create a Function. Call it many times passing in different values.
Example: 50 Circles
With just a few lines of code 50 circles are drawn on the canvas, each a little bigger than the last.
Watch this Tutorial |
A Complete Guide to Turtle Commands |
Watch the following tutorial to find out more. Try out the code in Python Sandbox.
|
Click here and try out some of the other commands.
|
Draw the USA Flag in Python Turtle
Watch the following video for an example of how to drag the flag for the USA. This is for advanced Python turtle programmers.
|
|
Tutorial Sites
Advanced GraphicsClick here to learn how to create some more advanced shapes.
|
Draw a House with Python Turtle |
Create a Racing GameClick here to learn how to create a racing game.
|
Self Documenting CodeSelf documenting code means that your code is so clear that you don't need to document it ( explain it in your comments). Check out this article for tips on self documenting code.
|