>>> b=copy.copy(a)
A deep copy copies an object into another. This means that if you make a change to a copy of an object, it won’t affect the original object. In Python, we use the function deepcopy() for this, and we import the module copy. We use it like:
>>> import copy
>>> b=copy.deepcopy(a)
[on true] if [expression] else [on false]
If the expression is True, the statement under [on true] is executed. Else, that under [on false] is executed.
Below is how you would use it:
>>> a,b=2,3
>>> min=a if a<b else b
>>> min
>>> with open('file.txt') as f: count=0 for i in f.read(): if i.isupper(): count+=1 print(count)
In a dictionary, each entry will have a key and a value, but the order will not be guaranteed. Elements in the dictionary can be accessed by using their key.
Lists can be used whenever you have a collection of items in an order. A dictionary can be used whenever you have a set of unique keys that map to values.
mydict={‘a’:1,’b’:2,’c’:3,’e’:5}
mydict.keys()
dict_keys([‘a’, ‘b’, ‘c’, ‘e’])
A tuple is similar to a list in Python, but the key difference between them is that tuples are immutable. They also use less space than lists and can only be used as a key in a dictionary. Tuples are a perfect choice when you want a list of constants.
Sets are a collection of unique elements that are used in Python. Sets are a good option when you want to avoid duplicate elements in your list. This means that whenever you have two lists with common elements between them, you can leverage sets to eliminate them.
>>>"python" in "python is good" >>>True >>> >>>"bad" not in "python is good" >>>True
>>> 10 is '10' >>> True >>> True is not False >>> True