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

Introduction to Programming

Software and Programming 1

Common Loop Algorithms 9/21/16 42

Control Flow: Loop Statements

The while Loop 4/6/16 4

Software and Programming 1

Flow Control: Branches and loops

Programming for Engineers in Python. Recitation 1

Programming for Engineers in Python. Autumn

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Using IDLE for

Selection Statement ( if )

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?

CSE 115. Introduction to Computer Science I

LOOPS. Repetition using the while statement

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Teaching London Computing

Individual research task. You should all have completed the research task set last week. Please make sure you hand it in today.

Introduction to Python (All the Basic Stuff)

Previously. Iteration. Date and time structures. Modularisation.

CE151-4-AU UNIVERSITY OF ESSEX. Sample Examination Questions 2011 INTRODUCTION TO PROGRAMMING

Loops / Repetition Statements

Semester 2, 2018: Lab 1

Lab1. Introduction to Python. Lab 4: Selection Statement. Eng. Mai Z. Alyazji

Introduction to Python

Conditionals and Recursion. Python Part 4

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

CS111: PROGRAMMING LANGUAGE II

Unit E Step-by-Step: Programming with Python

An Introduction to Programming with C++ Sixth Edition. Chapter 7 The Repetition Structure

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Python Part III - Repeating Actions with Loops Jean-Yves Sgro February 23, 2017

Course Outline - COMP150. Lectures and Labs

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

Glossary Unit 1: Hardware and Software

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

Pace University. Fundamental Concepts of CS121 1

Python 2 Conditionals and loops

Previously. Iteration. Date and time structures. Modularisation.

CONTROL FLOW CREATING ALTERNATE PATHS OF EXECUTION USING: BRANCHING WHILE LOOPS FOR LOOPS

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Python Activity 5: Boolean Expressions and Selection Statements

LAB 13: ARRAYS (ONE DIMINSION)

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Unit 7: Algorithms and Python CS 101, Fall 2018

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

Fundamentals of Python: First Programs. Chapter 4: Strings (Indexing, Slicing, and Methods)

ME30 Lab3 Decisions. February 20, 2019

CSCI 141 Computer Programming I. Filip Jagodzinski

Algorithms and Data Structures

CS Week 14 Lab Exercise

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

Professor Hugh C. Lauer CS-1004 Introduction to Programming for Non-Majors

Visual Basic 2008 The programming part

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014

Lab # 02. Basic Elements of C++ _ Part1

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

CSE 101 Introduction to Computers Development / Tutorial / Lab Environment Setup

APCS Semester #1 Final Exam Practice Problems

ME 365 EXPERIMENT 3 INTRODUCTION TO LABVIEW

CMSC 201 Spring 2018

Fundamentals of Programming Session 23

Grouping Objects. Primitive Arrays and Iteration. Produced by: Dr. Siobhán Drohan. Department of Computing and Mathematics

General Guidelines: SAS Analyst

ECOR Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Python Games. Session 1 By Declan Fox

Chapter 4 Introduction to Control Statements

Introduction to Software Development (ISD) Week 3

Text Input and Conditionals

INFORMATION SHEET 24002/1: AN EXCEL PRIMER

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Iteration, Lists, and Algorithm Design 9/14/2017

Java Bytecode (binary file)

Lecture 4. Defining Functions


Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

CE221 Programming in C++ Part 1 Introduction

Introduction to Python

Fundamentals: Expressions and Assignment

Quiz 1: Functions and Procedures

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

COMP Assignment 1

Algorithms and Programming I. Lecture#12 Spring 2015

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

CSI33 Data Structures

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Before Class Install SDCC Instructions in Installing_SiLabs-SDCC- Drivers document. Solutions to Number Systems Worksheet. Announcements.

Computer Programming I - Unit 5 Lecture page 1 of 14

Introduction to Python and Programming. 1. Python is Like a Calculator. You Type Expressions. Python Computes Their Values /2 2**3 3*4+5*6

CMSC 201 Spring 2017 Lab 12 Recursion

Pseudo Code and Flow Charts. Chapter 1 Lesson 2

Transcription:

Introduction to Programming Python Lab 8: Loops PythonLab8 lecture slides.ppt 20 November 2018 Ping Brennan (p.brennan@bbk.ac.uk) 1

Getting Started Create a new folder in your disk space with the name PythonLab8 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 Python 3.4.1:Untitled (DCSIS) or Untitled (ITS) 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 PythonLab8 and double click on it. In the box File name at the bottom of the window 1. Type Vowels.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 Vowels.py. 4

Objectives of the exercises set Objectives Use for statements to implement count-controlled loops that iterate over a range of integer values or the contents of any container. Syntax Example Explanation for variable in container: statements #loop body All the statements in the block (loop body) have the same level of indentation. The header ends in a colon statename = "Ohio" for letter in statename: print(letter) # loop body The variable letter takes each of the values 'O', 'h', 'i', 'o' in turn for each iteration. iteration letter Output 1 O O 2 h h i 3 i o The string "Ohio" is stored in the variable statename. The loop body is executed for each successive character of the string in statename, starting with the first character. 4 o 5

Objectives of the exercises set (2) Objectives Use while statements to implement event-controlled loops. A while loop executes instructions repeatedly while a condition is true. Syntax Example Flow chart while condition : statements All the statements in the block (loop body) have the same indentation. The header ends in a colon. 1 i = 0 while i < 3 : print(i) i = i + 1 Output 0 1 2 In the example, the variable i is initialised outside the while loop (statement ) and updated in the loop body (statement ). 4 2 3 4 1 2 3 4 condition 1 6

Objectives of the exercises set (3) The table below shows the working of the while loop example shown on page 6. i i < 3? Output using print(i) 0 True 0 1 1 True 1 2 2 True 2 3 3 False end of the while loop i = i + 1 7

Program Vowels.py: Vowels Question 2: Problem statement Consider the following for loop, statename = "Ohio" for letter in statename : print(letter) The variable letter takes each of the values "O", "h", "i", "o" in turn. The output of the code is O h i o 8

Program Vowels.py: Vowels (2) Problem statement (continued) i. Write a program that requests a string from the keyboard and then prints out the characters in the string in a vertical line, as in the above example. (see page 8) ii. Add to your program code to count the number of lower case vowels in the input string and then print out this number after printing out the vertical line of characters. The vowels are a, e, i, o, u. 9

Program Vowels.py: Vowels (3) Problem solving - Convert the following pseudo code into a sequence of Python statements in your program. Input Process the input and display the output (steps 2 to 5). 1. Read in a string and store it in a variable named s. 2. Declare a variable named n and assign a value of 0 to it. The variable n is used to keep a count of the number of lower case vowels (a, e, i, o, u) in the input string s. 3. Add the following for statement to print out the characters in the string s in a vertical line, as shown in the output example on page 8. for letter in s : print(letter) # loop body Indent the print statement in the block (loop body). 10

Program Vowels.py: Vowels (4) Problem solving (continued) 4. Inside the for loop, add an if statement to check if the letter is equal to an "a", "e", "i", "o", or "u" after the print statement in step 3. Align the print and if statements at the same level of indentation. If the combined Boolean expression is true, then add 1 to the current value of n. Use the if statement below to help you get started. if letter == "a" or letter == "e" or complete the combined Boolean expression by adding test conditions for the remaining vowels (i,o,u) : Note the indentation of the code. # True branch - if the combined expression is true Indent & write an assignment statement to add 1 to n and store the result in n. Recall n keeps a count of the number of lower case vowels. 5. Outside the for loop, write the code to print out the number of lower case vowels. Align the print and for statements. 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 Vowels.py and then run it. 11

Program NumberProperties.py: NumberProperties Create a new Editor for a new file called NumberProperties.py Question 3: Problem statement Write a program that reads a list of strictly positive integers from the keyboard, one at a time. Use a while loop to read these integers. The end of the input is indicated by the integer 0. Note that this 0 is a sentinel in that it marks the end of the input but does not belong to the set of integers whose properties are sought (PFE Section 4.3). The program then prints out the following numbers. i. the average value ii. iii. iv. the smallest of the values the largest of the values the inclusive range, that is one more than the difference between the smallest and the largest values. It can be assumed that the input contains at least one number other than 0. See PFE P4.5. 12

Program NumberProperties.py: NumberProperties (2) Problem statement (continued) Include a comment with each number that is printed out, for example: The average value is: If you need to check how a while loop behaves, then test the following code. nextnumber = 5 while not (nextnumber == 0) : # test for entry to the loop print(nextnumber) # in the loop do something with the data nextnumber = nextnumber-1 # get the next number All the statements in the block (loop body) have the same indentation. 13

Program NumberProperties.py: NumberProperties (3) Input A set of positive integer numbers. For example, the numbers: 2 5 3 9 are typed in at the keyboard, one at a time. Computations 1. average = (2 + 5 + 3 + 9) / 4 2. mn = min(2, 5, 3, 9) 3. mx = max(2, 5, 3, 9) 4. range = (mx - mn) + 1 Output The average value is 4.75 The smallest value is 2 The largest value is 9 The range is 8 Note: The Python built-in function max returns the largest value of the arguments; and the function min returns the smallest value of the arguments. 14

Flow chart of a while loop for the program NumberProperties.py (4) The largest and smallest of the values are stored in the variables mx and mn respectively. 15

Program NumberProperties.py: NumberProperties (5) Input Process the input and display all required results (steps 2 to 5). Problem solving - Convert the following pseudo code into a sequence of Python statements in your program. 1. Read in an integer and store it in a variable named number * 2. Create a variable named count and initialise it with the value of 1. The variable keeps a count of the number of inputs. 3. Create variables, for example, total to store the total of the inputs, mx to store the largest value and mn to store the smallest value. Set the value of each variable to be the value of the variable number initially. An example is shown below for the variable total. total = number * Assume that the first numeric value read in from the keyboard is a positive integer greater than 0. 16

Program NumberProperties.py: NumberProperties (6) Problem solving (continued) 4. Write a while loop statement to read in more integers as long as the condition, number > 0, remains true. Below is an outline of the algorithm needed to solve the problem set inside the loop. Note the indentation of the statements. while number > 0 : # loop body # Add code below to read in a number number = read in an integer similar to step 1 # Add an if statement below to check if the number is not # equal to zero. if (condition tests if the number is not equal to zero) : # True branch of the if statement # i) add 1 to count as shown, indented below. count = count + 1 # Write more code inside the True branch to # ii) add number to total and store result in total # iii) Set mx to be the larger of the two values in # the variables mx and number using the max function. # iv) Set mn to be the smaller of the two values in # the variables mn and number using the min function. 17

Program NumberProperties.py: NumberProperties (7) Problem solving (continued) 5. Outside the while loop, write additional code to find the average value and the inclusive range. Then write code to print all required results separately together with suitable messages. For example, the print statement below can be used to display the inclusive range. print("the range is ", mx mn + 1) Align the while statement and the additional code for step 5, such as print statements. Provide a comment at the beginning of the program to explain the purpose of the program together with your name and the date. Save your program to the file NumberProperties.py and then run it. 18

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