Getting Started | Basics

Table of contents

1. Modules and Packages

In the Python Course-Part 01 Getting Started with Python| Development Environment different environments to write Python code, like Jupyter Notebook, have been introduced. Independent of the used environment there are some basic functions, which are directly available. In the Python standard library are many functions, which are defined in modules.

Many functions and classes, which are defined in modules, are not contained in the Python standard distribution. To use functions from external packages, you must first download and install the package

1.1 Install and download external packages

External Python packages are available from the software repository Python Package Index (PyPI). The application pip is the package manager of PyPI. It can be launched from the command line. You can download, install, update or remove packages.

The most important commands are:

pip install package_name
pip search package_name
pip show package_name
pip uninstall package_name

All functions are described in the official pip documentation

CMD Windows(Command-Line-Interpreter)

1.2 Conda

If Anaconda is installed, the included conda package manager can be applied as an alternative to the pip package manager. The available packages are listed on the Anaconda package list.

conda install package_name

The packages and virtual environments can be managed via the environment view of the Anaconda Navigator

Anaconda Navigator

1.3 Import of packages an modules

All installed packages can imported by: import package_name. For example to import the time package of the Python standard library:

import time

Help on modules and functions can be obtained by help(package_name) .

help function

Online documentation is available for all modules of the Python standard library and for all modules provided in the PyPI repository.

For example to get the current local time you can apply the ctime() function of the time package


Once a package is imported, you can see all functions and classes. Just type the name, followed by a dot, and then press the tab key.

packages all functions

Importing the whole package an be inefficient, if only a few functions are required

from package_name import module_name, function_name, function_name

In the case of long package name it may be a good choice to apply an alias for import import package_name as alias_name

For example, you can apply to the package name math an alias as follows:

1.4 Print

The value of a variable can be printed by print(variable_name)

Formatted output the placeholders are defined like this {2:2.3f} This implies that a float variable with 2 digits before and 3 digits after the decimal point can be inserted here. The 2 before the “:” indicates, that the variable at index 2 of the variable-list in the format method will be inserted here

5 divided by 2.000000 is 2.500
2.000000 divided by 5 is 0.400

1.5 Input

Python programs may require input of users. For this the input() function can be applied.

Float Input 1: 1.5
Float Input 2: 2.0
Input 1: 1.5 | Input 2: 2.0
1.5 * 2.0 = 3.0

1.6 Read from file

In Python input from files and output to files is realized by file objects. A file object is returned by the function open(filename,mode), where the string-variable filename defines the name of the file that shall be accessed, and the string variable mode defines how the file shall be accessed.

mode = 'w' for writing to a file (if the file already exists it will first be erased)
mode = 'a' for appending to a file
mode = 'r' for reading from a file
mode = 'r+' for reading from and writing to a file

First we need to download the file from github, create the 00_data folder and save the text from the http request in the 02_textfile_read.txt.

Once a file object has been created, it’s read() method can be called to read the entire file content into a single string-variable. If file access is no longer needed the file object’s close() method shall be called to clean up all system resources.

Another way to read in all contents of a file into a single string variable is:

If not the whole file content but only single lines shall be read the readline() method can be applied as follows:

You also can loop over the file object as follows:

The readlines() method returns a list of string-variables. Each of these elements in this list contains the contents of a single line:

1.7 Write to file

Writing to files can be done in 2 different modes:

- mode='w': if the file shall always be erased before writing new text to it
- mode='a': if the new content shall be appended to the already available contents in the file

For writing, the file object, which is returned by the open(filename,mode)-method provides the methods write() and writelines(). The former writes the string in the methods argument to the file. The argument of the latter must be a list of strings, which is written to the file.

The sourcecode is available at my GitHub repository.

Next Python Course-Part 03-Data Types | Data Structures

Data Analyst