Python

Python Basic Fundamentals

Basics

This page covers the basic fundamentals of Python programming.

It encompasses:

  • language syntax
  • commenting
  • variables
  • collections and collections management (PIP)

Python syntax

Python’s syntax is organised very well and easy to learn. Its foundation is similar to the English language. In many programming languages, indentation applies mainly to improve readability of the code, but in Python it is a vital structural concept.

Indentation is the empty space on each line in front of the code (from left to right) – below is an example.

# Line 2 is without indentation
print("hello")
# Line 4 is with indentation 
    print("hello")

The programming language requires at least 1 space to operate successfully, but within the Python community 4 spaces is the general usage (the example above uses 4 spaces of indentation).

python syntax

Comments

In Python and other programming languages, comments are an important part of coding. Comments help developers explain and make Python code more readable. They do not appear in the output section, but rather in the development coding stage. Comments, alongside explicit variables names as well as obvious names of functions and classes, can significantly improve the organisation and soundness of Python code.

In Python, comments start with “#“. The following is an example of commenting.

# This is a comment and it will not show as an output
print("This is not a comment")

"""
This is a multiline commenting.
Nothing from these line
will be displayed as an outcome
after executing the code.
"""

The second example shows how to comment multiple lines at once, with three single (”’) or double (“””) quotes at each end.

Variables

Variables are part of the building block of Python and their use is to store data values. In many programming languages, variables have to be declared in order to work properly. But in Python, variable declaring happens automatically within the writting of the code.

In addition, semicolon (;) is a must in many languages at the end of each statement. Semicolons are very non-pythonic and are not required.

Below is an example of both variable declaration and semicolon usage. The first is a Python variable and the second is in Java.

# Python
num = 24
# Java
int num = 24;

Modules, Packages, and Libraries

The three terms, modules, packages, and libraries can often be confusing. They are all interconnected, but different in meaning.

  • Module is a collection of code, saved in a file with the extension “.py
  • Package is a collection of modules
  • Library is a collection of modules and packages

Note: The terms packages and libraries often refer to the same thing, as a library generally means any reusable piece of code.

All three code sources contain already built statements, functions, and classes. Instead of building everything from scratch, developers can reuse said blocks of code. Before usage, any collection of code has to be imported first.

The following shows how to import modules.

# The keyword "import" imports the whole module, followed by the module name.
import modulename

# If only a specific function is needed, the keyword "from" can import it instead of the whole module.
from moduleName import function_name

The following shows how to use “import” and “from” keywords.

import modulename
# When calling a function or other item from the module, the module name should appear before the item.
modulename.functiona_name()


from moduleName import function_name
# When only the function or other item is imported, the module name does not appear in front of the item.
function_name()

Rename modules

It is common practice among developer to rename modules. The renaming is often an abbreviation (alias) of the module name, creating a more readable as well as easier to use code. The word “as” creates the alias.

# The word "as" imports the module "someModule" and renames it as "sn". The abbreviation does not change the original name of the module, but rather during the development of the code.
import someModule as sm

PIP

PIP is a package management system. If PIP is not present by default, it can be downloaded and installed from here.

The package management is able to install, reinstall, and uninstall packages.

The following are some of the functions and ways of using PIP in the command line.

# Check version of PIP
pip --version

# List already installed packages
pip list

# Install package
pip install packageName

# Uninstall package
pip uninstall packageName

For more details about PIP and how to install packages, see our article here.


Previous: Python Data Types

by AICorr Team

We are proud to offer our extensive knowledge to you, for free. The AICorr Team puts a lot of effort in researching, testing, and writing the content within the platform (aicorr.com). We hope that you learn and progress forward.