CS 3101-3 - Programming Languages: Python - Lecture 2 ...

Strings

Files and IO

Functions

CS 3101-3 - Programming Languages: Python

Lecture 2: Strings/IO/Functions

Daniel Bauer (bauer@cs.columbia.edu)

September 17 2014

Daniel Bauer

CS3101-3 Python - 02-Strings/Functions/IO

1/47

Strings

Contents

Strings Files and IO Functions

Files and IO

Functions

Daniel Bauer

CS3101-3 Python - 02-Strings/Functions/IO

2/47

Strings

Files and IO

Functions

else in loops

while and for loops, that contain a break statement can contain an optional else clause. The else block is executed after the loop finishes normally, but NOT when it is terminated by break.

for n in range(2, 10): for x in range(2, n): # check if x is a factor of n if n % x == 0: print(n, '=', x, '*', n /x) break else: # didn't find a factor for n print(n, 'is prime')

$python prime.py 2 is prime 3 is prime 4=2*2 5 is prime number 6=2*3 7 is prime 8=2*4 9=3*3

Daniel Bauer

CS3101-3 Python - 02-Strings/Functions/IO

3/47

Strings

Strings Files and IO Functions

Files and IO

Functions

Daniel Bauer

CS3101-3 Python - 02-Strings/Functions/IO

4/47

Strings

Files and IO

Functions

String literals (1)

String literals can be defined with single quotes or double quotes. Can use other type of quotes inside the string.

>>> str = 'Hello "World"'

Can use ''' or """ to delimit multi-line strings.

>>> s = """Hello "World"

""" >>> print(s) Hello "World"

Daniel Bauer

CS3101-3 Python - 02-Strings/Functions/IO

5/47

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download