strings, files and arrays Flashcards

1
Q

mystring = “Hello”

how can i save the length of the string in the variable x

A

x = len(mystring)

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

methods

A

-methods work similarly to functions.
- they are written like this

mystring.upper()

and return a value

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

mystring = “Hello”

what methods will return
1) “HELLO”
2) “hello”

A

1) mystring.upper()
2) mystring.lower()

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

mystring = “Hello”

what method will count the number of times “e” occurs in the string

A

mystring.count(“e”)

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

mystring = “Hello”

what method will replace all instances of “e” with “x” in the string. (output “Hxllo”)

A

mystring.replace(“e”, “x”)

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

mystring = “Hello”

how can i retreive the first letter of the string

A

mystring[0]

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

mystring = “Hello”
write a loop which prints each letter of this string seperately.

A

for i in range (len(mystring)):
print(mystring[i])

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

mystring = “Hello”

how can i take a “slice” of this string, leaving only the letters “ell”

A

mystring[1:3]

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

mystring = “Hello”

how can i take a “slice” of this string, leaving out the first two letters

A

mystring[2:]

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

mystring = “Hello”

how can i take a “slice” of this string, leaving out the last two letters

A

mystring[:3]

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

what does slicing look like in psuedocode

A

mystring, substring(1,4)

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

how to reference a file name in your program.

A

save the name as a string constant. eg.

FILENAME = “highscore.txt”

then it can be used as myfile

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

given

FILENAME = “highscore.txt”
savetext= “200”

write some code to save 200 to the text document.

A

with open( FILENAME, “a”) as myfile:
myfile.write(savetext)

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

given

FILENAME = “highscore.txt”

write some code to save the contents of the document to a variable x

A

with open (FILENAME, “r”) as myfile:
x= myfile.read()

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

what are the conditions for reading a text file into a variable

A
  • file must be text only
  • must be in your python directory
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

write a list called mylist , that holds the strings “a” “b” “c”

A

mylist = [“a” , “b”, “c”]

17
Q

what are items in a list called

A

elements
they can be different data types

18
Q

make an empty list called mylist. Then append “a” to the list.

A

mylist = []
mylist.append(“a”)

19
Q

given
mylist =[ “a”, “b”, “c”]
write one line of code to print “b” from the list

A

print(mylist[1])

20
Q

given
mylist =[ “a”, “b”, “c”]
write one line of code to ovverwrite the element “b” with “x”

A

mylist[1] = “x”

21
Q

mylist =[ “a”, “b”, “c”]

delete the element “b” using it’s element number

A

del mylist[1]

22
Q

mylist =[ “a”, “b”, “c”]

remove the element “b” using it’s content

A

mylist.remove(“b”)

23
Q

write a boolean expression that checks if the string “a” is in the list called mylist

A

“a” in mylist

24
Q

write an if structure that prints “correct” if the string “b” is found in the list mylist

A

if “b” in mylist:
print(“correct”)

25
Q

what is the difference between

  1. mylist.sort()
  2. sorted(mylist)
A
  1. sort: a method that rearranges a list
  2. sorted: a function that creates a new list and needs to be captured with a variable
26
Q

does slicing a list alter the value of the original list?

A

no

26
Q

list 6 operations that can be performed on data structures

A

appending
inserting
editing
deleting
traversing / searching
sorting

26
Q

what is a tuple

A

a python data structure
not mutable/ values cannot be changed
uses round brackets instead of square

26
Q

create a tuple called mytuple which holds the letters “a” “b” and “c”

A

mytuple = (“a”,”b”,”c”)

26
Q

what is a mutable data structure and give an example

A

a data structure that can change. Append, edit, reorganise etc.

eg. a list

27
Q

what operations can be done on a tuple

A
  • traversing
  • searching
28
Q

why would i use a tuple rather than a list

A
  • store a list of data that dosen’t change, eg. months
  • protect the program from unwanted errors
29
Q

mytuple = (“a”,”b”,”c”)

write a line of code to print the first element of this tuple.

A

print(mytuple[0])

30
Q

static vs dynamic data structures

A

static- fixed in size. eg. an array

dynamic - can get bigger and littler. eg. a list

31
Q

features of an array

A

fixed in size , can have empty spaces
all values must be the same data type
values remain at the same index address
values are shown in square brackets

32
Q

features of a list

A

mutable (can change size)
the index of a value can change as the list changes in size
values shown in square brackets

33
Q

traversing

A

visiting every value in a data structure.

eg. traverse a list/array/tuple with a for loop

34
Q

2D array

A
  • can be thought of as a table of rows and columns
  • each position is identified by row value and column value

myarray[4,2]

35
Q

2D list

A

a list of lists
every element is identified by two numbers

mylist[2][4]