>>> def greeting(name): print('Hello, ' + name) import mymodule mymodule.greeting('Jonathan') person1 = { 'name': 'John', 'age': 36, 'country': 'Norway' } import mymodule a = mymodule.person1['age'] print(a)
# Create a Module # To create a module just save the code you want in a file with the file extension .py: # Save this code in a file named mymodule.py >>> def greeting(name): print("Hello, " + name) # Use a Module : Now we can use the module we just created, by using the import statement: # Import the module named mymodule, and call the greeting function: >>> import mymodule >>> mymodule.greeting("Jonathan") # Note: When using a function from a module, use the syntax: module_name.function_name. # Variables in Module # The module can contain functions, as already described, but also variables of all types (arrays, dictionaries, objects etc): # Save this code in the file mymodule.py >>> person1 = { "name": "John", "age": 36, "country": "Norway" } # Import the module named mymodule, and access the person1 dictionary:
def __init__(self, name, age): self.name = name self.age = age # python modules def myfunc(self): print("Hello my name is " + self.name) p1 = Person("Reuben", 19) p1.myfunc() import mymodule mymodule.greeting("penalope") import mymodule a = mymodule.person1["age"] print(a) import platform x = platform.system() print(x) import platform x = dir(platform) print(x)
import mymodule mymodule.greeting("Jonathan") a = mymodule.person1['age'] print(a) #Import only person1 dictionary from the module from mymodule import person1 print(person1["age"]) #Create an alias for mymodule called mx: import mymodule as mx a = mx.person1["age"] print(a) #Built-in Modules import platform x = platform.system() print(x) ##dir() function #lists allfunction name in a module import platform
import mymodule as mx mx.greeting("Thomas") a = mx.person["age"] print(a) # Naming a Module # You can name the module file whatever you like, but it must have the file extension .py # Re-naming a module # you can create an alias when you import a module, by using the "as" keyboard # Built-in Modules # These are several built-in modules in python, which you can import whenever you like print("\nTask 2") # import and use the platform module import platform x = platform.system() print(x) # Using the dir() function names (or variable names) in a module. The dir() function # The dir() function can be used on all modules, also the ones you create yourself print("\nTask 3") x = dir(platform) print(x) # tyring dir on my own python file
To create a module just save the code you want in a file with the file extension **.py** """ def greeting(name): print("Hello, " + name) """Save this code in a file named **mymodule.py** ##**Use a Module** Now we can use the module we just created, by using the ** import ** statement: """ import mymodule mymodule.greeting("Joey") """You can use any Python source file as a module by executing an import statement in some other Python source file. The import has the following syntax − **import module1[, module2[,... moduleN]** When the interpreter encounters an import statement, it imports the module if the module is present in the search path. A search path is a list of directories that the interpreter searches before importing a module. For example, to import the module support.py, you need to put the following command at the top of the script ##**Variables in Module** The module can contain functions, as already described, but also variables of all types (arrays, dictionaries, objects etc) """
import mymodule mymodule.greeting("ALi")
import mymodule, function mymodule.greeting("Awan") function.my_function("Muhammad", "Farhan") function.my_function2("Farhan", "Andi", "Awan") print(mymodule.nama) mymodule.greeting(mymodule.nama)
import mymodule import mymodule as mx from mymodule import person1 # There are several built-in modules in Python, which you can import whenever you like. # Import and use the platform module import platform mymodule.greeting("Jonathan") # Hello Jonathan a = mymodule.person1["age"] print(a) # 36 a = mx.person1["age"] # Using aliasing print(a) x = platform.system() print(x) # Windows # There is a built-in function to list all the function names (or variable names) in a module. x = dir(platform) print(x) print(person1['age']) # 36
import mymodule as mm mm.greeting("Surendra")
import mymodule mymodule.greeting('Jeferson!')
#Use a Module import mymodule mymodule.greeting("George")
#!/usr/bin/env python3.7 import mymodule mymodule.greeting("jona") a = mymodule.person1["age"] print(a)
# Python Modules - Part 1 # Use a Module import mymodule mymodule.greeting("Zinab") print(mymodule.person["name"])
import mymodule as m from mymodule import person1 import datetime as d m.greeting("pin") print(person1["country"]) x = d.datetime.now() print(x)
from mymodule import person1, greeting import platform import datetime import json x = platform.system() y = dir(platform) print(x) # print(y) print(greeting("Brajesh")) print("Hey, " + person1["name"]) print(datetime.datetime.now()) print(json.dumps(person1))
cookbook = int( input("What recipe would you like to make? (Please state the number)\n")) if cookbook == 1: import mymodule as mx mx.greeting("Nana") a = mx.person1["age"] print(a) mx.omin(2) mx.tmin(4) mx.cmin(10) mx.smallest(0) else: print("Updates pending")
def module_greetings(name): greeting = mymodule.greeting(name) return greeting
import mymodule mymodule.greeting("John") a = mymodule.person1["age"] print(a)
import mymodule print(mymodule.greeting("simran")) from mymodule import greeting print(greeting("simran")) from mymodule import greeting,age print(greeting("simran"),"Age=",age)
global z z = 300 myfuncGKeyW() print(z) z=200 def myfuncGKeyW1(): global z z = 350 myfuncGKeyW1() print(z) # Day 50:Modules import mymodule mymodule.greeting("Razan") print(mymodule.person["Age"]) # Day 51:Modules import mymodule as mx print(mx.person["Name"]) import platform print(platform.system()) print(platform.python_version()) print(dir(platform)) # Day 52:Datetime import datetime date=datetime.datetime.now()
import mymodule mymodule.greeting("Aniket")
from mymodule import greeting greeting()
import mymodule mymodule.greeting("Deepa")
import mymodule, function # mymodule.greeting("Farhan") # function.my_function("Farhan", "Mardadi") print(mymodule.nama) mymodule.greeting(mymodule.nama)
import mymodule as mx import platform #import only part from a module from mymodule import greeting a = mx.info["name"] print(a) #device sysytem a = platform.system() print(a) #sow all the function and var b = dir(platform) print(b) greeting("Sara")
return x myclass = MyNumbers() myiter = iter(myclass) print(next(myiter)) print(next(myiter)) print(next(myiter)) print(next(myiter)) print(next(myiter)) #This is a module example import mymodule mymodule.greeting("Leo") #This is a math example import math x = math.pi print(x) #This is an example from Converting python object to JSON import json x = { "name": "John", "age": 30,
import mymodule mymodule.greeting("Sahil Rajput.") print("Hello, world!") print("Hello, world!") print("Hello, world!") x = 1 if x == 1: # indented four spaces; print("Boom")
import mymodule import platform from mymodule import person1 print(person1["age"]) x = platform.system() print(x) mymodule.greeting("Nancy")
import mymodule mymodule.greeting("Simran")