Previously. Iteration. Date and time structures. Modularisation.

Similar documents
Previously. Iteration. Date and time structures. Modularisation.

Level 3 Computing Year 2 Lecturer: Phil Smith

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

Pseudocode Structure 5th October 2018

UNDERSTANDING PROBLEMS AND HOW TO SOLVE THEM BY USING COMPUTERS

SNS COLLEGE OF ENGINEERING

Chapter 6: Files and Exceptions. COSC 1436, Summer 2016 Dr. Ling Zhang 06/23/2016

Chapter 6: Files and Exceptions. COSC 1436, Spring 2017 Hong Sun 3/6/2017

Control Statements: Part 1

Learning Language. Reference Manual. George Liao (gkl2104) Joseanibal Colon Ramos (jc2373) Stephen Robinson (sar2120) Huabiao Xu(hx2104)

Introduction to Programming

Announcements COMP 141. Writing to a File. Reading From a File 10/18/2017. Reading/Writing from/to Files

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Stepwise Refinement. Lecture 12 COP 3014 Spring February 2, 2017

Algorithms and Problem Solving

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

Programming Languages & Translators. XML Document Manipulation Language (XDML) Language Reference Manual

Understanding the problem

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

CHAPTER 2 PROBLEM SOLVING TECHNIQUES. Mr Mohd Hatta Bin Hj Mohamed Ali Computer Programming BFC2042

Repetition Structures

Chapter Two: Program Design Process and Logic

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Lecture 4. Defining Functions

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

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

The while Loop 4/6/16 4

UEE1302 (1102) F10: Introduction to Computers and Programming

Problem Solving With Loops

3.2 Pseudocode. Introduction. Definition. 1 Common pseudo code terms

Chapter 3 Structured Program Development in C Part II

Lecture 4. Defining Functions

Programming for Engineers Iteration

Scheme of work Cambridge International AS & A Level Computing (9691)

Introduction to Programming

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Iteration. # a and b are now equal # a and b are no longer equal Multiple assignment

Fundamentals of Programming (Python) Getting Started with Programming

Introduction to Programming

Developing Algorithms

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

Introduction to C++ with content from

CS110D: PROGRAMMING LANGUAGE I

BEPI FOR PROGRAMMERS

Microsoft Visual Basic 2005: Reloaded

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Operating System Interaction via bash

do fifty two: Language Reference Manual

Scope of this lecture. Repetition For loops While loops

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

Fall 2017 CISC124 9/16/2017

Lab 2: Structured Program Development in C

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

PROBLEM SOLVING AND PYTHON PROGRAMMING

SNS COLLEGE OF ENGINEERING,

Introduction to Algorithms and Programming (COMP151)

2. Explain the difference between read(), readline(), and readlines(). Give an example of when you might use each.

Iterative Languages. Scoping

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

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Chapter 3 Problem Solving and the Computer

Variable A variable is a value that can change during the execution of a program.


Introduction to Programming (day 1)

LECTURE 5 Control Structures Part 2

Chapter 2 Working with Data Types and Operators

Python Basics. Lecture and Lab 5 Day Course. Python Basics

File Operations. Working with files in Python. Files are persistent data storage. File Extensions. CS111 Computer Programming

Introduction to C Programming

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR. VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS

Introduction to Programming

Lecture 7. Memory in Python

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

Common Loop Algorithms 9/21/16 42

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?

Lecture 5 Tao Wang 1

File Operations. Working with files in Python. Files are persistent data storage. File Extensions. CS111 Computer Programming


Chapter 2: Functions and Control Structures

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

[Page 177 (continued)] a. if ( age >= 65 ); cout << "Age is greater than or equal to 65" << endl; else cout << "Age is less than 65 << endl";

Pathologically Eclectic Rubbish Lister

Introduction to Programming

Repetition Algorithms

Lecture Writing Programs. Richard E Sarkis CSC 161: The Art of Programming

Data Types primitive, arrays, objects Java overview Primitive data types in Java

Programming with Python

Lab 09: Advanced SQL

Computer System and programming in C

CS112 Lecture: Loops

Class 8 ALGORITHMS AND FLOWCHARTS. The City School

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Full file at

Transcription:

Lecture 7

Previously Iteration. Date and time structures. Modularisation.

Today Pseudo code. File handling.

Pseudo code Pseudocode is an informal high-level description of the operating principle of a computer program or other algorithm. It uses the structural conventions of a programming language, but is intended for human reading rather than machine reading. Pseudo code typically omits details that are not essential for human understanding of the algorithm, such as variable declarations, system-specific code and some subroutines. http://en.wikipedia.org/wiki/pseudocode

Pseudo code The rules of Pseudo code are reasonably straightforward. All statements showing "dependency" are to be indented. These include while, do, for, if, switch.

Pseudo code - can you write this program? Examples: If student's grade is greater than or equal to 60 Print "passed" else Print "failed" Set total to zero Set grade counter to one While grade counter is less than or equal to ten Input the next grade Add the grade into the total Set the class average to the total divided by ten Print the class average.

Pseudo code keywords to use For looping and selection, The keywords that are to be used include Do While...EndDo; Do Until...Enddo; Case...EndCase; If...Endif; Call... with (parameters); Call; Return...; Return; When; Always use scope terminators for loops and iteration, even though Python does not use them.

Pseudo code keywords to use As verbs, use the words Generate, Compute, Process, etc. Words such as set, reset, increment, compute, calculate, add, sum, multiply,... print, display, input, output, edit, test, etc. with careful indentation tend to foster desirable pseudo code. Do not include data declarations in your pseudo code.

Pseudo code. Pseudo code is a feature of assignment 1.

Pseudo code - Task Partial requirement A technician must check a substance s temperature every 15 minutes. If the substance s temperature does not exceed 102.5 degrees Celsius, then the technician does nothing. However, if the temperature is greater than 102.5 degrees Celsius, the technician must turn down the vat s heater, wait 5 minutes, and check the temperature again. The technician repeats these steps until the temperature does not exceed 102.5 degrees Celsius.

Pseudo code - Task Write pseudo code for the preceding partial requirement.

Reading and writing files. In order for a program to work with a file, the program must create a file object in memory. A file object is an object that is associated with a specific file, and provides a way for the program to work with that file. In the program, a variable references the file object. This variable is used to carry out any operations that are performed on the file. file_variable = open( filename, mode)

Reading and writing files. file_variable = open( filename, mode) In the general format: file_variable is the name of the variable that will reference the file object. This is also called a file handle. filename is a string specifying the name of the file. mode is a string specifying the mode (reading, writing, etc.) in which the file will be opened.

Reading and writing files. Some of the main Python file modes. Mode r w a Description Open a file for reading only. The file cannot be changed or written to. Open a file for writing. If the file already exists, erase its contents. If it does not exist, create it. Open a file to be written to. All data written to the file will be appended to its end. If the file does not exist, create it.

Reading and writing files. For example, suppose the file customers.txt contains customer data, and we want to open for reading. Here is an example of how we would call the open function: customer_file = open('customers.txt', 'r') After this statement executes, the file named customers.txt will be opened, and the variable customer_file will reference a file object that we can use to read data from the file.

Reading and writing files. Suppose we want to create a file named sales.txt and write data to it. Here is an example of how we would call the open function: sales_file = open('sales.txt', 'w') Note: When you use the 'w' mode you are creating the file on the disk. If a file with the specified name already exists when the file is opened, the contents of the existing file will be erased.

Reading and writing files. When you pass a file name that does not contain a path as an argument to the open function, the Python interpreter assumes that the file s location is the same as that of the program. So customer_file = open('customers.txt', 'r') Python will look in the program folder.

Reading and writing files. If you want to open a file in a different location, you can specify a path as well as a filename in the argument that you pass to the open function. However- If you specify a path in a string literal (particularly on a Windows computer), be sure to prefix the string with the letter r. Here is an example: customer_file = open(r'c:\users\phil\customers.txt', 'r')

Reading and writing files. customer_file = open(r'c:\users\phil\customers.txt', 'r') The r prefix specifies that the string is a raw string. This causes the Python interpreter to read the backslash characters as literal backslashes. Without the r prefix, the interpreter would assume that the backslash characters were part of escape sequences, and an error would occur.

Writing Data to a File This is how you write data to a file. file_variable.write( string) The file first must be opened for writing (using the 'w' or 'a' mode) or an error will occur. Let s assume that customer_file references a file object, and the file was opened for writing with the 'w' mode. Here is an example of how we would write the string HND Students to the file: customer_file.write( HND Students ') This is how you close a file. customer_file.close()

Writing Data to a File Do the (file_write.py) program in the Lab07 (on the wiki) WHAT does the \n do?

Reading Data From a File If a file has been opened for reading (using the 'r' mode) you can use the file object s read method to read its entire contents into memory. When you call the read method, it returns the file s contents as a string. In Python we use infile.read method to read data from a file. Do Lab07 (file_read.py)

Reading Data From a File Although the read method allows you to easily read the entire contents of a file with one statement, many programs need to read and process the items that are stored in a file one line at a time. In Python you can use the readline method to read a line from a file. (A line is simply a string of characters that are terminated with a \n.) Do Lab07 (create_file.py) and (line_read.py)

Reading Data From a File Quite often a program must read the contents of a file without knowing the number of items that are stored in the file. This presents a problem if you want to write a program that processes all of the items in the file, however many there are. We know how to read lines from a file - We could use a loop to read all of the items in the file, but you need a way of knowing when the end of the file has been reached.

Reading end of File In Python, the readline method returns an empty string ('') when it has attempted to read beyond the end of a file. This makes it possible to write a while loop that determines when the end of a file has been reached. Here is the general algorithm, in pseudo code: Open the file Use readline to read the first line from the file While the value returned from readline is not an empty string: Process the item that was just read from the file Use readline to read the next line from the file. EndWhile Close the file

Reading Data From a File NOTE: In this algorithm we call the readline method just before entering the while loop. The purpose of this method call is to get the first line in the file, so it can be tested by the loop. This initial read operation is called a priming read.

Reading Data - flowchart Shape used for input (read) and output (write)

Summary what we have done today - Pseudo code Reading and writing files.

Next Time GUI s part 1.

Finally Continue with lab07 exercises.