Docsity

Python String Operations and Functions: Reading and Exercises, Slides of Introduction to Computing

Toggle sidebar
bg1
Readings for this week!
• 2.6, python files (referred to
there as "scripts"); 2.9,
comments; 4.9, docstring!
• 3.3, for import and dot
notation!
• 8.1, 8.2, 8.4, 8.5, 8.8, about
string operations, including
the dot notation. Don't worry
about the "method"
terminology (yet).!
• 3.1-3.6, about functions!
• PLive: "
Activities 3-3.1, 3-3.2, 3-3.4
(not 3-3.3), 3-4.1, 3-4.2.!
• (Old) Lecture on VideoNote!
[xkcd.com]!
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Python String Operations and Functions: Reading and Exercises and more Slides Introduction to Computing in PDF only on Docsity!

Readings for this week

  • 2.6, python files (referred to there as "scripts"); 2.9, comments; 4.9, docstring
  • 3.3, for import and dot notation
  • 8.1, 8.2, 8.4, 8.5, 8.8, about string operations, including the dot notation. Don't worry about the "method" terminology (yet).
  • 3.1-3.6, about functions
    • PLive :  Activities 3-3.1, 3-3.2, 3-3. (not 3-3.3), 3-4.1, 3-4.2.
    • (Old) Lecture on VideoNote [xkcd.com]

Operations for Extracting Data from Strings 

(from last lecture's handout)

  • s = 'abc d'
  • Access portions with [] § s[0] is 'a' § s[4] is 'd' § s[5] causes an error § s[0:2] is 'ab' (excludes c) § s[2:] is 'c d'
  • Called “string slicing” a b c d 0 1 2 3 4 s = 'abracadabra' # the following all evaluate to True 'a' in s == True 'cad' in s == True not('foo' in s) == False s.index('a') == 0 s.index('rac') == 2 s.count('a') == 5 len(s) == 11 s.strip('a') == ‘bracadabr’ ' cs1110 '.strip() == 'cs1110' A '#' marks a comment for the reader (including the code's author). Python ignores the rest of the line. Better/more compact style

Given : info contains a comma-separated string

with last name, difficulty, execution, and penalty.

§ Example: info = 'RAISMAN, 6.7, 9.1,0'

Goal : store the difficulty as a string, with no extra

spaces or punctuation, in variable df

Where, in the following sequence of commands, does the first (conceptual) error occur? A: startcomma = info.index(',') B: tail = info[startcomma+1:] C: endcomma = tail.index(',') D: df = tail[:endcomma-1] E: this sequence achieves the goal .

Writing a Python File in Komodo

Current working directory Tabs for open files Execution output when file is “run” Current active file Line numbers Run the file; see website for how to add this

Given : info contains a comma-separated string

with last name, difficulty, execution, and penalty.

§ Example: info = 'RAISMAN, 6.7, 9.1,0'

Goal : store the difficulty as a string, with no extra

spaces or punctuation, in variable df

Where, in the following sequence of commands, does the first (conceptual) error occur? A: startcomma = info.index(',') B: tail = info[startcomma+1:] C: endcomma = tail.index(',') D: df = tail[:endcomma-1] E: this sequence achieves the goal tail[:endcomma].strip() (We'll post correct code) +2 instead, or use

Function Calls

  • Python supports expressions with math-like functions
  • Function expressions have the form fun (x,y,…) function name argument
  • Examples of built-in functions:

§ Numerical functions: round( number), pow( base, exp)

§ Getting user input: raw_input() § Help function: help()

Python Comes with Many Modules

  • io § Read/write from files
  • math § Mathematical functions
  • random § Generate random numbers § Can pick any distribution
  • string § Useful string functions
  • sys § Information about your OS - Complete list: - http://docs.python.org/library - Library : built-in modules § May change each release § Why version #s are an issue

Reading the Python Documentation

Function name Argument list What the function evaluates to