blibSpace is a
project that contains different
concepts:
- The Hub - A space for collaborative coding.
- howto - Instructions to projects.
- edu - A course for schools to teach programming.
To explore the different concepts, just scroll down or use the navigation bar on the left.
The Hub
A space for collaborative coding. Check it out and hack away with your friends!
Sign in
Create an account
blibSpace EDU
blibSpace edu is a course that schools all over the world can use to get their students started with programming.
In the modern world everything is connected.
Programming is essential for you to be able to shape your own future and to extend your possibilities in a world in which the next generation – that I am also part of – will live in.
To get started with the course immediately I have developed a paper containing all information needed.
< paper/>
The full set of instructions
PreBlib
PreBlib is a Part of blibSpace edu. It consists of multiple stages, that make it easy to get started with programming in Python.
For further information read the paper in edu.
stage4
Arrays & Dictionaries
Variables
A variable can be considered to be a container that is used to store certain values.
vname = 6
This creates a variable with name 'vname' and value 6
There are multiple different types of variables
An integer (int) variable is used to store numbers
A string (str) variable is used to store text (text must be enclosed by speechmarks)
A boolean (bool) variable is used to store one out of two conditions: True or False (capitalised)
To find out what is in a variable it is normally printed (output to the commanline)
This can be done using the command print(variable)
Task1: create an int variable called 'number' containing the number 52
Task2: create a str variable called 'text' containing the sentence "I like cake"
output the variable 'text'
Task3: create a bool variable called 'condition' containing the value False
Calculations
You can use a programming language to make calculations.
+, -, * and / can be used to add, subtract, multiply and divide numbers
Brackets () can be used as regular
Powers such as x^2, x^3 or x to the power of 26 (x^26) can be displayed as x**26
Task1: Calculate the average of three int variables 'a', 'b' and 'c' and name it 'avg'.
Task2: Find the value of avg to the power of the sum of a, c and 27 and name it 'd'
Task3: Add "I like " and "cake" together and name the outcome 'str_sum'
To find out what is in the variable 'str_sum' output it (using 'print(variable)')
Basic Commands
Def. Debugging:
Identify and remove errors (from computer hardware or software)
You already came across one command: print()
. This outputs / prints something to the commandline
Here are a couple more basic and important commands:
input()
requires textinput from the user
eg: a = input("enter something for a:")
print()
outputs / prints something to the commandline
eg: print(a)
str()
converts another variable-type (for example integer) to a string (text)
eg: b = str(1)
int()
converts another variable-type (for example string) to an integer (number)
eg: c = int('1')
len()
gets the length of a variable
eg: d = len("length")
this will be 6
Task1: Get the age of the user and insert it into a variable called 'age_in'.
Task2: Convert 'age_in' to an integer and name the variable containing the int 'age'
Task3 (debugging): Place the following lines of code (without '#>>>') at the end of your program and find and remove the error:
text_out = "You are " + age + " years old"
print(text_out)
Arrays & Dictionaries
Basically array is a fancy name for list.
An array is a type of variable that contains multiple values instead of only one.
a = [4,78,"Some text"]
This creates an array 'a' containing the integers 4 and 78 and the string "Some text"
In programming you don't count 1, 2, 3 but you start counting from 0: 0, 1, 2, 3, ...
print( a[0] )
This prints the first value of a: 4
print( a[2] )
prints the third value of a: "Some text"
After having created the array you can still change, remove and add entries
a[0] = True
This changes the first entry to the boolean value True
a[] = "New value"
This adds "New value" to the end of an the array 'a'
del a[1]
This removes the second value: 78
Arrays can contain not only strings, True or False, integers, but also arrays:
b = [[2, 'Two'], [458, True], ['Water', [1.6, 948]], [False, 0]]
This type of array is sometimes also referred to as a Matrix
print( a[0][1] ) prints the second value of the first value: 'Two'
print( a[-2][-1][0] ) prints the first value of the last value of the second-last value: 1.6
A dictionary has two values per entry: key and value
eng_ger = {"green" : "gruen", "yellow" : "gelb", "blue" : "blau"}
In this case "gruen" is the value that maps onto its key "green".
The german translation of blue can be printed by:
print( eng_ger["blue"] )
Adding a new entry / translation can be done like follows:
eng_ger["black"] = "schwarz"
Dictionaries cannot only link string values to other string values.
They can also link to other dictionaries. For example:
translate = {"eng_ger" : eng_ger, "eng_spa" : eng_spa}
print( translate["eng_ger"]["yellow"] ) outputs "gelb"
Task1: Create a dictionary called 'cake' containing the recipe for lemon-coconut-cake (edu.blibspace.com/cake)
eg: "baking powder" - "0.5 packs"
Task2: Create another dictionary called 'cookie' containing the recipe for chocolate-chip-cookies (edu.blibspace.com/cookie)
Task3: Create a recipe booklet linking to the two dictionaries 'cake' and 'cookie'
print(recipe["cake"])
should output the ingredients and how much of them are needed
Task4: You want to bake two cakes: make a shoppinglist (array) and name it 'bday_carl'
eg: "0.5 packs of baking powder", "125g flower", "butter", ...
Conditions
if condition:
action
This is the basic structure of the If-command
The indented code 'action' is only executed if the condition is true
When wanting to check if two values / variables are equal we use '==' instead of '='
'=' is not used because its purpose is to assign something to a variable.
An example of '==' can look like follows:
a = 1
if a == 1:
print("a is 1")
When checking if something is the opposite of the condition, 'not' is put behind 'if'
if not a == 2:
print("a is not 2")
But sometimes different conditions are needed.
'!=' checks if something is not equal
'>' and '<' check if something is bigger or smaller
'in' checks if something is in a certain array or dictionary
a = input("number (in words): ")
numbers = {"one":1, "two":2, "three":3}
if a in numbers:
print(numbers[a])
age = input("Your age: ")
if age >= 16:
print("You are allowed to drink Beer")
else:
print("You will have to wait until you are allowed to drink Beer")
This piece of code asks the user to input his/her age and will then act according to the age:
If the user is 16 years or above, "You are allowed to drink Beer" is printed.
Otherwise (else:) "You will have to wait until you are allowed to drink Beer" is output
age = input("Your age: ")
if age >= 18:
print("You are allowed to drink Longdrinks")
elif age >= 16:
print("You are allowed to drink Beer")
else:
print("You will have to wait until you may consume alcoholic drinks")
In this scenario the code works like follows:
1. Get the age of the user
2. If the age is 18 or above: print "You are allowed to drink Longdrinks"
(3.) If it is not, but is 16 or above: print "You are allowed to drink Beer"
(4.) If this also is not the case: print "You will have to wait until you may consume alcoholic drinks"
Task1: Create a dictionary called 'eng_spa' containing some spanish translations of english words:
horse - caballo
dog - perro
blue - azul
shoes - zapatos
juice - zumo
book - libro
Task2: Create a program that prints the dictionary, then asks the user to input an english word:
If it is an english word that is contained in the dictionary, output the translation.
If the english word is not a key in the dictionary, ask the user for the spanish translation and add it to the dictionary. Then print the dictionary.
Loops
Loops are used to repeat algorithms (a series of conditions and commands) until something ends the loop.
There are two types of loops in python: while-loops and for-loops
A for-loop runs an algorithm for each item in a given sequence
languages = ["english", "german", "spanish", "french", "italian"]
for lang in languages:
print(lang)
This for-loop goes through every item of languages step by step (lang is the current item that the loop is at)
For each item in languages: print the item
A for-loop that counts from 0 to 10 looks like follows:
for i in range(0,10):
print(i)
For each item in a sequence ranging from 0 to 10: print the item
While loops run through a given algorithm while a certain condition is true
i = 0
while i <= 10:
print(i)
i += 1
print("end")
A variable (in this case i) has to be defined before it is used in a condition.
First i is set to be 0
While i <= 10 check if i is smaller or equal to 10.
If this is true, i is printed and increased by 1
If this is false (not true), the while loop is exited
When done with the loop the program prints "end"
This loop outputs the numbers from 0 to 10
The loop above is run until a certain condition is false.
If a while-loop is always true, it will never stop running.
A loop can be exited not only if the condition is false, but also by the command 'break'
i = 0
while True:
if i <= 10:
print(i)
i += 1
else:
print("end")
break
1. i is set to 0
2. while-loop checks if True is true. (This is always the case)
3. checks if i is smaller or equal 10
True: prints i and increases it by 1
False: prints "end" and exits infinite loop
'break' can not only be used in infinite loops but also in every other one
i = 0
while i <= 10:
print(i)
if i == 7:
break
i += 1
print("end")
# This loop counts from 0 to 10 but when i is 7 the loop exits
# After the loop is done, "end" is printed
Task1: Use the eng_spa translation-program you created in stage5 and adjust it.
The program should repeatedly ask for the user to input a word.
It should only stop when entering 'quit' as an english word.
Libraries
You can use a programming language to make calculations.
+, -, * and / can be used to add, subtract, multiply and divide numbers
Brackets () can be used as regular
Powers such as x^2, x^3 or x to the power of 26 (x^26) can be displayed as x**26
Task1: Calculate the average of three int variables 'a', 'b' and 'c' and name it 'avg'.
Task2: Find the value of avg to the power of the sum of a, c and 27 and name it 'd'
Task3: Add "I like " and "cake" together and name the outcome 'str_sum'
To find out what is in the variable 'str_sum' output it (using 'print(variable)')
Modules
When importing a library, new functions are added. These functions are also referred to as modules.
If you can't find a library suiting your needs, you can also create your own functions or modules.
This can be done using the 'def' command
def fname(arg):
actions
Let's say you want to create a function that calculates the average of the integers in a given array:
def add2(number):
finalnumber = number + 2
str() converts int to str
return( "Number + 2 = " + str(finalnumber) )
a = add2(56)
print(a)
'return' tells the funtion to return / output (not print) the value
this value is assigned to 'a'
and then printed
Task1: Create a function that calculates the average of the integers in a given array.
tipp: len() gets the length / amount of items in a variable. len([1,-8,39,66]) will be 4
Task2: Create an array called 'numbers' containing multiple integers
Task3: Find the average of the integers in 'numbers' using your function and name it 'average'