Learning to Program in Python

Slides:



Advertisements
Similar presentations
PSEUDOCODE & FLOW CHART
Advertisements

ITEC113 Algorithms and Programming Techniques
Conditional Statements Introduction to Computing Science and Programming I.
Chapter 6 Horstmann Programs that make decisions: the IF command.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
CIS101 Introduction to Computing Week 12 Spring 2004.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Chapter 2 - Algorithms and Design
By the end of this session you should be able to...
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
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.
1 Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping.
Agenda Basic Logic Purpose if statement if / else statement
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
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.
Controlling Program Flow with Decision Structures.
Algorithms and Pseudocode
Introduction to Programming Python Lab 7: if Statement 19 February PythonLab7 lecture slides.ppt Ping Brennan
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Algorithms and Flowcharts
GCSE COMPUTER SCIENCE Practical Programming using Python Lesson 4 - Selection.
PYTHON PROGRAMMING Year 9. Objective and Outcome Teaching Objective Today we will look at conditional statements in order to understand how programs can.
Foundations of Programming: Java
Learning outcomes 5 Developing Code – Using Flowcharts
GCSE COMPUTER SCIENCE Practical Programming using Python
Topic: Conditional Statements – Part 1
Selection and Python Syntax
Python: Control Structures
Debugging and Random Numbers
IF statements.
Introduction to Programmng in Python
If, else, elif.
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Computer Science and an introduction to Pascal
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Learning to Program in Python
Selection CIS 40 – Introduction to Programming in Python
Learning to Program in Python
Introduction to Programming
Learning to Program in Python
Learning to Program in Python
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.
Learning to Program in Python
1) C program development 2) Selection structure
Learning to Program in Python
Introduction to TouchDevelop
Introduction to Programming
Java Programming Control Structures Part 1
3. Decision Structures Rocky K. C. Chang 19 September 2018
Chapter 5: Control Structure
Learning to Program in Python
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
PYTHON: BUILDING BLOCKS Sequencing & Selection
CHAPTER 5: Control Flow Tools (if statement)
Just Basic Lessons 9 Mr. Kalmes.
Basic Concepts of Algorithm
Introduction to Python
Introduction to Programming
WJEC GCSE Computer Science
Year 8 Computer Science Digital Portfolio
Presentation transcript:

Learning to Program in Python Concept 4 Conditionals (if statements) This lesson will focus on the IF statement. It is assumed that variables, data types, casting, and input-output have been covered before this lesson. Conditionals are considered to be the first key Threshold Concepts in learning how to Program. The tasks and ideas would require several classes to ensure students have time to engage with the concept.

Learning Intentions From this lesson the students will: Understand the concept of a conditional if statement. Read and Write code to carry out simple tasks. Use relational operators to make comparisons. The concept learned here, plus some of the programs written later in the lesson by the students, are used in the follow-up lesson on if..elif..else statements. It will make a difference if any programs written here are saved with appropriate file names so they can be recalled in the follow-up lesson. The main Learning Outcomes (LO) achieved are highlighted throughout the lesson and tasks. The list is not exhaustive, and consultation with the LCCS spec is encouraged at all times throughout the implementation of the course.

if Statement indentation if statement: executes a set of commands only if a certain condition is True. Otherwise, the commands are skipped. Syntax: The If statement is read as “If the temperature is greater than zero is True then execute the following commands” Note the indentation. indentation

if Statement You can execute as many lines of code within an if statement as you wish. Note that all lines within the If statement are indented. These will all be executed when the condition is True.

if Statement if (condition is True): Execute these set of commands Otherwise the commands are skipped. Syntax if condition: execute statements (note indentation) A flow chart of the If statement follows

if flowchart Flow Chart starts here Execute your code Continue Execute these lines Execute your code A FLOW chart representation ….

Multiple if statements Can you predict what the output will be ? Encourage students to make a prediction before testing out the code. The purpose of this exercise is to highlight that the interpreter will continue executing If statements whether the previous ones were True Or False. The PRIMM method will encourage engagement. (Predict Run Investigate Modify Make) LO 1.22 students should be able to read, write, test, and modify computer programs

output from Multiple if program All three if statements were true so all three commands are executed. The output from program with multiple If statements. LO 1.4 students should be able to solve problems using skills of logic

Testing 2 conditions in 1 if Read this code. Especially the and operator. Predict what happens. 3 temperatures will slip through? One of them is 40. Encourage students to make a prediction before testing out the code. There are three temperatures that will produce no output (0, 40 and 70). Pose the question as a brain teaser. (It is mentioned on the slide.)

== != < > <= >= Fill in the Operation and Answer columns for each comparison - Describe the operation in your own words- Relational Operator True / False Operation Answer (True/False) == 1 + 1 == 2 equals True != 3.2 != 2.5 does not equal < 10 < 5 less than False > 10 > 5 greater than <= 24 <= 13 less than or equal to >= 5.0 >= 5.0 greater than or equal to Encourage the students to use their IDE to figure out and understand the less familiar operations. Draw attention to the “equals” relational operator. Ask why there are two equals. (One equal sign is an assignment!!) The etasksheet above can be downloaded from the website, completed by the students (as a fillable pdf) and stored in their online space. Or it can be printed out for a written class task. The answers are revealed when the slide show is run.

Write a program that compares two numbers entered by the user. Comparing User Inputs Write a program that compares two numbers entered by the user. If the numbers are different, tell the user which number is greater. If the numbers are equal, tell the user the numbers are equal. Before you begin, some reminders……. Ask the students what the reminders might be ….. In particular casting inputs from strings to integers LO 1.1 students should be able to describe a systematic process for solving problems and making decisions

Reminders on User Inputs Python takes all input as STRINGS. You must CAST the input to the DATA TYPE you want. The example below casts the user input from a string to an integer. Before showing the sample code on the next slide, give the students some time to write the algorithm / program to compare two numbers entered by the user.

Comparing User Inputs .. sample code Sample Code for comparing two numbers entered by the user. This will be used as one of the kick off points for introducing if..then..else statements What code might be behind me?

Create Your Own … Guessing Game Your task is to write a program that asks the user to guess a number between 1 and 5 that your program randomly generates. You must write the algorithm first and then code it in Python. User Interface You must tell the user if they guessed correctly and congratulate them. If they guessed incorrectly, you must tell them if their guess was too high or too low. And let them have 1 more go. If they are wrong, then say Hard Luck!!!! This is the bones of the guessing game. At this point, students can only control the flow of their programs using If statements. Students must write the algorithm first. The next slide shows a possible algorithm in pseudo-code. On ncca.ie, use the html resource to write the guessing game in Scratch and analyse its CT level. Compare the 2 programs and the 2 User Interfaces. When students study LOOPS they will be able to offer the user unlimited amount of turns and the option to exit the program. LO 1.16 students should be able to compare two different user interfaces and identify different design decisions that shape the user experience

pseudo code .. for guessing game generate a random number between 1 and 5; userNumber = user’s first guess; if userNumber == gameNumber { print congratulations; } if userNumber > gameNumber { print your guess is too high; } if userNumber < gameNumber { print your guess is too low; } if userNumber != gameNumber { userNumber = user’s second guess; if userNumber == gameNumber { print congratulations; } print Hard Luck; } } NESTED if statements Students finding it hard to write the algorithm/program unaided could use this pseudo code as a scaffold for writing their own code. The algorithm must introduce the concept of NESTED if statements … that is if statements within if statements. LO 2.5 students should be able to use pseudo code to outline the functionality of an algorithm

Lesson Review As a result of this lesson I am able to: Understand the concept of a conditional if. Read and Write code to carry out simple tasks. Use relational operators to make comparisons. The main Learning Outcomes (LO) achieved are highlighted throughout the lesson and tasks. The list is not exhaustive, and consultation with the LCCS spec is encouraged at all times throughout the implementation of the course.