Python Programming/Modules and how to use them
From Wikibooks, the open-content textbooks collection
Modules are libraries that can be called from other scripts. For example, a popular module is the time module, or the time module. You can call it using:
import time
. Then, open up time.py You will see a bunch of various functions in the file. An example is below:
import time def main(): #define the variable 'current_time' as a tuple of time.localtime() current_time = time.localtime() print current_time # print the tuple # if the year is 2008 (first value in the current_time tuple) if current_time[0] == 2008: print 'The year is 2008' # print the year if __name__ == '__main__': # if the function is the main function ... main() # ...call it
Modules can be called in a various number of ways. For example, we could import the time module as t:
import time as t # import the time module and call it 't' def main(): current_time = t.localtime() print current_time if current_time[0] == 2008: print 'The year is 2008' if __name__ == '__main__': main()
| Previous: User Interaction | Index | Next: Files |

