Hello, this is a markdown cell. It is made to write text. It provides very basic functionalities italics ,bolds
and other fancy stuff like links, images and mathematical formulas:
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")
In [10]:
# we can define variables on the fly and put stuff in them. Here we create a variable a
a="Hello"
print(a)
In [11]:
# What you put in variables is memorized from one cell to the other
print(a)
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
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)
In [ ]:
In [14]:
if 1+1==2: # This is a (trivial) conditional statement
print("I knew it!")
In [15]:
if i==3:
print("i equals 3")
if i==4:
print("i equals 4")
In [16]:
# We can combine loops and conditionals
for i in range(5):
if(i==3):
print("Ah!")
else:
print("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))
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)
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)
In [20]:
# To access an element of a list, specify its index:
print(l[2])
print(l[5])
In [21]:
# In case of nested lists You can have several indices
print(l[5][2])
In [22]:
# Actually, strings are a bit similar to lists, in that we can access individual letters with indices:
print("Hello"[3])
In [23]:
# Python is actually very convenient to manipulate strings and lists
# It allows addition:
print("Hello" + " world")
In [24]:
# and multiplication:
print("A"+"ta"*15)
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
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(" "))
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)
In [33]:
# You can then choose the field you find the most relevant
print(lst[6])
In [34]:
# Of course you can nest these indices too:
print(lst[6][0])
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]:
In [ ]: