Introduction to Python
Status Not started
Text
Week 1
Basic of Python Syntax
Each line in a Python script is a statement. Unless broken with \ or in lists.
Naming convention in Python. Identifier is the name given to our programming
elements (variables, functions, classes, module, packages, etc.):
an identifier should start with either an alphabet letter or an underscore.
After that, more than one alphabet letter (a-z or A-Z) digits (0-9), or
underscore may be used to form an identifier. No other characters are
allowed.
Identifiers in Python are case sensitive variables (list_1 different than List_1).
The easiest way to display output is print() that is a function that serves as an
output statement in python.
Indexing and slicing in Python
Objects in Python can be non-sliceable or sliceable. Sliceable objects—as strings,
lists, tuples, arrays, dataframes, etc—are objects containing indexed elements—as
cells in an array, elements in a list, columns/rows in a dataframe.
Caution: Indexing in Python starts with zero!
This means that the First element is [0], the second element is [1] and the n-th
element is [n-1].
Therefore, in a "2-D" object [0,:] is the first set of elements such as the first row of
an array, instead [:,0] represent the first column.
To recover the last element we use the minus: last element [-1], the previous last
element [-2] etc. Instead, the entire last column [:,-1]
Introduction to Python
1
Slicing: to recover closed intervals [i ,i + n] on x we use the syntax x[i−1 : i + n].
So from the 2nd to the 4th element: [1:4]. From the 4th element to the last one [3:].
Last 2 rows, first three columns [-2:, 0:3].
Python works with identification
Python uses whitespace and indentation to construct the code structure. Thus,
indentation is the equivalent to the curly brackets {} in Stata, R, and other
programs. Indentation helps preserve the order and readiness of the code.
Identation also makes it faster to write codes and avoid missing } or {.
Blocks of code
Indentation rules to indicate blocks of code:
Use the colon ":" to start a block and press Enter
All the lines in a block must use the same indentation, either 4 space or a tab.
A block can have inner blocks with next-level indentation. (So nested
indentation)
Modules
Python has a relatively small core language supported by many modules or
libraries. We import them using the import routine.
NumPy: fundamental package for scientific computing. import numpy as np.
PanDas: fundamental library for data manipulation and data analysis. import
pandas as pd
SciPy: fundamental library for numerical analysis: optimization, integration,
interpolation, etc. import scipy.optimize as opt or from scipy.optimize import
minimize.
Matplotlib: main library for plots and visual representation. import
matplotlib.pyplot as plt
Other libraries we will use: seaborn—statistical data visualization—
statsmodels—fundamental statistics and econometrics package—etc.
Introduction to Python
2
We can create our own modules! Any script we do is a module itself. In Python
pretty much everything you want to do, there is a great module designed for that.
Explore them!
Operators
Arithmetic Operations
Master Table
Name
+
*
Select
Arithmetic Operation
String Operation
Arithmetic Operation
String Operation
Number
of Code
Text 1
19
Addition / Appends the second
string to the first.
18
Multiplication / Concatenates
multiple copies of the same
string.
@
Arithmetic Operation
17
Matrix Multiplication (in NumPy
arrays)
/
Arithmetic Operation
16
Division
//
Arithmetic Operation
15
Floor Division
**
Arithmetic Operation
14
Power
==
Comparison Operators
13
Equal Condition Statement
</ < =
Comparison Operators
12
Smaller than / equal or smaller
than. (same for >)
!=
Comparison Operators
11
Not Equal
is
Identical Operators
10
True if both are true (they are
equal).
is not
Identical Operators
9
True if they are not equal
in
Belonging Operators
8
True if the element is present in
the object / Returns true if a
String Operation
Introduction to Python
3
Name
Select
Number
of Code
Text 1
character exists in the given
string
not in
Belonging Operators
7
True if element is not present in
the object.
and
Logical operators
6
True if both are true.
or
Logical operators
5
True if at least one logical
operator is true.
not
Logical operators
4
returns true if an expression is
false
&
Bitwise Operators
3
bitwise and logic operator
|
Bitwise Operators
2
The bitwise OR logic operator is
represented by the symbol |.
∼
Bitwise Operators
1
invert operator (the opposite)
+=
Augmentation Operations
20
equal to x = x+1
-=
Augmentation Operations
21
equal to x = x-1
*=
Augmentation Operations
22
equal to x = x*2
/=
Augmentation Operations
23
equal to x = x/2
abs
Operation
24
returns the absolute value of a
number without considering its
sign
round
Operation
25
Returns the rounded number
//=
Augmentation Operations
26
equal to x = x//2
-
Arithmetic Operation
27
Subtraction
Comparison Operators
Introduction to Python
4
Master Table
Name
Select
Number
of Code
Text 1
**
Arithmetic Operation
14
Power
//
Arithmetic Operation
15
Floor Division
/
Arithmetic Operation
16
Division
@
Arithmetic Operation
17
Matrix Multiplication (in NumPy
arrays)
18
Multiplication / Concatenates
multiple copies of the same
string.
19
Addition / Appends the second
string to the first.
*
+
Arithmetic Operation
String Operation
Arithmetic Operation
String Operation
==
Comparison Operators
13
Equal Condition Statement
</ < =
Comparison Operators
12
Smaller than / equal or smaller
than. (same for >)
!=
Comparison Operators
11
Not Equal
is
Identical Operators
10
True if both are true (they are
equal).
is not
Identical Operators
9
True if they are not equal
8
True if the element is present in
the object / Returns true if a
character exists in the given
string
in
Belonging Operators
String Operation
not in
Belonging Operators
7
True if element is not present in
the object.
and
Logical operators
6
True if both are true.
or
Logical operators
5
True if at least one logical
operator is true.
Introduction to Python
5
Select
Name
Number
of Code
Text 1
not
Logical operators
4
returns true if an expression is
false
&
Bitwise Operators
3
bitwise and logic operator
|
Bitwise Operators
2
The bitwise OR logic operator is
represented by the symbol |.
∼
Bitwise Operators
1
invert operator (the opposite)
+=
Augmentation Operations
20
equal to x = x+1
-=
Augmentation Operations
21
equal to x = x-1
*=
Augmentation Operations
22
equal to x = x*2
/=
Augmentation Operations
23
equal to x = x/2
abs
Operation
24
returns the absolute value of a
number without considering its
sign
round
Operation
25
Returns the rounded number
//=
Augmentation Operations
26
equal to x = x//2
-
Arithmetic Operation
27
Subtraction
Identical Operators
Master Table
Name
Select
Number
of Code
Text 1
!=
Comparison Operators
11
Not Equal
</ < =
Comparison Operators
12
Smaller than / equal or smaller
than. (same for >)
Introduction to Python
6
Name
Select
Number
of Code
Text 1
==
Comparison Operators
13
Equal Condition Statement
**
Arithmetic Operation
14
Power
//
Arithmetic Operation
15
Floor Division
/
Arithmetic Operation
16
Division
@
Arithmetic Operation
17
Matrix Multiplication (in NumPy
arrays)
18
Multiplication / Concatenates
multiple copies of the same
string.
19
Addition / Appends the second
string to the first.
*
+
Arithmetic Operation
String Operation
Arithmetic Operation
String Operation
is
Identical Operators
10
True if both are true (they are
equal).
is not
Identical Operators
9
True if they are not equal
8
True if the element is present in
the object / Returns true if a
character exists in the given
string
in
Belonging Operators
String Operation
not in
Belonging Operators
7
True if element is not present in
the object.
and
Logical operators
6
True if both are true.
or
Logical operators
5
True if at least one logical
operator is true.
not
Logical operators
4
returns true if an expression is
false
&
Bitwise Operators
3
bitwise and logic operator
|
Bitwise Operators
2
The bitwise OR logic operator is
represented by the symbol |.
Introduction to Python
7
Select
Name
Number
of Code
Text 1
∼
Bitwise Operators
1
invert operator (the opposite)
+=
Augmentation Operations
20
equal to x = x+1
-=
Augmentation Operations
21
equal to x = x-1
*=
Augmentation Operations
22
equal to x = x*2
/=
Augmentation Operations
23
equal to x = x/2
abs
Operation
24
returns the absolute value of a
number without considering its
sign
round
Operation
25
Returns the rounded number
//=
Augmentation Operations
26
equal to x = x//2
-
Arithmetic Operation
27
Subtraction
Belonging Operators
Master Table
Name
Select
Number
of Code
Text 1
is not
Identical Operators
9
True if they are not equal
is
Identical Operators
10
True if both are true (they are
equal).
!=
Comparison Operators
11
Not Equal
</ < =
Comparison Operators
12
Smaller than / equal or smaller
than. (same for >)
==
Comparison Operators
13
Equal Condition Statement
Introduction to Python
8
Name
Select
Number
of Code
Text 1
**
Arithmetic Operation
14
Power
//
Arithmetic Operation
15
Floor Division
/
Arithmetic Operation
16
Division
@
Arithmetic Operation
17
Matrix Multiplication (in NumPy
arrays)
18
Multiplication / Concatenates
multiple copies of the same
string.
19
Addition / Appends the second
string to the first.
8
True if the element is present in
the object / Returns true if a
character exists in the given
string
*
+
in
Arithmetic Operation
String Operation
Arithmetic Operation
String Operation
Belonging Operators
String Operation
not in
Belonging Operators
7
True if element is not present in
the object.
and
Logical operators
6
True if both are true.
or
Logical operators
5
True if at least one logical
operator is true.
not
Logical operators
4
returns true if an expression is
false
&
Bitwise Operators
3
bitwise and logic operator
|
Bitwise Operators
2
The bitwise OR logic operator is
represented by the symbol |.
∼
Bitwise Operators
1
invert operator (the opposite)
+=
Augmentation Operations
20
equal to x = x+1
-=
Augmentation Operations
21
equal to x = x-1
Introduction to Python
9
Select
Name
Number
of Code
Text 1
*=
Augmentation Operations
22
equal to x = x*2
/=
Augmentation Operations
23
equal to x = x/2
abs
Operation
24
returns the absolute value of a
number without considering its
sign
round
Operation
25
Returns the rounded number
//=
Augmentation Operations
26
equal to x = x//2
-
Arithmetic Operation
27
Subtraction
Logical Operators
Master Table
Name
not in
in
Select
Belonging Operators
Belonging Operators
String Operation
Number
of Code
Text 1
7
True if element is not present in
the object.
8
True if the element is present in
the object / Returns true if a
character exists in the given
string
is not
Identical Operators
9
True if they are not equal
is
Identical Operators
10
True if both are true (they are
equal).
!=
Comparison Operators
11
Not Equal
</ < =
Comparison Operators
12
Smaller than / equal or smaller
than. (same for >)
==
Comparison Operators
13
Equal Condition Statement
Introduction to Python
10
Name
Select
Number
of Code
Text 1
**
Arithmetic Operation
14
Power
//
Arithmetic Operation
15
Floor Division
/
Arithmetic Operation
16
Division
@
Arithmetic Operation
17
Matrix Multiplication (in NumPy
arrays)
18
Multiplication / Concatenates
multiple copies of the same
string.
19
Addition / Appends the second
string to the first.
*
+
Arithmetic Operation
String Operation
Arithmetic Operation
String Operation
and
Logical operators
6
True if both are true.
or
Logical operators
5
True if at least one logical
operator is true.
not
Logical operators
4
returns true if an expression is
false
&
Bitwise Operators
3
bitwise and logic operator
|
Bitwise Operators
2
The bitwise OR logic operator is
represented by the symbol |.
∼
Bitwise Operators
1
invert operator (the opposite)
+=
Augmentation Operations
20
equal to x = x+1
-=
Augmentation Operations
21
equal to x = x-1
*=
Augmentation Operations
22
equal to x = x*2
/=
Augmentation Operations
23
equal to x = x/2
24
returns the absolute value of a
number without considering its
sign
abs
Introduction to Python
Operation
11
Number
of Code
Name
Select
Text 1
round
Operation
25
Returns the rounded number
//=
Augmentation Operations
26
equal to x = x//2
-
Arithmetic Operation
27
Subtraction
Bitwise Operators
Notice that Bitwise is level of operation that involves working with individual bits
which are the smallest units of data in a computing system.
Master Table
Name
Select
Number
of Code
Text 1
not
Logical operators
4
returns true if an expression is
false
or
Logical operators
5
True if at least one logical
operator is true.
and
Logical operators
6
True if both are true.
not in
Belonging Operators
7
True if element is not present in
the object.
8
True if the element is present in
the object / Returns true if a
character exists in the given
string
in
Belonging Operators
String Operation
is not
Identical Operators
9
True if they are not equal
is
Identical Operators
10
True if both are true (they are
equal).
!=
Comparison Operators
11
Not Equal
Introduction to Python
12
Name
Select
Number
of Code
Text 1
</ < =
Comparison Operators
12
Smaller than / equal or smaller
than. (same for >)
==
Comparison Operators
13
Equal Condition Statement
**
Arithmetic Operation
14
Power
//
Arithmetic Operation
15
Floor Division
/
Arithmetic Operation
16
Division
@
Arithmetic Operation
17
Matrix Multiplication (in NumPy
arrays)
18
Multiplication / Concatenates
multiple copies of the same
string.
19
Addition / Appends the second
string to the first.
*
+
Arithmetic Operation
String Operation
Arithmetic Operation
String Operation
&
Bitwise Operators
3
bitwise and logic operator
|
Bitwise Operators
2
The bitwise OR logic operator is
represented by the symbol |.
∼
Bitwise Operators
1
invert operator (the opposite)
+=
Augmentation Operations
20
equal to x = x+1
-=
Augmentation Operations
21
equal to x = x-1
*=
Augmentation Operations
22
equal to x = x*2
/=
Augmentation Operations
23
equal to x = x/2
abs
Operation
24
returns the absolute value of a
number without considering its
sign
round
Operation
25
Returns the rounded number
//=
Augmentation Operations
26
equal to x = x//2
Introduction to Python
13
Name
-
Number
of Code
Select
Arithmetic Operation
27
Text 1
Subtraction
Data Types
The OOP paradigm: data and functions are bundled together into objects:
In the OOP functions are usually called methods.
In Python the data and methods of an object are referred to as attributes.
Depending on the class of object (like float, array, string, dataframe, etc) the
object will have different methods.
In the OOP functions are usually called methods.
Let’s see the main data types in Python.
Variables and data types
A variable in Python is a reference to a place in memory where data resides.
There are many types of variables. The simplest types are called atomic
variables, which cannot be changed, only overwritten—they store data directly.
Other types of variables are containers for data, aptly called containers.
Types of Variable
Variable
Type
Integer
Atomic Variable
Float (decimal Numbers)
Atomic Variable
Booleans(True/False)
Atomic Variable
Strings
Atomic Variable
Introduction to Python
14
Variable
Type
Lists
Container
Arrays
Container
Dictionaries
Container
Tuples
Container
Pandas Data Frames
Container
There are also other types of variables but those are the most frequent. The
explanation of each of them is inside the page.
Introduction to Python
15
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )