You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: episodes/02-image-basics.md
+54Lines changed: 54 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -75,6 +75,60 @@ import ipympl
75
75
~~~
76
76
{: .language-python}
77
77
78
+
> ## Import Statements in Python
79
+
>
80
+
> In Python, the `import` statement is used to load additional functionality
81
+
> into a program. This is necessary when we want our code to do something more
82
+
> specialised, which cannot easily be achieved with the limited set of basic
83
+
> tools and data structures available in the default Python environment.
84
+
>
85
+
> Additional functionality can be loaded as a single function or object,
86
+
> a module defining several of these, or a library containing many modules.
87
+
> You will encounter several different forms of `import` statement.
88
+
>
89
+
>
90
+
> ~~~
91
+
> import skimage # form 1, load whole skimage library
92
+
> import skimage.io # form 2, load skimage.io module only
93
+
> from skimage.io import imread # form 3, load only the imread function
94
+
> import numpy as np # form 4, load all of numpy into an object called np
95
+
> ~~~
96
+
> {: .language-python }
97
+
>
98
+
> > ## Further Explanation
99
+
> >
100
+
> > In the example above, form 1 loads the entire `skimage` library into the
101
+
> > program as an object. individual modules of the library are then available
102
+
> > within that object, e.g. to access the `imread` function used
103
+
> > in the example above, you would write `skimage.io.imread()`.
104
+
> >
105
+
> > Form 2 loads only the `io` module of `skimage` into the program. When we run
106
+
> > the code, the program will take less time and use less memory because we will
107
+
> > not load the whole `skimage` library. The syntax needed to use the module
108
+
> > remains unchanged.: to access the `imread` function, we would use the same
109
+
> > function call as given for form 1.
110
+
> >
111
+
> > To further reduce the time and memory requirements for your program,
112
+
> > form 3 can be used to import only a specific function/class from a library/module.
113
+
> > Unlike the other forms, when this approach is used, the imported function
114
+
> > or class can be called by its name only, without prefacing it with the name
115
+
> > of the module/library from which it was loaded,
116
+
> > i.e., `imread()` instead of `skimage.io.imread()` using the example above.
117
+
> > One hazard of this form is that importing like this will overwrite any
118
+
> > object with the same name that was defined/imported earlier in the program,
119
+
> > i.e., the example above would replace any existing object called `imread`
120
+
> > with the `imread` function from `skimage.io`.
121
+
> >
122
+
> > Finally, the `as` keyword can be used when importing, to define a name to be
123
+
> > used as shorthand for the library/module being imported. You may see `as`
124
+
> > combined with any of the other first three forms of `import` statement.
125
+
> >
126
+
> > Which form is used often depends on the size and number of additional tools
127
+
> > being loaded into the program.
128
+
> >
129
+
> {: .solution }
130
+
{: .callout }
131
+
78
132
Now that we have our libraries loaded, we will run a Jupyter Magic Command that will ensure our images display in our Jupyter document with pixel information that will help us more efficiently run commands later in the session.
0 commit comments