Show Menu
Cheatography

Python for Business Analytics (Part 1) Cheat Sheet by

Python for Business Analytics

Data Type

Integer
-100, 0, -100
Float
-100.98, 0.0001, 90.00
String
'Python', '400', '100+200', 'True'
Boolean
True, False

Operators

Numeric
 
Comparison
+
Addition
==
Equal
-
Subtra­ction
!=
Different
*
Multpl­ication
>
Higher
/
Division
<
Lower
**
Exponent
>=
Higher or Equal
%
Modulus
<=
Lower or Equal
//
Floor Division
 
Boolean
 
String
&
Logical AND
+
Concat­enation
|
Logical OR
not
Logical NOT

Assignment Statement & Expression

Expression is a combin­ation of values, variables, and operators
222
'text'
25+36
'App' + 'le'
Assignment Statement links a variable name on the left hand side of the operator, with an expression on the right hand side.
a = 222
b = 'text'
c = 25 +36
d = 'App' + 'le'
a = a + 1

List Operations

Create a List
L1 = [1, 2, 3, 4, 5, 6]
Get the first element
L1[0]
Get the last element
L1[-1]
Count List elements
len()
Insert an element
insert()
Insert an element to the end
append()
Sort all elements
sort()
Remove an element
pop()
Convert object to List
list()
 

Slicing

Slicing Expression
List Name[start index : stop index : step size]
(Step size is optional)
Examples
2nd - 5th elements
L1[1:5]
2nd - Last elements
L1[1:]
1st - 3rd elements
L1[0:3]
All alternate elements
L1[::2]

Dictionary (Dict.) Operations

Create a Dict.
D1 = {"An­dre­w":18, "­Joh­nso­n":23, "­Oli­via­":22}
Create a Dict. from two Lists
dict(z­ip())
Access Dict. value
D1["­And­rew­"]
Update Dict. value
D1["­And­rew­"] = 20
Add an element
D1["­Sue­"] = 25
Drop an element
del D1["­Joh­nso­n"]
Count Dict. elements
len()
Return all keys
keys()
Return all values
values()

Tuple Operations

Create a Tuple
T1 = (1, 2, 3, 4, 5, 6)
Convert List to Tuple
tuple()
Note: Tuple elements are immutable and cannot be changed via operat­ions.

Set Operations

Create a Set
S1 = {1, 2, 3, 4, 5}
Insert an element
add()
Find unique elements
set()
Create a Set with all elements from 2 Sets
union()
Create a Set with common elements from 2 Sets
inters­ect­ion()

List Compre­hension

newlist = [expre­ssion for variable in sequence if condition]
 

Condit­ional Statements

if Condition 1:
    Code Block 1
elif Condition 2:
    Code Block 2
elif Condition 3:
    Code Block 3
else: 
    Code Block 4

for Loops

for <variable> in <sequence>:
    Code Block

Example:
for x in range(0,5):
    print(x)

while Loops

while <expression>:
    Code Block

Example:
i = 2
while i <= 10:
    print(i)
    i = i + 3

Nested loop

for iterating_var in sequence:
    for iterating_var in sequence:
        Code Block1
    Code Block2

while <expression>:
    while <expression>:
        Code Block1
    Code Block2

Statements used with Loops

break
Terminate the whole loop
continue
Stop the current iteration of the loop, and continue with the next. Loop does not terminate.
pass
Do nothing and continue the rest of the code inside a loop for the current iteration
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Nature of Business and Accounting Cheat Sheet
          Accounting Principles and Business Transactions Cheat Sheet