ISOM 3400 – PYTHON FOR BUSINESS ANALYTICS
2. Python Basics
Yingpeng Robin Zhu
JUN 22, 2022
1
Recap
Advantages vs. disadvantages of Python
Course focus is on business analytics, whose core goal is to
To extract useful information from data to make decision
Two main categories of machine learning
Supervised machine learning
Unsupervised machine learning
2
Recap
In this course, we will apply Python language enabled with packages
such as pandas to build machine learning model in order to analyze
the business problem
3
Recap
4
Recap
5
About Guest Speaker
Final Exam (Date)
WeChat or WhatsApp or Zoom
7
Assignments of Lab Session 1
8
Class Objectives
Basic syntax for statements
Variables and values
Datatypes
Operators
Functions
Strings
9
Python Syntax
Execute Python Syntax
As we learned in the previous session, Python syntax can be executed by writing
directly in the Command Line:
10
Python Indentation
Python Indentation
Indentation refers to the spaces at the beginning of a code line
Where in other programming languages the indentation in code is for readability
only, the indentation in Python is very important
Python uses indentation to indicate a block of code
11
Python Indentation
Python Indentation
The number of spaces is up to you as a programmer, the most common use is four
(after you finish one line, press the key “Enter”), but it has to be at least one.
Number of space = 4
Number of space = 1
Number of space = 8
12
Python Indentation
Python Indentation
You have to use the same number of spaces in the same block of code, otherwise
Python will give you an error
Number of space = 1
Number of space = 4
How can you fix this error?
13
Class Objectives
14
Print() Method
The Python print() function takes in any number of parameters and prints them out on
one line of text. The items are each converted to text form, separated by spaces, and
there is a single '\n' at the end (the "newline" char)
Syntax: print(object(s), sep=separator, end=end, file=file, flush=flush)
print("Hello World")
print("Hello",'World')
print('hi', 'there', 'great')
print('hi' 'there' 'great')
print("Hello", "World", sep='...', end='!!')
print("Hello", "New", "World", sep='...', end='!!')
Note that print is different in old versions of Python (2.7) where it was a statement and
did not need parenthesis around its arguments
15
Variables & Values
A name that is used to denote something or a value is called a variable. In
python, variables can be declared by assigning values to them
The basic data types built into Python include int (integers), float (floating
point numbers), str (unicode character strings) and bool (boolean). Some
examples of each are provided in the following slides
16
Variables & Values
Variables are created when you assign a value to it.
Bind a name on the left to the right object
When we enter a name in python, it gives us back the object bound to it
17
Variables & Values
Variables do not need to be declared with any particular type, and can even
change type after they have been set
18
Variables & Values
Rules for Python variables
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-
9, and _ )
Variable names are case-sensitive (age, Age and AGE are three different variables)
myvar = “Robin"
my_var = "Robin" 2myvar = "Robin"
_my_var = "Robin" my-var = "Robin"
myVar = "Robin" my var = "Robin"
MYVAR = "Robin"
myvar2 = "Robin"
19
Variables & Values
Operation on variables (more details about operation will be shown in the
next a few slides):
20
Variables & Values
Output Variables
The Python print() function is often used to output variables
In the print() function, you output multiple variables, separated by a comma
You can also use the + operator to output multiple variables
Notice the space character after "Python " and "is ", without them the result would be "Pythonisawesome"
21
Variables & Values
Output Variables
In the print() function, when you try to combine a string and a number with the +
operator, Python will give you an error
The best way to output multiple variables in the print() function is to separate them
with commas, which even support different data types
22
Variables & Values
Global Variables
Variables that are created outside of a function (as in all of the examples above) are
known as global variables
Global variables can be used by everyone, both inside of functions and outside
23
Variables & Values
Global Variables
If you create a variable with the same name inside a function, this variable will be
local, and can only be used inside the function.
The global variable with the same name will remain as it was, global and with the
original value
24
Python Data Types
Data Type Examples
Text Type str “Hi”, “Mike", 'Hello Python’
Numeric Types int 1,2,3
float 5.0, 6.01
complex 5+7j
Sequence Types list ["apple", "banana", "cherry"]
tuple ("apple", "banana", "cherry")
range range(6)
Set set {"apple", "banana", "cherry"}
Mapping Type dict {"name" : "John", "age" : 36}
boolean bool True, False
25
Python Data Types
26
Python Data Types
Setting/Changing the Specific Data Type
27
Basic syntax for statements
Comments
The ‘#’ character indicates that the rest of the line is a comment
Typically, you use comments to explain formulas, algorithms, and complex business
logic
When executing a program, the Python interpreter ignores the comments and only
interprets the code
The '''.....''' (triple single quotation marks) characters indicate that text
inside is a multi-line comment. << See an example in the next cell>>
Writing comments is a good habit, why?
Interpretation of the code (for yourself and for your collaborators)
28
Examples of Comments
29
Basic syntax for statements
The basic rules for writing simple statements and expressions in
Python are:
No spaces or tab characters allowed at the start of a statement:
Indentation plays a special role in Python (see the section on control
statements). For now, simply ensure that all statements start at the
beginning of the line
Statements finish at the end of the line, except for several cases
30
Help()
Python has extensive help built in. You can execute help() for an
overview or help(x) for any library, object or type x to get more
information. For example:
Help(max) - shows you the meaning of this function, and statements.
31
Help()
32
Some Basic Built-in Functions
https://docs.python.org/3/library/functions.html
33
Some Basic Built-in Functions
34
Content
So far, we have discussed:
Basic syntax for statements
Variables and values
Different data types supported by Python
Built-in Functions
We will continue to learn:
Different operators you can perform on different types of data (e.g., arithmetic,
relational, boolean)
A few mathematical functions and conversion functions
How to accept user input
String type and indexing
35
Python
36
Content
So far, we have discussed:
Basic syntax for statements
Variables and values
Different data types supported by Python
Built-in Functions
We will continue to learn:
Different operators you can perform on different types of data (e.g., arithmetic,
relational, boolean)
A few mathematical functions and conversion functions
How to accept user input
String type and indexing
37
Operators
Python Operators
Operators are used to perform operations on variables and values
In the example below, we use the + operator to add together two values
Python divides the operators in the following groups
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators 38
Operators
Python Arithmetic Operators
Arithmetic operators are used with numeric values to perform common
mathematical operations
divides two numbers and rounds the result down to the nearest integer
39
Operators
Python Assignment Operators
Assignment operators are used to assign values to variables
40
Operators
Relational/Comparison Operators
These operators compare the values on either sides of them and decide the
relation among them. It returns a value of True or False.
41
Operators
Logical Operators
Logical operators are used to combine conditional statements. It returns a value of
True or False.
42
Operators
Membership Operators
Membership operators are used to test if a sequence is presented in an object.
43
Mathematical functions
Mathematical functions include logarithms, trigonometric functions,
power and so on. For example, you can define:
Once a function is defined, we can “call” it by name with necessary inputs provided
(i.e., 𝑓(3,5))
Python looks up its definition and returns a putout (11 for 𝑓(3,5))
44
Functions in Python Math Module
The math module is a standard module in Python and is always available.
To use mathematical functions under this module, you have to import the
module using import math
45
Accepting User Inputs
Input(prompt), prompts for and returns input as a string. You may assign
the input value into a variable, to be used later
46
Accepting User Inputs
Input(prompt), prompts for and returns input as a string. You may assign
the input value into a variable, to be used later
47
String
Strings are constructed by enclosing string literals in single quote or
double quotes
A string is a sequence of characters. Strings are reducible to their
component characters
48
String
Multiline Strings
You can assign a multiline string to a variable by using three quotes
49
String
The characters in a string are indexed by integers
(representing positions in the sequence), and can be
individually accessed by using the indexing operator (Square
brackets [ ] that enclose the index)
50
String Slicing Operator
Characters from a specific index of the string can be accessed with the string[index]
operator, indexing starts with zero in Python
If we claim a positive number, then start from the left. Remember to start reading
from zero
The negative indices can be used to start counting from the back. Remember
negative indexing starts reading from -1
Out of range indexing will give an error message
51
String Slicing Operator
string[a], returns a character from a positive index a of the string from the left side
string[-a], returns a character from a negative index a of the string from the right side
string[a:b], returns characters from positive index a to positive index b, the last
character is not included
52
String Slicing Operator
string[a:-b], returns characters from positive index a to the negative index b, the last
character is not included
string[a:], returns characters from positive index a to the end of the string
string[:b], returns characters from the start of the string to the positive index b
53
String Concatenate Operator
The + operator can be used to concatenate two different strings
Appending the same string to a string can be done using the * operator (Repetition)
54
Jupyter Notebook
55