Strings in Python 1 Midterm#1 Exam Review CS 8: Introduction to Computer Science Lecture #6

Similar documents
Review for the Final Exam CS 8: Introduction to Computer Science, Winter 2018 Lecture #15

Strings in Python: Cipher Applications CS 8: Introduction to Computer Science Lecture #7

(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Strings in Python: Cipher Applications CS 8: Introduction to Computer Science, Winter 2018 Lecture #9

Pre- Defined Func-ons in C++ Review for Midterm #1

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

Arrays 2 CS 16: Solving Problems with Computers I Lecture #12

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

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

UNIT 5. String Functions and Random Numbers

More on Strings in C++ Arrays CS 16: Solving Problems with Computers I Lecture #11

Func%ons in C++ Part 2 CS 16: Solving Problems with Computers I Lecture #5

Linked Lists CS 16: Solving Problems with Computers I Lecture #16

More on Lists, Dictionaries Introduction to File I/O CS 8: Introduction to Computer Science Lecture #10

Advanced Flow Control CS 16: Solving Problems with Computers I Lecture #5

Numerical Conversions Intro to Strings in C/C++ CS 16: Solving Problems with Computers I Lecture #8

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

Python Lists 2 CS 8: Introduction to Computer Science Lecture #9

CS 102 Lab 3 Fall 2012

Design and Debug: Essen.al Concepts CS 16: Solving Problems with Computers I Lecture #8

Introduc)on to Arrays in C++ Review for Midterm #2 CS 16: Solving Problems with Computers I Lecture #12

CS1 Lecture 3 Jan. 18, 2019

Python, Part 2 CS 8: Introduction to Computer Science Lecture #4

Finding Pi: Applications of Loops, Random Numbers, Booleans CS 8: Introduction to Computer Science, Winter 2018 Lecture #6

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

COMP1730/COMP6730 Programming for Scientists. Strings

CS 115 Lecture 13. Strings. Neil Moore. Department of Computer Science University of Kentucky Lexington, Kentucky

File I/O in Python CS 8: Introduction to Computer Science Lecture #11

Lecture 1. Course Overview, Python Basics

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

MIPS Assembly: Practice Questions for Midterm 1 Saving to and Loading from Memory CS 64: Computer Organization and Design Logic Lecture #6

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Lecture 1. Course Overview, Python Basics

Introduction to Python

CS 1110, LAB 10: ASSERTIONS AND WHILE-LOOPS 1. Preliminaries

CS1 Lecture 3 Jan. 22, 2018

Lecture 1. Course Overview Types & Expressions

File Input / Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #9

Vectors and Pointers CS 16: Solving Problems with Computers I Lecture #13

Welcome to Solving Problems with Computers I

Variables, Constants, and Data Types

what are strings today: strings strings: output strings: declaring and initializing what are strings and why to use them reading: textbook chapter 8

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

CIS192 Python Programming. Robert Rand. August 27, 2015

Lists in Python CS 8: Introduction to Computer Science, Winter 2018 Lecture #10

CS3: Introduction to Symbolic Programming. Lecture 5:

Introduction to Programming, Aug-Dec 2006

CS 303E Fall 2011 Exam 2 Solutions and Criteria November 2, Good Luck!

CS1110. Lecture 22: Prelim 2 Review Session. Announcements. Processed prelim regrade requests: on the front table.

Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14

Dynamic Arrays Makefiles and Mul3ple File Compiles

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

CMSC 201 Spring 2016 Lab 04 For Loops

File I/O and String Manipula3ons CS 16: Solving Problems with Computers I Lecture #11

CMSC201 Computer Science I for Majors

CMSC 201 Spring 2017 Homework 4 Lists (and Loops and Strings)

Variable and Data Type I

Strings are actually 'objects' Strings

Variable and Data Type I

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

CS 1110, LAB 2: FUNCTIONS AND ASSIGNMENTS

More on File I/O Strings in C++ CS 16: Solving Problems with Computers I Lecture #10

BLM2031 Structured Programming. Zeyneb KURT

WELCOME TO CS 16! Problem Solving with Computers-I

PYTHON MOCK TEST PYTHON MOCK TEST III

Student Number: Instructor: Brian Harrington

CS Introduction to Programming Fall 2016

CompSci 101 Introduction to Computer Science

Introduction to Computer Programming for Non-Majors

Lecture 1. Types, Expressions, & Variables

CS3: Introduction to Symbolic Programming. Lecture 11: Tree Recursion, beginning lists, and Midterm 2. Spring 2007 Nate Titterton

Midterm 1 Review. Important control structures. Important things to review. Functions Loops Conditionals

ENGR/CS 101 CS Session Lecture 9

S206E Lecture 19, 5/24/2016, Python an overview

cs1114 REVIEW of details test closed laptop period

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CMSC201 Computer Science I for Majors

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

Previous Lecture: Today s Lecture: Announcement: Working with images. Characters and strings. Project 4 due Mon 10/26 at 11pm

CS101: Intro to Computing Summer Week 1

CSE 142. Lecture 1 Course Introduction; Basic Java. Portions Copyright 2008 by Pearson Education

Introduction to: Computers & Programming: Strings and Other Sequences

CS 1110, LAB 2: ASSIGNMENTS AND STRINGS

BCIS 3630 Dr. GUYNES SPRING 2018 TUESDAY SECTION [JAN version] GRADER COURSE WEBSITE

CS457/557 Functional Languages

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

Strings. Upsorn Praphamontripong. Note: for reference when we practice loop. We ll discuss Strings in detail after Spring break

CS 111X - Fall Test 1 - KEY KEY KEY KEY KEY KEY KEY

File I/O CS 16: Solving Problems with Computers I Lecture #9

COMPUTER SCIENCE IN THE NEWS (TWO YEARS AGO) CS61A Lecture 16 Mutable Data Structures TODAY REVIEW: OOP CLASS DESIGN 7/24/2012

61A Lecture 4. Monday, September 9

Introduction to: Computers & Programming: Strings and Other Sequences

C10552: Intro to Computation. Lecture 2 July 17, 2016

CS Prelim 1 Review Fall 2016

Transcription:

Strings in Python 1 Midterm#1 Exam Review CS 8: Introduction to Computer Science Lecture #6 Ziad Matni Dept. of Computer Science, UCSB

Administrative Turn in Homework #2 today Homework #3 is assigned and due next Thursday Lab #2 is due on Friday Your grades are now online! Access them through the class website and click on Class Grades, CMPSC 8, Spring 2017 Don t forget your TAs and Instructor s office hours!! 4/21/2017 Matni, CS16, Sp17 2

Material: Everything we ve done, incl. up to Th. 4/20 Homework, Labs, Lectures, Textbook Tuesday, 4/25 in this classroom Starts at 3:30pm **SHARP** Pre-assigned seating Duration: 1 hour long Closed book: no calculators, no phones, no computers Only 1 sheet (single-sided) of written notes Must be no bigger than 8.5 x 11 You have to turn it in with the exam You will write your answers on the exam sheet itself. 4/21/2017 Matni, CS16, Fa16 3

4/21/2017 Matni, CS16, Sp17 4

Study Session with a TA!! TA Sourav Medya (medya@cs.ucsb.edu) will lead a review session for anyone interested Friday, April 21 st from 1:00 2:00 PM In PSYCH 1924 4/21/2017 Matni, CS16, Sp17 5

What s on the Midterm#1? All Lecture Materials, Including What is CS? What are computers? Brief history What is programming? How does abstraction fit in? Numbers and Arithmetic in Python Variables in Python Modules in Python including turtle Loops using for Different uses of range Implementing accumulations Conditional statements using if/elif/else Boolean Logic Random Number Generation Functions how to define them, how to call them Strings in Python 4/21/2017 Matni, CS16, Sp17 6

Ch. 1 (all) What s on the Midterm#1? Intro to Python Textbook Readings Ch. 2 (all) Finding Pi: a context to learn/use loops, functions, random numbers Ch. 3 (sections 3.1 and 3.2) Strings and their manipulations 4/21/2017 Matni, CS16, Sp17 7

What s on the Midterm#1? Homework and Labs Review them and understand what you did The lab processes and experiences, especially 4/21/2017 Matni, CS16, Sp17 8

Sample Question Multiple Choice What is the answer to this operation: 1+3j**2? A. 1 + 9j B. -9 C. -9 + 0j D. -8 E. -8 + 0j 4/21/2017 Matni, CS16, Sp17 9

Sample Question Multiple Choice What is exactly printed by this code? for z in range(3, 5, 1): print( z * z) A. 3, 5, 1 on separate lines B. 9, 16 on separate lines C. 9, 16, 25 on separate lines D. 3, 5 on separate lines E. None of the above 4/21/2017 Matni, CS16, Sp17 10

Sample Question Short Answer Write Python code that does the following: if the value of a variable, v, is less than 5, you will print out UCSB v times. Otherwise you will print out Gaucho once. if v < 5: for j in range(v): print UCSB else: print Gaucho 4/21/2017 Matni, CS16, Sp17 11

4/21/2017 Matni, CS16, Sp17 12

Starting Chapter 3 Strings Chapter 3 s problem context is cryptography, but mostly it is about strings and related ideas Strings are basically sequences of characters A string literal is enclosed in quotes ('' or in Python): hello == hello >>> True 4/21/2017 Matni, CS16, Sp17 13

Strings Actually objects of a Python class named str type( kitty ) >>> <class 'str'> Can assign names like any other type of object message = Don t be late! print(message) >>> Don t be late! Lots of built-in functions work for string objects, and class str has useful operators and methods too 4/21/2017 Matni, CS16, Sp17 14

Operations on Strings Concatenation Merging multiple strings into 1 Use the + operator say my + + name = say my name Repetition Easy way to multiply the contents of a string Use the * operator ja * 3 = ja ja ja 4/21/2017 Matni, CS16, Sp17 15

Indexing Every character in a string has an index associated with it 0 1 2 3 4 5 6 7 8 y o m a m a s In Python, indexing always starts at 0. So the 1 st character in the string is character #0 Indexing is called out with square brackets [n] If name = Jimbo Jones, then: name[0] = J name[4] = o name[5] = name[15] is undefined (error) 4/21/2017 Matni, CS16, Sp17 16

(Fun)ctions for Strings Length of string: len(string) Example: len( Gaucho ) = 6 Slice a string into a smaller string: [i:j] Where i = starting index, j = ending index Example: Gaucho [2:4] = uc Combinations are possible! Example: ( ( o + Gaucho [2:5] + ) * 3 ) +! 4/21/2017 Matni, CS16, Sp17 17

More (Fun)ctions! Boolean operators in and not in to check if a sub-string is found inside a longer string Examples: fun in functions = fun in Functions = True False Fan not in Functions = True 4/21/2017 Matni, CS16, Sp17 18

String Methods See Table 3.2 in textbook Assume: name = Bubba name.center(9) = Bubba centers w/ spaces on each side name.count( b ) = 2 counts how many times b occurs name.count( ubb ) = 1 name.ljust(9) = Bubba left justifies name in 9 spaces name.rjust(9) = Bubba right justifies name in 9 spaces name.upper() = BUBBA all uppercase letters name.lower() = bubba all lowercase letters name.index( bb ) = 2 Index of first occurrence of first letter name.find( bb ) = 2 Index of first occurrence of first letter name.find( z ) = 1 if not found, then returns -1 name.replace( bb, dd ) = Budda 4/21/2017 Matni, CS16, Sp17 19

Example Assume string s = h o w n o w b r o w n c o w m e o w! 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 What is: s.find( m ) = 18 s.find( r ) = 9 s.find( ow ) = 1 s.find( s ) = -1 s.replace( meow, moo? ) = how now brown cowmoo?! note: one space before meow 4/21/2017 Matni, CS16, Sp17 20

Functions chr(n) and ord(c) Characters are stored as numbers in memory There are standard codes for characters, e.g. ASCII codes, UTF-8, etc For example, 'A' has code 65 in ASCII Use ord function to verify: ord('a') >>> 65 Notice 'A' is not same as 'a': ord('a') >>> 97 Every character, seen (e.g. %,!, G, =, ) and unseen (e.g. CONTROL-X, newline ) has ASCII code 4/21/2017 Matni, CS16, Sp17 21

Functions chr(n) and ord(c) Likewise, you can find character associated with a particular code using chr function chr(65) >>> 'A Can manipulate numbers to process characters chr( ord('a') + 3) >>> 'd Notice digit characters have codes too! ord('6') >>> 54 4/21/2017 Matni, CS16, Sp17 22

Examples How can I find out what s 13 letters after e?? chr( ord( e ) + 13 ) How can I add 3 and 4 and get 7?? First ask: how can I make 3 into 3? HINT: We need a baseline! ord( 3 ) ord( 0 ) = 3 So the addition is done like this: ord( 3 ) ord( 0 ) + ord( 4 ) ord( 0 ) = 7 or, ord( 3 ) + ord( 4 ) 2*ord( 0 ) = 7 Then: chr(ord( 3 ) + ord( 4 ) 2*ord( 0 ) + ord( 0 )) = 7 4/21/2017 Matni, CS16, Sp17 23

So I Can Create a Function to do This! def addchars(char1, char2): numaddascii = ord(char1) + ord(char2) ord( 0 ) charnum = chr(numaddascii) return charnum Important Caveat! Only works with 1 character numbers! 4/21/2017 Matni, CS16, Sp17 24

YOUR TO-DOs Finish reading Chapter 3 for Thursday s class Finish Homework3 (due Thursday 4/27) Finish Lab2 (due Friday 4/21) Study for Midterm #1!!!! Remember the study session: Fri. 4/21 @ 1pm in PSYCH 1924 Come see the prof. or the TAs during office hours if you have questions Run through an open meadow 4/21/2017 Matni, CS16, Sp17 25

4/21/2017 Matni, CS16, Sp17 26