1. Basic Concepts

Python is an interpreted scripting language. This means that Python programs are, in their most basic form, just text files with a list of commands, which are executed by a program called the Python interpreter. Compiling Python programs, or "scripts", into binary is not necessary, as the interpreter can read the source code directly. In general, commands are executed in order, starting with the first line of the program and on down to the last, although certain statements give the possibility of repeating or skipping over large portions of code. 

A) Operators

The standard mathematical operators that you are familiar with work the same way in Python as in most other languages.

+ - * / // % **

Addition, subtraction, multiplication, division, and modulus are all part of the standard set of operators. Python has two division operators, a single slash ( / ) character for classic division and a double-slash ( // ) character for “floor” division. Classic division means that if the operands are both integers, it will perform floor division, while for floating point numbers, it represents true division. If true division is enabled, then the division operator will always perform that operation, regardless of operand types. There is also an exponentiation operator, the double star/asterisk (**). Although we are emphasizing the mathematical nature of these operators, please note that some of these operators are overloaded for use with other data types as well, for example, strings and lists.

Python also provides the standard comparison operators, which return a Boolean value indicating the truthfulness of the expression. 

< <= > >= == != <>

B) Variables

Rules for variables in Python are the same as they are in most other high-level languages inspired by C. They are simply identifier names with a case-sensitive alphabetic first character, including the underscore ( _ ). Any additional characters may be alphanumeric or underscore.

Python is dynamically typed, meaning that no pre-declaration of a variable or its type is necessary. The type (and value) are initialized on assignment. Assignments are performed using the equal sign.

counter = 0
name = 'Alice'
counter = counter + 1

C) Numbers

Python supports four basic number types

  • int (signed integers)
  • bool (Boolean values)
  • float (floating point real numbers)
  • complex (complex numbers)

Boolean values are a special case integers. Although represented by the constants True and False, if put in a numeric context such as addition with other numbers, True is treated as the integer with value 1, and False has a value of 0.

Complex numbers (numbers that involve the square root of -1, so-called “imaginary” numbers) are not supported in many languages and perhaps are implemented only as classes in others.

D) Strings

Strings in Python are identified as a contiguous set of characters in between quotation marks. Python allows for either pairs of single or double quotes. Subsets of strings can be taken using the index ( [ ] ) and slice ( [ : ] ) operators, which work with indexes starting at 0 in the beginning of the string and working their way from -1 at the end.

name = Alice
name[0]  # return 'A'
name[2:4]  # returns 'ice'

E) Lists

Lists can be thought of as generic “arrays” with which to hold an arbitrary number of arbitrary Python objects. The items are ordered and accessed via indices, similar to arrays, except that lists can store different types of objects. Subsets can be taken with the slice operator ( [ : ] ) in the same manner as strings.

squares = [1, 4, 9, 16, 25]
squares[-1]  # 25
squares[-3:]  # [9, 16, 25] slicing returns a new list

F) Functions

Like many other languages, functions in Python are called using the functional operator ( ( ) ), functions must be declared before they can be called. You do not need to declare function (return) types or explicitly return values (None, Python’s NULL object is returned by default if one is not given.) Python can be considered “call by reference.” This means that any changes to these parameters within the function affect the original objects in the calling function. However, the caveat is that in Python, it is really dependent on the object type being passed. If that object allows updating, then it behaves as you would expect from “call by reference,” but if that object’s value cannot be changed, then it will behave like “call by value.”

def function_name([arguments]):
    "optional documentation string"
    function_suite

G) Comments and docstrings

For your own convenience, and to help others understand your code, it is strongly recommended to include remarks (or comments) about what your code is doing together with the code. Comments in Python are designated with a # symbol: anything after the # is ignored by the Python interpreter.

a = (1+3)**2 #a is 16

A different method of documenting your code is by a docstring. Docstrings are a sort of introductory explanation of the program you're writing, put at the start of the program. These are started and ended by three double-quote symbols in a row. For example:

def printHelloWorld():
    """This function prints out Hello, World!
    Written July 1st, 2020 by JA. Coder.
    This comment will end after the triple quote"""
    print("Hello, World!")
return

Docstrings are not actual comments, in that they're not ignored by Python, but they're still a form of documentation.

Comments are also very useful when your code doesn't work. A debugging method is to "comment out" everything except the bare essentials to isolate what isn't working what it is working.