Run in Google Colab
|
View on GitHub
|
Module 2 - Python Basics 2¶
In this module we will cover more basic Python concepts. We will start by importing modules and defining functions. We will also cover file I/O, loading data, list comprehensions, lambda functions, and installing packages.
The topics covered in this module:¶
- Importing Modules
- Defining Functions
- File I/O
- Loading Data (JSON, CSV)
- List Comprehensions
- Lambda Functions
- Installing Packages
1. Importing Modules¶
Python has a lot of built-in modules that you can use. You can import these modules using the import statement.
The same command can be used to import installed packages.
# importing modules using import statement
import math
# Import under an alias (avoid this pattern except with the most
# common modules like pandas, numpy, etc.)
import math as m
# Access components with pkg.fn
m.pow(2, 3)
# Import specific submodules/functions (not usually recommended
# because it can be confusing to know where a function comes from)
from math import pow
pow(2, 3)
Q: After importing the math module, can you import the random module and generate five random floating-point numbers between 0 and 1?
- Tip: use random.random() in a for loop
# TODO: Write your code here
2. Defining Functions¶
A function is a block of code that only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result.
# Functions in python are defined using key word "def"
# simple function to print greetings `greet_word` is an optional argument with default value 'Hello'
def greet(name, greet_word='Hello'):
print(f"{greet_word} {name}. How are you doing?")
# here greet has 'Hello' as default argument for greet_word
print(greet('James'))
print(greet("Steven", greet_word="Howdy"))
# Observe that the function by default returns None
To return a value from a function, you can use the return statement.
# To return a value from a function, you can use the `return` statement.
def multiply_and_subtract_one(x, y):
return x * y - 1
print(multiply_and_subtract_one(2, 3))
Q: Define a function called "divide_by_two" that takes a single parameter and returns half its value. Then test it with both integer and float inputs.
# TODO: Write your code here
Python supports recursive functions. A recursive function is a function that calls itself.
# Function to print nth fibonacci number
def fib(n):
if n <= 1:
return 1
else:
return fib(n-1)+fib(n-2)
n = 10
print(f"{n}th fibonacci number is {fib(n)}")
3.File I/O¶
Python has functions for file handling and manipulation. The key function to work with files in Python is the open() function. The open() function takes two parameters; filename, and mode. The mode parameter specifies whether you want to read, write, or append to the file. Common modes are 'r' for reading, 'w' for writing (which overwrites the file), and 'a' for appending.
You can also define the format of the file by adding 't' for text or 'b' for binary.
# Writing to a text file
with open("example.txt", "wt") as f:
f.write("This is an example.\n")
f.write("This is another example in another line.\n")
You can load the content of a file using the read() method. You can also iterate over the lines of a file using a for loop. This will progressively load the file line by line.
# Reading from a text file
with open("example.txt", "rt") as f:
content = f.read()
print(content)
# Read line by line
with open("example.txt", "rt") as f:
for line in f:
print(line)
Note that the last print resulted into 2 line breaks because the print() function adds a newline character at the end of the line that already contains a newline character.
# Note that the last print resulted into 2 line breaks because the `print()` function adds a newline character at the end of the line that already contains a newline character.
# You can strip the newline character by using the `strip()` method of the string object.
with open("example.txt", "rt") as f:
for line in f:
print(line.strip())
Append a file: You can append to a file by opening it in append mode. When you open a file in append mode, new data is written to the end of the file.
with open("example.txt", "at") as f:
f.write("This is an appended line.\n")
with open("example.txt", "rt") as f:
content = f.read()
print(content)
Q: Append user input to a file named "user_input.txt" and then read it again to verify the content was appended.
- Use the
with open(..., 'at')approach. - Then open the file again in read mode and print its contents.
# TODO: Write your code here
4 Loading Data¶
4.1 Dealing with json files in Python¶
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.
An example of a JSON file:
{
"name": "John",
"age": 30,
"city": "New York"
"children": [
{
"name": "Anna",
"age": 5
},
{
"name": "Betty",
"age": 7
}
]
}
# creating a json file
import json
data = {
"name": "John",
"age": 30,
"city": "New York"
}
with open("data.json", "wt") as f:
json.dump(data, f)
# reading from a json file
with open("data.json", "rt") as f:
data = json.load(f)
print(data)
4.2 Dealing with csv files in Python¶
CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. A CSV file stores tabular data (numbers and text) in plain text.
NOTE THAT PANDAS IS A BETTER OPTION FOR DEALING WITH CSV FILES. WE WILL DISCUSS PANDAS IN THE NEXT SECTION.
# creating a csv file
import csv
with open("example.csv", "w") as f:
writer = csv.writer(f)
writer.writerow(["Name", "City"])
writer.writerow(["John", "New York"])
writer.writerow(["Peter", "Los Angeles"])
# reading from a csv file
with open("example.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
print(row)
5. List Comprehensions (Advanced)¶
List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operation applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.
# List comprehension is a concise way to create lists
# [expr for var in iterable]
result = [x**2 for x in range(10)]
print(f"x squared is {result}")
You can add a condition to the list comprehension to filter the elements. For example, you can create a list of even numbers from 0 to 9.
# List comprehension with condition
result = [x**2 for x in range(10) if x % 2 == 0]
print(f"x squared when x is even is {result}")
Q: Use a list comprehension to generate the cubes of numbers from 1 to 7.
- Then filter out cubes that are divisible by 3 and print the result.
# TODO: Write your code here
You can have complex nested loops in list comprehensions. For example, you can create a list of tuples by combining elements from two lists.
# List comprehension with nested loops
result = [(x, y) for x in range(3) for y in range(3)]
print(f"Cartesian product of [0, 1, 2] is {result}")
You can combine nested loops with conditions.
# List comprehension with nested loops and condition
result = [(x, y) for x in range(3) for y in range(3) if x != y]
print(f"Cartesian product of [0, 1, 2] without diagonal is {result}")
Nested loops can be useful to flatten a list of lists. For example, you can flatten a list of lists into a single list.
list_of_lists = [[1, 2], [3, 4], [5, 6]]
result = [item for sublist in list_of_lists for item in sublist]
print(f"Flattened list is {result}")
6. Lambda Functions (Advanced)¶
Lambda functions are small anonymous functions. They can have any number of arguments but only one expression. They are defined using the lambda keyword.
# Lambda functions are anonymous functions
# lambda arguments: expression
f = lambda x: x**2
print(f"Square of 10 is {f(10)}")
Lambda functions are used with map, filter and reduce functions.
The map() function takes in a function and a list. The function is applied to every item in the list. It returns a list of the results.
# map applies a function to all the items in an input list
# map(function, iterable)
result = list(map(lambda x: x**2, range(10)))
print(f"Square of 0 to 9 is {result}")
The filter() function takes in a function and a list. The function is applied to every item in the list. It returns a list of items for which the function returns True.
# filter creates a list of elements for which a function returns true
# filter(function, iterable)
result = list(filter(lambda x: x % 2 == 0, range(10)))
print(f"Even numbers in 0 to 9 are {result}")
The reduce() function is defined in the functools module. It applies a rolling computation to sequential pairs of values in a list. For example, you can use the reduce() function to calculate the sum of a list of numbers.
# reduce applies a rolling computation to sequential pairs of values in a list
# reduce(function, iterable)
from functools import reduce
result = reduce(lambda x, y: x + y, range(10))
# The above code is equivalent to sum(range(10))
print(f"Sum of 0 to 9 is {result}")
Lambda functions have other uses as well, such as in sorting and in defining functions that take functions as arguments.
pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
pairs.sort(key=lambda pair: pair[1])
print(f"Sorted pairs based on second element is {pairs}")
7. Installing Packages¶
There are many packages available for Python. Most of them are available on the Python Package Index (PyPI). You can install packages using the pip command. For example, to install the numpy package, you can use the command !pip install numpy (on collab).
Check out the PyPI website for more information on available packages.
Some advanced packages can only be installed via conda. For example, to install the numpy package, you can use the command !conda install <package_name>. Refer to the conda documentation for more information.
If you are on colab, you can install extra packages using
!pip install package_name
If you are on jupyter notebook, you can install extra packages using:
pip install package_name
8. References to other advanced topics¶
Python has a lot of advanced topics that you can learn. For instance it is possible to create classes and objects, use decorators, context managers, and more. These topics are not covered in this course but you can find more information in the Python documentation or in other tutorials.
Extra questions¶
For each of the following question, first think about the result without running the code. Then test it by running the code. Reach out if you don't understand why!
What's the output?¶
def func(a):
a = a + 2
a = a * 2
return a
print(func(2))
# TRY IT OUT
True? False? Why?¶
0.1 + 0.2 == 0.3
# YOUR SOLUTION HERE
3. What is list_1 and list_2 and why?¶
list_1 = [1,2,3]
list_2 = list_1
list_1.append(4)
list_2 += [5]
list_2 = list_2 + [10]
# YOUR SOLUTION HERE
What's the output?¶
l = [i**2 for i in range(10)]
l[-4:2:-3]
# YOUR SOLUTION HERE
What does the code do? If the ordering doesn't matter, how can it be simplified?¶
def func1(lst):
a = []
for i in lst:
if i not in a:
a.append(i)
return a
# YOUR SOLUTION HERE
What would be the output?¶
val = [0, 10, 15, 20]
data = 15
try:
data = data/val[0]
except ZeroDivisionError:
print("zero division error - 1")
except:
print("zero division error - 2")
finally:
print("zero division error - 3")
val = [0, 10, 15, 20]
data = 15
try:
data = data/val[4]
except ZeroDivisionError:
print("zero division error - 1")
except:
print("zero division error - 2")
finally:
print("zero division error - 3")
# YOUR SOLUTION HERE
What does the code do?¶
def func(s):
d = {}
for c in s:
if c in d:
d[n] += 1
else:
d[n] = 1
return d
(Btw, the same operation can be done by simply running Counter(s) by using Counter data structure in the collections module.)
# YOUR SOLUTION HERE
What's the output?¶
def func(l):
l.append(10)
return l
a = [1,2,3]
b = func(a)
a == b
# YOUR SOLUTION HERE
What's happening to a in each step? Why?¶
# step 1
a = [ [ ] ] * 5
# step 2
a[0].append(10)
# step 3
a[1].append(20)
# step 4
a.append(30)
# YOUR SOLUTION HERE
What's the output?¶
L = list('abcdefghijk')
L[1] = L[4] = 'x'
L[3] = L[-3]
print(L)
# YOUR SOLUTION HERE
What's the output?¶
y = 8
z = lambda x : x * y
print(z(6))
# YOUR SOLUTION HERE
What's the output?¶
count = 1
def func(count):
for i in (1, 2, 3):
count += 1
func(count = 10)
count += 5
print(count)
# YOUR SOLUTION HERE
Run in Google Colab
View on GitHub