1 / 20

Introduction To Python

Introduction To Python. Sarah Farley. What is Python?. High level language Extensible No compilation or linking. Interpreter. Type python on the command line to start the interpreter python -c command [arg] is an alternative Exit by ctl- d or quit() >>> Is the prompt for next command

crofoot
Download Presentation

Introduction To Python

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction To Python Sarah Farley

  2. What is Python? High level language Extensible No compilation or linking

  3. Interpreter Type python on the command line to start the interpreter python -c command [arg] is an alternative Exit by ctl- d or quit() >>> Is the prompt for next command … indicates a continuation Comments are indicated with #

  4. Python as a Calculator • +, -, *, / act as you would expect them to • >>> (50-5*6)/4 • 5 • >>> 2+2 • 4 • >>> 7/-3 • -3

  5. Assigning Variables • = is used to assign variables • >>> width = 20 • >>> height = 5*9 • >>> width * height • 900 • Values can be assigned simultaneously • >>> x = y = z = 0

  6. Floating Numbers • When working with mixed operators, integers are turned into floats • >>> 3 * 3.75 / 1.5 • 7.5 • >>> 7.0 / 2 • 3.5

  7. Strings • Strings can be declared with ‘ or “. • >>> 'spam eggs' • 'spam eggs' • >>> 'doesn\'t' • "doesn't" • >>> "doesn't" • "doesn't" • >>> '"Yes," he said.' • '"Yes," he said.' • >>> "\"Yes,\" he said." • '"Yes," he said.' • >>> '"Isn\'t," she said.' • '"Isn\'t," she said.'

  8. Strings • To input string across multiple lines use the / character • hello = "This is a rather long string containing\n\ several lines of text just as you would do in C.\n\ Note that whitespace at the beginning of the line is\ significant." • Or “”” • print """ Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to """

  9. Raw Strings To get a raw string, you add an r in front >>>hello = r"This is a rather long string containing\n\ several lines of text much as you would do in C.“ It prints the string exactly ignoring things like newlines …This is a rather long string containing\n\ several lines of text much as you would do in C.

  10. Strings • Strings are concatenated with + and repeated with * • >>> word = 'Help' + 'A' • >>> word • 'HelpA' • >>> '<' + word*5 + '>' • '<HelpAHelpAHelpAHelpAHelpA>' • Placing two literal strings next to each other automatically concatenates them. • >>> 'str' 'ing' • 'string'

  11. Strings • Strings are indexed, starting at 0 • >>> word[4] • 'A' • >>> word[0:2] • 'He' • >>> word[2:4] • 'lp' • Strings can also be sliced. • >>> word[:2] • 'He' • >>> word[2:] • 'lpA' • Giving negative numbers for the indexes starts the string at the right instead of the left

  12. Lists • List do not need to have the same data type throughout • >>> a = ['spam', 'eggs', 100, 1234] • >>> a • ['spam', 'eggs', 100, 1234] • Like strings, the index starts at 0 and lists can be sliced, concatonated, etc.

  13. Lists • It is possible to change a list once its been made. • >>> # Replace some items:... a[0:2] = [1, 12] • >>> a [1, 12, 123, 1234] • >>> # Remove some:... a[0:2] = [] • >>> a [123, 1234] • >>> # Insert some:... a[1:1] = ['bletch', 'xyzzy'] • >>> a [123, 'bletch', 'xyzzy', 1234] • >>> # Insert (a copy of) itself at the beginning • >>> a[:0] = a • >>> a [123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234] • >>> # Clear the list: replace all items with an empty list • >>> a[:] = [] >>> a []

  14. if Statements • >>> x = int(raw_input("Please enter an integer: ")) • Please enter an integer: 42 • >>> if x < 0: ... x = 0 ... print 'Negative changed to zero' ... elif x == 0: ... print 'Zero' ... elif x == 1: ... print 'Single' ... else: ... print 'More'

  15. for Statements >>> # Measure some strings:... a = ['cat', 'window', 'defenestrate'] >>> for x in a: ... print x, len(x) ... cat 3 window 6 defenestrate 12

  16. range() Function • >>> range(5, 10) • [5, 6, 7, 8, 9] • >>> range(0, 10, 3) • [0, 3, 6, 9] • >>> range(-10, -100, -30) • [-10, -40, -70]

  17. Other Statements The break statement breaks out of the for or while loop. The continue statement continues onto the next section of the loop. The else clause terminates when the list exhausts or becomes false. The pass funtion is a placeholder. It doesn’t do anything.

  18. Default Argument def() is the default argument for a function. def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):

  19. Keyword Arguments They have the form keyword = value. def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'): print "-- This parrot wouldn't", action, print "if you put", voltage, "volts through it." print "-- Lovely plumage, the", type print "-- It's", state, "!"

  20. Resource http://docs.python.org/tutorial/index.html

More Related