1 of 48

AIBridge

Lecture 2

2 of 48

Lecture Outline

I/O

2

List Manipulation

What is a function?

Built-ins

Importing

3 of 48

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

4 of 48

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

5 of 48

Lecture Outline

I/O

5

List Manipulation

What is a function?

Built-ins

Importing

6 of 48

List Manipulation

Indexing

6

List Operations

Listcomp

String/list Interop

7 of 48

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

8 of 48

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

9 of 48

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

10 of 48

List Manipulation

Indexing

10

List Operations

Listcomp

String/list Interop

11 of 48

List Manipulation

List Operations

1

2

12 of 48

List Manipulation

List Operations

my_list =

[3, 14, 0, -2, 5

]

13 of 48

List Manipulation

List Operations

append()

[3, 14, 0, -2, 5

]

my_list.append(19)

19

14 of 48

[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)

15 of 48

[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]

16 of 48

[3, 14, 0, -2, 5, 19, 8]

[3, 14, 0,

List Manipulation

List Operations

remove()

my_list.remove(-2)

5, 19, 8]

-2,

17 of 48

[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,

18 of 48

List Manipulation

List Operations

remove()

my_list.remove(-2)

my_list.remove(19)

[3, 14, 0, 5,

8]

[3, 14, 0, 5, 8]

19 of 48

1,

1

,

14, 5, 8]

List Manipulation

List Operations

pop()

my_list.pop(3)

[3, 14, 0,

20 of 48

14, 5, 8]

14

5, 8]

my_list.pop(3)

1

List Manipulation

List Operations

pop()

[3, 14, 0,

,

my_list.pop(3)

21 of 48

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]

22 of 48

[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]

23 of 48

[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]

24 of 48

[0, 3, 5, 7, 8, 8, 9, 10, 14]

List Manipulation

List Operations

len()

len(my_list)

9

25 of 48

[0, 3, 5, 7, 8, 8, 9, 10, 14]

List Manipulation

List Operations

max()

max(my_list)

14

26 of 48

[0, 3, 5, 7, 8, 8, 9, 10, 14]

List Manipulation

List Operations

min()

min(my_list)

0

27 of 48

List Manipulation

Indexing

27

List Operations

Listcomp

String/list Interop

28 of 48

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

29 of 48

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

30 of 48

List Manipulation

Indexing

30

List Operations

Listcomp

String/list Interop

31 of 48

separator

separator

List Manipulation

String/list Interop

join()

separator.join(my_list)

List of strings

my_list = [str1, str2, str3]

str1

str3

str2

32 of 48

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

33 of 48

my_list = ["Hello,", "my", "name", "is", "Bob!"]

List Manipulation

String/list Interop

join()

' '.join(my_list)

"Hello,"

"my"

"name"

"is"

"Bob!"

' '

' '

' '

' '

34 of 48

"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)

' '

35 of 48

Lecture Outline

I/O

35

List Manipulation

What is a function?

Built-ins

Importing

36 of 48

What is a function?�

Input

Output

Function

37 of 48

What is a function?�

Args and Kwargs

  • Normal arguments are based on position (nth input mapped to nth argument)
  • Kwargs (keyword arguments) are assigned

38 of 48

What is a function?�

Arguments

  • They are positional
  • Can have defaults

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)

39 of 48

What is a function?�

Keyword Arguments

  • You can specify a specific argument instead of relying on positions (helpful when there are a lot of arguments and you only need a few)

def my_function(a,b,c,d):

 print(a,b,c,d)

my_function(b=2,c=3,d=4,a=1)

40 of 48

What is a function?��

"Hello world!"

print()

Print

41 of 48

What is a function?�

Args and Kwargs

42 of 48

Lecture Outline

I/O

42

List Manipulation

What is a function?

Built-ins

Importing

43 of 48

Built-ins

1

2

44 of 48

Lecture Outline

I/O

44

List Manipulation

What is a function?

Built-ins

Importing

45 of 48

45

Importing

Module

Python

Burp

46 of 48

46

Importing

import sklearn

from sklearn import linear_model

import pandas as pd

47 of 48

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 of 48

48

That was a lot!

Let’s get to the lab!