python l1

16
Introducing Python 3 Introduction to Python

Upload: lerpo

Post on 15-Aug-2015

8 views

Category:

Data & Analytics


1 download

TRANSCRIPT

Introducing Python 3

Introduction to Python

Let look at how a computer follow instructions,What do you think?

On the sheets on your desk, Frying an Egg

Sequencing Instructions:What do you think is the correct order for these instructions?

1. Put frying pan on the oven2. Crack open the egg3. Take frying pan out of the cupboard4. Pour oil into the frying pan5. Turn on the oven6. Hold the egg over the frying pan

The important rules of programming…

• Sequence of Instructions• The order of instructions is

Important• Accuracy is very Important

Frying an Egg1. Get frying pan2. Put pan on hob3. Turn on hob4. Put oil in pan5. Hold egg over pan6. Break egg

Example Code

Python’s Development Environment

• The development program is called the IDLE – Integrated Development Environment

• It has two modes to use:– An Interactive Mode

This lets you see line by line what the results are– Script Mode

This allows you save your program and run it in full

Screens on,

Open up Python IDE on your computer

“Hello World!”• This is the most simple program to create.

It is you telling the computer to say ‘Hello World’ to the world.

• In Pythons Interactive Mode type:

print ("Hello World!")

• Press Enter• Try again with some different text.

Getting it WrongType in these lines of code. There are errors in each one. See what happens• In IDLE type the following erroneous lines:

prilt ("Hello World!")

PRINt ("Hello World!")

print Hello World

print "Hello World!"

Using Script Mode

• In IDLE, Open a New Window (CTRL + N)• Type:

print ("What is your name?")firstname = input()print ("Hello,",firstname)

• Save the program as MyFirstProgram• Press F5 to Run the program

What is a variable?• A variable is a location in the computer where you can

temporarily store data for your program• Think of it as an empty cup. The empty cup holds the data

inside it, ready for when your computer needs the information

Using a Variable to store your name

print ("What is your name?")firstname = input()print ("Hello,",firstname)

Adding Comments to your code

• Comments are useful when you are programming• They are ignored by the program, they are not counted as code• Comments appear in red• Comments have a # symbol at the start

#name is a variable (or cup!)

print ("What is your name?")name = input()print ("Hello,",name)

Functions• Functions are special keywords that do a

specific job• Functions appear in purple• print() and input() are examples of functions

print ("What is your name?")firstname = input()print ("Hello,",firstname)

Lesson Plenary

Remember when you worked out the order to fry an egg?

Write me out a new algorithm, with your choice of what it is based on,

For example, Washing Your Hair