Python Fundamentals Tutorial: Getting Started

NewCircle's Python Fundamentals Tutorial

Robert Zuber


1. Getting Started

1.1. The Interactive Interpreter

The Python installation includes an interactive interpreter that you can use to execute code as you type it. This is a great tool to use to try small samples and see the result immediately without having to manage output or print statements.

1.2. Lab

If you have not done it yet, download the lab files at the following URL: https://thenewcircle.com/static/student-files/python_fundamentals_labs.zip

(Linux / Mac) Open a terminal and type python. (Windows) Open the Python IDLE IDE from the Start menu.

  1. What is the version number?
  2. Type help(str) This is a simple way to get documentation for a builtin or standard library function. You can also use the online HTML documentation.
>>> help(str)
Help on class str in module __builtin__:

class str(basestring)
 |  str(object) -> string
 |
 |  Return a nice string representation of the object.
 |  If the argument is a string, the return value is the same object.
 |
 |  Method resolution order:
 |      str
 |      basestring
 |      object
 |
 |  Methods defined here:
 |
 |  __add__(...)
 |      x.__add__(y) <==> x+y
 |
 ...
  1. Note the use of methods with names with two underscores at the beginning and end of the name. These are methods that you will generally never call directly. How do you think the __add__() method gets executed?
  2. Now try typing the following commands to see the output. Note that you don’t assign a result, you get that result in the interpreter.
>>> 'hello world'
'hello world'
>>> _ + '!'
'hello world!'
>>> hw = _
>>> hw
'hello world!'
[Tip]Tip

In the interactive interpreter, you can use the special variable "_" to refer to the result of the last statement. Handy in this mode, but meaningless in scripts.

[Note]Note

Throughout the rest of this courseware, the ">>>" in a listing indicates that the code is being executed in the interactive interpreter.

1.3. Lab

Enter the following into a new file "hello.py" in your text editor of choice.

print 'hello world!'

Save and exit, then execute the script as shown below.

$ python hello.py
hello world
[Tip]Tip

On unix, you can also use shebang (#!) notation on the first line.

Table of Contents

1. Getting Started
1.1. The Interactive Interpreter
1.2. Lab
1.3. Lab
2. Types
2.1. Strings
2.2. Integers
2.3. Floats
2.4. Complex
3. Variables
3.1. Definining
3.2. Dynamic Typing
3.3. Strong Typing
3.4. Internals
4. Simple Expressions
4.1. Boolean Evaluation
4.2. Truthiness
4.3. Branching (if / elif / else)
4.4. Block Structure and Whitespace
4.5. Lab
4.6. Multiple Cases
4.7. Lab
5. Advanced Types: Containers
5.1. Lists
5.2. Lab
5.3. Strings Revisited
5.4. Tuples
5.5. Lab
5.6. Dictionaries
5.7. Lab
5.8. Sets
5.9. Collection Transitions
6. A Bit More Iteration
6.1. Loop-Else
7. Functions
7.1. Defining
7.2. Arguments
7.3. Mutable Arguments and Binding of Default Values
7.4. Accepting Variable Arguments
7.5. Unpacking Argument Lists
7.6. Scope
7.7. Lab
8. Exceptions
8.1. Basic Error Handling
9. Code Organization
9.1. Namespaces
9.2. Importing modules
9.3. Creating Modules
10. Working with Files
10.1. File I/O
11. Interacting with the Outside World
11.1. Options
12. Regular Expressions (re)
12.1. Lab
13. Functional Programming
13.1. Functions as Objects
13.2. Higher-Order Functions
13.3. Sorting: An Example of Higher-Order Functions
13.4. Anonymous Functions
13.5. Nested Functions
13.6. Closures
13.7. Lexical Scoping
13.8. Useful Function Objects: operator
13.9. Lab
13.10. Decorators
13.11. Lab
14. Advanced Iteration
14.1. List Comprehensions
14.2. Generator Expressions
14.3. Generator Functions
14.4. Iteration Helpers: itertools
14.5. Lab
15. Debugging Tools
15.1. logging
15.2. pprint
15.3. Lab
16. Object-Oriented Programming
16.1. Classes
16.2. Emulation
16.3. classmethod and staticmethod
16.4. Lab
16.5. Inheritance
16.6. Lab
16.7. Encapsulation
16.8. Lab
17. Easter Eggs