In [1]:
print('Hello, world!')
In [2]:
2 + 2
Out[2]:
In [3]:
import numpy
In [4]:
a_integer = 5
In [5]:
a_float = 1.41421356237
In [6]:
a_integer + a_float
Out[6]:
In [7]:
a_number = a_integer + a_float
In [8]:
print(a_number)
In [9]:
a_string = 'How you doing, world?'
In [10]:
print(a_string)
In [11]:
a_integer + a_string
In [12]:
print(a_integer, a_string)
In [13]:
str(a_integer) + a_string
Out[13]:
In [14]:
a_float = 3.1417
In [15]:
print(a_float)
In [16]:
who
In [17]:
# This is a comment
In [18]:
"""This is a multi-
line comment"""
Out[18]:
Comments are handy to temporarily turn some lines on or off and to document Python files. In Jupyter notebooks using markdown cells gives more control.
In [19]:
9/3
Out[19]:
In [20]:
9/4.0
Out[20]:
In [21]:
9%4
Out[21]:
In [22]:
2**6
Out[22]:
In [23]:
64**(0.5)
Out[23]:
In [24]:
a = True
b = False
In [25]:
2 == 2.0
Out[25]:
In [26]:
2 != 2.000001
Out[26]:
In [27]:
6 > 10
Out[27]:
In [28]:
if 6 > 4:
print('Six is greater than four!')
In [29]:
if 6 > 10:
print('Six is greater than ten!')
else:
print('Six is not greater than ten!')
In [30]:
a = 25
if a%2 == 0:
print('This number is even!')
elif a%3 == 0:
print('This number is divisable by three!')
elif a%5 == 0:
print('This number is divisable by five!')
else:
print(str(a) + ' is not divisable by 2, 3 or 5!')
In [31]:
b = 0
while b < 5:
print(b)
b = b + 1
In [32]:
b
Out[32]:
In [33]:
while True:
pass
In [34]:
a = 25
b = 1
c = 2
while b != 0:
b = a%c
if b == 0:
print(str(a) + ' is divisable by ' + str(c))
else:
c += 1
Exercise: Can you rework this code so that you can test if a number is prime (so only it and 1 divides it)
In [35]:
a = 29
b = 1
c = 2
while b != 0:
b = a%c
if a == c:
print(str(a) + ' is a prime number.')
elif b == 0:
print(str(a) + ' is divisable by ' + str(c))
else:
c += 1