import myModule import myModule2 myModule.greeting("Note") print(myModule2.plus(1, 2)) print(myModule2.allNumber) myModule2.allNumber[0] = -1 print(myModule2.allNumber)
from myModule import greeting, age print(greeting(' Kabita\t') + "Age=", age)
#Use a myModule # show execution in shell import myModule myModule.greeting("Jonathan")
import myModule import myModule as m from myModule import greeting # import part of module import platform # build-in modules myModule.greeting() m.greeting() greeting() print(platform.system()) # print(dir(platform)) # list of all function names or variables in module
''' Created on Jun 18, 2018 from YourClassParentDir.YourClass import YourClass @author: ramchennale ''' import myModule myModule.greeting("Ram") myModule.I_am_From_MyModule() print("") name = myModule.person1["name"] age = myModule.person1["age"] num = myModule.person1["num"] print(name, age, num) #o1=myModule() #o1.greeting("Ram Chennale") #print("") #o1.I_am_From_MyModule() #Traceback (most recent call last): # File "C:\Users\ramchennale\workspace\pythonDemo\examples\myModule1.py", line 12, in <module> # o1=myModule() #TypeError: 'module' object is not callable
# Use a Module import myModule # 使用Module內的Function,要加上Module Name myModule.greeting("KL.JOJO") # Variable in Module age = myModule.person1["age"] print(age) # Naming a Module(ReNameing a Module) import myModule as mx a = mx.person1["age"] print(a) # Import Built-in Modules import platform x = platform.system() print(x) # Use the dir() Function , 可以列出 Module內的函式或變數名稱 n = dir(myModule) print(n) p = dir(platform) print(p) # Import From Module , 只引用 Module 內想要的項目(函式或變數均可) # Notice:利用from <moduleName> import <moduleFn|moduleVar>後, # 再使用時,就毋須加上<moduleName>. Ex. (O)person1 , (X)myModule.person1 from myModule import person1
import myModule import datetime # import classObject myModule.greeting("Rith") a = myModule.person1["age"] print(a) getTime = datetime.datetime.now() #Display real time print(getTime.year, getTime.strftime("%a"), getTime.month) print(getTime.month, getTime.strftime("%A"), getTime.year) i = ["apple", "banana", "cc"] #display item array i from -3 to -1 print(i[-3:-1]) #change first item in array to aa i[0] = "aa" print(i) #loop to show result of array i for k in i: print(k) #add bb to array i i.append("bb") print(i) #add dd to first item in array i
from myModule import greeting from myModule2 import plus from myModule2 import divide greeting("Note") print(plus(1, 2)) print(divide(1, 2))
#module "Modules are code libraries with functions in them to create a module save a function in another file with the .py extension " "myModule.py" "When the module has been created you can then call it using the import keyword" import myModule "You can then call the method" myModule.greeting("Tomi") "Varibales can be accessed from modules as well" brand = myModule.car["Brand"] print(brand) "you can rename modules using the as keyword" import myModule as mx mx.greeting("Bob") "You can use the dir() fucntion to print all the functions or varibales in a module" import platform x = dir(platform) print(x) "you can also specify waht you want to import from a module by using the keyword from" from myModule import car print(car)