Python Programming: An Introduction to Computer Science



Similar documents
Python Loops and String Manipulation

Outline Basic concepts of Python language

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

Chapter 2 Writing Simple Programs

ARIZONA CTE CAREER PREPARATION STANDARDS & MEASUREMENT CRITERIA SOFTWARE DEVELOPMENT,

6.170 Tutorial 3 - Ruby Basics

Python Lists and Loops

Introduction to Python

CS177 MIDTERM 2 PRACTICE EXAM SOLUTION. Name: Student ID:

CS106A, Stanford Handout #38. Strings and Chars

Exercise 1: Python Language Basics

Sources: On the Web: Slides will be available on:

AQA GCSE in Computer Science Computer Science Microsoft IT Academy Mapping

Writing Simple Programs

Lab 4.4 Secret Messages: Indexing, Arrays, and Iteration

Introduction to Computer Science I Spring 2014 Mid-term exam Solutions

Enhancing the SAS Enhanced Editor with Toolbar Customizations Lynn Mullins, PPD, Cincinnati, Ohio

Obfuscation: know your enemy

ESPResSo Summer School 2012

Chapter 3 Writing Simple Programs. What Is Programming? Internet. Witin the web server we set lots and lots of requests which we need to respond to

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

Automating SQL Injection Exploits

Arithmetic Coding: Introduction

Informatica e Sistemi in Tempo Reale

Computer Programming I

Exercise 4 Learning Python language fundamentals

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

Cyber Security Workshop Encryption Reference Manual

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

CSCE 110 Programming I Basics of Python: Variables, Expressions, and Input/Output

CUSTOMER WORK REQUEST INFORMATION

Chapter 2: Elements of Java

An Interface from YAWL to OpenERP

HTML Web Page That Shows Its Own Source Code

1 Stored Procedures in PL/SQL 2 PL/SQL. 2.1 Variables. 2.2 PL/SQL Program Blocks

Security at Scale: Effective approaches to web application

Beginning to Program Python

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Python Standard Library: Data Representation 4-1

1. Define: (a) Variable, (b) Constant, (c) Type, (d) Enumerated Type, (e) Identifier.

Crash Dive into Python

Instructional Design Framework CSE: Unit 1 Lesson 1

USC VITERBI SCHOOL OF ENGINEERING INFORMATICS PROGRAM

Microsoft Windows PowerShell v2 For Administrators

So far we have considered only numeric processing, i.e. processing of numeric data represented

ARIZONA CTE CAREER PREPARATION STANDARDS & MEASUREMENT CRITERIA SOFTWARE DEVELOPMENT,

Regular Expressions and Automata using Haskell

AWK: The Duct Tape of Computer Science Research. Tim Sherwood UC Santa Barbara

Command Scripts Running scripts: include and commands

Introduction to Python

Introduction to Python

Some programming experience in a high-level structured programming language is recommended.

A Python Tour: Just a Brief Introduction CS 303e: Elements of Computers and Programming

Advanced PostgreSQL SQL Injection and Filter Bypass Techniques

This section describes how LabVIEW stores data in memory for controls, indicators, wires, and other objects.

Test Automation Architectures: Planning for Test Automation

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Java 7 Recipes. Freddy Guime. vk» (,\['«** g!p#« Carl Dea. Josh Juneau. John O'Conner

Making SharePoint Work with Workflow. Robert L. Bogue 3 Part Presentation Series

Safeguard Ecommerce Integration / API

Computational Mathematics with Python

Python Evaluation Rules

Object Oriented Software Design

Using Files as Input/Output in Java 5.0 Applications

PYTHON Basics

Cardbox: macros and programming.

Introduction to Python for Text Analysis


The programming language C. sws1 1

Basic Programming and PC Skills: Basic Programming and PC Skills:

Hadoop and Map-reduce computing

Secret Communication through Web Pages Using Special Space Codes in HTML Files

Object Oriented Software Design

Preparing your data for analysis using SAS. Landon Sego 24 April 2003 Department of Statistics UW-Madison

monoseq Documentation

Lossless Data Compression Standard Applications and the MapReduce Web Computing Framework

HOMEWORK # 2 SOLUTIO

Programming and Software Development CTAG Alignments

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Lecture 2: Regular Languages [Fa 14]

CS Matters in Maryland CS Principles Course

Base Conversion written by Cathy Saxton

Chapter One Introduction to Programming

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Python Tutorial. Release Guido van Rossum Fred L. Drake, Jr., editor. June 18, Python Software Foundation

Matt Cabot Rory Taca QR CODES

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Python Programming: An Introduction To Computer Science

PROFESSIONAL. Node.js BUILDING JAVASCRIPT-BASED SCALABLE SOFTWARE. Pedro Teixeira WILEY. John Wiley & Sons, Inc.

Hands-on Exercise 1: VBA Coding Basics

ML for the Working Programmer

Programming Languages

AP Computer Science Static Methods, Strings, User Input

LabVIEW Internet Toolkit User Guide

JavaScript: Control Statements I

Integrating NLTK with the Hadoop Map Reduce Framework Human Language Technology Project

Unit 6. Loop statements

Transcription:

Python Programming: An Introduction to Computer Science Sequences: Strings and Lists Python Programming, 2/e 1

Objectives To understand the string data type and how strings are represented in the computer. To be familiar with various operations that can be performed on strings through built-in functions and the string library. To understand the basic idea of sequences and indexing as they apply to Python strings and lists. Python Programming, 2/e 2

Outline String List Examples Python Programming, 2/e 3

String What s string? How to obtain substring? Build-in functions in String library Python Programming, 2/e 4

String What s string? How to obtain substring? Build-in functions in String library Python Programming, 2/e 5

String: Data Type The most common use of personal computers is word processing. Text is represented in programs by the string data type. A string is a sequence of characters enclosed within quotation marks (") or apostrophes ('). Python Programming, 2/e 6

Examples Print two strings Check string types: type Get string from keyboard input Python Programming, 2/e 7

String: Indexing We can access the individual characters in a string through indexing. The positions: Numbered from the left, starting with 0. Index from the right: negative indexes. <string>[<expr>] value of expr determines which character is selected from the string. Python Programming, 2/e 8

String: Indexing Notes: In a string of n characters, the last character is at position n-1 since we start counting with 0. Indexing returns a string containing a single character from a larger string. Python Programming, 2/e 9

String: Slicing We can also access a contiguous sequence of characters, called a substring, through a process called slicing. Slicing: <string>[<start>:<end>] start and end should both be ints The slice contains the substring beginning at position start and runs up to but doesn t include the position end. Python Programming, 2/e 10

String operators Operator Meaning + Concatenation * Repetition <string>[] Indexing <string>[:] Slicing len(<string>) Length for <var> in <string> Iteration through characters Python Programming, 2/e 11

Examples: Given a string hello world! What is the character at the 6 th position? What is the character at the -3 rd position? Please print out the characters at the 1 st, 4 th, and 8 th position. Please print out world in the given string. What s the length of this string? Python Programming, 2/e 12

Simple String Processing Usernames on a computer system The scheme for generating usernames is to use the user s first initial followed by up tp seven letters of the user s last name. Example: Elmer Thudpucker: ethudpuc John Smith: jsmith Python Programming, 2/e 13

Simple String Processing Converting an int that stands for the month into the three letter abbreviation for that month. Python Programming, 2/e 14

Simple String Processing One weakness this method only works where the potential outputs all have the same length. How could you handle spelling out the months? Python Programming, 2/e 15

Lists Strings are always sequences of characters, but lists can be sequences of arbitrary values. Lists can have numbers, strings, or both! mylist = [1, "Spam ", 4, "U"] Python Programming, 2/e 16

Lists We can use the idea of a list to make our previous month program even simpler! Python Programming, 2/e 17

Lists Notes: The months line overlaps a line. Python knows that the expression isn t complete until the closing ] is encountered. Lists are mutable, meaning they can be changed. However, strings can not be changed. Python Programming, 2/e 18

Strings Representation Inside the computer, strings are represented as sequences of 1 s and 0 s, just like numbers. A string is stored as a sequence of binary numbers, one number per character. It doesn t matter what value is assigned as long as it s done consistently. Python Programming, 2/e 19

String Representation How to program an encoder? Using python programs to automate the process of turning messages into sequence of numbers. How to program a decoder? Using python programs to turn a sequence of numbers back into a readable message. Python Programming, 2/e 20

Encoder program A for loop iterates over a sequence of objects, so the for loop looks like: for ch in <string> The ord function returns the numeric (ordinal) code of a single character. Python Programming, 2/e 21

Decode program How do we get the sequence of numbers to decode? Read the input as a single string split it apart into substrings, each of which represents one number. (split method) The chr function converts a numeric code to the corresponding character. Python Programming, 2/e 22

Other String/List Methods There are a number of other string methods. Try them all! http://docs.python.org/library/string.html There are a number of other list methods. http://docs.python.org/tutorial/datastructures. html Python Programming, 2/e 23

Example Input/Output as String Manipulation Often we will need to do some string operations to prepare our string data for output ( pretty it up ) Let s say we want to enter a date in the format 05/24/2003 and output May 24, 2003. How could we do that? Python Programming, 2/e 24

Example-solution Input the date in mm/dd/yyyy format (datestr) Split datestr into month, day, and year strings Convert the month string into a month number Use the month number to lookup the month name Create a new date string in the form Month Day, Year Output the new date string Python Programming, 2/e 25