A short summary of the functions, operators, and commands we discuss in class.

Terminal

CommandDescriptionExample
pwdPrints the current directory.pwd
lsLists the contents of the current directory.ls
cdChanges the current directory. Can be called with the name of a subdirectory of the current folder or .. to go one folder up. (You can also use absolute paths or longer relative path.)cd .. (goes one folder up)
cd test (goes into the folder test that is located in the current directory)
catPrints the contents of a file. Should be called with the name of the file to print.cat test.py
mkdirCreates a new directory. Takes the name of the new folder as an argument.mkdir myNewFolder
pythonIf called without a parameter, the python shell is started. If called with a file name of a python script, the script is executed.python (Starts the python shell.)
python myScript.py (Executes the script myScript.py.)

Python

Check out the Python Standard Library for more functions and methods.

Operators

OperatorDescriptionExample
+Adding two numbers, or concatenating two strings.4 + 5
"Hello " + "World!"
-Subtracting two numbers.6 - 5
*Multiplying two numbers or 'multiplying' a string.6 * 5
"Hello" * 4
/Dividing two numbers.6 / 3
%Calculates modulo of two numbers. Divides left number by right number and returns remainder6 % 3 (Returns 0, as 3 * 2 + 0 = 6)
8 % 3 (Returns 2, as 3 * 2 + 2 = 8)
==Compares two values and returns True if the values are equal, otherwise False."Hello" == "World" (Returns False)
5 == 5 (Returns True)
!=Compares two values and returns True if the values are not equal, otherwise False."Hello" != "World" (Returns True)
5 != 5 (Returns False)
>Compares two values and returns True if the the first value is greater than the second, otherwise False.9 > 8
<Compares two values and returns True if the the first value is smaller than the second, otherwise False.5 < 8
>=Compares two values and returns True if the the first value is greater or equal than the second, otherwise False.8 >= 8 (Returns True)
<=Compares two values and returns True if the the first value is smaller or equal to the second, otherwise False.5 <= 8 (Returns True)
=Assigning a value to a variable.a = 4 (The variable a will now have the value 4.)

Functions

FunctionDescriptionExample
print()Writing something to the python shell or terminal.print("Hello World!")
type()Returns the type of a value or variable.type("Hello World!")
type(5)
type(a)
(Where a could be 1.34)
input()Prints the text that is given to the function, waits for the user to enter some text and hit enter, then returns the input of the user.input("What's your name? ")
float()Tries to turn whatever argument is given to the function into a float value.float("4.5")
int()Tries to turn whatever argument is given to the function into an integer value.int("4.5")
str()Tries to turn whatever argument is given to the function into a string value.str(4.5)
randint()Lets you generate a randome number in a range you provide.from random import randint
randint(1, 5)

Statements

Keyword/StatementDescriptionExample
#Use this character if you want to make a comment on a piece of code. Python will ignore whatever you write after the # character and continue with the next line.# define variable
a = 5
# now add one
a = a + 1
if condition:
   if block
elif condition:
   elif block
else:
   else block
Allows you to execute different code depending on a condition.if a < 5:
   print("a is smaller than 5")
elif a > 5:
   print("a is greater than 5")
else:
   print("a equals 5")
while condition:
   while block
Lets you repeat the execution of a specific code block until a the specified condition is no longer true.x = 1
while x < 5:
   print("x is " + x)
   x = x + 1
for element in elements:
   for block
Lets you repeat the execution of a specific code block for each element in a list.numbers = [1,3,5]
for num in numbers:
   print("number is " + num)
defUse this keyword to define a function.def myFunc():
   print("Hello World!")
returnUse this keyword to return something from a function.def myFunc():
   return 5
importAllows you to use functions from a different Python file (module).import myModule
# now you can call the function
myModule.myFunc()
from module import partAllows you to use specific functions from a different Python file (module).from myModule import myFunc
# now you can call the function
myFunc()

Methods

MethodDescriptionExample
append()Is a method of lists. Appends the value you provide to the list it is called on.my_list = [1, 2, 3]
my_list.append(4)
(Afterwards the list looks like: [1, 2, 3, 4])
list[i]Is a method of lists. Returns the ith element of the list listmy_list = [1, 2, 3]
my_list[2]
(Returns 3. Remember that indices start at 0.)
keys()Is a method of dictionaries. Returns all keys of the dictionary.my_dict = {'one':1, 'two':2}
my_dict.keys()
(Returns: 'one, 'two')
values()Is a method of dictionaries. Returns all values of the dictionary.my_dict = {'one':1, 'two':2}
my_dict.values()
(Returns: 1, 2)
items()Is a method of dictionaries. Returns all tuples of the dictionary in a list.my_dict = {'one':1, 'two':2}
my_dict.items()
(Returns: [('two', 2), ('one', 1)])
get()Is a method of dictionaries. Returns all the value for the provided key or None if the key doesn't exist. If a default value is provided, it returns that one instead of none.my_dict = {'one':1, 'two':2}
my_dict.get('one')
(Returns: 1)
my_dict.get('three', 5) (Returns 5)