In [1]:
print('Hello World!')
In [2]:
import __hello__
In [3]:
import this
In [4]:
love = this
In [5]:
this is love
Out[5]:
In [6]:
love is True
Out[6]:
In [7]:
love is False
Out[7]:
In [8]:
love is not True or False
Out[8]:
In [9]:
love is love
Out[9]:
In [10]:
4 + 5 # A single-line comment.
Out[10]:
In [11]:
4 + 5 # A
# multi
# line
# comment.
Out[11]:
In [12]:
# Commenting multiple lines of code: the proper way.
# 4 + 5
# 9 + 6
# 1 + 3
# 2 + 7
In [13]:
# Commenting multiple lines of code: the lazy way.
"""
4 + 5
9 + 6
1 + 3
2 + 7
"""
Out[13]:
In [14]:
# As you can see, triple quotes are actually strings.
"""
Try not to use triple quotes for commenting your code.
Triple quotes are best used for code documentation. We'll
see how to do that when we talk about functions and modules.
"""
# Also, learn how to use a good text editor like emacs or vim.
Out[14]:
In [15]:
x = 42
y = 3.14
z = 'Hi!'
x = 'Hello!'
z = 100
In [16]:
print(x, y, z) # Let's print their values
In [17]:
import math
π = math.pi
r = 5
area = π * r**2
print(area)
In [18]:
m = 2
M = 8
µ = (M * m) / (M + m)
print(µ)
In [19]:
h = 6.62607004e-34 # Planck constant: m^2 kg s^-1
ħ = h / (2 * π)
print(ħ)
In [20]:
# Varibles can be assigned with the same value...
x = y = z = 5
y += 10
print(x)
print(y)
print(z)
In [21]:
# ...or with multiple values of any type
x, y, z = 42, 3.14, 1+5j
b, s = True, "Hello!"
print(b, '\t\t', 'bool')
print(x, '\t\t', 'int')
print(y, '\t\t', 'float')
print(z, '\t\t', 'complex')
print(s, '\t\t', 'str')
In [22]:
a, b = 42, 3.14
print(a, b)
b, a = a, b # swap values
print(a, b)
In [23]:
# It works with 3 variables too! Or 4, or 5, or 6...
a, b, c = 1, 2, 3
print(a, b, c)
b, c, a = a, b, c # swap values
print(a, b, c)
In [24]:
x = 5
y = x + 4
x = 2
print(x, y)
In [25]:
x, y = y + 3, x + 4
print(x, y)
In [26]:
a, b, c, d = 2, 3, 0, 1
c, d, a, b = d + 7, b, c + 5, a
print(a, b, c, d)
In [27]:
42 # Just a number
Out[27]:
In [28]:
type(42) # What's the type of 42?
Out[28]:
In [29]:
print(type(42)) # Let's print it!
In [30]:
print(dir(42)) # WTF?!
In [31]:
# import antigravity