Week 8 - text (strings) & data from CSV files Flashcards

1
Q

How to use the index operator with strings?

A

[] used to index a character. Each character is a unique index.
Starts at 0
-1 looks to the last character in the string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does a dot.notation combine

A

objects with methods.

example - object.method()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to use the slice operator with strings?

A

[n:m] - returns part of a string from the nth character to the mth character. However excludes the mth character (stops at the one before).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What do the “in” and “not in” operators do?

A

‘in’ tests if one string is a subset of another string

‘not in’ returns the logic opposite to ‘in’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What string operators also work for lists too?

A
indexing
slicing
searching (in and not in)
looping
concatenating
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to open a file in python?

A
open file = open(file_path, mode)
modes:
'r' = reading a text file
'rb' = reading a binary file
'w' = writing a text file
'wb' writing a binary file
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What methods are used to read a file?

A

variable_name.readlines(file_variable) - reads all the lines into a list
variable_name.read(file_variable) - reads whole file into one big string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to read CSV files

A
import CSV - python module that helps read compex csv files.
variable_name = csv.reader(file)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to use the CSV dictionary reader?

A

csv.DictReader() converts each row into a dictionary with the column header as the key.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you count using a looping program?

A

This counts a sequence of values

  1. Initialise a counter variable with zero before the loop
  2. Increment the counter in the loop (i.e. count = count + 1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you sum using a looping program?

A

Sums a sequence of values

  1. Initialise a sum variable with zero before the loop.
  2. Add the value to the sum variable each time it loops (i.e. salary = salary + float(loop_variable[“key”])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you average using a looping program?

A
  1. Combine the count and sum programs

2. Then print(sum/count)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you accumulate data through a looping program?

A

Save the data to a list []
1. Initiate a variable with a blank list []
2. choose a particular colum:
a. create a variable that selects the key (top of the
program.)
b. create a variable inside the loop that selects
only the key (i.e. value = Ist_col[key]
3. Append key to list (ie. blank_list_variable.append(key_variable)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly