import
import module_name
import sys print(sys.argv)
from ... import
from module_name import attribute_name
from math import sqrt print(sqrt(16))
as
import module_name as alias
import math as m print(m.sqrt(16))
sys
os
time
math
csv
random
re
datetime
Create a mymodule.py file with functions and variables.
mymodule.py
def say_hi(): print('Hello!')
Usage:
import mymodule mymodule.say_hi()
__name__
if __name__ == "__main__": # This block runs only when the module is executed directly print("Module is run directly") else: print("Module is imported")
import json data = {'name': 'Alice', 'age': 25} json.dumps(data)
'{"name": "Alice", "age": 25}'
from pprint import pprint data = {'name': 'Alice', 'age': 25} pprint(data)
{'age': 25, 'name': 'Alice'}
from datetime import datetime # This one is weird now = datetime.now() hp_birthday = datetime(1980, 7, 31) hp_seconds_alive = (now - hp_birthday).total_seconds()
http://gg.gg/1b8kph