strings, files and arrays Flashcards
mystring = “Hello”
how can i save the length of the string in the variable x
x = len(mystring)
methods
-methods work similarly to functions.
- they are written like this
mystring.upper()
and return a value
mystring = “Hello”
what methods will return
1) “HELLO”
2) “hello”
1) mystring.upper()
2) mystring.lower()
mystring = “Hello”
what method will count the number of times “e” occurs in the string
mystring.count(“e”)
mystring = “Hello”
what method will replace all instances of “e” with “x” in the string. (output “Hxllo”)
mystring.replace(“e”, “x”)
mystring = “Hello”
how can i retreive the first letter of the string
mystring[0]
mystring = “Hello”
write a loop which prints each letter of this string seperately.
for i in range (len(mystring)):
print(mystring[i])
mystring = “Hello”
how can i take a “slice” of this string, leaving only the letters “ell”
mystring[1:3]
mystring = “Hello”
how can i take a “slice” of this string, leaving out the first two letters
mystring[2:]
mystring = “Hello”
how can i take a “slice” of this string, leaving out the last two letters
mystring[:3]
what does slicing look like in psuedocode
mystring, substring(1,4)
how to reference a file name in your program.
save the name as a string constant. eg.
FILENAME = “highscore.txt”
then it can be used as myfile
given
FILENAME = “highscore.txt”
savetext= “200”
write some code to save 200 to the text document.
with open( FILENAME, “a”) as myfile:
myfile.write(savetext)
given
FILENAME = “highscore.txt”
write some code to save the contents of the document to a variable x
with open (FILENAME, “r”) as myfile:
x= myfile.read()
what are the conditions for reading a text file into a variable
- file must be text only
- must be in your python directory
write a list called mylist , that holds the strings “a” “b” “c”
mylist = [“a” , “b”, “c”]
what are items in a list called
elements
they can be different data types
make an empty list called mylist. Then append “a” to the list.
mylist = []
mylist.append(“a”)
given
mylist =[ “a”, “b”, “c”]
write one line of code to print “b” from the list
print(mylist[1])
given
mylist =[ “a”, “b”, “c”]
write one line of code to ovverwrite the element “b” with “x”
mylist[1] = “x”
mylist =[ “a”, “b”, “c”]
delete the element “b” using it’s element number
del mylist[1]
mylist =[ “a”, “b”, “c”]
remove the element “b” using it’s content
mylist.remove(“b”)
write a boolean expression that checks if the string “a” is in the list called mylist
“a” in mylist
write an if structure that prints “correct” if the string “b” is found in the list mylist
if “b” in mylist:
print(“correct”)
what is the difference between
- mylist.sort()
- sorted(mylist)
- sort: a method that rearranges a list
- sorted: a function that creates a new list and needs to be captured with a variable
does slicing a list alter the value of the original list?
no
list 6 operations that can be performed on data structures
appending
inserting
editing
deleting
traversing / searching
sorting
what is a tuple
a python data structure
not mutable/ values cannot be changed
uses round brackets instead of square
create a tuple called mytuple which holds the letters “a” “b” and “c”
mytuple = (“a”,”b”,”c”)
what is a mutable data structure and give an example
a data structure that can change. Append, edit, reorganise etc.
eg. a list
what operations can be done on a tuple
- traversing
- searching
why would i use a tuple rather than a list
- store a list of data that dosen’t change, eg. months
- protect the program from unwanted errors
mytuple = (“a”,”b”,”c”)
write a line of code to print the first element of this tuple.
print(mytuple[0])
static vs dynamic data structures
static- fixed in size. eg. an array
dynamic - can get bigger and littler. eg. a list
features of an array
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
features of a list
mutable (can change size)
the index of a value can change as the list changes in size
values shown in square brackets
traversing
visiting every value in a data structure.
eg. traverse a list/array/tuple with a for loop
2D array
- can be thought of as a table of rows and columns
- each position is identified by row value and column value
myarray[4,2]
2D list
a list of lists
every element is identified by two numbers
mylist[2][4]