LEARN PYTHON QUICKLY AND PYTHON CODING EXERCISES: Coding For Beginners by TAM JJ
>>> temp . pop()
7
>>> temp . pop()
13
>>> temp . pop()
Traceback (most recent call last):
File "<stdin>" , line 1 , in <module>
KeyErro r : 'pop from an empty set'
Remove all elements from set
âclearâ method is used to remove all elements from set.
>>> powersOf2
{16, 8, 2, 4, 1}
>>>
>>> powersOf2 . clear()
>>> powersOf2
set()
Python modules
Module is a file, which contains definitions. We can define classes, modules, variables etc., in module file and reuse them in other python scripts.
For example, create a file âarithmetic.pyâ and copy following code.
arithmetic.py
def sum (a, b):
â return a+ b
def subtract (a, b):
â return a- b
def mul (a,b):
â return a* b
def div (a, b):
â return a/ b
Open python interpreter, import the module using âimportâ key word and call the functions defined in module .
>>> impor t arithmetic
>>>