introduction to python i
play

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


  1. Introduction to Python I Fall 2013 Carola Wenk

  2. 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 (i % 2 == 0): sequence of statements evens += i else: odds.append(i) print evens; print sum(odds) 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.

  3. Python Programming def f(a, b): print "this is function f" function declaration return a+b; evens = 0; odds = [] variable assignment print f(1, 2) system library calls print f('z', f('a', 'b')) for i in range(1,10): if (i % 2 == 0): loop and conditional evens += i statements else: odds.append(i) print evens; print sum(odds) 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.

  4. Python Syntax There are four primary categories: Conditional and Logical Expressions if, elif, else, <, >, ==, and, or, not Operators and Assignment +, -, *, /, =, ... Looping Constructs for, while, range() Functions def <name>(<arguments>): <statement block> We will learn syntax as we go, so we won’t go into exhaustive detail.

  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: odds.append(i) The first statement in a nested block determines how many spaces the entire block should be indented.

  6. Variables and Python “Types” Unlike most languages, we do not need to “declare" variables up front. Memory Data x = 25 y = [1,2,3] z = "cmps1500" x = "hello" Instructions Python allows types of variables to be unspecified and allocates storage as necessary.

  7. Variables and Python “Types” Unlike most languages, we do not need to “declare" variables up front. Memory Data x = 25 x 25 y = [1,2,3] z = "cmps1500" x = "hello" Instructions Python allows types of variables to be unspecified and allocates storage as necessary.

  8. Variables and Python “Types” Unlike most languages, we do not need to “declare" variables up front. Memory Data x = 25 x 25 y = [1,2,3] z = "cmps1500" [1,2,3] y x = "hello" Instructions Python allows types of variables to be unspecified and allocates storage as necessary.

  9. Variables and Python “Types” Unlike most languages, we do not need to “declare" variables up front. Memory Data x = 25 x 25 y = [1,2,3] "cmps1500" z z = "cmps1500" [1,2,3] y x = "hello" Instructions Python allows types of variables to be unspecified and allocates storage as necessary.

  10. Variables and Python “Types” Unlike most languages, we do not need to “declare" variables up front. Memory Data x = 25 25 "hello" x y = [1,2,3] "cmps1500" z z = "cmps1500" [1,2,3] y x = "hello" Instructions Python allows types of variables to be unspecified and allocates storage as necessary.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend