Introduction to Python I Fall 2013 Carola Wenk Python Programming - - PowerPoint PPT Presentation

introduction to python i
SMART_READER_LITE
LIVE PREVIEW

Introduction to Python I Fall 2013 Carola Wenk Python Programming - - PowerPoint PPT Presentation

Introduction to Python I Fall 2013 Carola Wenk Python Programming def f(a, b): print "this is function f" function declaration return a+b; evens = 0; odds = [] print f(1, 2) print f('z', f('a', 'b')) for i in range(1,10): if


slide-1
SLIDE 1

Introduction to Python I

Fall 2013 Carola Wenk

slide-2
SLIDE 2

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:

  • dds.append(i)

print evens; print sum(odds)

slide-3
SLIDE 3

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 assignment system library calls loop and conditional 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:

  • dds.append(i)

print evens; print sum(odds)

slide-4
SLIDE 4

Python Syntax

There 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>

slide-5
SLIDE 5

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:

  • dds.append(i)

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

slide-6
SLIDE 6

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 = 25 y = [1,2,3] z = "cmps1500" x = "hello"

Memory

slide-7
SLIDE 7

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 = 25 y = [1,2,3] z = "cmps1500" x = "hello"

Memory

x 25

slide-8
SLIDE 8

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 = 25 y = [1,2,3] z = "cmps1500" x = "hello" [1,2,3]

Memory

x y 25

slide-9
SLIDE 9

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 = 25 y = [1,2,3] z = "cmps1500" x = "hello" [1,2,3]

Memory

x y 25

slide-10
SLIDE 10

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 = 25 y = [1,2,3] z = "cmps1500" x = "hello" [1,2,3]

Memory

x y 25 "hello"