Getting Started

Hello World!


In [1]:
print('Hello World!')


Hello World!

In [2]:
import __hello__


Hello world!

The Zen of Python


In [3]:
import this


The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Python code looks almost like English


In [4]:
love = this

In [5]:
this is love


Out[5]:
True

In [6]:
love is True


Out[6]:
False

In [7]:
love is False


Out[7]:
False

In [8]:
love is not True or False


Out[8]:
True

In [9]:
love is love


Out[9]:
True

Commenting your code


In [10]:
4 + 5  # A single-line comment.


Out[10]:
9

In [11]:
4 + 5  # A
       # multi
       # line
       # comment.


Out[11]:
9

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]:
'\n4 + 5\n9 + 6\n1 + 3\n2 + 7\n'

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]:
"\nTry not to use triple quotes for commenting your code.\nTriple quotes are best used for code documentation. We'll\nsee how to do that when we talk about functions and modules.\n"

Whetting your appetite

Variable Assignments

In languages like C/C++/Fortran a variable name has a data type, p.ex., float, int, etc.

In Python, variable names have no type. The data type belongs to the value. A variable is like an alias to a value. It just points to a value. So, you can redefine it whenever you want.


In [15]:
x = 42
y = 3.14
z = 'Hi!'

x = 'Hello!'
z = 100

In [16]:
print(x, y, z)  # Let's print their values


Hello! 3.14 100

Naming your variables can be fun - Python 3 only :-)


In [17]:
import math

π = math.pi
r = 5
area = π * r**2

print(area)


78.53981633974483

In [18]:
m = 2
M = 8

µ = (M * m) / (M + m)

print(µ)


1.6

In [19]:
h = 6.62607004e-34  # Planck constant: m^2 kg s^-1

ħ = h / (2 * π)

print(ħ)


1.0545718001391127e-34

Multiple Assignments


In [20]:
# Varibles can be assigned with the same value...

x = y = z = 5

y += 10

print(x)
print(y)
print(z)


5
15
5

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')


True 		 bool
42 		 int
3.14 		 float
(1+5j) 		 complex
Hello! 		 str

Swapping values of variables


In [22]:
a, b = 42, 3.14
print(a, b)

b, a = a, b  # swap values
print(a, b)


42 3.14
3.14 42

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)


1 2 3
3 1 2

What will be the output of the following code?


In [24]:
x = 5
y = x + 4
x = 2

print(x, y)


2 9

In [25]:
x, y = y + 3, x + 4

print(x, y)


12 6

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)


5 2 8 3

Blowing your mind!


In [27]:
42  # Just a number


Out[27]:
42

In [28]:
type(42)  # What's the type of 42?


Out[28]:
int

In [29]:
print(type(42))  # Let's print it!


<class 'int'>
Wait a minute!

Is int a class?

Is 42 an object?

Whaaat?! :o

Yep! In Python everything is an object!


In [30]:
print(dir(42))  # WTF?!


['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']

Keep calm! Soon you'll be flying too!


In [31]:
# import antigravity