Hands-on Python Tutorial - UC Homepages
Hands-on Python Tutorial
Release 1.0 for Python Version 3.1+ Dr. Andrew N. Harrington, Loyola University Chicago
March 06, 2015
CONTENTS
1 Beginning With Python
1
1.1 Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 The Python Interpreter and Idle, Part I . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.3 Whirlwind Introduction To Types and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4 Integer Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.5 Strings, Part I . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.6 Variables and Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.7 Print Function, Part I . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.8 Strings Part II . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.9 The Idle Editor and Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.10 Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.11 Defining Functions of your Own . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.12 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
1.13 Loops and Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
1.14 Decimals, Floats, and Floating Point Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
1.15 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
2 Objects and Methods
71
2.1 Strings, Part III . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
2.2 More Classes and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
2.3 Mad Libs Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
2.4 Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
2.5 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
2.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
3 More On Flow of Control
119
3.1 If Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
3.2 Loops and Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
3.3 While Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
3.4 Arbitrary Types Treated As Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
3.5 Further Topics to Consider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
3.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
4 Dynamic Web Pages
163
4.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
4.2 Web page Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
4.3 Composing Web Pages in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
4.4 CGI - Dynamic Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
4.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Index
183
i
ii
CHAPTER
ONE
BEGINNING WITH PYTHON
? Released under the Creative commons Attribution-Noncommercial-Share Alike 3.0 United States License
1.1 Context
You have probably used computers to do all sorts of useful and interesting things. In each application, the computer responds in different ways to your input, from the keyboard, mouse or a file. Still the underlying operations are determined by the design of the program you are given. In this set of tutorials you will learn to write your own computer programs, so you can give the computer instructions to react in the way you want.
1.1.1 Low-Level and High-Level Computer Operations
First let us place Python programming in the context of the computer hardware. At the most fundamental level in the computer there are instructions built into the hardware. These are very simple instructions, peculiar to the hardware of your particular type of computer. The instructions are designed to be simple for the hardware to execute, not for humans to follow. The earliest programming was done with such instructions. If was difficult and error-prone. A major advance was the development of higher-level languages and translators for them. Higher-level languages allow computer programmers to write instructions in a format that is easier for humans to understand. For example
z = x+y is an instruction in many high-level languages that means something like:
1. Access the value stored at a location labeled x 2. Calculate the sum of this value and the value stored at a location labeled y 3. Store the result in a location labeled z. No computer understands the high-level instruction directly; it is not in machine language. A special program must first translate instructions like this one into machine language. This one high-level instruction might be translated into a sequence of three machine language instructions corresponding to the three step description above: 0000010010000001 0000000010000010 0000010110000011 Obviously high-level languages were a great advance in clarity! If you follow a broad introduction to computing, you will learn more about the layers that connect low-level digital computer circuits to high-level languages.
1
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- jpt a s imple java python translator
- hands on python tutorial uc homepages
- mobilesigntranslatorforthethailanguage
- python programmer books
- hands on python tutorial loyola university chicago
- how to implement a programming language
- real time american sign language recognition with
- 15 123 systems skills in c and unix
- dna features viewer a sequence annotations formatting and
- data visualization and rendering with blender and vtk