Python - Import Modules and Exploring The Standard Library
** Import Modules and Exploring The Standard Library
============my_module.py===================
print('Imported my_module...')
test = 'Test String'
def find_index(to_search, target):
"""Find the index of a value in a sequence"""
for i, value in enumerate(to_search):
if value == target:
return i
return -1
-------------my.py-------------
import my_module
courses = ['History', 'Math', 'Physics', 'CompSci']
index = my_module.find_index(courses, 'Math')
print(index)
---------------------------------
Imported my_module...
1
-------------my.py-------------
import my_module as mm
courses = ['History', 'Math', 'Physics', 'CompSci']
index = mm.find_index(courses, 'Math')
print(index)
---------------------------------
Imported my_module...
1
-------------my.py-------------
from my_module import find_index
courses = ['History', 'Math', 'Physics', 'CompSci']
index = find_index(courses, 'Math')
print(index)
---------------------------------
Imported my_module...
1
-------------my.py-------------
from my_module import find_index, test
courses = ['History', 'Math', 'Physics', 'CompSci']
index = find_index(courses, 'Math')
print(index)
print(test)
---------------------------------
Imported my_module...
1
Test String
-------------my.py-------------
from my_module import find_index as fi, test
courses = ['History', 'Math', 'Physics', 'CompSci']
index = fi(courses, 'Math')
print(index)
print(test)
---------------------------------
Imported my_module...
1
Test String
-------------my.py-------------
from my_module import *
courses = ['History', 'Math', 'Physics', 'CompSci']
index = find_index(courses, 'Math')
print(index)
print(test)
---------------------------------
Imported my_module...
1
Test String
-------------my.py-------------
from my_module import find_index, test
import sys
courses = ['History', 'Math', 'Physics', 'CompSci']
index = find_index(courses, 'Math')
print(sys.path)
---------------------------------
Imported my_module...
['C:\\Users\\purunet\\Documents\\py1', 'C:\\Users\\purunet\\AppData\\Local\\Programs\\Python\\Python38-32\\python38.zip', 'C:\\Users\\purunet\\AppData\\Local\\Programs\\Python\\Python38-32\\DLLs', 'C:\\Users\\purunet\\AppData\\Local\\Programs\\Python\\Python38-32\\lib', 'C:\\Users\\purunet\\AppData\\Local\\Programs\\Python\\Python38-32', 'C:\\Users\\purunet\\AppData\\Roaming\\Python\\Python38\\site-packages', 'C:\\Users\\purunet\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages']
--------------------------------------------------------------------------------
==========================================
import random
courses = ['History', 'Math', 'Physics', 'CompSci']
random_course = random.choice(courses)
print(random_course)
---------------------------------
Physics # 실행 할 때 마다` 결과 변동
---------------------------------
==========================================
import math
courses = ['History', 'Math', 'Physics', 'CompSci']
rads = math.radians(90)
print(rads)
---------------------------------
1.5707963267948966
---------------------------------
==========================================
import math
rads = math.radians(90)
print(math.sin(rads))
---------------------------------
1.0
---------------------------------
==========================================
import datetime
import calendar
today = datetime.date.today()
print(today)
print(calendar.isleap(2020))
print(calendar.isleap(2021))
---------------------------------
2020-05-05
True
False
---------------------------------
==========================================
import os
print(os.getcwd())
print(os.__file__)
---------------------------------
C:\Users\purunet\Documents\py1
C:\Users\purunet\AppData\Local\Programs\Python\Python38-32\lib\os.py
---------------------------------