# bprerak/cs4661-lec04-pythonsyntaxreview-lab1

2 years ago

## Introduction to Data Science (CS4661). Cal State LA, CS Dept.

##### Instructor: Dr. Mo Porhomayoun

In [4]:
``````import numpy as np

``````
In [6]:
``````ave = np.std([1,2,3,4,5])
print (ave)``````
```1.4142135623730951 ```

## Jupyter Notebook (ipython Notebook)

The Jupyter Notebook (previously known as ipython Notebook) is a web-based interactive development environment, in which you can combine code execution, text and notations (markdown), mathematics, and plots. It will be run under your brrowser. The Jupyter Notebook supports a number of programming languages including Python, Julia, Octave, and R.

The Jupyter Notebook include cells. Each cell can be run independently. You can add/remove cells, or arrange the order of them. A piece of code, or several lines of the code that we want to run together can be written in a single cell. We strongly recommend using Jupyter Notebook as a framework

## Python Programming

##### This is a quick review of some python syntax. Please refer to the suggested resources for more information.

In [1]:
``````# Variables:
a = 2
b = 3
c = a+b
print(c)
``````
In [2]:
``````# Variables:
a = 5
b = 5/2
print(type(a), type(b))
print(a,"\n",b)``````
```<class 'int'> <class 'float'> 5 2.5 ```
In [3]:
``````# Integer and floating-point division:
print(5/2) # int/int returns int in python ver2.x! But, in python ver3.x in returns float.

print(5/2.0)
print(5.0/2)
print(5/float(2))

print(5.0//2) # floor (truncate)

print(10%3)  # remainder

print(10**3) # exponent``````
```2.5 2.5 2.5 2.5 2.0 1 1000 ```
In [4]:
``````a = 2
a += 1   # a = a + 1
print(a)
``````
```3 ```

### String:

In [5]:
``````# String:
s = 'hello world!'
print(s)
``````
```hello world! ```
In [6]:
``````s = 'hello world!'
print(s)

print(s[0])

print(s[0:2]) # 0 is included, 2 is NOT included

print(s[2:10]) # 10 is NOT included

print(s[2:])

print(s[:5])

print(s[-1]) # last element

``````
```hello world! h he llo worl llo world! hello ! ```
In [7]:
``````s = 'hello world!'
print(s)

print(len(s)) # length

print('hello' + ' ' + 'world' + '!' + '\n')  # concat with +

d = s + ' I love CS4661.'
print(d)
``````
```hello world! 12 hello world! hello world! I love CS4661. ```
In [8]:
``````# converting to int:
a = 2016
s = str(a)
print(a)
print(s)
print(s[0])

b = int(s)
b += 1
print(b)
``````
```2016 2016 2 2017 ```
In [9]:
``````s = 'hello world! I love CS4661!'
print(s)

i = s.find('love')      # returns 15
print(i,'\n',j)

t = s.replace('love','like')    # replacing 'love' by 'like'
print(t)
``````
```hello world! I love CS4661! 15 -1 hello world! I like CS4661! ```

### List:

In [10]:
``````# List (aka array or vector):
# a list of elements such as numbers or char or strings separated by comma

list1 = [1,2,3,7,-12,0,157]

print(list1)
print(len(list1))  # lenght of a list``````
```[1, 2, 3, 7, -12, 0, 157] 7 ```
In [11]:
``````empt = [] # empty list
print(empt)
``````
```[] ```
In [12]:
``````# indexing for a list:

list1 = [1,2,3,7,-12,0,157]

print(list1[0])   # indexing starts from 0
print(list1[1])
print(list1[6])
print(list1[-1])  # -i points to the last element
print(list1[-2])``````
```1 2 157 157 0 ```
In [13]:
``````# Slicing:

l1 = [1,2,3,7,-12,0,157]

print(l1[1:5])     # does not include index 5
print(l1[1:])
print(l1[:5])
print(l1[0:7:2])   # [start:end:step]``````
```[2, 3, 7, -12] [2, 3, 7, -12, 0, 157] [1, 2, 3, 7, -12] [1, 3, -12, 157] ```
In [14]:
``````list2 = ['hello', 231, 2.0, '2.0', 'CS4661']

print(list2)
print(list2[0])
print(list2[0][0])``````
```['hello', 231, 2.0, '2.0', 'CS4661'] hello h ```
In [15]:
``````# '+' for concatenating the lists
v1 = [1,2,3]
v2 = [4,5,6]
u = v1 + v2

print(u)

``````
```[1, 2, 3, 4, 5, 6] ```
In [16]:
``````# some useful opperations/methods on Lists:

l = [23,5,4,5,-19]

l.append(6)    # ==> [23, 5, 4, 5, -19, 6]
print(l)
``````
```[23, 5, 4, 5, -19, 6] ```
In [17]:
``````# some useful opperations/methods on Lists:

my_list = [23,5,4,5,-19]

my_list.append(6)    # ==> [23, 5, 4, 5, -19, 6]
print(l)

my_list.pop()        # ==> returns 6, and l=[23, 5, 4, 5, -19]
print(l)

my_list.sort()       # ==> [-19, 5, 5, 7, 23]
print(l)

print(my_list.count(5))   # counts the number of 5 repetitions

print(my_list.index(5))   # returns index of first 5

my_list_s = sorted(my_list)
print(my_list_s)
``````
```[23, 5, 4, 5, -19, 6] [23, 5, 4, 5, -19, 6] [23, 5, 4, 5, -19, 6] 2 2 [-19, 4, 5, 5, 23] ```
In [18]:
``````# copying a list:
l1 = [1,2,3,7,-12,0,157]
l2 = l1 # notice: l2 is just another reference to the same list!!!!

l1[0]=8 # since they both refer to the same list, changing an element of l1 will change the same elemnet of l2 too!

print(l1)
print(l2)
``````
```[8, 2, 3, 7, -12, 0, 157] [8, 2, 3, 7, -12, 0, 157] ```
In [19]:
``````# copying a list as a new list:
l1 = [1,2,3,7,-12,0,157]
l2 = l1[:] # notice: l2 is a new list!!!!

l1[0]=8

print(l1)
print(l2)
``````
```[8, 2, 3, 7, -12, 0, 157] [1, 2, 3, 7, -12, 0, 157] ```
In [20]:
``````# List of lists (Matrix):
X = [[1,2],[5,6],[0,-3]]
print(X)
print(X[1][0])``````
```[[1, 2], [5, 6], [0, -3]] 5 ```
In [21]:
``````# Converting to string:

# converting a list into a string using a delimiter
string_list = ['I','love','CS4661!']

s = ' '.join(string_list)   # 'I love CS4661!'
print(s)

# converting a string into a list separated by a delimiter
l = s.split(' ')        # returns ['I','love','CS4661!']
print(l)

``````
```I love CS4661! ['I', 'love', 'CS4661!'] ```

### Dictionary:

Super helpful! It is very important for you to be able to work comfortably with dictionaries in python.

In [22]:
``````# key-value pairs
# Defining a dictionary:          d = {'key0':'value0','key1':'value1',...}
# Defining a blank dictionary:    d = {}
# Note1: Dictionary is unorderred! (ordering in meaningless for dictionaries
# Note2: keys can be strings or numbers, values can be any type
# Note3: keys must be unique, but there is no limitation on value

age_dict = {'Tom':27, 'Joe':34, 'Ryan':19, 'Sarah':28}

print(age_dict)
``````
```{'Tom': 27, 'Joe': 34, 'Ryan': 19, 'Sarah': 28} ```
In [23]:
``````# accessing the pairs
age_dict = {'Tom':27, 'Joe':34, 'Ryan':19, 'Sarah':28}

print(age_dict['Tom'])
print(age_dict['Joe'])``````
```27 34 ```
In [24]:
``````# changing/assigning/adding the pairs
age_dict = {'Tom':27, 'Joe':34, 'Ryan':19, 'Sarah':28}  # Name:Age

print(age_dict['Joe'])

age_dict['Joe'] = 51   # changing a value
age_dict['Rose'] = 63  # adding new key-value

print(age_dict)
``````
```34 {'Tom': 27, 'Joe': 51, 'Ryan': 19, 'Sarah': 28, 'Rose': 63} ```
In [25]:
``````# value can be a list

# Name:[Gender,Age,Weight]
age_dict = {'Tom':['m',27,155], 'Joe':['m',34,175], 'Ryan':['m',19,183], 'Sarah':['f',28,150]}

print(age_dict)
print(age_dict['Joe'])

``````
```{'Tom': ['m', 27, 155], 'Joe': ['m', 34, 175], 'Ryan': ['m', 19, 183], 'Sarah': ['f', 28, 150]} ['m', 34, 175] ```
In [26]:
``````# accessing all key,value
age_dict = {'Tom':27, 'Joe':34, 'Ryan':19, 'Sarah':28}  # Name:Age

print(len(age_dict))     # returns 4

print(list(age_dict.keys()))      # returns a list: ['Tom', 'Joe', 'Ryan', 'Sarah']
print(list(age_dict.values()))     # returns a list: [27, 34, 19, 28]
print(list(age_dict.items()))     # returns a list of tuples: [('Tom',27),('Joe',34),('Ryan',19),('Sarah',28)]

``````
```4 ['Tom', 'Joe', 'Ryan', 'Sarah'] [27, 34, 19, 28] [('Tom', 27), ('Joe', 34), ('Ryan', 19), ('Sarah', 28)] ```
In [27]:
``````# checking if a *key* is already available:

'Ryan' in  age_dict    # returns True
'Annie' in  age_dict # returns False

``````
Out[27]:
``False``

### Conditional Statements:

In [28]:
``````# comparisons:
print(7 > 5)
print(7 >= 5)
print(7 != 5)
print(7 == 7)

# boolean operations:
print ((3 > -3) and (1 > 0))
print ((11 > 21) or (9 < 11))
print ((11 > 21) or not(9 < 11))

``````
```True True True True True True False ```
In [29]:
``````# if statement:
n = 2
if n > 0:
print(n ,'is positive')``````
```2 is positive ```
##### Note: Four Spaces for indentation level is the Python standard.
In [30]:
``````# if/else statement:
n = -2
if n > 0:
print(n ,'is positive')
else:
print(n ,'is not positive')

``````
```-2 is not positive ```
In [31]:
``````# if/elif/else statement
n = 2
if n > 0:
print(n ,'is positive')
elif n == 0:
print(n ,'is zero')
else:
print(n ,'is negative')

``````
```2 is positive ```
##### Note: Four Spaces for indentation level is the Python standard.
In [32]:
``````# Example:
m = 3
n = 2

if m>n:
print('m is greater than n')
print('I will replace them')
m,n = n,m
print('succes!')``````
```m is greater than n I will replace them succes! ```
In [33]:
``````# using dictionary in if statement:

age_dict = {'Tom':27, 'Joe':34, 'Ryan':19, 'Sarah':28}

if 'Tom' in age_dict:
print('Tom is already in the dictionary')
else:
age_dict['Tom'] = 27

``````
```Tom is already in the dictionary ```

### Loops:

In [34]:
``````# For Loop:

foods = ['chicken','pizza','sandwitch']

for f in foods:
print(f)       # inside the loop
print(len(f))  # inside the loop

print(foods)       # outside the loop

``````
```chicken 7 pizza 5 sandwitch 9 ['chicken', 'pizza', 'sandwitch'] ```
##### Note: Four Spaces for indentation level is the Python standard.
In [35]:
``````# range:
# range returns a list of numbers

r1 = range(5)     # r1 = [0, 1, 2, 3, 4]

r2 = range(3,5)     # r2 = [3, 4]

r3 = range(0,5,2)     # r3 = [0, 2, 4]

``````
In [36]:
``````# for loop with range:

for x in range(5):
print(x)

print('\n')

for y in range(0,20,5):
print(y)
``````
```0 1 2 3 4 0 5 10 15 ```
In [37]:
``````# for loop over a dictionary items:

major_dict = {'Tom':'CS', 'Joe':'CS', 'Ryan':'EE', 'Sarah':'CE'}

for k in major_dict.keys():
print(k,'is studying in', major_dict[k])

print('\n')

# equivalent approach:
for k,v in major_dict.items():
print(k,'is studying in',v)

# Again remember that dictionary items have no definite order``````
```Tom is studying in CS Joe is studying in CS Ryan is studying in EE Sarah is studying in CE Tom is studying in CS Joe is studying in CS Ryan is studying in EE Sarah is studying in CE ```
In [38]:
``````# break and continue:

magic_number = 11
for i in range(101):
if i == magic_number:
print(i," is the magic number")
break # Jump out of the current "For Loop" to the line: print("done!\n")
else:
print(i)
print("done!\n")

MyList = [1, 5 , 7, 9]
for j in range(0,10):
if j in MyList:
continue # stop the iteration here and continue with the next j
print(j)

``````
```0 1 2 3 4 5 6 7 8 9 10 11 is the magic number done! 0 2 3 4 6 8 ```
In [39]:
``````# for loop in line:

# Example1:
mylist=[1,2,3,4,5]
mylist_sqr = [i*i for i in mylist]
print(mylist_sqr)    # returns [1, 4, 9, 16, 25]

# Example2:
x = [i for i in range(10)]
print(x)    # returns [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# Example3:
x = [j**2 for j in range(10) if j>4]
print(x)    # returns [25, 36, 49, 64, 81]

# Example4: Matrix
Matrix = [[0 for x in range(5)] for x in range(3)]
print(Matrix)

``````
```[1, 4, 9, 16, 25] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [25, 36, 49, 64, 81] [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] ```
In [40]:
``````# While Loop:

n = 0
while n<5:
print(n)
n += 1

``````
```0 1 2 3 4 ```

### Functions:

In [41]:
``````# simplest case: defining a function with no input argument and no return results:

def myPrint():
print('This is a test!')

# calling this function:
myPrint()
``````
```This is a test! ```
In [42]:
``````# defining a function to get two input arguments and return the sum:
# Note: we usually add function/input/output description in the first line after def. We use """text""" to do this documentation.
# Anything between 3 quotation-marks will be considered as comment

def mySum(a = 0, b = 0): # default values are optional
"""This is a function for summation
inputs: a,b
output: it returns a+b as output"""
c = a + b
return c

# calling this function:
s = mySum(7,3)
print(s)
``````
```10 ```
In [43]:
``````# defining a function to get two input arguments and return the sum and sub:

def mySum(a = 0, b = 0): # default values are optional
"""This is a function for summation and subtraction
inputs: a,b
output: it returns a+b and a-b as outputs"""
c = a + b
d = a - b
return c,d

# calling this function:
sm,sb = mySum(7,3)
print(sm,sb)
``````
```10 4 ```

### Lambda:

In [44]:
``````# Lambda: in-line functions!

# Example1 (one input, one output):
# defining a function that gets an input, and calculate and return squared value using lambda
mySqr = lambda x: x**2

# calling:
y = mySqr(2)
print(y)

``````
```4 ```
In [45]:
``````# Lambda: in-line functions!

# Example2 (more than one input, one output):
# defining a function that gets two input, and calculate and return the summation of squared values using lambda
mySqr = lambda x1,x2: x1**2 + x2**2

# calling:
y = mySqr(2,4)
print(y)

``````
```20 ```
In [46]:
``````# Lambda: in-line functions!

# Example3 (more than one input, more than one output):

mySqr = lambda x1,x2: (x1**2 + x2**2 , x1**2 - x2**2)

# calling:
y,z = mySqr(2,4)
print(y,z)

``````
```20 -12 ```

### Exceptions:

In [47]:
``````a = 0
b = 1
try:
c = b/a
except:
print("devided by zero")``````
```devided by zero ```
In [ ]:
`` ``