




























































































Estude fácil! Tem muito documento disponível na Docsity
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Esta página não é visível na pré-visualização
Não perca as partes importantes!





























































































Board of Series editors
Leopoldo Angrisani, Napoli, Italy Marco Arteaga, Coyoacán, México Samarjit Chakraborty, München, Germany Jiming Chen, Hangzhou, P.R. China Tan Kay Chen, Singapore, Singapore Rüdiger Dillmann, Karlsruhe, Germany Haibin Duan, Beijing, China Gianluigi Ferrari, Parma, Italy Manuel Ferre, Madrid, Spain Sandra Hirche, München, Germany Faryar Jabbari, Irvine, USA Janusz Kacprzyk, Warsaw, Poland Alaa Khamis, New Cairo City, Egypt Torsten Kroeger, Stanford, USA Tan Cher Ming, Singapore, Singapore Wolfgang Minker, Ulm, Germany Pradeep Misra, Dayton, USA Sebastian Möller, Berlin, Germany Subhas Mukhopadyay, Palmerston, New Zealand Cun-Zheng Ning, Tempe, USA Toyoaki Nishida, Sakyo-ku, Japan Bijaya Ketan Panigrahi, New Delhi, India Federica Pascucci, Roma, Italy Tariq Samad, Minneapolis, USA Gan Woon Seng, Nanyang Avenue, Singapore Germano Veiga, Porto, Portugal Haitao Wu, Beijing, China Junjie James Zhang, Charlotte, USA
An Introduction to Python
and Computer Programming
123
Yue Zhang Singapore University of Technology and Design Singapore Singapore
ISSN 1876-1100 ISSN 1876-1119 (electronic) Lecture Notes in Electrical Engineering ISBN 978-981-287-608-9 ISBN 978-981-287-609-6 (eBook) DOI 10.1007/978-981-287-609-
Library of Congress Control Number: 2015943362
Springer Singapore Heidelberg New York Dordrecht London © Springer Science+Business Media Singapore 2015 This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use. The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, express or implied, with respect to the material contained herein or for any errors or omissions that may have been made.
Printed on acid-free paper
Springer Science+Business Media Singapore Pte Ltd. is part of Springer Science+Business Media (www.springer.com)
In addition, I include the most basic concepts in computer science into the introduction to Python, including information theory, computer architecture, data structure and algorithms, numerical analysis, and program design thinking such as functional programming and object-oriented programming. All these concepts are necessary backgrounds on which Python operates, and through which students can acquire a basic idea of computer science. I try to merge them into relevant sections, and make my introduction most simple and gentle. In terms of Python syntax and libraries, I prioritize the former over the latter, introducing the full Python syntax and the most important libraries. The goal is to introduce Python to students with no programming or computer science back- ground, equipping them with knowledge of the programming language and its underlying mechanisms, so that they can learn the usage of additional libraries with little difficulty, by consulting the Python documentation. It took me over a year to transform my lecture notes into the book, during which many people gave me valuable help. Special thanks to Qiujing Xu, Likun Qiu, Chen Lü, Yanan Lu, Chingyun Chang, Yijia Liu, Zhongye Jia, Bo Chen, Jie Yang, Meishan Zhang, Xiao Ding, Hao Zhou, and Ji Ma for helping me to draw the figures, and enter handwritten text in some chapters, to Haoliang Qi for inviting me to give a guest lecture series and encouragement to write up my lecture notes, and to all the students who gave me valuable feedback.
Yue Zhang
vi Preface
2 1 An Introduction to Python and Computer Programming
Fig. 1.1 Comparison of Python, Java and C++
Python was invented in the 1990s, named after the comedy show Monte Python’s Flying Circus. Due to its simplicity and elegance, it gained popularity among programmers quickly, and was adopted by large companies, such as Google Inc. Compared with other programming languages, such as Java and C++, Python is particularly useful for learning computer programming. Figure 1.1 shows a comparison between Python, Java and C++, three popular pro- gramming languages. Among the three, C++ was invented the earliest. Its statements were the closest to hardware instructions. As a result, C++ programs run very fast, and can be optimized in many ways. However, mastering C++ requires knowledge in computer hardware. Java was invented decades after C++. It offers higher levels of abstraction compared to C++, making it easier to program. As a trade-off, Java programs run slower than C++. Python takes one step further in abstracting away hardware details, offering a conceptually very simple system for statement execu- tion. As a result, Python is the easiest to learn and fully master, but Python programs are typically the slowest to run. In terms of functionalities, Python is as powerful as Java and C++. It supports all the functionalities that typical computer hardware supports. In addition, libraries provided by Python and open-source contributers enrich the power of Python, making it the most convenient choice for many applications, such as text processing. Python libraries such as numpy and scipy are implemented in C++ in the lower level, offering very fast ways to perform scientific computation using Python. Problem-solving skills are as important as knowledge of programming languages to computer programming. It is therefore important to learn common approaches to typical problems when learning computer programming. Python supports virtually all the programming paradigms that Java and C++ supports, and even more, making it convenient to apply flexible problem-solving techniques using the Python pro- gramming language.
As preliminary knowledge to Python programming, it is helpful to know the basic structure of a computer, the operating system, the file system, and how to install and configure Python. This section introduces these backgrounds, so that it is easier to embark on Python programming from the next chapter.
1.2 Preliminaries 3
Fig. 1.2 The structure of a computer
The structure of a computer and the mechanism in which Python code is executed are illustrated in Fig. 1.2. On the bottom of the figure is the hardware computer, which is physically tangible. It consists of three main components: the central processing unit (CPU), the memory and devices. They will be discussed in the next chapter. On top of the computer runs an operating system , such as Microsoft Windows , Linux and Mac OS. Operating systems connect computer hardware and software, providing basic interfaces to software programs for controlling the hardware. For example, the file system is an interface that operating systems provide for organizing data in the hard drive (hardware). Browsers, text editors, music players, and the vast majority of other application programs are managed by the operating system. As shown in Fig. 1.2, Python is an application program running on top of the operating system, just like a browser or a text editor. Its main function, however, is to execute generic Python code, rather than performing a specific task, which browsers and text editors do. IDLE is another application program, which provides an interactive interface for Python. On the top of the figure is Python code, which is executed by the Python application. Given a piece of Python code, or a Python program , Python executes it by interpreting the code into computer instructions, executed on computer hardware via the operating system. In the following chapters, how various types of Python statements are interpreted will be discussed in detail.
Operating systems organize data in a hard drive as files. Each file contains a certain type of data. For example, a music track can be stored as an mp3 file, a video clip can be stored as an mpeg file, and a text document can be stored as a txt file. Application programs are also files. In Windows , for example, application programs such as
1.2 Preliminaries 5
Fig. 1.4 Example text console Terminal on a Mac OS
An operating system consists of many components, and one way to categorize operat- ing system components is to put them into two types. The first type of OS components operates the computer hardware; it is called the kernel in Linux systems, for per- forming the “core” functionalities. The second type of OS components provides a user interface; it is called the shell in Linux systems. OS user interfaces have evolved from text user interface to graphical user inter- face (GUI). A text user interface is also called a text console (Fig. 1.4). It works by repeatedly displaying a prompt message to the user and waits for a line of user command. After receiving a command, it executes the command, displays results and feedbacks in text format, and prompts for a next command. On the other hand, a GUI (Fig. 1.5) displays information in graphical windows and message boxes, and receives user command via buttons, menu items and dialog boxes.
Fig. 1.5 Example graphical user interface
6 1 An Introduction to Python and Computer Programming
Compared with a text console, a GUI is more intuitive to use, not requiring one to remember the names of text commands. However, a text console is often more convenient for executing complex commands with many parameters, because such advanced commands may not have GUI at all, and even if they do, entering a row of parameters in text can be faster than choosing and entering into text boxes in a large dialog window. With respect to programming, text input and output can be much easier to program compared to GUI. As a result, text is the default input and output mechanism for many programming languages, including Python, and basic knowledge of the text console is highly useful for learning programming. In Windows , Linux and Mac OS , the text console is a program. In Windows , the program is named ‘ Command Line ’; in Linux , the program is named ‘ Terminal ’ or ‘ Console ’; in Mac OS , the program is named ‘ Terminal ’ (Fig. 1.4). Because the text console commands of Linux and Mac OS are mostly similar, they are not introduced separately in this book. After being launched, Command Line can show the following message:
C :\ Users \ yue_zhang >
After being launched, Terminal can show the following message:
Zhangs - MacBook - Pro :~ y u e _ z h a n g $
In the examples above, ‘C:\ Users \ yue_zhang >’ and ‘ Zhangs-MacBook-Pro: ∼ (^) yue_zhang$’ are the prompt messages, after which a command can be entered.
Each text command finishes with a new line (the Enter key). In a text console, all commands are entered in a current working folder , which is a folder in the file system. By default, all the commands that are entered into the text console operates on the current working folder. Without a full path being specified, all file names entered into the text console also refer to files in the current working folder by default. The current working folder is typically reflected by the prompt message. In Windows , the following command shows its full path:
C :\ Users \ yue_zhang > cd C :\ Users \ y u e _ z h a n g
In Linux and Mac OS , the following command shows its full path:
Zhangs - MacBook - Pro :~ y u e _ z h a n g $ pwd / Users / y u e _ z h a n g
By default, C:\ Users \ yue_zhang and / Users / yue_zhang are the home folder of the user yue_zhang under Windows and Mac OS , respectively. Under Linux , the home folder can be / home / yue_zhang. One can list out the files and folders contained in the current working folder. In Windows , the command is dir :
C :\ Users \ yue_zhang > dir 2 0 1 5 / 0 3 / 3 0 15:30 < DIR >. 2 0 1 5 / 0 3 / 3 0 15:30 < DIR > .. 2 0 1 5 / 0 3 / 1 2 1 0 : 1 5 < DIR > C o n t a c t s 2 0 1 5 / 0 3 / 3 0 1 4 : 4 9 < DIR > D e s k t o p 2 0 1 5 / 0 3 / 1 2 10:15 < DIR > D o c u m e n t s
8 1 An Introduction to Python and Computer Programming
Two special folder names are ‘.’ and ‘..’, which represent the current working folder and its parent folder, respectively. ‘cd .’ does nothing, for the working folder is changed to the same folder. ‘cd ..’ enters the parent folder, the folder one level up towards the root.
C :\ Users \ yue_zhang > cd C :\ Users \ y u e _ z h a n g
C :\ Users \ yue_zhang > cd ..
C :\ Users >
In all the examples above, < folder_name > is a folder name in the current working folder. This type of path is called a relative path. On the other hand, absolute path s, which specify a folder from the root, can also be used for < folder_name >. For example,
C :\ Users \ yue_zhang > cd C :\ Users \ y u e _ z h a n g
C :\ Users \ yue_zhang > cd \ Users \ y u e _ z h a n g \ D e s k t o p
C :\ Users \ y u e _ z h a n g \ Desktop >
The syntax of the cd < folder > command is the same in Linux and Mac OS as in Windows. However, if cd is used without specifying < folder >, the functionality is showing the current working directory on Windows , while entering the home folder on Linux and Mac OS.
Python programs are executed by the Python application program , or Python in short, which is a software program like TextEdit and Firefox. Python can be down- loaded from the official Python website www.python.org, and installed into a com- puter. There are two commonly used dialects of Python: Python 2 and Python 3. This book uses Python 2, in which most existing Python programs are written. However, the underlying mechanisms are mostly the same for both Python 2 and Python 3, and they are only mildly different in syntax for certain functionalities. The Python installer program allows the specification of a path, in which Python is installed. By default, the folder is C: \ Python2.7 on Windows , where 2.7 is the version number of Python. After installation, the executable Python application is the file:
C :\ P y t h o n 2 .7\ p y t h o n. exe
On Linux and Mac OS , Python is typically pre-installed, and can be found at / usr / bin / p y t h o n 2.
Python can be executed by setting the current working folder to the folder con- taining Python:
1.2 Preliminaries 9
c :\ Users \ yue_zhan g > cd \ P y t h o n 2. C :\ P y t h o n 2 .7 > p y t h o n Python 2.7.1 ( r271 :86832 , Jul 31 2011 , 1 9 : 3 0 : 5 3 ) [ MSC v .1500 32 bit ( Intel ) ] on win Type " help " , " c o p y r i g h t " , " c r e d i t s " or " l i c e n s e " for more i n f o r m a t i o n.
By typing ‘ python ’, Windows executes the file python.exe in the current working folder, which leads to the interactive execution mode of Python. On Linux and Mac OS , Python can be executed by typing ‘./ Python2.7 ’ in the current working folder.
Zhangs - MacBook - Pro :~ y u e _ z h a n g $ cd / usr / bin Zhangs - MacBook - Pro :~ y u e _ z h a n g $ ./ p y t h o n 2. Python 2.7.1 ( r271 :86832 , Jul 31 2011 , 1 9 : 3 0 : 5 3 ) [ GCC 4.2.1 ( Based on Apple Inc. build 5658) ( LLVM build 2 3 3 5. 1 5. 0 0 ) ] on l i n u x 2 Type " help " , " c o p y r i g h t " , " c r e d i t s " or " l i c e n s e " for more i n f o r m a t i o n.
Note that ‘./’ must be used when executing a program in the current working folder in Linux and Mac OS. To exit the interactive Python application, press and hold the [Ctrl] key and press the D key (i.e. press Ctrl-D ).
( c o n t i n u e d from above )
^ D Zhangs - MacBook - Pro :~ y u e _ z h a n g $
The content of this section is not typically necessary to know, but can be useful when customizing Python. Environment variables are a set of settings that an operating system uses to configure itself. For Windows , the current environment variables can be listed and modified by clicking Start → Control Panel → System → Advanced system settings. In the Advanced tab , select Environment Variables , as shown in Fig. 1.6. For Linux and Mac OS , the current environment variables can be viewed and modified by editing the file $PATH. The PATH environment variable stores a list of paths from which the operating system searches for an executable program automatically. If the specified executable is not in the current working folder ( Windows ), or the path (e.g. ‘./’; ‘/ Desktop /’) is not specified ( Linux and Mac OS ), the operating system will search the paths in the PATH environment variable for the executable. Python is typically added to the PATH environment variable when installed. There- fore, it should be possible to execute python at any path. In the rare cases when the operating system cannot find the python executable program, the path in which