GCSE COMPUTER SCIENCE Practical Programming using Python Lesson 4 - Selection.

Slides:



Advertisements
Similar presentations
From Scratch to Python Learn to program like the big boys / girls!
Advertisements

An Introduction to Textual Programming
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Variables When programming it is often necessary to store a value for use later on in the program. A variable is a label given to a location in memory.
PROGRAMMING In Lesson 2. STARTER ACTIVITY Complete the starter activity in your python folder – lesson 2 Now we will see how you got on and update your.
Python Selection. All the programs you have been developing so far have been sequential, this means that each instruction is executed in a set order.
Sequencing The most simple type of program uses sequencing, a set of instructions carried out one after another. Start End Display “Computer” Display “Science”
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals.
GCSE Computing: Programming GCSE Programming Remembering Python.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Input, Output and Variables GCSE Computer Science – Python.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
PYTHON PROGRAMMING Year 9. Objective and Outcome Teaching Objective Today we will look at conditional statements in order to understand how programs can.
JavaScript: Conditionals contd.
GCSE COMPUTER SCIENCE Practical Programming using Python
GCSE COMPUTER SCIENCE Practical Programming using Python
Create Your Own Quiz using_Python KS3 COMPUTING KS3 Computing
Writing algorithms Introduction to Python.
Whatcha doin'? Aims: To start using Python. To understand loops.
GCSE COMPUTER SCIENCE Practical Programming using Python
Input and Output Upsorn Praphamontripong CS 1110
Lesson 1 - Sequencing.
GCSE COMPUTER SCIENCE Practical Programming using Python
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
Introduction to Python
Line Continuation, Output Formatting, and Decision Structures
Formatting Output.
Lesson 4 - Challenges.
Lesson 1 An Introduction
Programming Mehdi Bukhari.
IF statements.
Introduction to Programming
Chapter 2.1 Control Structures (Selection)
Variables, Expressions, and IO
Lesson 3 - Repetition.
Engineering Innovation Center
Lesson 1 Learning Objectives
Learning to Program in Python
Learning to Program in Python
Line Continuation, Output Formatting, and Decision Structures
Introduction to Programming
And now for something completely different . . .
Learning to Program in Python
Programming In Lesson 3.
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
Story time Task 5 Computer Programming Gray , Calibri 24
Introduction to Programming
Selection Control Structure
Topics The if Statement The if-else Statement Comparing Strings
Starter answer these questions in your book
Introduction In today’s lesson we will look at: why Python?
PYTHON: BUILDING BLOCKS Sequencing & Selection
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
CISC101 Reminders All assignments are now posted.
Python Basics with Jupyter Notebook
Beginning Python Programming
Chapter 3: Selection Structures: Making Decisions
Introduction to Python
Introduction to Programming
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Selection Control Structure
Class code for pythonroom.com cchsp2cs
Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness
Year 8 Computer Science Digital Portfolio
Getting Started in Python
Presentation transcript:

GCSE COMPUTER SCIENCE Practical Programming using Python Lesson 4 - Selection

Inputting Numbers When inputting numbers into variables you need to tell Python what data type to expect We use int to tell Python that we are going to enter an integer (whole number). We use float when working with decimal places. We use str to convert a number into a string.

Activity 1 Write the following Python program and press F5 to run. Save the program as numbers1. Place a screenshot of your code here Create a program to ask somebody to enter two numbers. Once entered it should take the second number away from the first number and output the result.

Activity 2 Write the following Python program and press F5 to run. Save the program as numbers2. Place a screenshot of your code here Create a program to ask somebody to enter the height and width of a rectangle and work out the area. Think about what to call your variables carefully.

Activity 3 Write the following Python program and press F5 to run. Save the program as numbers3. Place a screenshot of your code here Create a program to ask somebody to enter three numbers. Once entered it should multiply them all together and output the result.

Selection All the programs you have been developing so far have been sequential, this means that each instruction is executed in set order. With selection the path through a program can be decided by looking at a condition and then taking one of a set of alternative paths based on the result. Selection in flowcharts is represented using the decision symbol.

Selection Example 1 Start End Is Age>=1 5? No Yes Input Age Display “You can watch this movie” Display “You are too young to watch this movie” To indent your text (four spaces) use the TAB key, above caps lock.

Activity 4 Write the following Python program and press F5 to run. Save the program as selection1. Place a screenshot of your code here Create the program on the previous slide so when you run the program you are asked for your age. Run the program once and enter a number above 15, then again and enter a number below 15 to see the difference.

Comparison Operators == Is equal to > Is greater than < Is less than != Is not equal to >= Is greater than or equal to <= Is less than or equal to You can also use the logical operators AND, OR and NOT. As well as the >= operator there are many more you can use in your programs.

Selection Example 2 A program to guess your teachers favourite subject! Start End If guess = favsub No Yes favsub = “Computing” Input Subject Display “That is my fav..” Display “Unlucky, try again” favsub = “computing” guess = input(“What is your favourite subject?”) if guess.lower() == favsub: print(“That is my..”) print(“Unlucky, try again!”) else:

Activity 5 Write the following Python program and press F5 to run. Save the program as selection2. Place a screenshot of your code here Create the program on the previous slide, you can change the subject to whichever you please. You may also change what is printed out.

Activity 6 Write the following Python program and press F5 to run. Save the program as selection3. Place a screenshot of your code here Create a program to ask someone what gender they are. If they are male it should say “You are male”, if they are female it should say “You are female”.

Activity 7 Write the following Python program and press F5 to run. Save the program as selection4. Place a screenshot of your code here Create a program to ask someone if the are between the ages of 4 and 18, if they are it should print out “You should be at school”, if they are not print out “You are not of school age”. Hint: Use the and operator.

Activity 8 Write the following Python program and press F5 to run. Save the program as selection5. Place a screenshot of your code here Create a program to ask someone what their name is, if it is the same name as yours it should say “You’re cool, else it should say ”You suck!”.

More Options Sometimes in a program you need more than two options. In this case we use the elif keyword. character = input(“Who is your favourite computer game character? ”) if character.lower() == “sonic”: print(“Me too! Sonic is also my favourite!”) elif character.lower() == “mario”: print(“Mario is ok but he’s overrated.”) else: print(“This character is just rubbish, why did you choose them!?”) An If statement must start with If and end with Else, however, you may use as many Elif statements in the middle as you need.

Activity 9 Write the following Python program and press F5 to run. Save the program as selection6. Place a screenshot of your code here Create the program on the previous slide using two of your own favourite characters.

Activity 10 Write the following Python program and press F5 to run. Save the program as selection7. Place a screenshot of your code here Write a program that asks the user their favourite team and if it the same as your team display “Cool team!”. If it is not the same display “They’re a rubbish team!”

Activity 11 Write the following Python program and press F5 to run. Save the program as selection8. Place a screenshot of your code here Update your previous program to add an Elif statement to allow a third option. For example use your second favourite team or a team from a different sport.

Activity 12 Write the following Python program and press F5 to run. Save the program as selection9. Place a screenshot of your code here Create a program that outputs something different for each day of the week. You will need to use multiple Elif statements.

Activity 13 Write the following Python program and press F5 to run. Save the program as selection10. Place a screenshot of your code here Create a program that of your own choice that showcases the techniques you have learn from the first four Python lessons. It should take advantage of variables, sequencing and selection.

How Can I Improve My Coding Skills? Coding is something you need to practice often to get good at it. Codecademy is a website that allows you to practice the syntax of Python (..and other languages). Best of all, its free and fun! Sign up using a personal address and start working through the Python course. ACTIVITY Start working through each exercise to practice.