Python

edit

Created by a Dutch programmer Guido Von Rossum, who had worked on ABC programming language, which itself derives from SETL programming language. ABC had indenting for syntax scoping and reading clarity, and SETL had list comprehensions and aimed for the clarity of set notation and theory.

Type

edit

Python is described as a scripting language, and is usually run interpreted, although the interpreter scans the entire source for parsing prior to execution, and will halt if a syntax or type error occurs, presumably when constructing a parse tree that has nodes with type restrictions on their children.

Execution Entry Point

edit

The entry point is a scripting if-conditional at the leftmost code containing indentation, using the global variable __name__, and the looking for the value "__main__"

if __name__ == "__main__":
      #do program
      return

General Syntax

edit

Example of the assignment of 0 to a, then a to b:

a = 0
b = a

Instead of the use of semi-colons to denote the end of statements in languages such as C++ and Java, python uses line breaks to denote the end of a statement. This means that each statement must be put on a separate line.

Comments

edit
# This is an inline comment. Everything after the # is a comment.

Block comments are specified by starting and ending with a set of triple quotation marks ("""). They can span multiple lines.

"""
This is a
block comment
"""

Variable Declarations

edit

Variables are declared when first assigned. Python is dynamically typed, so the type of the variable depends on its initial value:

# integer variable:
a = 0

# float variable
c = 0.0

# string variable
d = 'Hello, world!'

Function Declaration/Implementation

edit

A function can be declared using the "def" keyword, then the function name. The arguments for the function are put in the parentheses. Code will only be part of a function if it is within the scope of that function.

def myFunction(arg1, arg2):
    # function start

# function end

You can create a function with an unknown number of arguments by using an asterisk:

def myFunction(*args):
    # function start

# function end

Scope

edit

Statements belong to the functional scope of the closest lower indent. There is a unnamed global functional scope. Variables and functions declared in any scope can't be accessed outside the scope they are declared in.

print("I will always be printed")
a = 1

if __name__ == "__main__":
    print("I will also always be printed, and I can access a=", a)
    b = 2
    def f():
           c = 1

    f()

def g():
       d = 2
       def h():
           e = d
           x = b

g()


print("I can  access b=", b, "because b belongs in a compound statement block of the same functional scope (global)")
print("and f() can also be called")
print("But c, d, e, and h() can't be accessed")

Conditional Statements

edit

The "if" keyword denotes a conditional statement. If the condition is true, it will execute the code under the scope of the if statement. Otherwise, it will execute the code under the scope of the else statement.

if A == B:
    D = C 
else:
    F = E

To check multiple conditions one after another, you can use the "elif" keyword (short for "else if"):

if A == B:
    D = C
elif A > B:
    F = E 
else:
    H = G

Looping Statements

edit

For loops will loop through values between a given range:

for i in range(0, 10):
    # code here

This range includes the starting value (0) but not the ending value (10), so it will go from i = 0 to i = 9. You can also use a for loop to iterate through a list:

values = []

for value in values:
    # code here

While loops will loop until a condition becomes false. It will check the condition at the beginning of each loop.

while i > 20:
    # code here

Output Statements

edit

Output in python is done using the built-in print function. The print function will automatically start a new line of output after printing.

print("Hello, world!")

To print multiple items, separate them by a comma. Each item will automatically be printed with a space in between them.

print("hello", "world")

Error Handling/Recovery

edit

<Describe error handling and recovery. Give examples as appropriate.>

Containers

edit

A list can be created like this:

# empty list
list = []

# list with values
list = [1, 2, 3]

Python also has 3 more built-in containers: tuples, sets, and dictionaries. More types of containers can be added through external libraries.

Algorithms

edit

<List algorithms or references to lists of algorithms available natively for this language. List ways to incorporate algorithms if they are not native to the language. Or, if not available, describe that.>

Garbage collection

edit

Garbage collection is automatic.

Physical Structure

edit

<Describe how the files, libraries, and parts are typically divided and arranged.>

Tips

edit

<Please include tips that make it easier to switch to this language from another language.>

Web References

edit

Books and Articles

edit

<List additional books and articles that may be helpful. Please include for what level reader the references are appropriate. (beginner/intermediate/advanced)>