Hello, this is a markdown cell. It is made to write text. It provides very basic functionalities italics ,bolds

  • list
  • of
  • items

and other fancy stuff like links, images and mathematical formulas:

link

$$c = \sqrt{a^2 + b^2} $$

In [9]:
# This is a python cell
# <- lines that start with a # are ignored and are called comments
# Python cells are executed by pressing shift-enter
print("Hello")


Hello

In [10]:
# we can define variables on the fly and put stuff in them. Here we create a variable a
a="Hello"
print(a)


Hello

In [11]:
# What you put in variables is memorized from one cell to the other
print(a)


Hello

In [12]:
for i in range(5):  # <- This is called a loop. Range(5) indicates we will execute it 5 times
    print("Hi")     # <- These two lines will be executed 5 times. Because they starts with an indentation (a 'tab'), 
    print("Hello")  #    it means they are part of the loop
print("Everybody !")  # <- this one does not have indentation: the loop block is finished


Hi
Hello
Hi
Hello
Hi
Hello
Hi
Hello
Hi
Hello
Everybody !

In [13]:
# Actually the first line of the loop means that we will put the values 
# from 0 to 4 into a variable named 'i' as we execute the loop.
# We can also use the content of this variable:
for i in range(5):
    print(i)


0
1
2
3
4

In [ ]:


In [14]:
if 1+1==2:              # This is a (trivial) conditional statement
    print("I knew it!")


I knew it!

In [15]:
if i==3:
    print("i equals 3")
if i==4:
    print("i equals 4")


i equals 4

In [16]:
# We can combine loops and conditionals

for i in range(5):
    if(i==3):
        print("Ah!")
    else:
        print("Ha!")


Ha!
Ha!
Ha!
Ah!
Ha!

In [17]:
# There are many types in python but we are concerned with only 4 basic types now: strings, integers, floats and lists
# you can check the type of something with the function type()

print(type(12))
print(type(12.5))
print(type("Hello"))
print(type([]))
print("")          # This prints an empty line
print(type(i))
print(type(a))


<type 'int'>
<type 'float'>
<type 'str'>
<type 'list'>

<type 'int'>
<type 'str'>

In [18]:
# You can also define lists in python. It is a convenient type that holds a list of stuff: integers, float, strings 
# or even other lists
l = [12, i, "Hello"]
print(l)


[12, 4, 'Hello']

In [19]:
# You can append content to a list:
l.append(8)
print(l)
l.append(a)
print(l)
l.append([5,1,"Everyone"])
print(l)


[12, 4, 'Hello', 8]
[12, 4, 'Hello', 8, 'Hello']
[12, 4, 'Hello', 8, 'Hello', [5, 1, 'Everyone']]

In [20]:
# To access an element of a list, specify its index:
print(l[2])
print(l[5])


Hello
[5, 1, 'Everyone']

In [21]:
# In case of nested lists You can have several indices
print(l[5][2])


Everyone

In [22]:
# Actually, strings are a bit similar to lists, in that we can access individual letters with indices:
print("Hello"[3])


l

More on indices and strings


In [23]:
# Python is actually very convenient to manipulate strings and lists
# It allows addition:
print("Hello" + " world")


Hello world

In [24]:
# and multiplication:
print("A"+"ta"*15)


Atatatatatatatatatatatatatatata

In [30]:
# It has a concise syntax to select a substring:
a = "Hello world"
print(a[1:10]) # print characters from index 1 to 10


ello worl

In [31]:
# One of the most useful features is the ability to split a string in an array:
a="I think this is going to be cut in many places"
# We specify we want to cut around the whitespace characters
print(a.split(" "))


['I', 'think', 'this', 'is', 'going', 'to', 'be', 'cut', 'in', 'many', 'places']

In [32]:
# It is useful when you receive data in a CSV format for instance:
line = "123,5,8,0,0,2012-01-01,Taro,Johnson"
lst = line.split(",")
print(lst)


['123', '5', '8', '0', '0', '2012-01-01', 'Taro', 'Johnson']

In [33]:
# You can then choose the field you find the most relevant 
print(lst[6])


Taro

In [34]:
# Of course you can nest these indices too:
print(lst[6][0])


T

In [35]:
# Oh, and by the way, if you just type an expression, ipython assumes you want to print() it:
lst[6][0]


Out[35]:
'T'

In [ ]: