ks3 python an introduction to textual programming

43
KS3 Python An Introduction to Textual Programming

Upload: bertha-beasley

Post on 26-Dec-2015

245 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: KS3 Python An Introduction to Textual Programming

KS3 Python

An Introduction to Textual Programming

Page 2: KS3 Python An Introduction to Textual Programming

Todays objectives: – Use the Print function– Store Variables– Create a conversation– Carry out calculations

Python 1

Page 3: KS3 Python An Introduction to Textual Programming

• Open your Unit 2 Programming folder

• Create another folder called “Python Practice”

• This is where you are going to save all your Python practice work!

Getting Started

Page 4: KS3 Python An Introduction to Textual Programming

• Open IDLE (Python GUI)– This allows you to write code, which is checked

line by line as you write it.

• Click File New Window– This allows you to write full programs and then

run them. If you make a mistake you will not know until you run it.

The Basics

Page 5: KS3 Python An Introduction to Textual Programming

• Spelling and Capital letters are essential in Python!!!!

• 1 missed capital letter and your program may not work.

• Make notes in your exercise books, this will help you later!

Before we start

Page 6: KS3 Python An Introduction to Textual Programming

TEXT

Page 7: KS3 Python An Introduction to Textual Programming

• Now open Python.

• Type print (“Hello World”)

• Press enter

• Welcome to Programming!

Open Python

FYI:

In programming text is knows as a

string. Strings include all letters,

numbers and characters.

Page 8: KS3 Python An Introduction to Textual Programming

• Now lets get the user to input some text:

• Type: input (“What is your name”)

• What happens?

• What would make it neater?

User Input

Code What is does

\n Goes to a new line

\t Creates a tab indent

Page 9: KS3 Python An Introduction to Textual Programming

Useful

Code What is does

\n Goes to a new line

\t Creates a tab indent

Page 10: KS3 Python An Introduction to Textual Programming

Manipulating StringsMethod Description Example

capitalize() Changes the strings first letter to upercase string.capitalize()

upper() changes the string to all uppercase letters string.upper()

lower() changes the string to all lowercase letters string.lower()

replace(old, new)

replaces a letter with another letter string.replace (‘o’, ‘*’)

count(x) returns the number of occurrences of x string.count(‘o’)

find(x) finds the index of the first occurrence of x or -1 if its not found.

string.find (‘o’)

isalpha() Returns true ii string is all letters string.isalpha()

isnumeric() Returns true ii string is all numbers string.isnumeric()

Page 11: KS3 Python An Introduction to Textual Programming

• You learned about variables in Scratch.

• You can assign variables in a similar way in Python.

• Try: name = input (“Please enter your name: \n”) print (“Hello”, name)Try asking more questions and setting variables. Can you create a conversation?

• What do you notice about using text? • Do you have to put “ “ around variables?

Variables

Page 12: KS3 Python An Introduction to Textual Programming

MATHS

Page 13: KS3 Python An Introduction to Textual Programming

• 8*7

• 10253/565

• 568+452

• 45855-65

• “Hello ” * 3

Try These

Page 14: KS3 Python An Introduction to Textual Programming

LESSON 2

Page 15: KS3 Python An Introduction to Textual Programming

• Remember you can think of a variable as being a container where you can store data or a value for use later.

• You can choose virtually any name for a variable, except for protected words.

• Ways of storing a variable: A = 8Try : A*A

a,b,c = 1,2,3Try: a+b+c

Variables

Page 16: KS3 Python An Introduction to Textual Programming

• If you set a variable you can change the variable like this:

• a = 9

• a = 9 + 1 (So now a = 10)

• a = a + 1 (Again a = 10)

Good To Know

Page 17: KS3 Python An Introduction to Textual Programming

• There are different types of numbers that you can use in python. We are going to look at integers.

– Integer: A whole number, not a fraction

• To convert something x to an integer, type the following: – int(x)

• If you ask the user for input it will automatically be a string so you will need to convert it to an integer.

• Best way: – no = int (input (“Please enter a number: \t”))– no will be an integer!

NEED TO KNOW

Page 18: KS3 Python An Introduction to Textual Programming

• How could you write the code to ask the user for a number and then multiply the number by 10?

• Write a program to ask the user for a number and multiply it by 10.

• DON’T USE THE SHELL Press File, New

Logic: Think

Page 19: KS3 Python An Introduction to Textual Programming

• Ask the user for a number:

• Set the number as a variable

• Then multiply their number by 5.

Try converting

Page 20: KS3 Python An Introduction to Textual Programming

PROGRAMMINGNow you have the basics lets start programming

Page 21: KS3 Python An Introduction to Textual Programming

• Now lets write our first proper program.

• Click File New

• This will open up a new window

• This is where you will write your code.

• Press F5 to run your code. You must save the work in your H Drive Python Practice Folder

First Program

Page 22: KS3 Python An Introduction to Textual Programming

• Write a program to ask a user for a number of hours and then convert it to minutes.

• How can you convert from hours to minutes?

• Save it as “Hour to Minute Convertor”

• Extension: – Create other conversion programs:

• Miles to Kilometres• Feet to Meters• Years to days• Years to minutes• Or anything else you think might be useful.

Task

Don’t forget to convert the

input to an integer.

Page 23: KS3 Python An Introduction to Textual Programming

LESSON 3

Page 24: KS3 Python An Introduction to Textual Programming

print (“Hello World”)

input (“What is your name”)

name = input (“Please enter your name: \n) print (“Hello”, name)

A = 8

no = int (input (“Please enter a number: \t”))int(x)

Reminder

Page 25: KS3 Python An Introduction to Textual Programming

• What can you remember from IF statements from Scratch?

• Its very similar in Python.

IF Statements

Page 26: KS3 Python An Introduction to Textual Programming

if expression: statement(s)

else: statement(s)

If Else

Page 27: KS3 Python An Introduction to Textual Programming

• Write a program which asks the user for a number between 1 and 10 and then tells them if it is over 5 or under 5.

• Save your program

Try it.

Page 28: KS3 Python An Introduction to Textual Programming

LESSON 4

Page 29: KS3 Python An Introduction to Textual Programming

• What happens if you want to check more than one situation.

• E.G: If you wanted to calculate if someone was a child, teenager, or Adult.

• IF ELIF and ELSE can be used for this.

IF ELIF ELSE

Page 30: KS3 Python An Introduction to Textual Programming

if expression1: statement(s)

elif expression2: statement(s)

elif expression3: statement(s)

else: statement(s)

IF, ELIF, ElSE

Page 31: KS3 Python An Introduction to Textual Programming

• Write a program to say whether a person is a child, teenager, or adult.

• If they are over 18 they are an adult• If they are under 13 they are a child• Otherwise they are a teenager

Try it

Page 32: KS3 Python An Introduction to Textual Programming

The Code

Page 33: KS3 Python An Introduction to Textual Programming

WHILE

Page 34: KS3 Python An Introduction to Textual Programming

While Loop

Page 35: KS3 Python An Introduction to Textual Programming

• From 10 count to 50 using a while loop.

• Advanced: – Ask the user for the start number– Ask the user for the last number– Print the list of numbers

– You could ask the what jumps they want to go up to. E.G: Count in 10s up to 100.

Try it

Page 36: KS3 Python An Introduction to Textual Programming

ASSESSMENT

Page 37: KS3 Python An Introduction to Textual Programming

• Level 3: Make something happen by writing your own set of instructions

• Level 4: Create a set of instructions for a programme, and improve your instructions to make the programme more efficient

• Level 5: Create precise and accurate sequences of instructions

Assessment

Page 38: KS3 Python An Introduction to Textual Programming

• You must create an original game. The game will be in the format of a quiz. The way the game looks and works will be completely up to you.

• Your game will have the following; – Ask the users name and use this in the quiz– At least 3 questions– A scoring system– At the end of the game it will say the users score

• Your game might include: – An introduction to the quiz– Anything else you think would improve the game!

Assessment

Page 39: KS3 Python An Introduction to Textual Programming

LAST PYTHON LESSONCan you put together what you learned to create a game.

Page 40: KS3 Python An Introduction to Textual Programming

• Create a game where the computer will generate a random number and the user will try and guess the number.

• The game will feedback if the guess is too high or too low.

• You will need to use the while or for loop to get the game to keep running.

Random Function

Page 41: KS3 Python An Introduction to Textual Programming

Random

Page 42: KS3 Python An Introduction to Textual Programming

• Create a game where the computer will generate a random number and the user will try and guess the number.

• The game will feedback if the guess is too high or too low.

• You will need to use the while or for loop to get the game to keep running.

Random Function

Page 43: KS3 Python An Introduction to Textual Programming

• Set a variable as a random number

• Ask the user to enter a number and save as a variable.

• If the user number is smaller than the computer number then say its to small.

• If its too big say its too big.

• If they are the same say the got it.

Guessing Game