Jupyter notebook
Option(A): Install Anaconda (recommended):
- It is a package and enviornment manager
- Comes with Python and Jupyet Notebook installed
- Includes many packages like NumPy, Scikit-learn, Scipy, and pandas preinstalled.
- Instructions available at "Python for Data Analysis" Page 34
Option(B): Install Python and Jupyter
- Download and install Python https://www.python.org
- Install Jupyter https://jupyter.org/install
Python uses whitespace (tabs or spaces) to structure code instead of using braces
for x in array:
if x < pivot:
less.append(x)
x = 7
else:
greater.append(x)
Python statements do not need to be terminated by semicolons. But it can be used to separate multiple statements on a single line
a = 5; b = 6; c = 7
Any text preceded by the hash mark (pound sign) # is ignored by the Python interpreter
results = []
for line in file_handle:
# keep the empty lines for now
# if len(line) == 0:
# continue
results.append(line.replace('foo', 'bar'))
print("Reached this line") # Simple status report
a = [1, 2, 3]
b = a
b.append(4)
a
When you pass objects as arguments to a function, new local variables are created referencing the original objects without any copying
def append_element(some_list, element):
some_list.append(element)
element = 10
data = [1, 2, 3]
append_element(data, 4)
data
In contrast with many compiled languages, such as Java and C++, object references in Python have no type associated with them
a = 5
type(a)
a = 'foo'
type(a)
Objects in Python typically have both attributes and methods
a = 'foo'
dir(a)
In Python a module is simply a file with the .py extension containing Python code.
# some_module.py
PI = 3.14159
def f(x):
return x + 2
def g(a, b):
return a + b
import some_module
result = some_module.f(5)
pi = some_module.PI
a = [1, 2, 3]
b = a
c = list(a)
a == b
a == c
a is b
a is c
a = 'one way of writing a string'
b = "another way"
c = """
This is a longer string that
spans multiple lines
"""
a = 'this is a string'
# a[10] = 'f'
a.replace('s', 'f')
a
a = 'this is the first half '
b = 'and this is the second half'
a + b
amount = 5
currency = 'Jordinan Dinar'
rate = 0.7
template = f'{amount} {currency} are worth US$ { rate * amount}'
template = f'{amount:.2f} {currency} are worth US$ {rate * amount}'
template
Comparisons and other conditional expressions evaluate to either True or False. Boolean values are combined with the and and or keywords
True and True
The str, bool, int, and float types are also functions that can be used to cast values to those types
s = '3.14159'
fval = float(s)
print(fval)
type(fval)
None is the Python null value type
a = None
a is None
if x < 0:
print('It's negative')
elif x == 0:
print('Equal to zero')
elif 0 < x < 5:
print('Positive but smaller than 5')
else:
print('Positive and larger than or equal to 5')
sequence = [1, 2, None, 4, None, 5]
total = 0
for value in sequence:
if value is not None:
total += value
total
sequence = [1, 2, None, 4, None, 5]
while value in sequence:
if value is None:
continue
total += value
total
The range function returns an iterator that yields a sequence of evenly spaced integers:
for x in range(0, 10, 2):
print(x)
x = -5
result = None
if x > 0:
result = "Non-negative"
else:
result = "Negative"
result = "Non-Negative" if x > 0 else "Negative"
A tuple is a fixed-length, immutable sequence of Python objects.
# Create tuple
tup = (4, 5, 6)
tup
(4, 5, 6)
# Access elements by index
tup[0]
4
# Can we change tuple values ?
tup[0] = 3
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[3], line 2 1 # Can we change tuple values ? ----> 2 tup[0] = 3 TypeError: 'tuple' object does not support item assignment
# Can we do the following?
tup = ('Ali', [80, 85], True)
tup[1].append(90)
tup
('Ali', [80, 85, 90], True)
# Unpack tuple
name, scores, is_student = tup
is_student
True
# Unpack using *
scores = (80, 87, 83, 77, 76, 71, 69, 69, 69, 60)
first_grade, second_grade, *rest = scores
rest
[83, 77, 76, 71, 69, 69, 69, 60]
# Discard rest
first_grade, second_grade, *_ = scores
# Count elements
a = (1, 2, 2, 2, 3, 4, 2)
a.count(2)
4
Lists are variable-length and mutable
# Create list
l = ['foo', 'bar', 'baz']
l
['foo', 'bar', 'baz']
# Access elements by index
l[2]
'baz'
# Modify elements
l[1] = "fee"
l
['foo', 'fee', 'baz']
# Appending elements
l.append('dwarf')
l
['foo', 'fee', 'baz', 'dwarf']
# Insert at specific index
l.insert(2, 'red')
l
['foo', 'fee', 'red', 'baz', 'dwarf']
# remove element
l.remove("fee")
l
['foo', 'red', 'baz', 'dwarf']
# remove element from specifc index
print(l.pop(2))
l
# Search for element
"fee" in l
False
# Concatenate lists/tuples
l1 = [4, None, 'foo']
l2 = [7, 8, (2, 3)]
l1 + l2
[4, None, 'foo', 7, 8, (2, 3)]
l1.extend(l2)
l1
[4, None, 'foo', 7, 8, (2, 3)]
# Sorting list
a = [7, 2, 5, 1, 3]
a.sort()
a
[1, 2, 3, 5, 7]
# Can we sort tuples?
a = (7, 2, 5, 1, 3)
a.sort()
a
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[25], line 3 1 # Can we sort tuples? 2 a = (7, 2, 5, 1, 3) ----> 3 a.sort() 4 a AttributeError: 'tuple' object has no attribute 'sort'
# Slicing using start:stop
seq = [7, 2, 3, 7, 5, 6, 0, 1]
seq[1:5]
[2, 3, 7, 5]
seq[:5]
[7, 2, 3, 7, 5]
seq[-2:]
[0, 1]
seq[-6: -2]
[3, 7, 5, 6]
# Use steps
seq[::2]
[7, 3, 5, 0]
seq = ['d', 'a', 't', 'a', ' ', 's', 'c', 'i', 'e', 'n', 'c', 'e']
How to iterate over the sequnce and keep track of the index and the item?
# loop
for i in range(len(seq)):
print(f'index = {i}, value = {seq[i]}')
index = 0, value = 7 index = 1, value = 2 index = 2, value = 3 index = 3, value = 7 index = 4, value = 5 index = 5, value = 6 index = 6, value = 0 index = 7, value = 1
for index, value in enumerate(seq):
print(f"value {value} at location {index}")
seq = ('d', 'a', 't', 'a', ' ', 's', 'c', 'i', 'e', 'n', 'c', 'e')
sorted(seq)
[' ', 'a', 'a', 'c', 'c', 'd', 'e', 'e', 'i', 'n', 's', 't']
zip “pairs” up the elements of a number of sequences
names = ['Ali', 'Lina', 'Moneer', 'Haia', 'Haneen', 'Saad']
is_male = [True, False, True, False, False, True]
age = [19, 18, 18, 17, 19, 20, 21]
students = zip(names, is_male, age)
students
<zip at 0x7fe6027ff740>
list(students)
[('Ali', True, 19), ('Lina', False, 18), ('Moneer', True, 18), ('Haia', False, 17), ('Haneen', False, 19), ('Saad', True, 20)]
flexibly sized collection of key-value pairs, where key and value are Python objects
# Create dict
d1 = {'a' : 'some value', 'b' : [1, 2, 3, 4]}
d1
{'a': 'some value', 'b': [1, 2, 3, 4]}
# access elements by key
d1['c']
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) Cell In[37], line 2 1 # access elements by key ----> 2 d1['c'] KeyError: 'c'
d1.get('c', 20)
# default value
20
# change values
d1['a'] = "new value"
d1
{'a': 'new value', 'b': [1, 2, 3, 4]}
# Check if element in a dict
'b' in d1
True
# Remove from dict
d1 = {'a' : 'some value', 'b' : [1, 2, 3, 4]}
d1.pop('a')
d1
{'b': [1, 2, 3, 4]}
A set is an unordered collection of unique elements.
set([2, 2, 2, 1, 3, 3])
{1, 2, 3}
# Union
a = {1, 2, 3, 4, 5}
b = {3, 4, 5, 6, 7, 8}
a.union(b)
a | b
{1, 2, 3, 4, 5, 6, 7, 8}
# Intersection
a.intersection(b)
a & b
{3, 4, 5}
# Check other functions in book page 125
form a new list by filtering the elements of a collection,
strings = ['a', 'as', 'bat', 'car', 'dove', 'python']
[s.upper() for s in strings]
{s:len(s) for s in strings}
# How to convert all words to upper case
# How to create dict of (words,len) pairs
{'a': 1, 'as': 2, 'bat': 3, 'car': 3, 'dove': 4, 'python': 6}
# Function structure
def my_function(x, y, z=1.5):
if z > 1:
return z * (x + y)
else:
return z / (x + y)
# Call function
my_function(5, 6, 0.7)
my_function(x=5, y=6, z=0.7)
my_function(y=6, x=5)
16.5
def f():
a=5
b=6
c=7
return a, b, c
v1, v2, v3 = f()
strings = ['a', 'as', 'bat', 'car', 'dove', 'python']
list(map(len, strings))
[1, 2, 3, 3, 4, 6]
anonymous functions
def short_function(x):
return x * 2
equiv_anon = lambda x: x * 2
equiv_anon(2)
4
def convert_to_float(v):
return float(v)
convert_to_float('a')
#try catch
def convert_to_float(v):
try:
return float(v)
except:
return v
convert_to_float('a')