--If you are running Python in a text terminal or terminal window, enter the name of the Python program followed by the program file name.
test1.py
print("This stadalone program works!")
result
$ python test1.py
This stadalone program works!
test2.py
import sys
print("Program arguments:",sys.argv)
result
$ python test2.py
Program arguments: ['test2.py']
$ python test2.py tra la la
Program arguments: ['test2.py', 'tra', 'la', 'la']
-** Module ** is a file that summarizes Python code. --Refer to the code of other modules with the import statement. By doing this, you can use the imported module code and variables in the program.
--The simplest way to use the import statement is in the form of import module. Here, the module part is the file name of another Python file with the .py extension removed. --After passing the import statement, the main program will be able to access all parts of module.py as long as it is prefixed with module. --If the imported code is used in multiple places, consider importing it outside the function.
Main program
#Import report module
import report
description=report.get_description()
print("Today is weather:",description)
report.py
#module
#get_description()Imports the choice function from the Python standard random module.
def get_description():
"""Returns random weather like a pro"""
from random import choice
possibilities=["rain","snow","sleet","fog","sun","who knows"]
return choice(possibilities)
result
$ python weatherman.py
Today is weather: fog
$ python weatherman.py
Today is weather: sun
$ python weatherman.py
Today is weather: fog
Rewritable
#I'm importing the choice function directly from the random module.
def get_description():
"""Returns random weather like a pro"""
import random
possibilities=["rain","snow","sleet","fog","sun","who knows"]
return random.choice(possibilities)
>>> import random
>>> def get_description():
... possibilities=["rain","snow","sleet","fog","sun","who knows"]
... return random.choice(possibilities)
...
>>> get_description
<function get_description at 0x11035b950>
>>> get_description()
'who knows'
>>> get_description()
'who knows'
>>>
--Can be imported using an alias.
import report as x
description=x.get_description()
print("Today is weather:",description)
--Python allows you to import only one or more parts from a module.
Import with original name
from report import get_description
description = get_description()
print("Today is weather:",description)
do_Import with it
from report import get_description as do_it
description = do_it()
print("Today is weather:",description)
--The file used is the first matched file. So if you define a module called random yourself and it is included in the search path before the standard library, you will not be able to access the standard library random.
>>> for place in sys.path:
... print(place)
...
practice/lib/python37.zip
practice/lib/python3.7
practice/lib/python3.7/lib-dynload
usr/local/var/pyenv/versions/3.7.5/lib/python3.7
practice/lib/python3.7/site-packages
--Modules can be organized into a hierarchical structure called ** packages **. --In addition to the following two files, the sources directory requires a file named init.py. The contents can be empty, but Python treats the directory containing this file as a package.
Main program
#The enumerate function can get the value in the order of index number and element. The index can be specified as 1 by specifying 1 as the second argument.
from sources import daily, weekly
print("Daily forecast:",daily.forecast())
print("Weekly forecast:")
for number, outlook in enumerate(weekly.forecast(),1):
print(number, outlook)
sources/daily.py
#Module 1
def forecast():
"Fake weather forecast"
return "like yesterday"
sources/weekly.py
#Module 2
def forecast():
"Fake weekly weather forecast"
return ["snow","more snow","sleet","freezing rain","rain","fog","hail"]
result
$ python weather.py
Daily forecast: like yesterday
Weekly forecast:
1 snow
2 more snow
3 sleet
4 freezing rain
5 rain
6 fog
7 hail
It was a cold day today.
"Introduction to Python3 by Bill Lubanovic (published by O'Reilly Japan)"
Recommended Posts