Python Composite data types Flashcards

(32 cards)

1
Q

myStr = “Hello World”

print(myStr[2:-4:2])

A

‘loW’

H is index 0
index -4 is o but is not included
step size is 2

no comma!

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

str1 = “ABC”
str2 = “a b”
newStr = str1 * len(str2)
print(newStr)

A

ABCABCABC

Space and Tabs are also entities within the string, hence it is counted as len

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
str1 = "ababc"
str2 = "ab"
if str2 * len(str2) in str1:
     print("case1")
elif str2 in str1:
     print("case2")
else:
     print("case3")
A

Result = case1

str2len(str2) = ab2
ab*2 is in ababc

thus case1

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

membership operation, whats the operator used

A

in

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

Are string immutable?

A

yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
aStr = 'spam'
newStr = aStr[:1] + 'l' + aStr[2:]
A

result = slam

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

str1 = “couple”
str2 = “tea”
newStr = str2[:1] + str1[2:]
print(newStr, str1)

A

tuple couple

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

what does string.find do

A

find the character within the string and returns its index value.
if the character does not exist, return -1

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

str1 = “couple”
newStr = str1[str1.find(‘ou’):str1.find(‘l’)]
print(newStr)

A

result= oup

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

str1 = “couple“
str2 = “t”
newStr = str1[::str1.find(str2)]
print(newStr)

A

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

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

string.upper

A

converts all the string to upper cases

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

string.lower

A

converts all the strings to lower cases

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

string.find

A

find the first character within the string and returns the index position of it.

if not found, return the value of 0

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

string.capitalize

A

capitalizes the first part of the string only, subsequent strings are not included.

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

string.index

A

similar to string.find but returns exception error when not found

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

string.islower

A

Returns True if all characters in the string are lower case

17
Q

string.isupper

A

returns true if all characters in the string are upper case

18
Q

split the strings in accordance to the separator and returns it as an array

19
Q

swap both upper and lower cases

A

string.swapcases

20
Q

returns true of all the characters are digits

A

string.isdigit

21
Q

list1 = [1, “Python”, [3, 4], True]
list2 = list1[::-1] + list1[2]
print(list2)

A

[True, [3, 4], ‘Python’, 1, 3, 4]

22
Q
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])
A

Answer: 4

to call a list within a list

list[index with inner list][list index]

23
Q
list1 = ['d', 'c']
list2 = ['a', 'b’]
list1.reverse()
list2.reverse()
list3 = list1.extend(list2)
print(list3)
A

Answer: None

list methods are mostly functions and do not return any value, instead the act on the original list.

24
Q

Explain what does list methods doe

A

list methods are basically just functions and do not return and value. They just act to the list instead.

25
Does list actually copy the physical list or do they copy the reference id only? Explain
Reference ID only. When you create a list, it creates a reference id to the variable. Var2 = Var1. Both are attached to the same id. Changing the list for 1 of the variable will change the other variable
26
syntax for list comprehension
n for n in iterable if condition is true
27
what is list comprehension equivalent to?
for x in array1: if condition = met: append x into a new array
28
``` tuple1 = ('a', 'b', 'c') list1 = ['A', 'B', 'b', 'D', 'a'] result = [] for i in range(len(tuple1)): if tuple1[i] in list1: result.append(i) print(result ```
Result: [0,1]
29
what data type does key of the dictionary can hold
strings, integers, tuples basically anything that is immutable No list
30
if key contains list, what is the result
error
31
dict1 = { 'a': [1], 'b': [2], } dict2 = { 'c': [3] } dict1.update(dict2) print(dict1)
``` { ‘a’:[1], ‘b’:[2], ‘c’:[3] } ```
32
dict1 = { 'a': [1], 'b': [2], } dict2 = { 'a': [7], 'c': [3] } dict1.update(dict2) print(dict1)
{‘a’:[7], ‘b’:[2], ‘c’:[3]} updates and add to the key value specifically