Python Rules - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Python Rules

Description:

print hello' print goodbye' # does not compile. if y: print yes' print no' If/Else ... print hello' elif x == 20: print goodbye' print Charlie' else: print ... – PowerPoint PPT presentation

Number of Views:80
Avg rating:3.0/5.0
Slides: 22
Provided by: mco7
Category:
Tags: hello | python | rules

less

Transcript and Presenter's Notes

Title: Python Rules


1
Python Rules
  • A Quick Introduction to Python

2
Python What?
  • Python is a portable, open source object-oriented
    scripting language
  • Scripting Language?
  • Rapid, flexible application development
  • Flexible and rapid because there is no compile
    phase and very loose data typing
  • Estimated 500,000 to 1 million users as of 2003
    and over a decade old
  • www.python.org

3
Python Uses
  • Systems Programming (shell tools)
  • GUI Programming
  • Internet Scripting (i.e. CGI, XML)
  • Component Integration (glue code)
  • Database Programming
  • Rapid Prototyping
  • Numeric Programming
  • Gaming

4
Disadvantages
  • Interpreted and therefore execution speed can be
    relatively slow
  • Loose typing and lack of a compile phase can be
    the result of troubling runtime errors

5
Interactive Coding
  • Pythons interactive command line is a great
    place to experiment with Python and test your
    code
  • print Hello World!Hello world
  • print 2 8256
  • lumberjack okaylumberjackokay

6
Python Scripts
  • A text file that contains several Python
    commands, and has a .py extension
  • Can be executed using python myscript.py
  • Since Python scripts are just text, they are
    portable and can be run on any platform that has
    Python

7
IDLE
  • IDLE is an IDE that comes with the standard
    Python install
  • IDLE has
  • An interactive prompt with syntax highlighting
  • An editor with syntax highlighting
  • A code syntax checker
  • A debugger
  • A graphical way to run your Python scripts

8
Variables
  • No need to declare a variable
  • A variables type can change
  • The possible built-in types are
  • Numbers 3.141, 1234
  • Strings Jim, Toms
  • Lists 1, 2, 3, 4, 5
  • Dictionaries food spam, taste yum
  • Tuples (a, 1, b)
  • Files text open(myfile, r).read()

9
Strings
  • len(abc)3
  • abc defabcdef
  • print -10----------
  • s spams0, s-2 comma gives us a
    2-tuple(s, a)
  • Strings are immutable
  • s0 x error
  • s13, s1, s-1
  • (pa, pam, spa)

10
More Strings
  • Formatting
  • print i s i you (a, spam, 4)1 spam
    4 you
  • Methods
  • s.capitalize()
  • s.replace(old, new)
  • sspammys.replace(mm,
    xx)sspaxxy
  • s.find(sub)
  • s xxxxSPAMxxxxwhere
    s.find(SPAM)where4

11
Lists
  • mylist 1, 2, 3len(mylist)3
  • mylist 4, 5, 61, 2, 3, 4, 5, 6
  • Lists are mutable
  • mylist1 hellomylist1, hello, 3,
    4, 5, 6

12
More Lists
  • mylist spam, Spam, SPAM!mylist0
    2spam, Spam
  • mylist02 eat, moremylist
  • eat, more, SPAM!
  • Methods
  • mylist.append(hello)
  • mylist.extend(1, 2, hello)
  • mylist.pop()
  • del mylist1
  • mylist.reverse()

13
Dictionaries
  • ages Jon 35, Sally 36agesJon
    35
  • agesSally 36, Jon 35
  • len(ages)2
  • agesJon 20agesJon20

14
More Dictionaries
  • Methods
  • ages Jon 35, Sally 36d2.keys()
    Jon, Sally
  • d2.values()35, 36
  • d2.has_key(Sally)1

15
Block Delimiters
  • No Special Block Characters
  • Blocks are specified using indentation
  • If different indentation is used a syntax error
    will occur
  • Example
  • does compileif x print hello print
    goodbye
  • does not compileif y print yes print
    no

16
If/Else
  • x 10
  • if x 10 print hello... elif x
    20 print goodbye print
    Charlie else print jumphello

17
While Loops
  • x spamwhile x print x x
    x1
  • spam pam am m

18
For Loops
  • for var in list/string
  • Example
  • s lhu
  • for c in s print clhu
  • Use the range function for counter loops
  • range(3)0, 1, 2
  • for i in range(3) print I012

19
Functions
  • Unlike compiled languages, you have to execute
    code in order to create a function
  • Once the code executes, it creates a new function
    object, and assigns a name to the object
  • You can use the reference to call the function

20
Functions (cont.)
  • Syntax def (arg1, arg2, argN)
  • Example
  • def intersect(seq1, seq2) res
    for x in seq1 if x in seq2
    res.append(x) return res
  • s1 SPAMs2 SCAMintersect(s1,
    s2)
  • S, A, M

21
More I Say!
  • What I have not covered
  • Lots!
  • Python makes it possible to create and import
    modules for code reuse and good software design
  • Python is object-oriented and modules can contain
    classes
  • Python has a large, full-featured standard
    library that includes
  • GUI tools
  • Database tools
  • Internet programming tools
  • Regular expressions
  • And a lot more
  • Download Python now for free, install it, and
    have fun!
Write a Comment
User Comments (0)
About PowerShow.com