Hands-on Python Tutorial PDF

Title Hands-on Python Tutorial
Author Damian Sserunkuma
Course Computer science
Institution Makerere University
Pages 191
File Size 2.2 MB
File Type PDF
Total Downloads 92
Total Views 138

Summary

This will get you started with Python programming...


Description

Hands-on Python Tutorial Release 1.0 for Python Version 3.1+

Dr. Andrew N. Harrington, Loyola University Chicago

March 06, 2015

CONTENTS

1

2

3

4

Beginning With Python 1.1 Context . . . . . . . . . . . . . . . . . . . . . . 1.2 The Python Interpreter and Idle, Part I . . . . . . 1.3 Whirlwind Introduction To Types and Functions 1.4 Integer Arithmetic . . . . . . . . . . . . . . . . 1.5 Strings, Part I . . . . . . . . . . . . . . . . . . . 1.6 Variables and Assignment . . . . . . . . . . . . 1.7 Print Function, Part I . . . . . . . . . . . . . . . 1.8 Strings Part II . . . . . . . . . . . . . . . . . . . 1.9 The Idle Editor and Execution . . . . . . . . . . 1.10 Input and Output . . . . . . . . . . . . . . . . . 1.11 Defining Functions of your Own . . . . . . . . . 1.12 Dictionaries . . . . . . . . . . . . . . . . . . . . 1.13 Loops and Sequences . . . . . . . . . . . . . . 1.14 Decimals, Floats, and Floating Point Arithmetic 1.15 Summary . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

Objects and Methods 2.1 Strings, Part III . . . . . . . 2.2 More Classes and Methods . 2.3 Mad Libs Revisited . . . . . 2.4 Graphics . . . . . . . . . . 2.5 Files . . . . . . . . . . . . 2.6 Summary . . . . . . . . . .

1 1 6 12 14 15 17 19 20 20 23 28 41 45 59 62

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

71 . 71 . 78 . 80 . 86 . 112 . 115

More On Flow of Control 3.1 If Statements . . . . . . . . . . . . . 3.2 Loops and Tuples . . . . . . . . . . . 3.3 While Statements . . . . . . . . . . . 3.4 Arbitrary Types Treated As Boolean . 3.5 Further Topics to Consider . . . . . . 3.6 Summary . . . . . . . . . . . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

119 119 136 141 155 158 158

Dynamic Web Pages 4.1 Overview . . . . . . . . . . . . . 4.2 Web page Basics . . . . . . . . . 4.3 Composing Web Pages in Python 4.4 CGI - Dynamic Web Pages . . . . 4.5 Summary . . . . . . . . . . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

163 163 164 166 170 179

Index

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . .

. . . . .

183 i

ii

CHAPTER

ONE

BEGINNING WITH PYTHON

© Released under the Creative commons Attribution-Noncommercial-Share Alike 3.0 United States License http://creativecommons.org/licenses/by-nc-sa/3.0/us/

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

Hands-on Python Tutorial, Release 1.0 for Python Version 3.1+

1.1.2 Why Python There are many high-level languages. The language you will be learning is Python. Python is one of the easiest languages to learn and use, while at the same time being very powerful: It is used by many of the most highly productive professional programmers. A few of the places that use Python extensively are Google, the New York Stock Exchange, Industrial Light and Magic, .... Also Python is a free language! If you have your own computer, you can download it from the Internet....

1.1.3 Obtaining Python for Your Computer Even if you have Python on your own computer, you may well not have the latest version. Go to the Downloads page below and check on the latest version. The instructions for individual operating systems describe how to check if you think you already have what you need. Go to the Downloads page linked to http://www.python.org. Be careful to choose the version for your operating system and hardware. Choose the latest stable version. You do not need a source code distribution. Do not choose a version 2.X, which is incompatible. (Version 2.6 and 2.7 are described in an older version of this tutorial.) Windows If you think you already have a current Python set to go, you can check by getting the examples, Your Python Folder and Python Examples (page 6). Then try starting Idle: Starting Idle (page 10). If Idle starts, see if the version matches the latest version of Python. If so, skip the part below! Note: there is one variation from the default installation below: Read everything before completing the installation. Make sure the version is at least 3.4 or later, and use a download package with the MSI installer, if it is available. If you do not know that your machine is using a 64 bit version of the operating system, a 32 bit version is safer. Except in one case, you just need to execute the installer, and interact enough to agree to all the default choices: On the step choosing components to install, if there is an X beside the last component, putting Python in your path, you are encouraged to click on that component and include it. If you forget this one variation from the defaults, you can return to the installation and repair it. OS X If you think you already have a current Python set to go, then try starting Idle: Starting Idle (page 10). If Idle starts, see if the version matches the latest version of Python. If so, skip the part below! Double-click on the installer for your version of OS X. Find and run the MacPython.mpkg that is inside. Follow the defaults for installation. Note that Python uses a Tcl/Tk library, and Macs come with a version, however, for some versions of the operating system, a different version of Tcl/Tk is needed. Be sure to see http:http://www.python.org/download/mac/tcltk/ and see if you need to do a separate download listed there. Linux An older version of Python is generally installed, and even if a current version, 3.1+, is installed, Idle is not always installed. Look for a package to install, something like ‘idle-python’ (the name in the Ubuntu distribution).

1.1.4 Philosophy and Implementation of the Hands-On Python Tutorials Although Python is a high-level language, it is not English or some other natural human language. The Python translator does not understand “add the numbers two and three”. Python is a formal language with its own specific rules and formats, which these tutorials will introduce gradually, at a pace intended for a beginner. These tutorials are also appropriate for beginners because they gradually introduce fundamental logical programming skills. Learning these skills will allow you to much more easily program in other languages besides Python. Some of the skills you will learn are 2

Chapter 1. Beginning With Python

Hands-on Python Tutorial, Release 1.0 for Python Version 3.1+

• breaking down problems into manageable parts • building up creative solutions • making sure the solutions are clear for humans • making sure the solutions also work correctly on the computer. Guiding Principals for the Hands-on Python Tutorials: • The best way to learn is by active participation. Information is principally introduced in small quantities, where your active participation, experiencing Python, is assumed. In many place you will only be able to see what Python does by doing it yourself (in a hands-on fashion). The tutorial will often not show. Among the most common and important words in the tutorial are “Try this:” • Other requests are for more creative responses. Sometimes there are Hints, which end up as hyperlinks in the web page version, and footnote references in the pdf version. Both formats should encourage you to think actively about your response first before looking up the hint. The tutorials also provide labeled exercises, for further practice, without immediate answers provided. The exercises are labeled at three levels No label Immediate reinforcement of basic ideas - preferably do on your first pass. * Important and more substantial - be sure you can end up doing these. Allow time to do them! ** Most creative • Information is introduced in an order that gives you what you need as soon as possible. The information is presented in context. Complexity and intricacy that is not immediately needed is delayed until later, when you are more experienced. • In many places there are complications that are important in the beginning, because there is a common error caused by a slight misuse of the current topic. If such a common error is likely to make no sense and slow you down, more information is given to allow you to head off or easily react to such an error. Although this approach is an effective way to introduce material, it is not so good for reference. Referencing is addressed in several ways: • Detailed Table of Contents • Extensive Index • Flexible Search Engine built into the html version (does not work on an html version that you download to your computer) • Cross references to sections that elaborate on an introductory section. Hyperlinks allow you to jump between the referenced parts in the html version or the pdf version viewed on a computer. The pdf version also gives page references. • Concise chapter summaries, grouping logically related items, even if that does not match the order of introduction.

1.1.5 Using the Tutorial - Text and Video The Hands-on Python Tutorial was originally a document to read, with both the html version and a pdf version. Even if you do not print it, some people use the pdf version online, preferring its formatting to the formatting in the html version. Some people learn better visually and verbally from the very beginning. The Tutorial has 800 by 600 pixel videos for many sections. Also mentioned for the convenience of my Comp 150 class are videos beyondPython, for the part of the class after Python.

1.1. Context

3

Hands-on Python Tutorial, Release 1.0 for Python Version 3.1+

The videos are in two places: • Box.com: https://luc.box.com/comp150video Download split in 5 parts, with no ID needed at all. The four chapters of the Hands-on Python Tutorial and beyondPython are collected in zip files for you to download, and then expand the zip files before using. • Google Drive: https://drive.google.com/a/cs.luc.edu/#folders/0B5WvvnDHeaIYMGE2MzU4OWEtYzQ4Zi00YzhiLTliMTItNj You need a Google Drive/Docs login ID. If you are not already logged into Google Drive/Docs, you will need to do it when you click on the link. If you have that ID, then the advantage of Google Drive is that you can select exactly what parts to download in one long download (548 MB total), and you do not need to separately unzip. This may not work with Internet Explorer, but it does work with Firefox, Safari or Chrome browser. If you want all the videos for the course click on the box in front of the Title line to get all the folders under comp150video. If you want less, drill down to the parts you want, and click in the boxes in front of them. Then in the lower headings, to the right of the Trash can icon, click on the More heading. Select Download.... Leave file types As Is, and click on the download button. This is best done before going to sleep for the night if you selected most of the course! From either site, the videos are also runnable by clicking on one and streaming from the Internet, but they only delivers a smaller version of the image, leaving the audio fine, but making text hard or imposible to read. If the video is following along with the tutorial text, and you look at the full sized web page while listening, the video size may sometimes not be an issue. The Box version is also likely to be slow to start from online. The downloaded versions will display in full size and resolution. To get the most out of the tutorial, I strongly suggest the following approach for each part: • Watch a video if you like. They are clearly labeled by numerical section. Stop the video where I ask you to think. The videos hit the high points and take advantage of being able to point at specific places on the screen. Some details may only appear in the written text. Stop the video frequently to test things for yourself! If a new function is introduced, do not only watch the video, but try it out for yourself, including with data not in the video. In some places the written version mentions more examples to try. I suggest looking at the written version after each video. • Whether you look at the video of a section or not, do look through a written version, either as a first pass or to review and fill in holes from the videos. Be sure to stop and try things yourself, and see how they actually work on your computer. • Look at the labeled exercises. You are strongly recommended to give the unstarred ones an immediate try to reinforce basic concepts. The starred ones (*) are important for a strong understanding. Do not get too far ahead in reading/watching before trying the starred exercises. Understanding earlier parts well enough to be able to solve problems is going to either be completely necessary for understanding some later sections or at least make later sections easier to follow and fully comprehend. • Python provides too rich an environment to be able to show you all interrelationships immediately. That can mean errors send you in a strange (to you) direction. I cannot head them all off for you in this text. Do not be afraid to ask for help from your instructor or another more experienced Python programmer if you look and look again at something and are still stuck. The frequency of such situations should decrease as you get more experience! Have fun and be creative, and discover your power with Python!

1.1.6 Learning to Problem-Solve While the tutorial introduces all the topics, there is more to say about using it effectively. There is way too much detail to just absorb all at once, So what are the first things to learn?

4

Chapter 1. Beginning With Python

Hands-on Python Tutorial, Release 1.0 for Python Version 3.1+

More important than memorizing details is having an idea of the building blocks available and how they are useful. For the most direct exercises, you might just look back over the most recent section looking for related things, but that will not work when you have scores of sections that might have useful parts! The basic idea of the building blocks should be in your head. For instance, looking ahead to when you have finished the Tutorial through 1.10.4, you will want to have these ideas very present in your head: • You can use numbers and do arithmetic. • You can store and retrieve data using variable names and assignment statements. • Python has many useful built-in functions that can affect the system or return results for you to use. • You can get keyboard input from the user and print things back for the user. • Data comes in different types, and you can convert where it makes sense. • You can use strings a...


Similar Free PDFs