Isom 3400 - Python For Business Analytics 2. Python Basics: Yingpeng Robin Zhu
Isom 3400 - Python For Business Analytics 2. Python Basics: Yingpeng Robin Zhu
2. Python Basics
Yingpeng Robin Zhu
1
Recap
2
Recap
3
Recap
4
Recap
5
About Guest Speaker
Final Exam (Date)
7
Assignments of Lab Session 1
8
Class Objectives
9
Python Syntax
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
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
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
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
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
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
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>>
29
Basic syntax for statements
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
35
Python
36
Content
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
divides two numbers and rounds the result down to the nearest integer
39
Operators
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
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
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
48
String
Multiline Strings
You can assign a multiline string to a variable by using three quotes
49
String
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
Appending the same string to a string can be done using the * operator (Repetition)
54
Jupyter Notebook
55