Course Development Team
Head of Programme : Assoc Prof Lee Yew Haur
Course Developer(s) : Dr Xiao Shu
Technical Writer : Chat Inoue, ETP
Video Production : Mohd Jufrie Bin Ramli, ETP
© 2018 Singapore University of S
...
Course Development Team
Head of Programme : Assoc Prof Lee Yew Haur
Course Developer(s) : Dr Xiao Shu
Technical Writer : Chat Inoue, ETP
Video Production : Mohd Jufrie Bin Ramli, ETP
© 2018 Singapore University of Social Sciences. All rights reserved.
No part of this material may be reproduced in any form or by any means without
permission in writing from the Educational Technology & Production, Singapore
University of Social Sciences.
ISBN 978-981-478-747-5
Educational Technology & Production
Singapore University of Social Sciences
463 Clementi Road
Singapore 599494
Release V1.0Table of Contents
Table of Contents
Course Guide
1. Welcome.................................................................................................................. CG-2
2. Course Description and Aims............................................................................ CG-3
3. Learning Outcomes.............................................................................................. CG-5
4. Learning Material................................................................................................. CG-6
5. Assessment Overview.......................................................................................... CG-7
6. Course Schedule.................................................................................................... CG-9
7. Learning Mode.................................................................................................... CG-10
Study Unit 1: Python Basics
Learning Outcomes................................................................................................. SU1-2
Overview................................................................................................................... SU1-3
Chapter 1: Python Programming Environment.................................................. SU1-4
Chapter 2: Variables and Types........................................................................... SU1-10
Chapter 3: Printing................................................................................................. SU1-14
Chapter 4: Input..................................................................................................... SU1-17
Summary................................................................................................................. SU1-19
Formative Assessment.......................................................................................... SU1-20
References............................................................................................................... SU1-28
iTable of Contents
Study Unit 2: Control Flow and Lists
Learning Outcomes................................................................................................. SU2-2
Overview................................................................................................................... SU2-3
Chapter 1: Control Flow......................................................................................... SU2-4
Chapter 2: Lists....................................................................................................... SU2-10
Summary................................................................................................................. SU2-14
Formative Assessment.......................................................................................... SU2-15
References............................................................................................................... SU2-23
Study Unit 3: Tuples and Dictionaries
Learning Outcomes................................................................................................. SU3-2
Overview................................................................................................................... SU3-3
Chapter 1: Tuples..................................................................................................... SU3-4
Chapter 2: Dictionaries............................................................................................ SU3-6
Summary................................................................................................................. SU3-11
Formative Assessment.......................................................................................... SU3-12
References............................................................................................................... SU3-20
Study Unit 4: Functions, Methods and Packages
Learning Outcomes................................................................................................. SU4-2
Overview................................................................................................................... SU4-3
Chapter 1: Functions................................................................................................ SU4-4
Chapter 2: Methods............................................................................................... SU4-10
iiTable of Contents
Chapter 3: Packages............................................................................................... SU4-14
Summary................................................................................................................. SU4-17
Formative Assessment.......................................................................................... SU4-18
References............................................................................................................... SU4-26
Study Unit 5: Scientific Computing and Plotting with Python
Learning Outcomes................................................................................................. SU5-2
Overview................................................................................................................... SU5-3
Chapter 1: NumPy................................................................................................... SU5-4
Chapter 2: Matplotlib............................................................................................ SU5-12
Summary................................................................................................................. SU5-17
Formative Assessment.......................................................................................... SU5-18
References............................................................................................................... SU5-28
Study Unit 6: Data Analysis with Python
Learning Outcomes................................................................................................. SU6-2
Overview................................................................................................................... SU6-3
Chapter 1: DataFrame............................................................................................. SU6-4
Chapter 2: Selection................................................................................................. SU6-9
Chapter 3: Missing Values.................................................................................... SU6-17
Chapter 4: Grouping.............................................................................................. SU6-18
Chapter 5: Time Series.......................................................................................... SU6-19
Summary................................................................................................................. SU6-27
Formative Assessment.......................................................................................... SU6-28
iiiTable of Contents
References............................................................................................................... SU6-38
ivList of Tables
List of Tables
Table 4.1 The meaning of formatting directives used in Figure 4.6.................... SU4-15
vList of Tables
viList of Figures
List of Figures
Figure 1.1 Installing Python 3 on windows: check the box to add Python to
PATH............................................................................................................................... SU1-4
Figure 1.2 Composing scripts in Atom and Executing in PowerShell.................. SU1-6
Figure 1.3 Comments in Python scripts.................................................................... SU1-8
Figure 1.4 Creating and using Python variables................................................... SU1-10
Figure 1.5 Math operations in Python..................................................................... SU1-12
Figure 1.6 Printing format strings............................................................................ SU1-14
Figure 1.7 Printing format strings using the format() method............................ SU1-15
Figure 1.8 Escape sequences in Python scripts...................................................... SU1-16
Figure 1.9 User input in Python............................................................................... SU1-17
Figure 2.1 Creating sequential if-elif-else blocks..................................................... SU2-6
Figure 2.2 Creating nested if-elif-else blocks............................................................ SU2-7
Figure 2.3 Creating a whil e-loop block.................................................................... SU2-8
Figure 2.4 Creating and subsetting Python list...................................................... SU2-11
Figure 2.5 Adding, removing and joining elements in Python list..................... SU2-11
Figure 2.6 Iterating elements in Python list using for-loop.................................. SU2-13
Figure 3.1 Creating and manipulating tuples.......................................................... SU3-4
Figure 3.2 Comparing indexes of lists and dictionaries......................................... SU3-6
Figure 3.3 Creating dictionaries................................................................................. SU3-7
viiList of Figures
Figure 3.4 Adding items into dictionaries and two ways of accessing items in
dictionaries..................................................................................................................... SU3-8
Figure 3.5 Iterating over items in dictionaries using for-loop............................... SU3-9
Figure 4.1 Using the built-in function len().............................................................. SU4-4
Figure 4.2 Defining and calling user-defined functions......................................... SU4-6
Figure 4.3 Using the string method split()............................................................. SU4-10
Figure 4.4 A sample of Apache web log................................................................. SU4-11
Figure 4.5 Reading and writing text files............................................................... SU4-12
Figure 4.6 Using the standard library datetime..................................................... SU4-14
Figure 5.1 Applying arithmetic operators on arrays elementwise........................ SU5-5
Figure 5.2 Subsetting NumPy array using index or Boolean masking................. SU5-6
Figure 5.3 2-D Numpy Array and its attributes...................................................... SU5-7
Figure 5.4 Subsetting 2-D Numpy array................................................................... SU5-8
Figure 5.5 Calculating summary statistics with 2-D Numpy array...................... SU5-9
Figure 5.6 Generating 30 random samples from standard normal
distribution................................................................................................................... SU5-10
Figure 5.7 Building a line plot with customised labels, title and axis ticks........ SU5-12
Figure 5.8 A line plot produced with the code in Figure 5.7............................... SU5-13
Figure 5.9 Building a histogram of random samples from a normal
distribution................................................................................................................... SU5-15
Figure 5.10 A histogram produced with the code in Figure 5.9.......................... SU5-16
Figure 6.1 A DataFrame of FAO.csv with the first five rows shown.................... SU6-5
viiiList of Figures
Figure 6.2 FAO.csv opened in a text editor.............................................................. SU6-6
Figure 6.3 Getting CSV data into a DataFrame....................................................... SU6-6
Figure 6.4 Setting index for a DataFrame................................................................. SU6-7
Figure 6.5 Sorting the column ‘Production’ in a descending order...................... SU6-8
Figure 6.6 Selecting one column using indexing operator..................................... SU6-9
Figure 6.7 Selecting one column as an attribute.................................................... SU6-10
Figure 6.8 Selecting multiple columns.................................................................... SU6-10
Figure 6.9 Adding a new column............................................................................ SU6-11
Figure 6.10 Selecting rows by label......................................................................... SU6-12
Figure 6.11 Selecting a row by position.................................................................. SU6-13
Figure 6.12 Selecting elements by specifying both column and row labels....... SU6-14
Figure 6.13 Using one column for Boolean masking............................................ SU6-15
Figure 6.14 Using multiple columns for Boolean masking.................................. SU6-16
Figure 6.15 Computing average production per area using the agg
method.......................................................................................................................... SU6-18
Figure 6.16 Resetting the DataFrame index............................................................ SU6-20
Figure 6.17 Setting the index using the Year column............................................ SU6-20
Figure 6.18 Converting the index to PeriodIndex................................................. SU6-21
Figure 6.19 Importing data with the index as DatetimeIndex............................. SU6-23
Figure 6.20 Code to plot the close price and its 30-day moving average........... SU6-24
Figure 6.21 Plotting the close price and its 30-day moving average................... SU6-25
ixList of Figures
Figure 6.22 Zooming out the plotting in Figure 6.21............................................ SU6-26
xCourse
Guide
Python ProgrammingANL251 Course Guide
1. Welcome
Presenter: Dr Xiao Shu
This streaming video requires Internet connection.
Access it via Wi-Fi to avoid incurring data charges on your personal mobile plan.
Click here to watch the video. i
Welcome to the course ANL251 Python Programming, a 5 credit unit (CU) course.
This Study Guide will be your personal learning resource to take you through the course
learning journey. The guide is divided into two main sections – the Course Guide and
Study Units.
The Course Guide describes the structure for the entire course and provides you with an
overview of the Study Units. It serves as a roadmap of the different learning components
within the course. This Course Guide contains important information regarding the
course learning outcomes, learning materials and resources, assessment breakdown and
additional course information.
i https://d2jifwt31jjehd.cloudfront.net/ANL251/IntroVideo/ANL251_Intro_Video.mp4
CG-2ANL251 Course Guide
2. Course Description and Aims
This course provides you with fundamental knowledge and skills to use the Python
programming language to write programs for problem solving. At the end of this course,
students will be competent in using Python to perform tasks such as data acquiring,
manipulation, visualization and analysis. Since this course is designed to help students
with little prior exposure to programming, it will focus on breadth rather than depth.
Course Structure
This course is a 5-credit unit course presented over 6 weeks.
There are six Study Units in this course. The following provides an overview of each Study
Unit.
Study Unit 1 – Python Basics
This unit takes the first steps Python programming, including variables, data types,
operators, formatted printing and user input.
Study Unit 2 – Control Flow and Lists
This unit will focus on the two types of control flow to dynamically change how our
Python program behaves, the conditional statement and loops. This unit also starts to
introduce the compound built-in data type in Python, lists.
Study Unit 3 – Tuples and Dictionaries
This unit continues to introduce another two types of compound built-in data types in
Python, tuples and dictionaries.
Study Unit 4 – Functions, Methods and Packages
This unit covers the import mechanisms in Python for reuse, distribute and manage code.
CG-3ANL251 Course Guide
Study Unit 5 – Scientific Computing and Plotting with Python
This unit introduces two important fundamental packages – the NumPy package to
efficiently store and do calculations with huge amounts of data, and the matplotlib
package to create customized plots.
Study Unit 6 – Data Analysis with Python
This unit introduces the Pandas package, the key data structure and data analysis tools
for Python.
CG-4ANL251 Course Guide
3. Learning Outcomes
Knowledge & Understanding (Theory Component)
By the end of this course, you should be able to:
• Describe and compare the basic data types, operators, strings, lists, tuples and
dictionaries in Python.
• Discuss the concepts of functions, methods, packages and modules, and how
Python manages and imports packages/modules
• Explain the vectorized operations on NumPy arrays
Key Skills (Practical Component)
By the end of this course, you should be able to:
• Write Python programs to apply appropriate programming concepts, Python builtin functions, user-defined functions and standard libraries for problem solving.
• Develop logic control flows in Python programs.
• Prepare and analyse datasets using NumPy and pandas.
• Construct customized plots using matplotlib.
CG-5ANL251 Course Guide
4. Learning Material
The following is a list of the required learning materials to complete this course.
Required Textbook(s)
Shaw, Zed. A. (2017). Learn Python 3 the Hard Way. Addison-Wesley Professional.
Register your textbook and then download the included videos at informit.com/
title/9780134692883.
Other recommended study material (Optional)
The following learning materials may be required to complete the learning activities:
Website(s):
The Python documentation. https://docs.python.org/3/
NumPy user guide. https://docs.scipy.org/doc/numpy/user/index.html
The Pyplot API. https://matplotlib.org/api/pyplot_summary.html
pandas documentation. http://pandas.pydata.org/pandas-docs/stable/
CG-6ANL251 Course Guide
5. Assessment Overview
The overall assessment weighting for this course is as follows:
Assessment Description Weight Allocation
PCOQ Pre-Course Quiz 2%
PCQ1 Pre-Class Quiz 1 2%
PCQ2 Pre-Class Quiz 2 2%
PCT Participation 6%
GBA Group-Based Assignment 19%
TMA Tutor-Marked Assignment 19%
ECA End-of-Course
Assignment
50%
TOTAL 100%
The following section provides important information regarding Assessments.
SUSS’s assessment strategy consists of two components: the Overall Continuous
Assessment Score (OCAS) and the Overall Examinable Score (OES), which make up the
overall course assessment score. Both components are equally weighted.
Continuous Assessment:
There will be continuous assessment in the form of participation, three quizzes, one groupbased assignment and one tutor-marked assignment. In total, this continuous assessment
will constitute 50 percent of overall student assessment for this course.
CG-7ANL251 Course Guide
Examination:
The end-of-course assignment will constitute the other 50 percent of overall student
assessment. All topics covered in the course outline will be covered.
Passing Mark:
To successfully pass the course, you must obtain a minimum passing mark of 40 percent
for the continuous assessment. That is, students must obtain at least a mark of 40 percent
for the combined assessments and also at least a mark of 40 percent for the final exam. For
detailed information on the Course grading policy, please refer to The Student Handbook
(‘Award of Grades’ section under Assessment and Examination Regulations). The Student
Handbook is available from the Student Portal.
CG-8ANL251 Course Guide
6. Course Schedule
To help monitor your study progress, you should pay special attention to your Course
Schedule. It contains study unit related activities including quizzes and assignments.
Please refer to the Course Timetable in the Student Portal for the updated Course
Schedule.
Note: You should always make it a point to check the Student Portal for any
announcements and latest updates.
CG-9ANL251 Course Guide
7. Learning Mode
The learning process for this course is structured along the following lines of learning:
a. Self-study guided by the study guide units. Independent study will require at
least 3 hours per week.
b. Working on assignments, either individually or in groups.
c. Classroom Seminar sessions (3 hours each session, 6 sessions in total).
iStudyGuide
You may be viewing the iStudyGuide version, which is the mobile version of the
Study Guide. The iStudyGuide is developed to enhance your learning experience with
interactive learning activities and engaging multimedia. Depending on the reader you are
using to view the iStudyGuide, you will be able to personalise your learning with digital
bookmarks, note-taking and highlight sections of the guide.
Interaction with Instructor and Fellow Students
Although flexible learning – learning at your own pace, space and time – is a hallmark
at SUSS, you are encouraged to engage your instructor and fellow students in online
discussion forums. Sharing of ideas through meaningful debates will help broaden your
learning and crystallise your thinking.
Academic Integrity
As a student of SUSS, it is expected that you adhere to the academic standards stipulated
in The Student Handbook, which contains important information regarding academic
policies, academic integrity and course administration. It is necessary that you read and
understand the information stipulated in the Student Handbook, prior to embarking on
the course.
CG-10Study
Unit1
Python Basic
[Show More]