LEARN PYTHON QUICKLY AND PYTHON CODING EXERCISES: Coding For Beginners by TAM JJ

LEARN PYTHON QUICKLY AND PYTHON CODING EXERCISES: Coding For Beginners by TAM JJ
Author: TAM, JJ [TAM, JJ]
Language: eng
Format: epub
Published: 2020-12-27

>>> 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

>>>