In [5]:
print("Hello, world")
In [6]:
# We can also create comments
print(1 + 1)
In [3]:
# Create string
template = "Hello %s"
print(template % "Sponge Bob")
In [4]:
template = "Hello, %s. %i + %i = %i."
print(template % ("Sponge Bob", 1, 2, 3))
In [5]:
template = "Hello, %s. %i + %i = "
print(template % ("Sponge Bob", 1, 2))
In [7]:
# Check the structure: tuple means 'should not be changed anymore'
type(("Hello, %s. %i + %i = %i."))
Out[7]:
In [10]:
extra ="""bla bla bla bla \
test 2 \
blq"""
type(extra)
Out[10]:
In [13]:
# Anything in Python is an object (split object method)
raw = "Hello, I'm Mr. Bumble"
print(raw)
parts = raw.split(" ")
print(parts)
In [14]:
# [] brackets point to list type (= ordered).
type(parts)
Out[14]:
In [ ]:
# Use tab to get to help
parts.
In [19]:
name = input("What is your name? ")
print("Hello, %s. " % name)
Convert an integer to char and back
In [20]:
age="40"
print(int(age)+1)
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"]))
In [26]:
person={0:"segma", 1:45, 2:95}
person[0]
Out[26]:
In [32]:
print(person.items())
In [33]:
print(person.keys())
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")
versus
In [39]:
if a < b:
print(" a < b ")
elif a > b:
print(" a > b ")
else:
print(" a == b")
print("done")
In [42]:
for person in ["han", "freya", "bieke", "jasper"]:
print("Hello there, %s , how are you doing?" % person)
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)
In [48]:
for ind, person in enumerate(["han", "freya", "bieke", "jasper"]):
print("[%i] Hello there, %s , how are you doing?" %(ind + 1 , person))
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)
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]:
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)
In [65]:
def square(x):
"""Computes the square of the input."""
return x ** 2
[square(x) for x in range(10)]
Out[65]:
In [81]:
# see also
tst=map(square, range(10))
In [82]:
print(*tst, sep='\n')
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")
In [90]:
import glob
In [93]:
# can invoke REG-expressions
glob.glob("*.py")
glob.glob("*.ipynb")
Out[93]:
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 [ ]: