AIBridge
Lecture 2
Lecture Outline
I/O
2
List Manipulation
What is a function?
Built-ins
Importing
I/O
Input from console: input('prompt')
Open file: file_object=open(file, mode)
'r' is read and 'w' is write for the mode
read()
"""Here is a file.
This file has multiple lines.
This is the last line."""
, readline()
, readlines()
"Here is a file."
"This file has multiple lines."
"This is the last line."
["Here is a file.",
"This file has multiple lines.",
"This is the last line."]
Always close file: file_object.close()
Standard Input
I/O
Output to Console: print(object1, object2, ...)
Open file: file_object=open(file, mode)
write()
Always close file
Note: This removes any existing file with that name
print('a', 'b', 'c', 'd')
print('e', 'f', 'g')
Standard Output
Lecture Outline
I/O
5
List Manipulation
What is a function?
Built-ins
Importing
List Manipulation
Indexing
6
List Operations
Listcomp
String/list Interop
List Manipulation
Single indexing
List slicing
[a, b, c, d, e]
0 1 2 3 4
-5 -4 -3 -2 -1
list_name[ ]
i
0
3
1
list_name[ : ]
list_name[-2]
i j
1 4
Indexing
Self-Test
What does the following code output?
arr = [4, 5, 6, 101, 102, 103, 104, 105]
new_arr = arr[2:6]
print(new_arr)
A. [5, 6, 7, 101, 102, 103, 104, 105]
B. [6, 7, 101, 102, 103, 104, 105]
C. [6, 101, 102, 103, 104]
D. [6, 101, 102, 103]
List Manipulation
Indexing
Self-Test
What does the following code output?
arr = [4, 5, 6, 101, 102, 103, 104, 105]
new_arr = arr[2:6]
print(new_arr)
A. [5, 6, 7, 101, 102, 103, 104, 105]
B. [6, 7, 101, 102, 103, 104, 105]
C. [6, 101, 102, 103, 104]
D. [6, 101, 102, 103]
List Manipulation
Indexing
List Manipulation
Indexing
10
List Operations
Listcomp
String/list Interop
List Manipulation
List Operations
1
2
List Manipulation
List Operations
my_list =
[3, 14, 0, -2, 5
]
List Manipulation
List Operations
append()
[3, 14, 0, -2, 5
]
my_list.append(19)
19
[3, 14, 0, -2, 5
[3, 14, 0, -2, 5, 19
my_list.append(19)
List Manipulation
List Operations
append()
]
19
,
my_list.append( )
8
my_list.append(8)
[3, 14, 0, -2, 5, 19
List Manipulation
List Operations
append()
my_list.append(19)
]
8
,
my_list.append(8)
[3, 14, 0, -2, 5, 19, 8]
[3, 14, 0, -2, 5, 19, 8]
[3, 14, 0,
List Manipulation
List Operations
remove()
my_list.remove(-2)
5, 19, 8]
-2,
[3, 14, 0, 5,
8]
19,
List Manipulation
List Operations
remove()
my_list.remove(-2)
5, 19, 8]
my_list.remove(19)
[3, 14, 0,
List Manipulation
List Operations
remove()
my_list.remove(-2)
my_list.remove(19)
[3, 14, 0, 5,
8]
[3, 14, 0, 5, 8]
1,
1
,
14, 5, 8]
List Manipulation
List Operations
pop()
my_list.pop(3)
[3, 14, 0,
14, 5, 8]
14
5, 8]
my_list.pop(3)
1
List Manipulation
List Operations
pop()
[3, 14, 0,
,
my_list.pop(3)
14
5, 8]
my_list.pop(3)
1
List Manipulation
List Operations
pop()
[3, 14, 0,
my_list.pop(3)
[3, 14, 0, 5, 8]
[3, 14, 0, 5, 8
]
[3, 14, 0, 5, 8]
my_list_2 = [
List Manipulation
List Operations
+
my_list = my_list + my_list_2
10, 9, 8, 7]
my_list_2 = [10, 9, 8, 7]
[3, 14, 0, 5, 8
List Manipulation
List Operations
+
my_list = my_list + my_list_2
10, 9, 8, 7]
,
my_list_2 = [10, 9, 8, 7]
[0, 3, 5, 7, 8, 8, 9, 10, 14]
List Manipulation
List Operations
len()
len(my_list)
9
[0, 3, 5, 7, 8, 8, 9, 10, 14]
List Manipulation
List Operations
max()
max(my_list)
14
[0, 3, 5, 7, 8, 8, 9, 10, 14]
List Manipulation
List Operations
min()
min(my_list)
0
List Manipulation
Indexing
27
List Operations
Listcomp
String/list Interop
Shorthand for “for” loops
new_list = [expression for object in iteration]
List Manipulation
Listcomp
[obj1, obj2, obj3, obj4, obj5, obj6, obj7 ...]
[new1, new2, new3, new4, new5, new6, new7 ...]
expression
new_list = [(i+1)/2 for i in range(7)]
List Manipulation
Listcomp
[0, 1, 2, 3, 4, 5, 6]
[0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5]
(i+1)/2
List Manipulation
Indexing
30
List Operations
Listcomp
String/list Interop
separator
separator
List Manipulation
String/list Interop
join()
separator.join(my_list)
List of strings
my_list = [str1, str2, str3]
str1
str3
str2
List Manipulation
String/list Interop
join()
separator.join(my_list)
List of strings
my_list = [str1, str2, str3]
str1
str3
str2
separator
separator
Final String
my_list = ["Hello,", "my", "name", "is", "Bob!"]
List Manipulation
String/list Interop
join()
' '.join(my_list)
"Hello,"
"my"
"name"
"is"
"Bob!"
' '
' '
' '
' '
"Bob!"
"is"
"Hello, my name is Bob!"
' '
"name"
' '
"my"
' '
"Hello,"
my_list = ["Hello,", "my", "name", "is", "Bob!"]
List Manipulation
String/list Interop
join()
' '.join(my_list)
' '
Lecture Outline
I/O
35
List Manipulation
What is a function?
Built-ins
Importing
What is a function?�
Input
Output
Function
What is a function?�
Args and Kwargs
What is a function?�
Arguments
def my_function(arg_1=-1, arg_2=-1, arg_3=-1, arg_4=-1):
print(arg_1, arg_2, arg_3, arg_4)
my_function(1,2,3,4)
my_function(3,4)
�
What is a function?�
Keyword Arguments
def my_function(a,b,c,d):
print(a,b,c,d)
my_function(b=2,c=3,d=4,a=1)
�
What is a function?��
"Hello world!"
print()
What is a function?�
Args and Kwargs
Lecture Outline
I/O
42
List Manipulation
What is a function?
Built-ins
Importing
Built-ins
1
2
Lecture Outline
I/O
44
List Manipulation
What is a function?
Built-ins
Importing
45
Importing
Module
Python
Burp
46
Importing
import sklearn
from sklearn import linear_model
import pandas as pd
47
Importing
import sklearn
from sklearn import linear_model
import pandas as pd
To find functions we might want to use, we need to look at the documentation, for example:
https://pandas.pydata.org/docs/
48
That was a lot!
Let’s get to the lab!