1.2 Python Data Structures, String, Date and Time Flashcards

(64 cards)

1
Q

A data structure in Python that is ordered and mutable (changeable)

A

List [ ]

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

Items in this data structure do not need to be homogenous or of the same type

A

List

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

Elements in this data structure are indexed according to a definite sequence and the indexing is done with 0 being the first index

A

List

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

A list inside a list is called

A

Nested list

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

It can have any number of items and they may be of different types (integer, float, string etc)

A

List

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

The index of -1 refers to the last item, -2 to the second last item and so on

A

Negative indexing

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

Add an element to the end of the list

A

append()

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

Add all elements of a list to the end of another list

A

extend()

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

Insert an item at the defined index

A

insert()

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

Removes an item from the list

A

remove()

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

Removes and returns an element at the given index

A

pop()

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

Removes all items from the list

A

clear()

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

Returns the index of the first matched item

A

index()

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

Returns the count of number of items passed as an argument

A

count()

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

Sort items in a list in ascending order

A

sort()

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

Reverse the order of items in the list

A

reverse()

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

Returns a shallow copy of the list

A

copy()

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

This data structure is an unordered collection of items. Every element is unique (no duplicates) and immutable (cannot be changed)

A

Set { }

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

Although a set element is immutable, a set itself is _____________. We can add or remove items from it

A

mutable

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

A set cannot have mutable elements like lists, sets, or ______________ as its elements

A

dictionaries

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

Adds an item to a set

A

add()

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

Adds multiple items to a set

A

update()

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

To remove an item in a set, use the remove() or _________ method

A

discard()

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

If the item to remove does not exist in a set, __________ will raise an error

A

remove()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
If the item to remove does not exist, __________ will NOT raise an error
discard()
26
Using ________ will remove the last item
pop()
27
This method empties the set
clear()
28
This keyword deletes the set completely
del
29
Returns a new set with all items from both sets; symbolized by | operator
Union
30
Returns a new set with Common Elements from both sets; symbolized by & operator
Intersection
31
To find difference in between sets; symbolized by - operator
Difference
32
To find difference from both sets; symbolized by ^ operator
Symmetric Difference
33
A data structure in Python that is ordered and immutable (unchangeable)
Tuple ( )
34
A tuple can be created without parentheses. This is called
tuple packing
35
Ways to Access a Tuple
1. Postive Indexing 2. Negative Indexing 3. Slicing
36
Returns the number of items (x) in a tuple
count(x)
37
Returns the index of the first item that is equal to x
index(x)
38
An unordered collection of items that is mutable
Dictionary
39
____ of a dictionary must be unique and immutable data type such as strings, integers, and tuples
Keys
40
This dictionary method returns None instead of KeyError, if the key is not found
get()
41
This method removes an item with the provided key and returns the value
pop()
42
This method can be used to remove and return an arbitrary (key, value) item pair from the dictionary
popitem()
43
Returns a new dictionary with keys from seq and value equal to v (defaults to None)
fromkeys(seq[,v])
44
Returns the value of the key. If the key doesn't exist, returns d (defaults to None)
get(key[,d])
45
Return a new object of the dictionary's items in (key, value) format
item()
46
Returns a new object of the dictionary's keys
keys()
47
Removes the item with the key and returns its value or d if key is not found. If d is not provided and the key is not found, it raises KeyError
pop(key[,d])
48
Returns the corresponding value if the key is in the dictionary. If not, inserts the key with a value of d and returns d (defaults to None)
setdefault[key[,d])
49
Updates the dictionary with the key/value pairs from other, overwriting existing keys
update([other])
50
Returns a new object of the dictionary's values
values()
51
Returns True if all keys of the dictionary are True (or if the dictionary is empty)
all()
52
Return True if any key of the dictionary is true. If the dictionary is empty, return False
any()
53
Return the length of dictionary
len()
54
Compares items of two dictionaries (Not available in Python 3)
cmp()
55
Return a new sorted list of keys in the dictionary
sorted()
56
Create a datetime object containing the current local date and time using this method
now()
57
Use this method defined in the date class to get a date object containing the current local date
today()
58
Commonly used classes in datetime module are:
- date Class - time Class - datetime Class - timedelta Class
59
You can convert a timestamp to date using this method
fromtimestamp()
60
A class from datetime module that contains information from both date and time objects
datetime.datetime Class
61
Print year, month, hour, minute and timestamp
datetime.datetime Class
62
Represents the difference between two dates or times
datetime.timedelta
63
This method is defines under classes date, datetime and time. Creates a formatted string from a given date, datetime or time object. Ex.: %Y, %m, %d etc
strftime()
64