Presentation is loading. Please wait.

Presentation is loading. Please wait.

Whatcha doin'? Aims: To start using Python. To understand loops.

Similar presentations


Presentation on theme: "Whatcha doin'? Aims: To start using Python. To understand loops."— Presentation transcript:

1 Whatcha doin'? Aims: To start using Python. To understand loops.
Objectives: All: Know the difference between the intepreter and the editor. Most: Type in, save and try out all the examples. Some: Experiment with the interpreter and simple programs.

2 Python We are going to learn the computer programming language Python.
Python is a high-level language created in 1989 by Guido van Rossum. Van Rossum chose that name because he is a fan of “Monty Python”. The home-page for Python is Some people are still using version 2. These lessons assume you are using Python 3.

3 IDLE When you start Python's built-in IDLE, you'll get an interpreter window first. This is for trying code out immediately. You'll see this prompt: >>> As long as the version you are using starts with 3, everything should work fine. To write a program, choose: File > New Window. Then you'll see this type of window (no prompt).

4 Mathematical Operators
Asterisk means “times”. Forward slash means “divide”. Double forward slash means “integer divide”. Double asterisk means “to the power of”. The percent sign gives you the remainder from doing the division.

5 Some more operators We use “==” to check if two things are the same.
When we ask Python if 2 equals 3, it replies “False”. We use “!=” to check if they are not equal. Less than & less than or equal to. Greater than and greater than or equal to.

6 We'll do it in the interpreter for now.
Print When learning a programming language, it is traditional to find out how to to print “Hello, World!”. We'll do it in the interpreter for now. Experiment Have a go at printing out some other phrases. It won't work if you forget the brackets or the quotation marks.

7 We are going to learn about the “for” loop. First try this one.
For Loops We are going to learn about the “for” loop. First try this one. Don't forget the colon!

8 Very often, we will use “for” loops with numbers.
Try this out – you can experiment with changing the numbers and the bit in quotation marks.

9 If you don't specify a start point and a step value,
Python assumes you want to go up in 1s from 0. Here's how to count down from 10 to 1 in 2s. Task: Write a “for” loop that counts from 1 to 100 in 10s. 0 and 100 must be displayed.

10 Variables In the loops we have used so far, we made use of variables (they were called “letter”, 'i' and 'x'.) When we make a variable, we ask the computer to set aside a bit of memory to store something. We have a name that points to that area of memory, so that we can get the information out again or change it. Have a go at making some variables and trying out some operations.

11 Variable Names If you are just using a variable as a counter, it's quite normal to use something very simple, like 'i'. Otherwise, it's a good idea to use a name that helps you to remember what information the variable is holding. Variable names cannot start with numbers and most punctuation marks will cause an error. It's safest to stick with letters, numbers and '_'.

12 When we want to get some input from the user and put it straight into a variable, we do it this way.
Python treats input as strings of characters, if you want to use them as numbers you have to specify that. If we try to divide the string version of '8' we get an error. After we convert it to an integer, using the int() function it works fine.

13 Our first program. We are going to write a program that will print a multiplication table. The first step is an interpreter version. Notice the colon. Also the lines to be repeated must be indented. The “for” loop makes the variable 'i' take on the values from 2 to 12. The “print()” function displays the strings and numbers and automatically adds a newline. Experiment with the “for” loop and the “print()” function.

14 I like to start each program with its name as a comment.
Let's turn it into a program. First make a “python” folder in your documents. Use File > New Window to open the editor. Save your program as “tables1.py” I like to start each program with its name as a comment. A print() statement to introduce the program. This means, wait for some input from the user and, treating it as a whole number, save it in a variable called 'n'.

15 Type in the program exactly as it is here.
Open a new window. Type in the program exactly as it is here. Save it and press F5 to run it. Check it is working. If it's not, try to fix it (hint – did you type the code it correctly?)

16 Extension: Write a program which asks the user for his or her name and then says hello to him or her.


Download ppt "Whatcha doin'? Aims: To start using Python. To understand loops."

Similar presentations


Ads by Google