Tutorial Brief

This tutorial is an introduction to Python 3. This should give you the set of pythonic skills that you will need to proceed with this tutorial series.

If you don't have the Jupyter installed, shame on you. No just kidding you can follow this tutorial using an online jupyter service:

https://try.jupyter.org/

Cell Input and Output


In [ ]:
1+2

In [ ]:
1+1
1+2

The print function

Notice: print is a function in Python 3. You should use parentheses around your parameter.


In [ ]:
print(1+2)

Variables

There are many variable types in Python 3. Here is a list of the most common types:

Numerical Types:

  • bool (Boolean)
  • int (Integer/Long)
  • float
  • complex

Notice: In Python 3 integer represents integer and long. Because there is no more long data type, you will not get L at the end of long integers.


In [ ]:
a = 4
b = 1.5
c = 121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212
d = 1j
e = 1/3
f = True

In [ ]:
a+b

In [ ]:
a*c

In [ ]:
(b+d)*a

In [ ]:
a+f

In [ ]:
type(1.5)

Other Value Types:

  • str (String)
  • list (Ordered Array)
  • tuple (Ordered Immutable Array)
  • dict (Unordered list of keys and values)

In [ ]:
my_name = "Roshan"
print(my_name)

In [ ]:
my_list = [1,2,3,4,5]
my_list

In [ ]:
my_list + [6]

In [ ]:
my_list

In [ ]:
my_list += [6,7,8]
my_list

In [ ]:
my_list.append(9)
my_list

In [ ]:
my_tuple = (1,2,3)
my_tuple

In [ ]:
my_tuple + (4,5,6)

In [ ]:
my_dict = {"name":"Roshan", "credit":100}
my_dict

In [ ]:
my_dict["name"]

In [ ]:
my_dict["level"] = 4
my_dict

In [ ]:
my_dict.values()

In [ ]:
my_dict.keys()

Selecting / Slicing

Use len function to measure the length of a list.


In [ ]:
len(my_list)

To access a single value in a list use this syntax:

list_name[index]

In [ ]:
my_list[0]

To select multiple value from a list use this syntax:

index[start:end:step]

In [ ]:
my_list[1:2]

In [ ]:
my_list[:3]

In [ ]:
my_list[3:]

Notice: negative index selected from the end of the list


In [ ]:
my_list[-1]

In [ ]:
my_list[-2]

You can use negative indexing in selecting multiple values.


In [ ]:
my_list[-2:]

In [ ]:
my_list[:-2]

In [ ]:
my_list[3:-1]

The third location in the index is the step. If the step is negative the the list is returned in descending order.


In [ ]:
my_list[::2]

In [ ]:
my_list[3::2]

In [ ]:
my_list[::-1]

Working with Strings

You can select from a string like a list suing this syntax:

my_string[star:end:step]

In [ ]:
my_name

In [ ]:
my_name[0]

Notice: You can also use negative indexing.


In [ ]:
my_name[:2]

Unicode

Notice: You can use unicode inside your string variables. Unlike Python 2, no need to use u"" to use unicode.


In [ ]:
# Sorted by most spoken languages in order
divide_by_zero = {"zho":"你不能除以零",
                  "eng":"You cannot divide by zero",
                  "esp":"No se puede dividir por cero",
                  "hin":"आप शून्य से विभाजित नहीं किया जा सकता \u2248",
                  "arb":"لا يمكن القسمة على صفر"}

print(divide_by_zero["hin"])
type(divide_by_zero["hin"])

String Formatting

You can use this syntax to format a string:

some_variable = 50
x = "Value: %s" % some_variable
print(x) # Value: 50

In [ ]:
first_name = "Roshan"
last_name = "Rush"
formatted_name = "%s, %s." % (last_name, first_name[0])
print(formatted_name)

Other formatters could be used to format numbers:


In [ ]:
print("π ≈ %.2f" % 3.14159)

In [ ]:
homeworks = 15.75
midterm = 22
final = 51
total = homeworks + midterm + final

print("Homeworks: %.2f\nMid-term: %.2f\nFinal: %.2f\nTotal: %.2f/100" % (homeworks, midterm, final, total))

Using format(*args, **kwargs) function


In [ ]:
url = "http://{language}.wikipedia.org/"
url = url.format(language="en")
url

Mathematics


In [ ]:
1+1

In [ ]:
4-5

Notice: The default behavior of division in Python 3 is float division. To use integer division like Python 2, use //


In [ ]:
14/5

In [ ]:
14//5

In [ ]:
2*5

To raise a number to any power use down asterisk **. To represent $a^{n}$:

a**n

In [ ]:
2**3

To calculate the remainder (modulo operator) use %. To represent $a \mod b = r$:

a % b # Returns r

In [ ]:
10 % 3

You can

You can use the math library to access a varaity of tools for algebra and geometry. To import a library, you can use one of these syntaxes:

import library_name
import library_name as alias
from module_name import some_class

In [ ]:
import math

In [ ]:
n=52
k=1
math.factorial(n) / (math.factorial(k) * math.factorial(n-k))

Loops


In [ ]:
for counter in [1,2,3,4]:
    print(counter)

range

In Python 3 range is a data type that generates a list of numbers.

range(stop)
range(start,stop[ ,step])

Notice: In Python 2 range is a function that returns a list. In Python 3, range returns an iterable of type range. If you need to get a list you can use the list() function:

list(range(start,stop[, step]))

In [ ]:
for counter in range(5):
    print(counter)

Notice: The list doesn't reach the stop value and stops one step before. The reason behind that is to make this syntax possible:


In [ ]:
list(range(1,10)) == list(range(1,5)) + list(range(5,10))

Notice: In Python 3 use use == to check if two values are equal. To check if two values are not equal use != and don't use <> from Python 2 because it is not supported any more in Python 3.


In [ ]:
for counter in range(1,5):
    print(counter)

In [ ]:
for counter in range(2,10,2):
    print(counter)

While Loop


In [ ]:
counter =1
while counter < 5:
    print(counter)
    counter += 1

If .. Else


In [ ]:
if math.pi == 3.2:
    print("Edward J. Goodwin was right!")
else:
    print("π is irrational")

In [ ]:
if math.sqrt(2) == (10/7):
    print("Edward J. Goodwin was right!")
elif math.sqrt(2) != (10/7):
    print("Square root of 2 is irrational")

If you like Math:

Fun story about pi where it was almost set by law to be equal to 3.2!

If you don't what is the "pi bill" you can read about it here: http://en.wikipedia.org/wiki/Indiana_Pi_Bill

Or watch Numberphile video about it: https://www.youtube.com/watch?v=bFNjA9LOPsg


In [ ]:
probability = 0.3

if probability >= 0.75:
    print("Sure thing")
elif probability >= 0.5:
    print("Maybe")
elif probability >= 0.25:
    print("Unusual")
else:
    print("No way")

Functions

Functions are defined in Python using def keyword.


In [ ]:
def get_circumference(r):
    return math.pi * r * 2

In [ ]:
get_circumference(5)

In [ ]:
def binomilal_coef(n,k):
    """
    This function returns the binominal coef
    
    Parameters:
    ===========
    n, k int
    
    return n!/(k!*(n-k)!)
    """
    value = math.factorial(n)/(math.factorial(k)*math.factorial(n-k))
    return value


binomilal_coef(52,2)

Notice: Use can use SHIFT+TAB to show the docstring of a function.