Python Composite data types Flashcards
(32 cards)
myStr = “Hello World”
print(myStr[2:-4:2])
‘loW’
H is index 0
index -4 is o but is not included
step size is 2
no comma!
str1 = “ABC”
str2 = “a b”
newStr = str1 * len(str2)
print(newStr)
ABCABCABC
Space and Tabs are also entities within the string, hence it is counted as len
str1 = "ababc" str2 = "ab" if str2 * len(str2) in str1: print("case1") elif str2 in str1: print("case2") else: print("case3")
Result = case1
str2len(str2) = ab2
ab*2 is in ababc
thus case1
membership operation, whats the operator used
in
Are string immutable?
yes
aStr = 'spam' newStr = aStr[:1] + 'l' + aStr[2:]
result = slam
str1 = “couple”
str2 = “tea”
newStr = str2[:1] + str1[2:]
print(newStr, str1)
tuple couple
what does string.find do
find the character within the string and returns its index value.
if the character does not exist, return -1
str1 = “couple”
newStr = str1[str1.find(‘ou’):str1.find(‘l’)]
print(newStr)
result= oup
str1 = “couple“
str2 = “t”
newStr = str1[::str1.find(str2)]
print(newStr)
elpuoc
string step size =-1 as str2 cannot be found in str1 hence -1.
with a step size of minus 1, the string gets reversed
string.upper
converts all the string to upper cases
string.lower
converts all the strings to lower cases
string.find
find the first character within the string and returns the index position of it.
if not found, return the value of 0
string.capitalize
capitalizes the first part of the string only, subsequent strings are not included.
string.index
similar to string.find but returns exception error when not found
string.islower
Returns True if all characters in the string are lower case
string.isupper
returns true if all characters in the string are upper case
split the strings in accordance to the separator and returns it as an array
string.split
swap both upper and lower cases
string.swapcases
returns true of all the characters are digits
string.isdigit
list1 = [1, “Python”, [3, 4], True]
list2 = list1[::-1] + list1[2]
print(list2)
[True, [3, 4], ‘Python’, 1, 3, 4]
list1 = [1, "Python", [3, 4], True] if 3 in list1: list2 = list1[2] * len(list1[2]) print(list2) elif [3, 4] in list1: print(list1[2][1]) else: print(list1[2])
Answer: 4
to call a list within a list
list[index with inner list][list index]
list1 = ['d', 'c'] list2 = ['a', 'b’] list1.reverse() list2.reverse() list3 = list1.extend(list2) print(list3)
Answer: None
list methods are mostly functions and do not return any value, instead the act on the original list.
Explain what does list methods doe
list methods are basically just functions and do not return and value. They just act to the list instead.