2 Introduction to Python

A series of commands to introduce beginners to Python.

Does it work?


In [1]:
print('Hello, world!')


Hello, world!

In [2]:
2 + 2


Out[2]:
4

In [3]:
import numpy

Define Variables


In [4]:
a_integer = 5

In [5]:
a_float = 1.41421356237

In [6]:
a_integer + a_float


Out[6]:
6.41421356237

In [7]:
a_number = a_integer + a_float

In [8]:
print(a_number)


6.41421356237

In [9]:
a_string = 'How you doing, world?'

In [10]:
print(a_string)


How you doing, world?

In [11]:
a_integer + a_string


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-bd0cee6f46e6> in <module>()
----> 1 a_integer + a_string

TypeError: unsupported operand type(s) for +: 'int' and 'str'

In [12]:
print(a_integer, a_string)


5 How you doing, world?

In [13]:
str(a_integer) + a_string


Out[13]:
'5How you doing, world?'

In [14]:
a_float = 3.1417

In [15]:
print(a_float)


3.1417

In [16]:
who


a_float	 a_integer	 a_number	 a_string	 numpy	 

In [17]:
# This is a comment

In [18]:
"""This is a multi-
line comment"""


Out[18]:
'This is a multi-\nline comment'

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.

Mathematical Operations


In [19]:
9/3


Out[19]:
3.0

In [20]:
9/4.0


Out[20]:
2.25

In [21]:
9%4


Out[21]:
1

In [22]:
2**6


Out[22]:
64

In [23]:
64**(0.5)


Out[23]:
8.0

Logical Operations


In [24]:
a = True
b = False

In [25]:
2 == 2.0


Out[25]:
True

In [26]:
2 != 2.000001


Out[26]:
True

In [27]:
6 > 10


Out[27]:
False

In [28]:
if 6 > 4:
    print('Six is greater than four!')


Six is greater than four!

In [29]:
if 6 > 10:
    print('Six is greater than ten!')
else:
    print('Six is not greater than ten!')


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


This number is divisable by five!

In [31]:
b = 0
while b < 5:
    print(b)
    b = b + 1


0
1
2
3
4

In [32]:
b


Out[32]:
5

In [33]:
while True:
    pass


---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-33-b7133701d76c> in <module>()
      1 while True:
----> 2     pass

KeyboardInterrupt: 

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


25 is divisable by 5

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


29 is a prime number.