In [5]:
print("Hello, world")


Hello, world

In [6]:
# We can also create comments
print(1 + 1)


2

In [3]:
# Create string
template = "Hello %s"
print(template % "Sponge Bob")


Hello Sponge Bob

In [4]:
template = "Hello, %s. %i + %i = %i."
print(template % ("Sponge Bob", 1, 2, 3))


Hello, Sponge Bob. 1 + 2 = 3.

In [5]:
template = "Hello, %s. %i + %i = "
print(template % ("Sponge Bob", 1, 2))


Hello, Sponge Bob. 1 + 2 = 

In [7]:
# Check the structure: tuple means 'should not be changed anymore'
type(("Hello, %s. %i + %i = %i."))


Out[7]:
str

In [10]:
extra ="""bla bla bla bla \
    test 2 \
blq"""
type(extra)


Out[10]:
str

In [13]:
# Anything in Python is an object (split object method)
raw = "Hello, I'm Mr. Bumble"
print(raw)
parts = raw.split(" ")
print(parts)


Hello, I'm Mr. Bumble
['Hello,', "I'm", 'Mr.', 'Bumble']

In [14]:
# [] brackets point to list type (= ordered).
type(parts)


Out[14]:
list

In [ ]:
# Use tab to get to help
parts.

In [19]:
name = input("What is your name? ")
print("Hello, %s. " % name)


What is your name? sanne
Hello, sanne. 

Convert an integer to char and back


In [20]:
age="40"
print(int(age)+1)


41

Dictionaries

dictionary is a data structure for storing key-value pairs vs lists ... numpy list and dictionaries are what you need. {} -> dictionary ;


In [25]:
person=dict(name="semga", age=45, weight=95)
print("%s is %i and weighs at %i" % (person["name"], person["age"], person["weight"]))


semga is 45 and weighs at 95

In [26]:
person={0:"segma", 1:45, 2:95}
person[0]


Out[26]:
'segma'

In [32]:
print(person.items())


dict_items([(0, 'segma'), (1, 45), (2, 95)])

In [33]:
print(person.keys())


dict_keys([0, 1, 2])

Conditionals

if statements


In [35]:
a=10
b=15

In [38]:
if a < b:
    print(" a < b ")
elif a > b:
    print(" a > b ")
else:
    print(" a == b")
    
    
    
    
print("done")


 a < b 
done

versus


In [39]:
if a < b:
    print(" a < b ")
elif a > b:
    print(" a > b ")
else:
    print(" a == b")
    
    
    
    
    print("done")


 a < b 

for loop


In [42]:
for person in ["han", "freya", "bieke", "jasper"]:
    print("Hello there, %s , how are you doing?" % person)


Hello there, han , how are you doing?
Hello there, freya , how are you doing?
Hello there, bieke , how are you doing?
Hello there, jasper , how are you doing?

In [46]:
cnt=0
for person in ["han", "freya", "bieke", "jasper"]:
    print("[%i] Hello there, %s , how are you doing?" %(cnt+1, person))
    cnt+=1

print(cnt)


[1] Hello there, han , how are you doing?
[2] Hello there, freya , how are you doing?
[3] Hello there, bieke , how are you doing?
[4] Hello there, jasper , how are you doing?
4

In [48]:
for ind, person in enumerate(["han", "freya", "bieke", "jasper"]):
    print("[%i] Hello there, %s , how are you doing?" %(ind + 1 , person))


[1] Hello there, han , how are you doing?
[2] Hello there, freya , how are you doing?
[3] Hello there, bieke , how are you doing?
[4] Hello there, jasper , how are you doing?

In [53]:
cnt=0
persons=["han", "freya", "bieke", "jasper"]
while cnt < len(persons) :
    print("[%i] Hello there, %s , how are you doing?" %(cnt+1, persons[cnt]))
    cnt+=1

print(cnt)


[1] Hello there, han , how are you doing?
[2] Hello there, freya , how are you doing?
[3] Hello there, bieke , how are you doing?
[4] Hello there, jasper , how are you doing?
4

functions


In [51]:
def addition(a,b):
    output= a+b
    return output

addition(100, 304)
addition??

In [55]:
class my_class:
    def addition(a,b):
        output= a+b
        return output

In [60]:
c=my_class()

my_class.addition(100,200)


Out[60]:
300

In [64]:
def print_banner(times=1):
    for _ in range(times):
        print("+" * 11)
        print("fMRI roels !")
        print("+" * 11)
        print()
        
print_banner()
print("extra")
print_banner(2)
print("extra")
# explicit versus non-explicit referencing to arguments
print_banner(times=2)


+++++++++++
fMRI roels !
+++++++++++

extra
+++++++++++
fMRI roels !
+++++++++++

+++++++++++
fMRI roels !
+++++++++++

extra
+++++++++++
fMRI roels !
+++++++++++

+++++++++++
fMRI roels !
+++++++++++


In [65]:
def square(x):
    """Computes the square of the input."""
    return x ** 2

[square(x) for x in range(10)]


Out[65]:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

In [81]:
# see also
tst=map(square, range(10))

In [82]:
print(*tst, sep='\n')


0
1
4
9
16
25
36
49
64
81

In [89]:
from time import sleep, localtime
print(localtime())
print("_" * 80)
print("sleeping for a while")
print(".zz.ZZz")
sleep(5)
print("i'm back")


time.struct_time(tm_year=2016, tm_mon=6, tm_mday=6, tm_hour=11, tm_min=41, tm_sec=25, tm_wday=0, tm_yday=158, tm_isdst=1)
________________________________________________________________________________
sleeping for a while
.zz.ZZz
i'm back

OK time for the serious business. GLOBBING files.


In [90]:
import glob

In [93]:
# can invoke REG-expressions
glob.glob("*.py")
glob.glob("*.ipynb")


Out[93]:
['crashCourse.ipynb']

In [11]:
import numpy as np
import matplotlib.pyplot as plt

In [10]:
x=np.linspace(-1,2,num=500)
y= .5 * x ** 2 - x
plt.plot(x,y)

plt.title("A quadratic curve")
plt.xlabel("x")
plt.ylabel("y")
plt.show()

In [12]:
import matplotlib.pyplot as plt
from numpy.random import rand


for color in ['red', 'green', 'blue']:
    n = 750
    x, y = rand(2, n)
    scale = 200.0 * rand(n)
    plt.scatter(x, y, c=color, s=scale, label=color,
                alpha=0.3, edgecolors='none')

plt.legend()
plt.grid(True)

plt.show()

In [ ]:


In [ ]: