def main(): scores = [88, 92, 79, 93, 85] mean = uf.mean(scores) curved = uf.add_five(scores) mean_c = uf.mean(curved) print("Scores:", scores) print("Original Mean:", mean, " New Mean:", mean_c) print(__name__) print(uf.__name__)
import useful_functions as uf scores = [88, 92, 79, 93, 85] mean = uf.mean(scores) curved = uf.add_five(scores) mean_c = uf.mean(curved) print("Scores:", scores) print("Original Mean: ", mean, "New Mean: ", mean_c) print(__name__) print(uf.__name__)
import useful_functions as uf scores = [88, 92, 79, 93, 85] mean = uf.mean(scores) curved = uf.add_five(scores) mean_c = uf.mean(curved) print("Scores:", scores) print("Original mean: ", mean, " New mean: ", mean_c) print(__name__) print(uf.__name__)
import other_script import useful_functions as uf print(5 * 5) try: print(greeting) except Exception as e: print(e) finally: print("The above is to demonstrate you cannot access an object from another\ script directly using just it's identifier") print(other_script.greeting) scores = [90, 82, 74, 56, 50, 40, 22] average = uf.mean(scores) standardized = uf.add_five(scores) mean_s = uf.mean(standardized) print("Scores:", scores) print("Standardized scores:", standardized) print("Original mean:", average, " standardized mean:", mean_s) print(__name__) print(uf.__name__)
import useful_functions as func scores = [88, 92, 79, 93, 85] mean = func.mean(scores) curved = func.add_five(scores) mean_c = func.mean(curved) print("Scores:", scores) print("Original Mean:", mean, " New Mean:", mean_c)
# demo.py import useful_functions as uf scores = [88, 92, 79, 93, 85] mean = uf.mean(scores) curved = uf.add_five( scores ) #new list where each element is the element in the same index in the scores list, with 5 added added to its value mean_c = uf.mean(curved) print("Scores:", scores) print("Original Mean:", mean, " New Mean:", mean_c) #what is __name__ ? print( __name__) #so you see this file is the '__main__' file (that's its 'name') print( uf.__name__ ) #and the name of the imported script is useful_functions (which is not the main) #import module 'math' import math print(math.factorial( 4)) #output = should print 24 (in gith bash or terminal below)
import useful_functions as af scores = [88, 92, 79, 93, 85] mean = af.mean(scores) curved = af.add_five(scores) mean_c = af.mean(curved) print("Scores ;", scores) print("Original Mean :", mean) print("New Mean :", mean_c) print(__name__) print(af.__name__)
#Importing Local Scripts #import - used for import Python code from other scripts #you just type import followed by the name of the file, without the .py extension import useful_functions # import statement creates a module object called useful_functions, To access objects from an imported module, you need to use dot notation. import useful_functions useful_functions.add_five([1, 2, 3, 4]) #We can add an alias to an imported module to reference it with a different name. import useful_functions as uf uf.add_five([1, 2, 3, 4]) #if __name__ == "__main__" - used to avoid running executable argument in current code during importing from another file #Or alternatively, include them in a function called main() and call this in the if main block.