KS3 Python An Introduction to Textual Programming

Preview:

Citation preview

KS3 Python

An Introduction to Textual Programming

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

Python 1

• 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

• 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

• 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

TEXT

• 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.

• 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

Useful

Code What is does

\n Goes to a new line

\t Creates a tab indent

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()

• 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

MATHS

• 8*7

• 10253/565

• 568+452

• 45855-65

• “Hello ” * 3

Try These

LESSON 2

• 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

• 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

• 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

• 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

• Ask the user for a number:

• Set the number as a variable

• Then multiply their number by 5.

Try converting

PROGRAMMINGNow you have the basics lets start 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

• 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.

LESSON 3

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

• What can you remember from IF statements from Scratch?

• Its very similar in Python.

IF Statements

if expression: statement(s)

else: statement(s)

If Else

• 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.

LESSON 4

• 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

if expression1: statement(s)

elif expression2: statement(s)

elif expression3: statement(s)

else: statement(s)

IF, ELIF, ElSE

• 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

The Code

WHILE

While Loop

• 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

ASSESSMENT

• 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

• 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

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

• 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

Random

• 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

• 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

Recommended