Programming for GCSE Topic 3.1: If Statements in Python T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science.

Slides:



Advertisements
Similar presentations
Lesson 6: Boolean and If Statements Computer Science 1 Mr. Bernstein.
Advertisements

Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.
Programming for GCSE Topic 9.1: Logic Circuits T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science Queen Mary.
3. S/E with Control Structures 3.1 Relational Operators and Expressions 3.2 If and if-else Statements 3.3 The Type Double 3.4 Program Design with the While.
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Making Decisions in Python Sec 9-10 Web Design. Objectives The student will: Understand how to make a decision in Python Understand the structure of an.
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
Conditional Statements Introduction to Computing Science and Programming I.
James Tam Making Decisions In Python In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
Chapter 6 Horstmann Programs that make decisions: the IF command.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Decision Making George Mason University. Today’s topics 2 Review of Chapter 2: Decision Making Go over exercises Decision making in Python.
Geography 465 Assignments, Conditionals, and Loops.
Programming for GCSE Topic 3.3: Boolean Logic and Truth Tables
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
Python Control of Flow.
index.php Palmyra Area High School 1.
Chapter 9 IF Statement Bernard Chen. If Statement The main statement used for selecting from alternative actions based on test results It’s the primary.
The Ruby Programming Language
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Python Lab: Control Statements Conditionals, Loops, Iterations Proteomics Informatics, Spring 2014 Week 4 18 th Feb, 2014
Decision Structures and Boolean Logic
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.
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
INLS 560 – C ONDITIONALS Instructor: Jason Carter.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Programming for GCSE Topic 4.2: Faults and Debugging T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science Queen.
Python Conditionals chapter 5
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
How the Session Works Outline Practical on arrival Talk 1 Reflect on practical Clarify concepts Practical exercises at your own pace Talk 2: Further concepts.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
Programming for GCSE Topic 8.1: Functions T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science Queen Mary University.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
Programming for GCSE Topic 9.2: Circuits for Adding T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science Queen.
4. If Statements 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.
 Type Called bool  Bool has only two possible values: True and False.
Computer Science 1000 LOGO II. Boolean Expressions like Excel and Scratch, LOGO supports three Boolean operators less than (
4. If Statements 1 Have your program make choices. Depending on a test have a program do different things Computer Programming BSc in Digital.
Python Basics.
Control Flow (Python) Dr. José M. Reyes Álamo.
Making Choices with if Statements
Topics The if Statement The if-else Statement Comparing Strings
4. If Statements Let's Learn Python and Pygame
PH2150 Scientific Computing Skills
Topics The if Statement The if-else Statement Comparing Strings
And now for something completely different . . .
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Selection Statements.
Introduction to Decision Structures and Boolean Variables
Computer Science Core Concepts
Topics The if Statement The if-else Statement Comparing Strings
Types, Truth, and Expressions (Part 2)
Understanding Conditions
The Data Element.
The Data Element.
Presentation transcript:

Programming for GCSE Topic 3.1: If Statements in Python T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science Queen Mary University of London

Aims If statement basics Visualising ‘If’ Language Boxes Scratch Comparisons Boolean expressions More complex ‘If’ statements

S IMPLE ‘I F ’ S TATEMENTS

Simple ‘If’ Statement Change the statements in a program age = int(input("How old?")) if age > 21: print("Cool! A grown up") Key word Condition : indentation

V ISUALISING ‘I F ’ Indentation versus brackets

Boxes An 'if' statement has an inside if condition A: Only enter the box when 'A'

Boxes An 'if' statement has an inside if condition A: else: Only enter this box when 'A' Only enter this box when not 'A'

If in Scratch Scratch blocks make the ‘inside’ idea obvious Conditio n Inside Conditio n Inside – condition true Inside – condition false

Other Languages Java uses ( ) – condition in brackets; no colon '{' – start of box '}' – end of box Pascal THEN instead of the colon BEGIN – start of box END – end of box

'If' Language Many instructions have 'if' language Do not use this service if you are travelling within the next four weeks, or if you are applying from outside the UK if travelling in next 4 weeks: print("Do not use this service") elif applying from outside UK: print("Do not use this service") else: print("Ok; use this service")

C OMPARISONS True and false expression – used in 'if'

True and False Expressions Comparisons Result can be 'True' or 'False' OperatorMeaning x > y x is greater than y x < y x is less than y x == y x is equals to y x != y x is NOT equal to y x <= y x is less than or equal to y x >= y x is greater than or equal to y

Examples What are the values of the following? ExpressionTrue or False? 10 > < != <= >= 11

Comparison of Strings Comparison operators work for strings Look at the following examples >>> "David" < "David Cameron" True >>> "Dave" < "David" True >>> "Tom" < "Thomas" False >>> "Bill" < "William" True >>> "AAA" < "AAB" True >>> "AAA" < "aaa" True >>> "aaa" < "AAA" False Using ASCII ordering, letter by letter

'=' and '==' Do not confuse Assignment Operator = Set variable to value (of expression) Equality Operator == Compare two expressions

M ORE C OMPLEX C ONDITIONAL S TATEMENTS

Else and Else If 'If' statement with an alternative if condition : statement 1 – when condition true else: statement 2 – when condition false if condition A : statement 1 – when condition A true elif condition B : statement 2 – when A false and B true else: statement 3 – when both A and B false

Compare these programs: age = input("How old are you?") if age < 22: print("Ah, youth!") if age >= 22: print("Cool! A grown up") age = input("How old are you?") if age < 22: print("Ah, youth!") else: print("Cool! A grown up") Equivalent

Teaching Issue Strong understanding of statements within statements Needed for loops too Only 'if' and 'if … else' are essential Boolean expression: true and false as values

Summary 'If' allows statements in the program to vary Comparison operator create 'conditions' (also called 'boolean expressions') Python uses 'indentation' to inside the 'if' from outside 'Inside' versus 'outside' essential for next topic: loops