Python Reference

Print Return to index

Table of Contents

What is Programming?

Programming is the process of writing instructions that a computer uses to perform tasks. These instructions are written in a very specific format called code. When a programmer writes a program in code, the computer executes the code one line at a time until it reaches the end. With good programming skills and knowledge, you can write code that can create almost anything imaginable.

Computers can only understand problems in a very specific way. In order to produce a program that a computer will be able to understand and run, a problem needs to be broken down into many small steps. In turn, these steps must be written in code of the appropriate language and in the explicit order in which the computer should complete them.

There are many different types of programming languages that consist of particular and unique terms, phrases and conventions. The set of rules that define the combination of symbols that are used within a certain language is called that language’s syntax. Every language, including English, has a different syntax and, therefore, must be written differently to accomplish correct form. For example, in English, we capitalize proper nouns and add punctuation to the ends of our sentences as a part of the syntax of the language. There are different and specific syntax rules for all of the different types of programming languages. Some of the most popular programming languages used today are Java, Python, and C.

Programming Terms

Boolean

A variable data type that stores a value that is either True or False

Commenting

Comments are sections of code that the computer does not execute. These sections are made for the programmer to explain portions of code in order to make his or her program more readable to himself or herself and to other people. In Python, comments are denoted with #. Any text after a # is considered to be a comment. Multi-line comments act the same way as single line comments, but must have three single quote marks at the beginning and end of the comment.

Example:

# This is a comment

'''
this is a multi-line comment
it extends across multiple lines
'''
		
Conditional

A type of statement that checks to see if an argument is True or False before executing some amount of code. Types of conditionals are if-statements, elif-statements and else-statements. For example,

if x < 0:
	println “Negative.”
elif x == 0:
	println  “Neither positive nor negative”
else:
	println “Positive.”
		

This code reads as: if the variable x (defined elsewhere in code) is less than zero, print negative, however, if the parameter x equals zero, it is neither positive nor negative. Otherwise, x is positive.

Dictionary

A collection of key-value pairs that maps from keys to values.

Example:

{‘hello’ : ‘hola’, ‘goodbye’ : ‘adios’}

hello and goodbye are keys whereas hola and adios are values.

Float

A Python data type that stores floating-point numbers. Floating-point numbers have decimal components.

Examples

0.5, 2.0, 6.67
Function

A named sequence of statements that performs some useful operation. Functions may or may not take in parameters. Each call you make to the EarSketch API is called a function call. This calls for the code that defines that function to execute.


#defining a function
def myFunction(string):
	println(string)

#calling a function
myFunction("hello world")

#the result is that 'hello world' gets printed to the Reaper console.
		
Import

A statement which permits functions and variable defined in a Python script to be brought into the environment of another script. For instance, we use the EarSketch API and, therefore, we must import that script into each project in the following way:

from earsketch import *
Index

An integer variable or value that indicates an element of a list. The first element of a list has an index of zero.

# an example list of colors
list = ["red", "blue", "green", "yellow"]

#call the value at index 2
list[2]

'green'

#assign a new value to index 2
list[2] = "orange"


#print index 2 again
list[2]

'orange'
		
int

A Python data type that holds positive and negative whole numbers.

Examples

44, -50, 5969
Keyword

A reserved word that is used by Python to parse the program; you cannot use keywords as variable names. (A list of Python keywords are available after this section)

List

A named collection of objects, where each object is identified by an index. The items within a list can be of any data type (e.g. int, str, float).

Example:

list = [1, 1.5, 2, 2.5, 3]
		
  • the value of list[0] is 1
  • the value of list[1] is 1.5
  • the value of list[4] is 3

After being created, it is possible to change elements of a list by reassigning the different indices to different values.

For example, if you have the list above, you could change the first element at index 0 in the following way:

list[0] = 5

Now the first element is no longer 1. It is 5.

Loop

A statement or group of statements that execute repeatedly until a terminating condition is satisfied. There are two types of loops: a for loop and a while loop.

For loop:

for i in range(1,10):
	print “Hello”
		

The above code prints “Hello” once for each number in the range between 1 and 9. For loops are useful if there is a specific number of items that you wish to iterate over.

While loop:

while n > 0:
	print n
	n = n-1
		

The above code first checks to see if n is greater than zero. If this is True, n is printed and then decremented by 1. The loop continues to execute until the condition that n is greater than zero is False. While loops are useful if you do not have a specific set of items to iterate through.

In any situation, a for loop can be written as a while loop and vice versa. It is ultimately up to the programmer to choose which will work best for any given situation.

Parameter

A name used inside a function to refer to the value passed as an argument. For example, in the function setTempo(tempo), tempo is the parameter.

Range

The range function is a function that is built into Python. The key to using the range function is to remember that the beginning point is inclusive and the ending point is exclusive. By this, we mean that if you wish for a range of numbers between 1 and 10, the corresponding range function would be range(1,11). The 1 will be included (inclusive) and the 11 will be excluded (exclusive). An optional third parameter that can be passed into the range function signifies the step used. For instance, if you wanted your values to be only every third number in a range, you would add a 3 as the third parameter.

Examples:

		
range(10) will be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

range(10,15) will be [10, 11, 12, 13, 14]

range(0,10,2) will be [0, 2, 4, 6, 8]
		
return

The return keyword signals to the program that the end of a function has been reached. When a return statement is executed, the currently running function will terminate. The function can simply return with no output, or it can return a value.

For example:

def add(x,y):
	result = x+y
	return result
		

The above code will return the sum of x and y.

str

A Python data type that holds characters. A character can be a letter, number, space, punctuation or symbol.

Examples:

myString = “This is a string!”
myOtherString = “$tring...”
		
tuple

A data type that contains a set of elements of any type, like a list.

Python Keywords

These are terms that are reserved by the Python interpreter. This means that they can't be used as variable or function names in your script, because Python expects them to mean particular things already.

andasassertclassbreakcontinue
defdelelifelseexceptexec
finallyforfromglobalifimport
inislambdanotorpass
printraisereturntrywhilewith
yield

Python Operators and Boolean Expressions

x + yThe sum of x and y
x – yThe difference of x and y
x * yThe product of x and y
x / yThe quotient of x and y
x % yThe remainder of x / y
x ** yx to the power of y
<Less than
<=Less than or equal
>Greater than
>= Greater than or equal
==Equal
!=Not equal