Fundamentals of Python: First Programs. Chapter 4: Strings and Text Files

Similar documents
Fundamentals of Python: First Programs. Chapter 4: Text Files

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

[CHAPTER] 1 INTRODUCTION 1

Lecture (01) Digital Systems and Binary Numbers By: Dr. Ahmed ElShafee

Computer Hardware 6. The

Lecture (01) Introduction Number Systems and Conversion (1)

SCHOOL OF ENGINEERING & BUILT ENVIRONMENT. Mathematics. Numbers & Number Systems

Python Working with files. May 4, 2017

CS & IT Conversions. Magnitude 10,000 1,

Step Octal Number Binary Number Step Step

Positional notation Ch Conversions between Decimal and Binary. /continued. Binary to Decimal

ENGG1811 Computing for Engineers Week 9A: File handling

This document contains additional questions; it is not intended to be treated as a complete paper.

EE292: Fundamentals of ECE

Python Class-Lesson1 Instructor: Yao

COMP6700/2140 Data and Types

Homework 1 graded and returned in class today. Solutions posted online. Request regrades by next class period. Question 10 treated as extra credit

Memory Addressing, Binary, and Hexadecimal Review

Number Systems. Dr. Tarek A. Tutunji Philadelphia University, Jordan

CHW 261: Logic Design

CMPE223/CMSE222 Digital Logic Design. Positional representation

Chapter 2 Exercises and Answers

Variables, Constants, and Data Types

Programming Studio #1 ECE 190

Number Systems and Binary Arithmetic. Quantitative Analysis II Professor Bob Orr

Lab Using the Windows Calculator with Network Addresses

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Digital Logic Lecture 2 Number Systems

Slicing. Open pizza_slicer.py

Number representations

CS242 COMPUTER PROGRAMMING

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

Bits. Binary Digits. 0 or 1

LAB A Translating Data to Binary

Computer Principles and Components 1

Angela Z: A Language that facilitate the Matrix wise operations Language Reference Manual

Programming Studio #1 ECE 190

1.3 Systems of numeration: To represent quantities in the different systems of numeration, specific symbols are used, which are also called ciphers.

Machine Architecture and Number Systems

Intro to C and Binary Numbers 8/27/2007

Number System. Introduction. Decimal Numbers

Introduction to MATLAB

Computer Organization and Assembly Language. Lab Session 01

MULTIPLE CHOICE. Chapter Seven

How to program with Matlab (PART 1/3)

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

Topics. Hardware and Software. Introduction. Main Memory. The CPU 9/21/2014. Introduction to Computers and Programming

Read this before starting!

ECE 364 Software Engineering Tools Laboratory. Lecture 4 Python: Collections I

Spoke. Language Reference Manual* CS4118 PROGRAMMING LANGUAGES AND TRANSLATORS. William Yang Wang, Chia-che Tsai, Zhou Yu, Xin Chen 2010/11/03

Decimal Binary Conversion Decimal Binary Place Value = 13 (Base 10) becomes = 1101 (Base 2).

CIS 3362 Final Exam 12/4/2013. Name:

Binary. Hexadecimal BINARY CODED DECIMAL

Advanced Algorithms and Computational Models (module A)

Python for Astronomers. Week 1- Basic Python

User. Application program. Interfaces. Operating system. Hardware

Introduction to Computer Programming for Non-Majors

MACHINE LEVEL REPRESENTATION OF DATA

Programming Fundamentals

1.1 Information representation

CHAPTER 2 Number Systems

Block Ciphers Tutorial. c Eli Biham - May 3, Block Ciphers Tutorial (5)

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

Chapter 1 Preliminaries

Lesson 4: Type Conversion, Mutability, Sequence Indexing. Fundamentals of Text Processing for Linguists Na-Rae Han

Basic data types. Building blocks of computation

Review of Data Representation & Binary Operations Dhananjai M. Rao CSA Department Miami University

Chapter 2 Getting Started with Python

Chapter 14 Sequential Access Files

Learning Programme Fundamentals of data representation AS Level

2 Number Systems 2.1. Foundations of Computer Science Cengage Learning

Introduction to Computer Programming for Non-Majors

Fundamental of Programming (C)

Machine Architecture and Number Systems CMSC104. Von Neumann Machine. Major Computer Components. Schematic Diagram of a Computer. First Computer?

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

Fundamentals of Programming Session 8

Introduction to Cryptography in Blockchain Technology. December 23, 2018

Decimal & Binary Representation Systems. Decimal & Binary Representation Systems

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

Chapter 4: Computer Codes. In this chapter you will learn about:

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

CMPS 10 Introduction to Computer Science Lecture Notes

Variables and Data Representation

Python for ArcGIS. Lab 1.

CMSC 104 -Lecture 2 John Y. Park, adapted by C Grasso

Binary Representations and Arithmetic

CIS 1.5 Course Objectives. a. Understand the concept of a program (i.e., a computer following a series of instructions)

Digital Logic Design Exercises. Assignment 1

Maciej Sobieraj. Lecture 1

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

Octal & Hexadecimal Number Systems. Digital Electronics

NUMERIC SYSTEMS USED IN NETWORKING

Lab: Supplying Inputs to Programs

LISTS WITH PYTHON. José M. Garrido Department of Computer Science. May College of Computing and Software Engineering Kennesaw State University

Chapter 3 DATA REPRESENTATION

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

For strings (and tuples, when we get to them), its easiest to think of them like primitives directly stored in the variable table.

Fundamentals of Programming Session 2

Information Science 1

Transcription:

Fundamentals of Python: First Programs Chapter 4: Strings and Text Files

Objectives After completing this chapter, you will be able to Access individual characters in a string Retrieve a substring from a string Search for a substring in a string Convert a string representation of a number from one base to another base Fundamentals of Python: First Programs 2

Objectives (continued) Use string methods to manipulate strings Open a text file for output and write strings or numbers to the file Open a text file for input and read strings or numbers from the file Use library functions to access and navigate a file system Fundamentals of Python: First Programs 3

Accessing Characters and Substrings in Strings In this section, we examine the internal structure of a string more closely You will learn how to extract portions of a string called substrings Fundamentals of Python: First Programs 4

The Structure of Strings An integer can t be factored into more primitive parts A string is an immutable data structure Data structure: Consists of smaller pieces of data String s length: Number of characters it contains (0+) Fundamentals of Python: First Programs 5

The Subscript Operator The form of the subscript operator is: Examples: index is usually in range [0,length of string 1]; can be negative Fundamentals of Python: First Programs 6

The Subscript Operator (continued) Subscript operator is useful when you want to use the positions as well as the characters in a string Use a count-controlled loop Fundamentals of Python: First Programs 7

Slicing for Substrings Python s subscript operator can be used to obtain a substring through a process called slicing Place a colon (:) in the subscript; an integer value can appear on either side of the colon Fundamentals of Python: First Programs 8

Testing for a Substring with the in Operator When used with strings, the left operand of in is a target substring and the right operand is the string to be searched Returns True if target string is somewhere in search string, or False otherwise Fundamentals of Python: First Programs 9

Data Encryption It is easy to observe data crossing a network, particularly in wireless networks Attacker may use sniffing software Data encryption can be used to protect information transmitted on networks Many protocols have secure versions (e.g., HTTPS) One or more keys are use to encrypt messages to produce cipher text, and to decrypt cipher text back to its original plain text form Examples: Caesar cipher, block cipher Fundamentals of Python: First Programs 10

Data Encryption (continued) Caesar cipher replaces each character in plain text with a character a given distance away Fundamentals of Python: First Programs 11

Data Encryption (continued) To decrypt, use inverse method Fundamentals of Python: First Programs 12

Data Encryption (continued) Caesar cipher worked well in ancient times, but is easy to break using modern computers Fundamentals of Python: First Programs 13

Data Encryption (continued) Block cipher Uses plaintext character to compute two or more encrypted characters Each encrypted character is computed using two or more plaintext characters Uses an invertible matrix Fundamentals of Python: First Programs 14

Strings and Number Systems The digits used in each system are counted from 0 to n - 1, where n is the system s base To represent digits with values larger than 9 10, systems such as base 16 use letters Example: A 16 represents the quantity 10 10, whereas 10 16 represents the quantity 16 10 Fundamentals of Python: First Programs 15

The Positional System for Representing Numbers In positional notation, a digit has a positional value, determined by raising the base to the power specified by the position (base position ) Fundamentals of Python: First Programs 16

Converting Binary to Decimal Each digit or bit in binary number has positional value that is power of 2 We occasionally refer to a binary number as a string of bits or a bit string To determine the integer quantity that a string of bits represents: Fundamentals of Python: First Programs 17

Converting Binary to Decimal (continued) Fundamentals of Python: First Programs 18

Converting Binary to Decimal (cont.) Fundamentals of Python: First Programs 19

Conversion Shortcuts Thus, a quick way to compute the decimal value of the number 11111 2 is 2 5-1, or 31 10 Fundamentals of Python: First Programs 20

Octal and Hexadecimal Numbers To convert from octal to binary, start by assuming that each digit in the octal number represents three digits in the corresponding binary number To convert binary to octal, you begin at the right and factor the bits into groups of three bits each Fundamentals of Python: First Programs 21

Octal and Hexadecimal Numbers (continued) To convert from hex to binary, replace each hex digit with the corresponding 4-bit binary number To convert from binary to hex, factor the bits into groups of 4 and look up the corresponding hex digits Fundamentals of Python: First Programs 22

String Methods Python includes a set of string operations called methods that make tasks like counting the words in a single sentence easy Fundamentals of Python: First Programs 23

String Methods (continued) A method behaves like a function, but has a slightly different syntax A method is always called with a given data value called an object Methods can expect arguments and return values A method knows about the internal state of the object with which it is called In Python, all data values are objects Fundamentals of Python: First Programs 24

String Methods (continued) Fundamentals of Python: First Programs 25

String Methods (continued) Fundamentals of Python: First Programs 26

String Methods (continued) Fundamentals of Python: First Programs 27

String Methods (continued) Fundamentals of Python: First Programs 28

String Methods (continued) Example: extracting a filename s extension The subscript [-1] extracts the last element Can be used to write a general expression for obtaining any filename s extension, as follows: Fundamentals of Python: First Programs 29

Text Files A text file is software object that stores data on permanent medium such as disk or CD When compared to keyboard input from human user, the main advantages of taking input data from a file are: The data set can be much larger The data can be input much more quickly and with less chance of error The data can be used repeatedly with the same program or with different programs Fundamentals of Python: First Programs 30

Text Files and Their Format Using a text editor such as Notepad or TextEdit, you can create, view, and save data in a text file All data output to or input from a text file must be strings Fundamentals of Python: First Programs 31

Writing Text to a File Data can be output to a text file using a file object To open a file for output: If file does not exist, it is created If it already exists, Python opens it; when data are written to the file and the file is closed, any data previously existing in the file are erased Failure to close output file can result in data being lost Fundamentals of Python: First Programs 32

Writing Numbers to a File The file method write expects a string as an argument Other types of data must first be converted to strings before being written to output file (e.g., using str) Fundamentals of Python: First Programs 33

Reading Text from a File You open a file for input in a manner similar to opening a file for output If the path name is not accessible from the current working directory, Python raises an error There are several ways to read data from a file Example: the read method Fundamentals of Python: First Programs 34

Reading Text from a File (continued) After input is finished, read returns an empty string Fundamentals of Python: First Programs 35

Examples: Reading Numbers from a File Fundamentals of Python: First Programs 36

Reading Numbers from a File (continued) Fundamentals of Python: First Programs 37

Accessing and Manipulating Files and Directories on Disk When designing Python programs that interact with files, it s a good idea to include error recovery For example, before attempting to open a file for input, you should check to see if file exists Function os.path.exists supports this checking Example: To print all of the names of files in the current working directory with a.py extension: Fundamentals of Python: First Programs 38

Accessing and Manipulating Files and Directories on Disk (continued) Fundamentals of Python: First Programs 39

Accessing and Manipulating Files and Directories on Disk (continued) Fundamentals of Python: First Programs 40

Case Study: Text Analysis In 1949, Dr. Rudolf Flesch proposed a measure of text readability known as the Flesch Index Index is based on the average number of syllables per word and the average number of words per sentence in a piece of text Scores usually range from 0 to 100, and indicate readable prose for the following grade levels: Fundamentals of Python: First Programs 41

Case Study: Request Write a program that computes the Flesch index and grade level for text stored in a text file Fundamentals of Python: First Programs 42

Case Study: Analysis Input is the name of a text file Outputs are the number of sentences, words, and syllables in the file, as well as the file s Flesch index and grade-level equivalent Fundamentals of Python: First Programs 43

Case Study: Design Fundamentals of Python: First Programs 44

Case Study: Implementation (Coding) Fundamentals of Python: First Programs 45

Case Study: Implementation (Coding) (continued) Fundamentals of Python: First Programs 46

Case Study: Testing Bottom-up testing: Each task is coded and tested before it is integrated into the overall program After you have written code for one or two tasks, you can test them in a short script, called a driver Fundamentals of Python: First Programs 47

Summary A string is a sequence of zero or more characters Immutable data structure [] used to access a character at a given position Can also be used for slicing ([<start>:<end>]) in operator is used to detect the presence or absence of a substring in a string Method: operation that is used with an object The string type includes many useful methods for use with string objects Fundamentals of Python: First Programs 48

Summary (continued) A text file is a software object that allows a program to transfer data to and from permanent storage A file object is used to open a connection to a text file for input or output Some useful methods: read, write, readline for loop treats an input file as a sequence of lines On each pass through the loop, the loop s variable is bound to a line of text read from the file Fundamentals of Python: First Programs 49