Chap 1
Chap 1
Basic Information
>>> spamneggs()
Enter the number of slices of spam followed by the number of eggs: 3, 2
You ordered 2 eggs and 3 slices of spam. Yum!
>>>
Outlines
• Software Development Process
• Elements of Programs
• Basic Assignments
• Python’s Core Data Types
Python’s Core Data Types
Numbers
• Python’s core objects set includes the usual suspects
• Integers: numbers without a fractional part
• floating-point numbers: roughly, numbers with a decimal point in them
• complex numbers: with imaginary parts
• fixed-precision decimals
• rational fractions with numerator and denominator
• ...
• Numbers in Python support the normal mathematical operations
• plus sign (+) performs addition
• a star (*) is used for multiplication
• two stars (**) are used for exponentiation
• -, \ …
Numbers
• Besides expressions, there are a handful of useful numeric modules that ship
with Python
• Math module: contains advanced numeric tools as functions
• Random module: performs random number generation and random selections
•…
Strings
• strings are sequences of one-character strings
• we can verify string’s length with the built-in len function
• In Python, indexes start from 0: the first item is at index 0, the second is at
index 1, and so on.
Strings
• we can also index backward, from the end
• positive indexes count from the left
• negative indexes count back from the right:
• In fact, lists, sets, and dictionaries can all be built with comprehensions
Dictionaries
• Python dictionaries are not sequences at all, but are instead known as
mappings
• collections of other objects
• store objects by key instead of by relative position
• simply map keys to associated values
• Dictionaries are also mutable
• dictionaries
• are coded in curly braces
• consist of a series of “key: value” pairs
Dictionaries
Mapping Operations
• We can index this dictionary by key
• We can access the components of this structure much as we did for our
matrix
Dictionaries
Sorting Keys: for Loops
• dictionaries are not sequences, they don’t maintain any dependable left-to-
right order
• if we make a dictionary and print it back, its keys may come back in a
different order than that in which we typed them:
Dictionaries
Sorting Keys: for Loops
• What do we do, though, if we do need to impose an ordering on a
dictionary’s items?
Dictionaries
Sorting Keys: for Loops
• We also can use newer sorted built-in function
Dictionaries
Missing Keys: if Tests
• We can test if the key is in the dictionary:
Tuples
• roughly like a list that cannot be changed
• tuples are sequences, like lists,
• but they are immutable, like strings
Tuples
• Tuples also have two type-specific callable methods
• Like lists and dictionaries, tuples support mixed types and nesting, but they
don’t grow and shrink because they are immutable
Why Tuples?
• their immutability is the whole point
• If you pass a collection of objects around your program as a list, it can be
changed anywhere; if you use a tuple, it cannot
Files
• file objects are Python code’s main interface to external files on your
computer
• call the built-in open function: to create a file object
• pass in its name and the 'w' processing mode string to write data
Files
• To read back what you just wrote
• reopen the file in 'r' processing mode
• read the file’s content into a string, and display it
• A file’s contents are always a string in your script
Files
• if you want a quick preview file object methods, run a dir call on any open
file and a help on any of the method names
Other Core Types
• Sets: unordered
collections of
unique and
immutable objects
• created by
calling the built-
in set function
• or using set
literals and
expressions
• support the usual
mathematical set
operations
Other Core Types
• decimal numbers: fixed- precision floating-point numbers
• fraction numbers: rational numbers with both a numerator and a denominator
Other Core Types
• Booleans: True and False objects
• None: used to initialize names and objects
User-Defined Classes
• classes define new types of objects that extend the core set
• you wish to have a type of object that models employees
• Worker class has name and pay attributes and two functions: lastName and
giveRaise
User-Defined Classes
• Calling the class like a function generates instances of our new type