Lecture 1 Pyhton Programming DOST 1
Lecture 1 Pyhton Programming DOST 1
Day 1
Learning Outcomes
A. To be able to gain the confidence to use Python as a programming language
B. To do basic operations in Numpy and Pandas
1. Indexing, Slicing, and Querying
2. Vector and matrix computations
5.) Using %pylab inline preceding everything else in the notebook, imports matplotlib and
numpy. It also enables graphics to be part of the notebook.
6.) You can use TAB to see available functions and SHIFT-TAB repeatedly for the
documentation
Variables and Data Types
Python Programming
Variables and Data Types
Python uses five standard data types:
Numbers Tuples
Strings Dictionaries
Lists
Arithmetic
Addition Exponent
Subtraction Division
Multiplication
Arithmetic
Increment/Decrement String Concatenation
String Conditions
Loops
For Loops Nested Loops
While Loops
def function_name:
commands *Note: Functions are defined with the keyword
def before the function_name. Similar to
loops, the function definition ends with a
colon (:) while its contents inside are
indented
Challenge 2!
Python Programming
Challenge 2: Coin Flip
Create a function coin_flip() that simulates coin flips repeated n times, with 0 representing
tails and 1 representing heads. Make the function a generator with the parameter n as the
number of coin flips.
Use the numpy library as np and the function of np.random.randint() in order to simulate a
coin flip. After defining this function, return the result as a List using List Comprehension
discussed previously.
Vector to Vector: Dot Product *Note: Dot product multiples the elements on
the same index and sums the result
[ 0 1 2 3 4 ]
x [ 5 6 7 8 9 ]
0 + 6 + 14 + 24 + 36 = 80
Challenge 3
Python Programming
Challenge 3: Compute the mean of 1000 coin flips
Using the coin_flip() function from Challenge 2, compute for the result of 1000 coin flips.
Recall that the mean is computed thus:
*Note: Since a coin flip would randomize from a value of 0 and 1, as the number
of flips increase, the mean of the result should get closer to about 0.5
Matrix Computations
Matrix Initialization Matrix to Matrix: Matrix Multiplication
https://www.kaggle.com/deepmatri
x/imdb-5000-movie-dataset
Slicing Data Frames
Slicing Data
Indexing Columns and Rows
Indexing Columns Indexing Rows
Once completed, try to find the films with whose actor_3_name is Piolo Pascual and the
actor_1_name is the person from Armageddon
*Note:
*Note: There are also readily
available aggregation
functions present such
as size(), mean(), and
Group together the title_year as the index the like
Data Visualization
Python Programming
Matplotlib: Line Plot of Average Facebook Likes per Year
Importing Seaborn
*Note: plt.figure(figsize=(15,8)) is
used to render the size of
the graph. You may try to
play with the parameters
in the size in order to
create a better fit for your
visualization
Matplotlib: Histogram of IMDB Scores
Answer :