Introduction to Programming

Similar documents
Introduction to Programming

Introduction to Programming

Introduction to Programming

Introduction to Programming

Introduction to Programming

Introduction to Programming

Introduction to Programming

Programming for Engineers in Python. Recitation 1

Programming for Engineers in Python. Autumn

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

Flow Control: Branches and loops

Hacettepe University Computer Engineering Department. Programming in. BBM103 Introduction to Programming Lab 1 Week 4. Fall 2018

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

Decisions, Decisions. Testing, testing C H A P T E R 7

Chapter 5 Conditional and Iterative Statements. Statement are the instructions given to the computer to perform any kind of action.

Software and Programming 1

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Add Subtract Multiply Divide

Introduction to: Computers & Programming: Review prior to 1 st Midterm

Conditionals and Recursion. Python Part 4

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

ME30 Lab3 Decisions. February 20, 2019

CMSC 201 Computer Science I for Majors

boolean Expressions Relational and Equality Operators and if-then-else Conditional Control Statement

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

Software and Programming 1

Text Input and Conditionals

Flow of Control Branching 2. Cheng, Wei COMP May 19, Title

Unit 7: Algorithms and Python CS 101, Fall 2018

CS 115 Lecture 8. Selection: the if statement. Neil Moore

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

cs1114 REVIEW of details test closed laptop period

Python Activity 5: Boolean Expressions and Selection Statements

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Exercise 2: Automata Theory

Computer Science 217

Python Programming. Introduction Part II

OCR Pseudocode to Python

Python Input, output and variables

1. What is the minimum number of bits needed to store a single piece of data representing: a. An integer between 0 and 100?

Part III Appendices 165

Pupil Name. Year. Teacher. Target Level. Key Stage 3 Self-Assessment Year 9 Python. Spelling Test No 3. Spelling Test No 2. Spelling Test No 1

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Computer Science 217

Lecture 3 Tao Wang 1

Python Input, output and variables. Lecture 22 COMPSCI111/111G SS 2016

Lab 5a Shell Script Lab 4 Using Arithmetic Operators in shell script

Programming in Python 3

Python 2 Conditionals and loops

Control Flow: Loop Statements

CS 105 Lab As a review of what we did last week a. What are two ways in which the Python shell is useful to us?

Python for Informatics

1. The programming language C is more than 30 years old. True or False? (Circle your choice.)

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

INTERMEDIATE LEVEL PYTHON PROGRAMMING SELECTION AND CONDITIONALS V1.0

CS313D: ADVANCED PROGRAMMING LANGUAGE

Looping Subtasks. We will examine some basic algorithms that use the while and if constructs. These subtasks include

CS 141, Lecture 3. Please login to the Math/Programming profile, and look for IDLE (3.4 or the unnumbered. <-- fine <-- fine <-- broken

CMSC201 Computer Science I for Majors

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Quiz. Introduction: Python. In this project, you ll make a quiz game to challenge your friends. Activity Checklist.

CS101 Lecture 24: Thinking in Python: Input and Output Variables and Arithmetic. Aaron Stevens 28 March Overview/Questions

Scripting Languages. Python basics

Fundamentals of Programming (Python) Getting Started with Programming

Introduction to programming using Python

Previously. Iteration. Date and time structures. Modularisation.

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

Short, Unique and Mysterious

Introduction to Python (All the Basic Stuff)

Eng. Mohammed S. Abdualal

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Basic Syntax - First Program 1

Brush up Python. December 20, 2018

CSE 115. Introduction to Computer Science I

Lab 2: Structured Program Development in C

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

Python Unit

Course Outline. Introduction to java

! Widely available. ! Widely used. ! Variety of automatic checks for mistakes in programs. ! Embraces full set of modern abstractions. Caveat.

CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have

CSCE 110: Programming I

Python Problems MTH 151. Texas A&M University. November 8, 2017

Fundamentals: Expressions and Assignment

CSI Lab 02. Tuesday, January 21st

Getting Started with Python

CS 112: Intro to Comp Prog

ENGR 101 Engineering Design Workshop

Lab 4 Fruitful Functions

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

CSE 115. Introduction to Computer Science I

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

CS 115 Exam 1, Fall 2015 Thu. 09/24/2015

Lecture 5: Making Decisions

Common Loop Algorithms 9/21/16 42

CSCI 141 Computer Programming I. Filip Jagodzinski

Transcription:

Introduction to Programming Python Lab 7: if Statement PythonLab7 lecture slides.ppt 14 November 2017 Ping Brennan (p.brennan@bbk.ac.uk) 1

Getting Started Create a new folder in your disk space with the name PythonLab7 Launch the Python Integrated Development Environment (IDLE) - begin with the Start icon in the lower left corner of the screen. If you are in a DCSIS laboratory, search using the keyword Python and click on IDLE (Python 3.6 64-bit) A window with the title Python 3.6.2 Shell should appear. This window is the Shell. 2

Getting Started (2) If you are in the ITS laboratory MAL 109, select the options in the order shown: Start -> All Programs -> Departmental Software -> Computer Science -> Python 3.4 -> IDLE (Python 3.4 GUI 64 bit) A window with the title Python 3.4.4rc1 Shell should appear. This window is the Shell. If the window does not appear, click on Start and then in the box Search programs and files write IDLE. A list will appear. Click on IDLE(Python 3.4 GUI-64 bit). A window with the title Python 3.4.4rc1 should appear. This window is the Shell. In the Shell click on File. A drop down menu will appear. Click on New File. A window with the title Untitled should appear. This window is the Editor. 3

Getting Started (3) In the Editor, click on File, and then in the drop down menu click on Save As. A window showing a list of folders should appear. To search any folder on the list, double click on the folder. Find the folder PythonLab7 and double click on it. In the box File name at the bottom of the window 1. Type QuizGrading.py 2. Then click on the button Save in the lower right corner of the window. The title of the Editor should change to show the location of the file QuizGrading.py. 4

Objectives of the exercises set Understand the use of multiple if statements to solve problems that have several levels of decision making. Python provides the special construct elif for creating if statements containing multiple branches (selections). When using multiple if statements, we usually test general conditions after we test the more specific conditions first. Use relational operators in conditions that involve comparing two values. Python relational operators Description < Less than <= Less than or equal > Greater than >= Greater than or equal == Equal!= Not equal 5

Objectives of the exercises set (2) An if statement is used to implement a decision. When a condition is satisfied (that is, True), one set of statements is executed. Otherwise, another set of statements is executed. The syntax of an if statement with No else branch is shown below. The colon indicates a header. Syntax Example Flow chart for if statement with No else branch if condition : statement(s) floor = int(input("floor: ")) actualfloor = floor if floor > 13 : actualfloor = actualfloor 1 Condition True floor > 13? False #execute the statement above #only if the condition is True #- True branch actualfloor = actualfloor - 1 No else branch Note: Indent the statements in the True branch. 6

Objectives of the exercises set (3) The syntax of an if statement with else branch is shown below. The colon indicates a header. Syntax Example Flow chart for if statement with else branch if condition : floor = int(input("floor: ")) statement(s) actualfloor = 0 else : if floor > 13 : statement(s) actualfloor = floor 1 # execute only if the # condition is True # True branch else : actualfloor = floor # execute only if the # condition is False # False branch Note: Align if and else Indent the statements in each branch. True actualfloor = floor - 1 floor > 13? Condition False actualfloor = floor 7

Objectives of the exercises set (4) The syntax of an elif statement is as follows. Syntax if condition : statement(s) elif condition : statement(s) else : statement(s) Example scorea = int(input("enter a score for player A: ")) scoreb = int(input("enter a score for player B: ")) if scorea > scoreb : print("a won") elif scoreb > scorea : print("b won") else : print("game tied") Note: Align if, elif and else Indent the statements in each branch. Understand the use of the remainder operator % in an arithmetic expression. 8

Program QuizGrading.py: Quiz grading Question 2: Problem statement Write a program that inputs an integer from the keyboard. This integer is the score. Use an appropriate prompt. A letter grade is assigned to the score, according to the following table. Score 90-100 A 80-89 B 70-79 C 60-69 D <60 E Grade Include in your program a statement to print the letter grade, together with an appropriate description. See Python for Everyone, R3.18. 9

Program QuizGrading.py: Quiz grading (2) Problem solving the flow chart below illustrates a possible solution. Read in a score score >=90? True Print the letter grade A False score >=80? True Print the letter grade B False score >=70? True Print the letter grade C False False score >=60? True Print the letter grade D Print the letter grade E 10

Program QuizGrading.py: Quiz grading (3) Problem solving - Convert the pseudo code below into a sequence of Python statements in your program. 1. Read in an integer and store it in the variable score *. 2. Write the statements below to check if the score is greater than or equal to 90, and then print the letter grade A in the True branch. if score >= 90 : print("grade A") # True branch 3. Add the following statements to check if the score is greater than or equal to 80, and then print the letter grade B. elif score >= 80 : print("grade B") # True branch 4. Write elif and print statements similar to step 3 to check if the score is greater than or equal to 70, and then print the letter grade C. *Hint: First use the input function to read in a numeric value typed in at the keyboard. Then use the function int to convert the input string to an integer and store it in the variable score. 11

Program QuizGrading.py: Quiz grading (4) Problem solving (continued) 5. Write elif and print statements similar to step 3 to check if the score is greater than or equal to 60, and then print the letter grade D. 6. Lastly, add the statements below to print the letter grade E using the else statement. else : print("grade E") Provide a comment at the beginning of the program to explain the purpose of the program together with your name and the date. Save the program to the file QuizGrading.py and then run it. Note: Align if, elif and else. Also, indent the statements in each branch. 12

Program LeapYear.py: Leap year Create a new Editor for a new file called LeapYear.py Question 3: Problem statement A year with 366 days is called a leap year. Usually years that are divisible by 4 are leap years, for example, 1996. However, years that are divisible by 100 are not leap years, unless the year is also divisible by 400. Write a program that asks the user for a year and computes whether the year is a leap year. Use an appropriate print statement to display the result of the computation. See Python for Everyone, P3.27. 13

Program LeapYear.py: Leap year (2) Problem solving - The following method can be used to check: o If a year is divisible by 4 but not by 100, it is a leap year. o If a year is divisible by 4 and by 100, it is not a leap year unless it is also divisible by 400. Three conditions to consider - we usually start with the tests for the more specific conditions first before testing the general conditions. (year%400) == 0 (year%100) == 0 (year%4) == 0 # First condition tests if # year is divisible by 400 # Second condition tests if # year is divisible by 100 # Third condition tests if # year is divisible by 4 14

Program LeapYear.py: Leap year (3) Problem solving - Convert the pseudo code below into a sequence of Python statements in your program. 1. Read in an integer and store it in the variable year 2. Write the statements below to check if the year is divisible by 400, and then print out the message, Leap year if year%400 == 0 : print("leap year") # True branch 3. Add the statements below to check if the year is divisible by 100, and then print the message, Not a leap year, in the True branch. elif year%100 == 0 : print("not a leap year") # True branch 4. Write elif and print statements similar to step 3 to check if the year is divisible by 4, and then print the message, Leap year 15

Program LeapYear.py: Leap year (4) Problem solving (continued) 5. Lastly, add the statements below to print the message, Not a leap year else : print("not a leap year") Provide a comment at the beginning of the program to explain the purpose of the program together with your name and the date. Save the program to the file LeapYear.py and then run it 16

Supplementary Questions for Private Study The laboratory worksheet contains supplementary questions in section 4 for private study. You are encouraged to complete the supplementary questions at home, or in the laboratory if you have time after completing questions 2 to 3. 17

Appendix A Testing different user inputs for the leap year problem The following table shows some user inputs for the year and the result of the computation. User input for the year year%400 == 0 year%100 == 0 year%4 == 0 result 2016 False False True Leap year 1600 True True True Leap year 1800 False True True Not a leap year 18