introduction to python 1 [read-only] - tulane...

10

Click here to load reader

Upload: doque

Post on 29-Jun-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Python 1 [Read-Only] - Tulane Universitycarola/teaching/cmps1500/fall13/slides... · loop and conditional statements def f(a, b): print "this is function f" ... Microsoft

Introduction to Python I

Fall 2013Carola Wenk

Page 2: Introduction to Python 1 [Read-Only] - Tulane Universitycarola/teaching/cmps1500/fall13/slides... · loop and conditional statements def f(a, b): print "this is function f" ... Microsoft

Python Programming

function declaration

A Python script is a sequence of function declarations followed by a sequence of statements.

A function is just a way to reuse useful blocks of statements.

sequence of statements

def f(a, b):print "this is function f"return a+b;

evens = 0; odds = []print f(1, 2)print f('z', f('a', 'b'))for i in range(1,10):if (i % 2 == 0):evens += i

else:odds.append(i)

print evens; print sum(odds)

Page 3: Introduction to Python 1 [Read-Only] - Tulane Universitycarola/teaching/cmps1500/fall13/slides... · loop and conditional statements def f(a, b): print "this is function f" ... Microsoft

Python Programming

function declaration

Variables can take on integer, string, floating point values, and can also be lists of these.

Python provides a large set of libraries with useful functions.

variable assignmentsystem library calls

loop and conditionalstatements

def f(a, b):print "this is function f"return a+b;

evens = 0; odds = []print f(1, 2)print f('z', f('a', 'b'))for i in range(1,10):if (i % 2 == 0):evens += i

else:odds.append(i)

print evens; print sum(odds)

Page 4: Introduction to Python 1 [Read-Only] - Tulane Universitycarola/teaching/cmps1500/fall13/slides... · loop and conditional statements def f(a, b): print "this is function f" ... Microsoft

Python SyntaxThere are four primary categories:

Conditional and Logical Expressions

Operators and Assignment

Looping Constructs

Functions

We will learn syntax as we go, so we won’t go into exhaustive detail.

if, elif, else, <, >, ==, and, or, not

+, -, *, /, =, ...

for, while, range()

def <name>(<arguments>): <statement block>

Page 5: Introduction to Python 1 [Read-Only] - Tulane Universitycarola/teaching/cmps1500/fall13/slides... · loop and conditional statements def f(a, b): print "this is function f" ... Microsoft

Nested Statements• Python syntax is designed to be uncluttered; unlike

most languages, blocks of statements are defined only by how they are indented.

for i in range(1,10):if (i % 2 == 0):evens += i

else:odds.append(i)

The first statement in a nested block determines how many spaces the entire block should be indented.

Page 6: Introduction to Python 1 [Read-Only] - Tulane Universitycarola/teaching/cmps1500/fall13/slides... · loop and conditional statements def f(a, b): print "this is function f" ... Microsoft

Variables and Python “Types”

Instructions

Data

Python allows types of variables to be unspecified and allocates storage as necessary.

Unlike most languages, we do not need to “declare" variables up front.

x = 25y = [1,2,3]z = "cmps1500"x = "hello"

Memory

Page 7: Introduction to Python 1 [Read-Only] - Tulane Universitycarola/teaching/cmps1500/fall13/slides... · loop and conditional statements def f(a, b): print "this is function f" ... Microsoft

Variables and Python “Types”

Instructions

Data

Python allows types of variables to be unspecified and allocates storage as necessary.

Unlike most languages, we do not need to “declare" variables up front.

x = 25y = [1,2,3]z = "cmps1500"x = "hello"

Memory

x 25

Page 8: Introduction to Python 1 [Read-Only] - Tulane Universitycarola/teaching/cmps1500/fall13/slides... · loop and conditional statements def f(a, b): print "this is function f" ... Microsoft

Variables and Python “Types”

Instructions

Data

Python allows types of variables to be unspecified and allocates storage as necessary.

Unlike most languages, we do not need to “declare" variables up front.

x = 25y = [1,2,3]z = "cmps1500"x = "hello" [1,2,3]

Memory

x

y

25

Page 9: Introduction to Python 1 [Read-Only] - Tulane Universitycarola/teaching/cmps1500/fall13/slides... · loop and conditional statements def f(a, b): print "this is function f" ... Microsoft

Variables and Python “Types”

Instructions

Data

z

Python allows types of variables to be unspecified and allocates storage as necessary.

"cmps1500"

Unlike most languages, we do not need to “declare" variables up front.

x = 25y = [1,2,3]z = "cmps1500"x = "hello" [1,2,3]

Memory

x

y

25

Page 10: Introduction to Python 1 [Read-Only] - Tulane Universitycarola/teaching/cmps1500/fall13/slides... · loop and conditional statements def f(a, b): print "this is function f" ... Microsoft

Variables and Python “Types”

Instructions

Data

z

Python allows types of variables to be unspecified and allocates storage as necessary.

"cmps1500"

Unlike most languages, we do not need to “declare" variables up front.

x = 25y = [1,2,3]z = "cmps1500"x = "hello" [1,2,3]

Memory

x

y

25 "hello"