cs workbook content Flashcards
**If the first index is greater than or equal to the second the result is an **
Such:
»> fruit = ‘banana’
»> fruit[3:3]
What is output?
what string indexing does this: the result is an empty string, represented by two quotation marks:
when changing strings, what should you know?
(ig)strings are immutable in Python, it means that once a string is created, it cannot be changed or modified.
Python has a function called “what?” which lists all the methods available for an object.
function dir does what?
the method “what” takes a string and returns a new string with all uppercase letters.
How would I write this and know about it?
(ig) instead of the function syntax, upper(word), it uses the method syntax, word.upper().
The empty parentheses indicate that this method takes no argument.
word = ‘banana’
2
new_word = word.upper()
3
print(new_word)
4
BANANA
explain the syntax of find() and explain its paramters
(ig)Syntax
string.find(value, start, end)
Parameter Values
Parameter Description
value Required. The value to search for
start Optional. Where to start the search. Default is 0
end Optional. Where to end the search. Default is to the end of the string
The find() method returns what? if the value is not found.
(ig) -1
The method finds the first occurrence of the specified value.
(ig) The find() method finds the first occurrence of the specified value.
You will note that** startswith** requires what, so what
You will note that startswith requires case to match, so sometimes we’ll convert a line to lowercase using the lower method before we do any checking:
line = ‘Have a nice day’
line.startswith(‘h’)
print(line.lower())
print(line.lower().startswith(‘h’))
have a nice day
True
Method that makes all the letters lowercased
(ig) string.lower()
the string method **what? ** to count the number of times a string appears in a another string. How would I write this
(ig) string.count(‘aa’)
def countSS(word):
counter = word.count(‘ss’)
return counter
What is used to format an integer, floating-point number, and string
(anw follows the statement)What does the following: %d, %g , and %s.
What list method adds a new element to the end of a list, also a way to add to an empty list:
string.append does what and should know(don’t look on the bottom)
»_space;> t = [‘a’, ‘b’, ‘c’]
»> t.append(‘d’)
»> t
[‘a’, ‘b’, ‘c’, ‘d’]
What list method adds the specified list elements (or any iterable) to the end of the current list.
Parameter Description
iterable Required. Any iterable (list, set, tuple, etc.)
What does string.extend() do? What do you use on this?
fruits = [‘apple’, ‘banana’, ‘cherry’]
points = (1, 4, 5, 9)
fruits.extend(points)
print(fruits)
[‘apple’, ‘banana’, ‘cherry’, 1, 4, 5, 9]
> > > t1 = [‘a’, ‘b’, ‘c’]
t2 = [‘d’, ‘e’]
t1.extend(t2)
t1
[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
What list method arranges the elements of the list from low to high:
What does string.sort do?() do(don’t look below)
> > > t = [‘d’, ‘c’, ‘e’, ‘b’, ‘a’]
t.sort()
t
[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
Adding up the elements of a list is such a common operation that Python provides it as a built-in function
Explain and give me the connotation of using sum()
_
»> t = [1, 2, 3]
»> sum(t)
6
If you know the index of the element you want to delete, you can use what: modifies the list and returns the element that was removed. If you don’t provide an index, it deletes and returns the last element.
What does string.pop() do?
-
> > > t = [‘a’, ‘b’, ‘c’]
x = t.pop(1)
t
[‘a’, ‘c’]
x
‘b’
If you don’t provide an index for pop(), what happens?
(ig)If provide an index, it deletes and returns the last element, basically deletes the last item.
If you don’t need the removed value like from pop(), you can use what?
What does del string[] do?
-
»> t = [‘a’, ‘b’, ‘c’]
»> del t[1]
»> t
[‘a’, ‘c’]
If you know the element you want to remove (but not the index), you can use what?
The return value from this is None.
What does string.remove() do/return explanation?
> > > t = [‘a’, ‘b’, ‘c’]
t.remove(‘b’)
t
[‘a’, ‘c’]
NT
To remove more than one element, you can use
(ig) you can use del with a slice index:
> > > t = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]
del t[1:5]
t
[‘a’, ‘f’]
To convert from a string to a list of characters, you can use?
What does list(string) do?
> > > s = ‘spam’
t = list(s)
t
[’s’, ‘p’, ‘a’, ‘m’]
The list function breaks a string into individual letters. If you want to break a string into words, you can use what?
What does string.split() do? What uses this?
-
> > > s = ‘pining for the fjords’
t = s.split()
t
[‘pining’, ‘for’, ‘the’, ‘fjords’]
s = ‘pining for the fjords’
t = s.split()
print(t)
print(t[2])
[‘pining’, ‘for’, ‘the’, ‘fjords’]
the