Python Language Intro (Part 1)

Agenda

  1. Language overview
  2. White space sensitivity
  3. Basic Types and Operations
  4. Statements & Control Structures
  5. Functions
  6. OOP (Classes, Methods, etc.)
  7. Immutable Sequence Types (Strings, Ranges, Tuples)
  8. Mutable data structures: Lists, Sets, Dictionaries

1. Language overview

Note: this is not a language course! Though I'll cover the important bits of the language (and standard library) that are relevant to class material, I expect you to master the language on your own time.

Python ...

2. White Space Sensitivity

Python has no beginning/end block markers! Blocks must be correctly indented (4 spaces is the convention) to delineate them.

3. Basic Types and Operations

In Python, variables do not have types. Values have types (though they are not explicitly declared). A variable can be assigned different types of values over its lifetime.

Note that all the types reported are classes. I.e., even types we are accustomed to thinking of as "primitives" (e.g., integers in Java) are actually instances of classes. All values in Python are objects!

There is no dichotomy between "primitive" and "reference" types in Python. All variables in Python store references to objects.

Numbers

Booleans

Strings

Strings are an example of a sequence type; https://docs.python.org/3.5/library/stdtypes.html#typesseq

Other sequence types are: ranges, tuples (both also immutable), and lists (mutable).

All immutable sequences support the common sequence operations, and mutable sequences additionally support the mutable sequence operations

Strings also support a large number of type-specific methods.

Type "Conversions"

Constructors for most built-in types exist that create values of those types from other types:

Operators/Functions as syntactic sugar for special methods

None

None is like "null" in other languages

note: notebooks do not display the result of expressions that evaluate to None

some functions return None, so when we call them, there is no "Out" cell

"Truthiness"

All objects in Python can be evaluated in a Boolean context (e.g., as the condition for an if statement). Values for most types act as True, but some act (conveniently, usually) as False.

What tests as False?

4. Statements & Control Structures

Assignment

Augmented assignment

pass

pass is the "do nothing" statement