Skip to Content
Learning Python
book

Learning Python

by Mark Lutz, David Ascher
April 1999
Beginner
384 pages
11h 15m
English
O'Reilly Media, Inc.
Content preview from Learning Python
  1. The basics. Here are the sort of results you should get, along with a few comments about their meaning:

                         Numbers
    >>> 2 ** 16              # 2 raised to the power 16
    65536
    >>> 2 / 5, 2 / 5.0       # integer / truncates, float / doesn't
    (0, 0.4)
    
    Strings
    >>> "spam" + "eggs"      # concatenation
    'spameggs'
    >>> S = "ham"
    >>> "eggs " + S
    'eggs ham'
    >>> S * 5                # repetition
    'hamhamhamhamham'
    >>> S[:0]                # an empty slice at the front--[0:0]
    ''
    >>> "green %s and %s" % ("eggs", S)  # formatting
    'green eggs and ham'
    
    Tuples
    >>> ('x',)[0]                        # indexing a single-item tuple
    'x'
    >>> ('x', 'y')[1]                    # indexing a 2-item tuple
    'y'
    
    Lists
    >>> L = [1,2,3] + [4,5,6]            # list operations
    >>> L, L[:], L[:0], L[-2], L[-2:]
    ([1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [], 5, [5, 6])
    >>> ([1,2,3]+[4,5,6])[2:4]
    [3, 4]
    >>> [L[2], L[3]]                     # fetch from offsets, store in a list
    [3, 4]
    >>> L.reverse(); L                   # method: reverse list in-place
    [6, 5, 4, 3, 2, 1]
    >>> L.sort(); L                      # method: sort list in-place
    [1, 2, 3, 4, 5, 6]
    >>> L.index(4)                       # method: offset of first 4 (search)
    3
    
    Dictionaries
    >>> {'a':1, 'b':2}['b']              # index a dictionary by key
    2
    >>> D = {'x':1, 'y':2, 'z':3}
    >>> D['w'] = 0                       # create a new entry
    >>> D['x'] + D['w']
    1
    >>> D[(1,2,3)] = 4                   # a tuple used as a key (immutable)
    >>> D
    {'w': 0, 'z': 3, 'y': 2, (1, 2, 3): 4, 'x': 1}
    >>> D.keys(), D.values(), D.has_key((1,2,3))          # methods
    (['w', 'z', 'y', (1, 2, 3), 'x'], [0, 3, 2, 4, 1], 1)
    
    Empties
    >>> [[]], ["",[],(),{},None]                          # lots of nothings
    ([[]], ['', [], (), {}, None])
  2. Indexing and slicing ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learning Python

Learning Python

Fabrizio Romano

Publisher Resources

ISBN: 1565924649Catalog PageErrata