Uploaded by Glenda Tan

Programming for Business Analytics

advertisement
1/9/23, 12:53 PM
Lecture1
Lecture 1: Programming for Business Analytics
Contents
Why Python programming?
High readability
Write readable Python code
The Python universe
Jupyter Notebook
Modal user interface
Try Jupyter Notebook as a scientific calculator
blue line part of each segment
represents a cell
Why Python programming?
High readability
If you ask Python programmers what they like most about Python, they will often cite its high
readability. Indeed, a high level of readability is at the heart of the design of the Python
language, following the recognized fact that code is read much more often than it is written. The Hitchhiker's Guide to Python (https://docs.python-guide.org/writing/style/)
Write readable Python code
Being "Pythonic"
markdown cell
One reason for the high readability of Python code is its relatively complete set of Code Style
guidelines and “Pythonic” idioms.
When a veteran Python developer (a Pythonista) calls portions of code not “Pythonic”, they
usually mean that these lines of code do not follow the common guidelines and fail to express
its intent in what is considered the best (most readable) way. - The Hitchhiker's Guide to
Python (https://docs.python-guide.org/writing/style/)
Consistency
Meaningful names
Explanatory comments
Python Enhancement Proposal (PEP) 8: Style Guide for Python Code
(https://www.python.org/dev/peps/pep-0008/#whitespace-in-expressions-and-statements)
The Zen of Python
another type of style guide
https://htmtopdf.herokuapp.com/ipynbviewer/temp/45b15e7e868bb161fecbaf0db07f31d3/Lecture1.html?t=1673239555319
1/6
1/9/23, 12:53 PM
Lecture1
code cell
In [1]:
import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
The Python universe
aka Python ecosystem
The usefulness of Python for data science stems primarily from the large and active
ecosystem of third-party packages: NumPy for manipulation of homogeneous array-based
data, Pandas for manipulation of heterogeneous and labeled data, SciPy for common
scientific computing tasks, Matplotlib for publication-quality visualizations, IPython for
interactive execution and sharing of code, Scikit-Learn for machine learning, and many more
tools that will be mentioned in the following pages. - Python Data Science Handbook
(https://jakevdp.github.io/PythonDataScienceHandbook/00.00-preface.html)
https://htmtopdf.herokuapp.com/ipynbviewer/temp/45b15e7e868bb161fecbaf0db07f31d3/Lecture1.html?t=1673239555319
2/6
1/9/23, 12:53 PM
Lecture1
Jupyter Notebook
The Jupyter Notebook is an open-source web application that allows you to create and share documents
used for data cleaning and transformation, numerical simulation, statistical modeling, data visualization,
machine learning, etc. It provides users with friendly browser interface that connects to the Python kernel,
and also enable users to add other components and add-ons such as equations, visualizations and narrative
text.
Modal user interface
The Jupyter Notebook has a modal user interface. This means that the keyboard does different things
depending on which mode the Notebook is in. There are two modes: edit mode and command mode.
Edit mode
Edit mode is indicated by a green cell border and a prompt showing in the editor area:
Edit the content of a cell
Press enter ( return for Mac OS) to enable edit mode, and esc to quit editing
Command mode
blue
Command mode is indicated by a grey cell border with a blue left margin:
change content such as change
type of cell, delete cell etc
Notebook level actions: insert/remove cells, and change cell types, etc.
Keyboard shortcuts
A : insert a cell above
B : insert a cell below
D , D : delete a cell
Z : recover a deleted cell
M : change to markdown mode in markdown mode can double click to change back to blue border. used
to show plain text and can add other things like hyperlinks, change font,
Y : change to code mode
insert pictures and tables etc
Other keyboard shortcuts
ctrl + enter ( control + return for Mac OS): run cells
shift + enter ( shift + return for Mac OS): run cells, and select (insert) the cell below
Once a code cell is executed, there will be a number appearing in the square bracket in front of the cell,
indicating the sequence of executing cells.
Try Jupyter Notebook as a scientific calculator
Operators and operands
In many programming languages, operators are special symbols that represent computations like addition
and multiplication. The values the operator is applied to are called operands. The commonly used math
operators of Python are presented in the table below.
https://htmtopdf.herokuapp.com/ipynbviewer/temp/45b15e7e868bb161fecbaf0db07f31d3/Lecture1.html?t=1673239555319
3/6
1/9/23, 12:53 PM
Lecture1
Operator
Description
Example
Result
+
addition
5 + 8
13
-
subtraction
9 - 2
7
*
multiplication
4 * 7
28
/
float point division
7 / 2
3.5
//
integer (truncating) division
7 / 2
3
%
modulus (remainder)
7 % 2
1
**
exponentiation
3 ** 4
81
square
truncating does not mean round
up/round down, just to drop the
fraction
modulus gives remainder of division
It can be seen that many of the operators are consistent with a scientific calculator, which enables us to do
some simple calculations as follows.
In [4]:
3 / 1.1
Out[4]:
2.727272727272727
In [3]:
(2+3.5) * 6 / (1.75-0.25)
Out[3]:
22.0
There are a few special operators, like // takes the integer division of two numbers, where the fraction part
of the result is truncated.
In [4]:
11 // 3
Out[4]:
3
The operator % takes the remainder of a division expression.
In [5]:
(2 + 3*5) % 7
Out[5]:
3
The operator ** calculates the exponentiation.
https://htmtopdf.herokuapp.com/ipynbviewer/temp/45b15e7e868bb161fecbaf0db07f31d3/Lecture1.html?t=1673239555319
4/6
1/9/23, 12:53 PM
Lecture1
In [6]:
2 ** 3
Out[6]:
8
In [7]:
4 ** 0.5
Out[7]:
2.0
Comments and displaying results
A comment is a piece of text in your program that is ignored by the Python interpreter. You might use
comments to clarify nearby Python code, make notes to yourself (or others) to fix something in the future, or
for whatever purposes you like. You mark a comment by using the # character; everything from that point
on to the end of the current line is part of the comment. Please check the following example.
In [8]:
3.5 * 2
#
#
#
#
Everthing follows the # character is the comment
2 + 3 + 6
The expression above is a part of the comment
It is ignored by the Python interpreter
Out[8]:
7.0
In cases for multi-line comments, it will be more convenient to use a text body surrounded by three single or
three double quotation marks. Any quotes, tabs, symbols, numbers, or newlines in between the "triple
quotes" are considered part of the text body.
In [9]:
"""
When you’re fundraising, it’s AI.
When you’re hiring, it’s ML.
When you’re implementing, it’s linear regression.
When you’re debugging, it’s printf().
- Baron Schwartz
"""
(2 + 5*7) // 3
Out[9]:
12
https://htmtopdf.herokuapp.com/ipynbviewer/temp/45b15e7e868bb161fecbaf0db07f31d3/Lecture1.html?t=1673239555319
5/6
1/9/23, 12:53 PM
Lecture1
In this course, please pay attention to comments in the code cell, because we will frequently use them to
explain the meanings and logics of the given code.
The results of the last line in a code cell is displayed as its Out message. Results that are not generated by
the last line of code will not be shown, as the example below.
In [10]:
2 + 3
1.5
1 + 2 + 3
# Result of this expression is not shown
# Result of this expression is not shown
# Result of the last line is displayed as "Out"
Out[10]:
6
https://htmtopdf.herokuapp.com/ipynbviewer/temp/45b15e7e868bb161fecbaf0db07f31d3/Lecture1.html?t=1673239555319
6/6
Download