CARVIEW |
- CCNA online course
- Linux online course
- VMware ESXi online course
- Nmap online course
- MySQL online course
- Raspberry Pi online course
- Apache HTTP Server course
- VMware Player online course
- Splunk online course
- SQL online course
- Oracle VirtualBox online course
- Python online course
- Asterisk course
- VMware Workstation Player course
- Process Explorer course
- Pillow online course
- Create a web crawler in Python
- A short introduction to…
Import modules
The most common way to create a module is to define a file with the .py extension that will contain the code you want to group separately from the rest of the application.
In the following example we will create a module with two simple functions. We will then import and use these functions in another file.
We will create a module with the following content:
def Welcome(name): print("Welcome", name) return def Bye(name): print("Bye", name) return
The module above contains two simple function that will print the supplied name, along with the greeting. If you follow along, make sure to save the module in the same directory that will also contain the top-level file.
There are two ways to import a module:
- using the import statement – imports the entire module.
- using the from…import statement – imports only individual module attributes.
Here is how we can import a module using the import statement:
import simple_module simple_module.Welcome("Bob")
The first line (import simple_module) imports the content of the file simple_module.py. The second line calls the function Welcome and passes the string value of “Bob“. Notice how we must precede the attribute name with the name of the module (without the .py extension). The result of the code above is:
>>> Welcome Bob >>>
We can also use the from…import statement to import only the attribute we need. Here is how it can be done:
from simple_module import Bye
When we import attributes using the from…import statement, we don’t need to precede the attribute name with the name of the module:
>>> from simple_module import Bye >>> Bye("John") Bye John
However, if we try to call the Welcome function, we will get an error, because only the Bye function was imported:
>>> Welcome("John") Traceback (most recent call last): File "<pyshell#5>", line 1, in <module> Welcome("John") NameError: name 'Welcome' is not defined >>>
Python course
- Introduction
- Python overview
- Install Python on Windows
- Install Python on Linux
- Add Python to the Windows Path
- Run Python code
- Interactive prompt
- IDLE editor
- Command line
- Help mode
- Basic programs
- Write your first program
- Use comments
- What are variables?
- Variable data types
- Variable names
- Numeric variables
- Strings
- Get the current date and time
- Operators overview
- Arithmetic operators
- Comparison operators
- Logical operators
- Assignment operators
- Membership operators
- Identity operators
- Conditional statements
- The if statement
- Get user input
- The if...else statement
- The if...elif statement
- Nested if statements
- Use logical operators
- Loops
- The for loop
- Use for loop with the range() function
- The break statement
- The continue statement
- The pass statement
- Use else statement in loops
- The while loop
- Nested loop statements
- Errors
- Types of errors
- Syntax and logical errors
- The try...except statements
- The try...except...else statements
- The try...except...finally statements
- Catch specific exceptions
- Raise exception
- Nest exception handling statements
- Modules
- What are modules?
- Import modules
- Find files on disk
- Display module content
- Strings
- What are strings?
- Escape characters
- Access individual characters
- String functions
- Search strings
- Concatenating strings
- Lists, sets, tuples, dictionaries
- What are lists?
- Modify lists
- Loop through a list
- Check whether a value is in a list
- Sorting lists temporarily
- Sorting lists permanently
- Obtaining the list length
- What are sets?
- What are dictionaries?
- Add new key-value pair to a dictionary
- Modify a value in a dictionary
- Delete a key-value pair in a dictionary
- Loop through a dictionary
- What are tuples?
- Looping over a tuple
- Working with files
- How to read and write files
- Read a file
- Read and write – with statement
- Make a list of lines from a file
- Functions
- What are functions?
- Return statement
- Positional arguments
- Keyword arguments
- Default values for parameters
- Flexible number of arguments
- Variable scopes