Run in Google Colab
|
View on GitHub
|
Tutorial Python¶
Are you still new to Python? Or your python skill is a bit rusty?
Then, feel free to use this to learn or refresh Python basics! You don't have to submit this notebook. It is just for your own reference.
We begin by focusing on constructing and executing Python scripts. This fundamental skill allows you to instruct your computer to perform a multitude of tasks. As you master this technique, you will be able to transform abstract ideas into executable programs, opening up a world of possibilities for automation and efficient problem-solving.
Moving forward, we will dive into loops and conditional statements. These powerful constructs enable your programs to dynamically react to varying scenarios and diverse data inputs. Leveraging these tools allows for the creation of responsive and adaptive code, which is essential for tackling complex challenges.
Towards the end of the module, we'll explore the concept of reusable functions. Crafting well-structured functions not only enhances the modularity and reusability of your code, but also simplifies maintenance. This skill is paramount for reducing redundancy, streamlining debugging, and ensuring that your code remains clear and adaptable.
We will also cover file I/O, loading data, list comprehensions, lambda functions, and installing packages.
Objectives¶
- Construct and Execute Python Scripts: Develop the ability to write and run basic Python scripts that adhere to standard syntax, establishing a firm foundation in programming.
- Apply Loops and Conditional Statements: Implement loops and conditional constructs to manipulate data effectively and solve intricate problems.
- Create Reusable Functions: Design and integrate Python functions that promote modularity and reusability, enhancing both code efficiency and maintainability.
The topics covered in this module:¶
- Variables and built in datatypes
- Printing and user input
- Operators
- Lists and Tuples
- Dictionaries
- Sets
- Flow control
- Importing Modules
- Defining Functions
- File I/O
- Loading Data (JSON, CSV)
- List Comprehensions
- Lambda Functions
- Installing Packages
1. Variables and built-in data types¶
1.1 Basic and variables¶
You can comment in Python using #. Comments are ignored by the Python interpreter.
To define a variable, assign a value to a variable name. Python will automatically detect the data type. The syntax is variable_name = value. You can print a variable using the print(variable_name) function.
# This is a comment and will be ignored by the interpreter
a_variable = "the value"
# This is another comment and will also be ignored by the interpreter
print(a_variable)
Variables can be reassigned with a different value and data type. For example, you can change an integer to a string:
a_variable = 10
print(a_variable) # Output: 10
a_variable = "Now I'm a string!"
print(a_variable) # Output: Now I'm a string!
Q: Create two variables and then swap their values. Print the variables before and after the swap.
- For example if
a = 1andb = 2, after the swapa = 2andb = 1.
# TODO: Write your code here
1.2 Printing composed variables and text¶
Use the print() function to print variables and text. You can use the + operator to concatenate strings. You can also concatenate variables and strings by separating them with a comma.
print("Hello, World!") # Output: Hello, World!
# Example of print with concatenation
a_planet = "Earth"
print("Hello from " + a_planet + "!")
# Output: Hello from Earth!
# Example of print with comma separated values
a_planet = "Mars"
another_planet = "Jupiter"
print("Hello from", a_planet, "and", another_planet + "!")
# Output: Hello from Mars and Jupiter!
Another way to format text is using the f-string method. You can use f-string by adding an f before the string and then use curly braces {} to insert variables.
# Example of f-string print
a_planet = "Earth"
another_planet = "Mars"
print(f"Hello from {a_planet} and {another_planet}!") # Output: Hello from Earth and Mars!
1.3 Int and Float¶
Numbers can be stored in variables. Integers are whole numbers, while floats are numbers with decimal points. Example: x = 5 or y = 3.14.
# Declaring an integer and a float variable
x = 10
y = 3.0
k = x + y + 100
# The value stored in the variable can be inspected by using print statement.
# Type of a variable var can be checked by calling type(var)
print("The value of x is", x, "and it is of type", type(x))
# f-strings can be used to write more elegant print statement.
print(f"The value of y is {y} and it is of type {type(y)}")
print(f"The value of k is {k} and it is of type {type(k)}")
# casting int to float
print(f"x is of type {type(x)}")
x = float(x)
print(f"x is of type {type(x)} after casting")
Numbers can also be written in different formats. For example, you can use scientific notation: z = 1.23e-4 (equivalent to 0.000123).
# Numbers can also be written in scientific notation
x = 1.05e6
print(f"x is {x} and it is of type {type(x)}")
# You can also use _ to make the number more readable
x = 1_000_000
print(f"x is {x} and it is of type {type(x)}")
You can perform arithmetic operations on numbers. The basic arithmetic operators are +, -, *, /, // (integer division), and % (modulo).
# Arithmetic operators
# Addition
z = x + y
print(f"Adding x and y gives {z}")
# Subtraction
z = x - y
print(f"Subtracting y from x gives {z}")
# Multiplication
z = x * y
print(f"Multiplying x and y gives {z}")
# Division
z = x / y
print(f"x divided by y gives {z}")
# Floor Division
z = x // y
print(f"x divided by y gives {z} as quotient")
# Modulus Operator
z = x % y
print(f"x divided by y gives {z} as reminder")
# Exponentiation
z = x ** y
print(f"x raised to y gives {z}")
# self increment by 1
x = x + 1
# This is equivalent to x += 1
print(f"x + 1 gives {x}")
1.4 Booleans and None¶
Bolean values are True and False and are used to represent binary values. None is a special value that represents the absence of a value, when tested, normally it results to False.
# True and False are the key words that represent bool values in python
a = True
b = False
print(f"a is {a} and b is {b}")
print(f"Type of variable a and b is {type(a)}")
# None in python represents the absence of something; similar to null value
c = None
print(f"c is {c} and is of type {type(c)}")
# Any non-zero integer value is true and zero is false.
# Also anything with a non-zero length is true and empty sequences are false.
You can use logical operators and, or, and not to combine boolean values.
# logical operators
# and, or and not operate on bool variables
# OR operator: Gives True when either of the expressions evaluates to True
# expr1 or expr2
print(f"a or b is {a or b}")
print(f"a or a is {a or a}")
print(f"b or b is {b or b}")
# AND operator: Gives True when both the expressions evaluates to True
# expr1 and expr2
print(f"a and b is {a and b}")
print(f"a and a is {a and a}")
print(f"b and b is {b and b}")
# NOT operator: negates a bool
# not expr1
print(f"Not of a is {not a}")
Comparison operators result in boolean values. The comparison operators are == (equal), != (not equal), >, <, >=, and <=.
You can also combine comparison operators to create more complex conditions, such as x > 5 and x < 10, which checks if x is between 5 and 10.
Additionally, logical operators can be used in conjunction with comparison operators to create more complex conditions, for example: if x > 5 and x < 10: print("x is between 5 and 10").
# comparison operators
x = 10
y = 3.0
z = 5
# greater that, less than, greater than equal to and lesser than equal to
x > y
x >= y
x < y
x <= y
# equals and not equals
x == y
x != y
# Chained Expressions
x > y > z
(x > y) or (x > z)
1.5 Strings¶
Strings are sequences of characters, e.g., pieces of text. You can create them by enclosing characters in quotes. Python treats single quotes the same as double quotes.
Strings can be concatenated using the + operator. You can also multiply strings by integers to repeat them.
You may need to escape special characters in strings using a backslash \. For example, to include a quote in a string, you can use \'.
You can use the len() function to get the length of a string.
# strings are represented using single or double quotes
first_name = "Adam"
last_name = 'Eve'
# \ is used to escape characters
middle_name = 'zero\'s'
# string concatenation
full_name = first_name +' ' + middle_name + ' ' + last_name
print(f"Full name is {full_name}")
print(f"Full name is of type {type(full_name)}")
# length of string
length = len(full_name)
print(f"Length of full name is {length}")
Strings can be indexed and sliced similar to lists and tuples.
List indexing is discussed in the list section
You can 'multiply' a string by an integer to repeat it.
repeated_string = "Hello " * 10
print(f"repeated_string is {repeated_string}")
Some characters have special meanings in strings. For example, \n represents a newline character, and \t represents a tab character. You can use r before the string to treat it as a raw string, ignoring special characters.
# characters with special meaning
# \n - newline
# \t - tab
# \\ - backslash
# examples
print("Hello\nWorld")
print("Hello\tWorld")
print("Hello\\World")
Casting is the process of converting one data type to another. You can cast strings to integers or floats, and vice versa. For example, you can convert a string to an integer using int("5").
# casting str to int
total = int('1') + int('2')
print(f"The value of total is {total} and it is of type {type(total)}")
# casting int to str
total = str(1) + str(2)
print(f"The value of total is {total} and it is of type {type(total)}")
# empty string is considered as False
empty_string = ''
bool(empty_string)
To replace a part of a string, you can use the replace() method. To split a string into a list of substrings, you can use the split() method.
# replace method
s = "Hello, World!"
s = s.replace("World", "Universe")
print(s)
Note that the result of the f-string composition is a string.
# f-string composition is a string
helloworld = f"Hello, World!"
full_sentence = f"This is a full sentence. {helloworld} and it is of type {type(helloworld)}"
print(full_sentence)
Q: Create a sentence about the weather using variables for the temperature and the weather condition.
- Print the sentence
- Example: "The temperature is 25 degrees and it is sunny."
- Then use replace to change the weather condition to "cloudy" and print the new sentence.
# TODO: Write your code here
1.4 Lists and Tuples¶
Lists are collections of items that are ordered and changeable. Tuples are collections of items that are ordered and unchangeable.
They can contain any type of object, even other lists or tuples. They can be used for instance to store a sequence of numbers, strings, or a mix of both.
# Arrays are implemented as lists in python
# creating empty list
names = []
names = list()
# list of strings
names = ['Zach', 'Jay']
print(names)
# list of intergers
nums = [1, 2, 3, 4, 5]
print(nums)
# list of different data types
l = ['Zach', 1, True, None]
print(l)
# list of lists
ll = [[1, 3], [2, 3], [3, 4]]
# finding the length of list
length = len(l)
print(length)
You can repeat a list or tuple by multiplying it by an integer.
# You can repeat a list or tuple by multiplying it by an integer.
l = [1, 2, 3] * 3
print(l)
t = (1, 2, 3) * 3
print(t)
You can change the content of a list by assigning new values to its elements. You can also add elements to a list using the append() method or extend a list with another list using the extend() method.
# Lists are mutable
names = names + ['Ravi']
names.append('Richard')
names.extend(['Abi', 'Kevin'])
print(names)
An element or a subset of a list can be accessed using element's index or slice of indices.
The same notation applies for strings but at the character level.
# some_list[index]
# some_list[start_index: end_index(not included)]
numbers = [0, 1, 2, 3, 4, 5, 6]
# indices start from 0 in python
print(f'The first element in numbers is {numbers[0]}')
print(f'The third element in numbers is {numbers[2]}')
print(f'Elements from 1st to 5th index are {numbers[1:6]}')
print(f'Elements from start to 5th index are {numbers[:6]}')
print(f'Elements from 4th index to end are {numbers[4:]}')
print(f'Last Element is {numbers[-1]}')
print(f'Last four element are {numbers[-4:]}')
# changing 1st element in the numbers list
numbers[0] = 100
print(numbers)
# changing first 3 numbers
numbers[0: 3] = [100, 200, 300]
print(numbers)
Q: Define a list of four items representing your favorite hobbies/activities.
- Print only the second and third items using list slicing.
# TODO: Write your code here
On the other hand, tuples are immutable, meaning that you cannot change the content of a tuple once it is created.
# Tuples are immutable lists. They are created using () instead of [].
names = tuple()
names = ('Zach', 'Jay')
print(names[0])
# trying to alter the tuple gives an error
names[0] = 'Richard'
# similar to tuples, strings are also immutable
# empty lists and tuples are considered as False
emptyList = []
emptyTuple = ()
print(f"emptyList is {bool(emptyList)} and emptyTuple is {bool(emptyTuple)}")
The split() method can be used to split a string into a list of substrings. By default, it splits the string by spaces, but you can specify a different separator.
# split method
s = "Hello, World!"
words = s.split()
print(words)
# split with a different separator
s = "apple,banana,orange"
fruits = s.split(',')
print(fruits)
You can use the join() method to concatenate a list of strings into a single string.
# join method
words = ['Hello', 'World']
s = ' '.join(words)
print(s)
Q: See what happens when you try to change the content of a tuple.
- Since you cannot change the content of a tuple, you need to create a new tuple with the new content.
- Create a new tuple with 3 elements based on the first three elements of the tuple you defined above.
# TODO: Write your code here
1.5 Dictionary¶
Dictionary is a collection of key-value pairs. It is unordered, changeable and indexed. Dictionaries are written with curly brackets, and have keys and values.
They can be used for example to associate a name with a phone number.
# hash maps in python are called Dictionaries
# dict{key: value}
# Empty dictionary
phonebook = dict()
# contruction dict using sequences of key-value pairs
dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
# Dictionary with one item
phonebook = {'jack': 4098}
# Add another item
phonebook['guido'] = 4127
print(phonebook)
print(phonebook['jack'])
print(phonebook.items())
print(phonebook.keys())
print(phonebook.values())
print('jack' in phonebook)
print('Kevin' in phonebook)
# Delete an item
del phonebook['jack']
print(phonebook)
# An empty dictionary is considered as False
emptyDict = {}
print(f"emptyDict is {bool(emptyDict)}")
Q: Define a dictionary where the keys are the names of the planets and the values are their positions relative to the Sun (like 3 for Earth).
- Print the dictionary.
# TODO: Write your code here
1.6 Sets¶
Sets are unordered collections of unique elements. They are used to store multiple items in a single variable. Sets are written with curly brackets.
# Sets are unordered collection of unique elements
# Sets are mutable
# Empty set
s = set()
# set of integers
s = {1, 2, 3, 4, 5}
print(f"The set is {s}")
# set of mixed data types
s = {1, 2.0, 'three'}
print(f"The set is {s}")
# set of tuples
s = {('a', 'b'), ('c', 'd')}
print(f"The set is {s}")
# set operations
s1 = {1, 2, 3, 4, 5}
s2 = {4, 5, 6, 7, 8}
print(f"s1 is {s1}")
print(f"s2 is {s2}")
# Union
s3 = s1 | s2
print(f"Union of s1 and s2 is {s3}")
# Intersection
s3 = s1 & s2
print(f"Intersection of s1 and s2 is {s3}")
# Difference
s3 = s1 - s2
print(f"Difference of s1 and s2 is {s3}")
# Symmetric Difference
s3 = s1 ^ s2
print(f"Symmetric Difference of s1 and s2 is {s3}")
# Empty set is considered as False
emptySet = set()
print(f"emptySet is {bool(emptySet)}")
Q: Define two sets of strings with some common elements and some unique elements.
Perform the following operations on the sets:
- Union
- Intersection
- Difference
Print the results.
# TODO: Write your code here
2. Flow control statements¶
Flow control statements are used to control the flow of a program. They include if, elif, and else statements, for and while loops, and break and continue statements.
Differently from other programming languages, Python uses indentation to define code blocks. The code block following an if, elif, else, for, or while statement must be indented.
IMPORTANT: The standard indentation is 4 spaces. Always use the same number of spaces for indentation in the same block of code. Avoid using tabs for indentation. Most of the code editors are configured to automatically convert tabs to spaces.
2.1 if... elif... else...¶
The if statement is used to test a condition. If the condition is true, the code block following the if statement is executed. The elif statement is used to test multiple conditions. If the condition is true, the code block following the elif statement is executed. The else statement is used to execute a block of code if the condition is false.
For instance, this is needed if you want to check if a number is positive, negative, or zero and then decide what to do based on the result.
# if expr1:
# code1
# elif expr2:
# code2
# .
# .
# .
# .
# else:
# code_n
# code1 is executed if expr1 is evaluated to true. Else it moves to expr2 and checks for true
# condition and moves to the next if not true.
# Finally if all the excpression's are false, code_n is executed
x = int(input("Please enter an integer: "))
if x < 0:
x = 0
print('Negative changed to zero')
elif x == 0:
print('Zero')
elif x == 1:
print('Single')
else:
print('More')
Q: Prompt the user to input a number and use an if-elif-else statement to check if the number is negative, zero, or positive.
- Print an appropriate message in each case.
# TODO: Write your code here
If statements can be used in conjunction with comparison operators to create more complex conditions.
x = int(input("Please enter an integer: "))
if x > 0 and x < 10:
print(f'x={x} is a positive single digit number')
else:
print(f'x={x} is not a positive single digit number')
2.2 For loops¶
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
In general, loops are used to repeat a block of code multiple times. The for loop iterates over a sequence of elements, such as a list or a tuple. The for loop can also iterate over a string, which is a sequence of characters.
# for loop is used to iter over any iterable object
# iterating over list
for name in ['Steve', 'Jill', 'Venus']:
print(name)
You can iterate over every character in a string.
# iterating over string
for char in "Hellooooo":
print(char)
To iterate over dictionary keys and values, you can use the items() method. The for loop can deal with multiple variables.
# iterating over dict keys
phone_nos = {"Henry": 6091237458,
"James": 1234556789,
"Larry": 5698327549,
"Rocky": 8593876589}
for name, no in phone_nos.items(): # items() returns a list of tuples
print(f"{name}: {no}")
To iterate over a sequence of numbers we use range() function.
The range() function generates a sequence of numbers. It can take one, two, or three parameters. For example, range(5) generates a sequence of numbers from 0 to 4.
for i in range(5):
print(i)
However, if you want to iterate over a sequence of numbers with a different starting point, you can use range(start, stop, step). For example, range(2, 10, 2) generates a sequence of numbers from 2 to 20 with a step of 2:
for i in range(2, 20, 2):
print(i)
You can use the len() function to get the length of a list or a string and use it in the range() function.
# using len of list/tuple in range
names = ['Steve', 'Rock', 'Harry']
for i in range(len(names)):
print(names[i])
Alternatively, if you want to have both the element in a sequences as well as an index, you can use the enumerate() function.
# using len of list/tuple in range
names = ['Steve', 'Rock', 'Harry']
for i in range(len(names)):
print(f"{i}: {names[i]}")
Q: Using a for loop, print the square of each number in range(1, 6).
# TODO: Write your code here
2.3 While Loop¶
With the while loop we can execute a set of statements as long as a condition is true.
# While loop executes as long as the condition remains true.
# while cond1:
# pass
i = 0
while i < 10:
print(i)
i += 1
Forever loop: The below code runs for ever (don't run it)
# while True:
# print('Forever...')
2.4 Break, Continue and Pass statements¶
break statement is used to exit a loop when a condition is met. continue statement is used to skip the current block and return to the for or while statement. pass statement is used as a placeholder for future code.
# break statement breaks out of the the loop
while True:
print('We’re stuck in a loop...')
break # Break out of the while loop
print("not!")
You can skip the current iteration without exiting the loop by using the continue statement.
# continue statement skips a loop
for i in range(5):
continue
print(i)
This is more usefult when you have a condition to skip the current iteration but you don't want to exit the loop. For instance, you can skip the iteration if the number is even.
for i in range(10):
if i % 2 == 0: # Calculate the remainder of i divided by 2 and check if it is 0
continue
print(i)
The pass statement is used as a placeholder for future code. When the pass statement is executed, nothing happens, but you avoid getting an error. It allows you to write minimal code structure without causing issues during execution, which is helpful in situations where code needs to be developed incrementally.
for i in range(10):
pass
Q: Ask the user for an input, print that input, and then print the input in reverse.
- Use a while loop to repeat this process until the user inputs 'quit'.
# TODO: Write your code here
3. 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
4. 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)}")
5.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
6 Loading Data¶
6.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)
6.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)
7. 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}")
8. 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}")
9. 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
10. 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