Over the last 10 years, it has grown to be one of the most popular programming languages.
It recent outpaced Java and JavaScript in popularity.


This presentation was created using tools built in Python.

By the end of the course:
Go to the download link and pick the correct version for your operating system
If you have a "32-bit" operating system, install python-3.6.3.exe
If you have a "64-bit" operating system, install python-3.6.3-amd64.exe
When you install it, make sure to check the option to add Python 3.6 to your PATH.

To start the Python console on Windows, go to Start -> Python 3.6 -> IDLE
This is the Python prompt.
You type the commands you see into it
This is a Python comment. Any line that begins with '#' is a comment.
Comments don't impact your code and are just for reading.
# Comments are notes for yourself about your code
This is an expression which is the basic unit of instruction.
An expression consists of values (like 2) and operators (like +).
Pressing <enter> evaluates the expression.
The result of an expression is a value.
2 + 2
4
This is an error. If the Python interpreter can't understand
your expressions, or there is another issue, there will be an error.

NoneVariables can be "assigned" with the = operator.
name = "Cass"
occupation = "Programmer"
age = 33
age = 33
print(age)
33
1 + 1
2
19 - 8.5
10.5
12 * 12
144
# Power operator
2 ** 4
16
100 / 6
16.666666666666668
# Fraction discarding the remainder
100 // 6
16
# "Modulus" or "remainder"
7 % 5
2
birth_year = 1980
this_year = 2017
age = this_year - birth_year
print(age)
37
Construct strings with '
Strings support operations +, *, and %
name = "Cass"
print("Hello " + name)
Hello Cass
Try with your own name now.
Strings also support "interpolation." You can define a template for your string and then "fill in the pieces."
message = "%s is a %s aged %d"
print(message % (name, occupation, age))
Cass is a Programmer aged 37
message = "{} is a {} age {}"
print(message.format(name, occupation, age))
Cass is a Programmer age 37
You can also multiply a string a number of times.
name * 5
'CassCassCassCassCass'
Conditions of logic. Their values are True and False.
They support operators as well, like or, and, as well as not.
Tricky: Any non-zero variable is treated as True in logical operations.
is_human = True
is_robot = False
is_human or is_robot
True
is_human and is_robot
False
not is_robot
True
None is a special variable, meaning nothing¶Typically it means "lack of value"
print(None)
None
Lists are useful for holding multiple values where order matters
ages = [33, 13, 49, 65, 1]
len(ages)
5
You can reference items of a list by number, called the index.
0 is the first element while len - 1 is the final element.
ages
[33, 13, 49, 65, 1]
print(ages[0])
33
print(ages[len(ages) - 1])
1
print(ages[-1])
1
if / else- decision & logicfor - doing something over and overwhilebreak - breaking or continuing for loopscontinueIn Python, indentation with whitespace marks a "block" of code.
The convention is to use 4 spaces for this indent.
In IDLE, pressing <TAB> produces spaces rather than a tab.

if is used for deciding¶if <Boolean>:
____<Expression if True>
else:
____<Expression if not True>
if is_human:
print("{} is a human".format(name))
else:
print("{} is a robot".format(name))
Cass is a human
if 1:
print("This 'if' evaluates to 'True'")
This 'if' evaluates to 'True'
if 0:
print("This 'if' evaluates to 'False'")
if name:
print("This 'if' evaluates to 'True'")
This 'if' evaluates to 'True'
if None:
print("This 'if' evaluates to 'False'")
for loops, doing something over and over¶Use the for statement to do things a fixed number of times (typically known ahead of time).
for supports iterating over various structures like lists and dictionaries.
To print something 7 times, use:
for i in range(7):
print("This will appear 7 times")
This will appear 7 times This will appear 7 times This will appear 7 times This will appear 7 times This will appear 7 times This will appear 7 times This will appear 7 times
for i in range(5):
print("Counting...{}".format(i))
Counting...0 Counting...1 Counting...2 Counting...3 Counting...4
They're also useful for accumulating things.
Below, += means "add and then set equal to."
str means "convert to string." Because numbers is a string, we can only add strings to other strings. We cannot add a number and a string.
numbers = ''
for i in range(10):
numbers += str(i)
numbers
'0123456789'
while loops¶Use while loops to do something multiple times.
Usually used when you don't know the number ahead of time.
# Add numbers until they are more than 100
total = 0
number = 0
while total < 100:
number += 1
total += number
print("Adding up to {} produced {}".format(number, total))
Adding up to 14 produced 105
break, continue, pass¶for i in range(10):
print("{} before break".format(i))
if i > 4:
# `break` brings us out of a `for` loop prematurely
break
print("{} after break".format(i))
0 before break 0 after break 1 before break 1 after break 2 before break 2 after break 3 before break 3 after break 4 before break 4 after break 5 before break
sum_of_evens = 0
for i in range(5):
print("Testing the number {}".format(i))
# If the number is even
if i % 2 == 0:
print("{} is an even number".format(i))
sum_of_evens += i
else:
continue
print("The total is now {}".format(sum_of_evens))
Testing the number 0 0 is an even number The total is now 0 Testing the number 1 Testing the number 2 2 is an even number The total is now 2 Testing the number 3 Testing the number 4 4 is an even number The total is now 6
The pass statement does nothing. It's used for when you need an expression but wish to do nothing.
The following code continues forever.
#while True:
# pass
If you put code above in your prompt, you can press <ctrl> + C or <ctrl> + Z to cancel it.
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print(n, 'equals', x, '*', n//x)
break
else:
# loop fell through without finding a factor
print(n, 'is a prime number')
2 is a prime number 3 is a prime number 4 equals 2 * 2 5 is a prime number 6 equals 2 * 3 7 is a prime number 8 equals 2 * 4 9 equals 3 * 3
Functions allow you to define behavior to execute later one.
They're useful for abstracting code.
def say_hello(name):
print("Hello, {}".format(name))
say_hello("Cass")
Hello, Cass
print("Hello, {}".format("Cass"))
print("Hello, {}".format("Morris"))
print("Hello, {}".format("Mariatu"))
print("Hello, {}".format("Mohamed"))
print("Hello, {}".format("Bockarie"))
print("Hello, {}".format("James"))
print("Hello, {}".format("Samuel"))
Hello, Cass Hello, Morris Hello, Mariatu Hello, Mohamed Hello, Bockarie Hello, James Hello, Samuel
def say_hello(name):
print("Hello, {}".format(name))
say_hello("Cass")
say_hello("Morris")
say_hello("Mariatu")
say_hello("Mohamed")
say_hello("Bockarie")
say_hello("James")
say_hello("Samuel")
Hello, Cass Hello, Morris Hello, Mariatu Hello, Mohamed Hello, Bockarie Hello, James Hello, Samuel
def say_hello(name):
print("Kushe-o, {}".format(name))
say_hello("Cass")
say_hello("Morris")
say_hello("Mariatu")
say_hello("Mohamed")
say_hello("Bockarie")
say_hello("James")
say_hello("Samuel")
Kushe-o, Cass Kushe-o, Morris Kushe-o, Mariatu Kushe-o, Mohamed Kushe-o, Bockarie Kushe-o, James Kushe-o, Samuel
Lists contain sequences of items.
names = ["Morris", "Bockarie", "Mariatu", "Fodi"]
You can add lists.
places = ["Sensi", "NP", "Total", "Raza"]
names + places
['Morris', 'Bockarie', 'Mariatu', 'Fodi', 'Sensi', 'NP', 'Total', 'Raza']
len(names)
4
names.append("Cass")
names
['Morris', 'Bockarie', 'Mariatu', 'Fodi', 'Cass']
languages = ["Python", "Java"]
languages.extend(["Javascript", "HTML"])
languages
['Python', 'Java', 'Javascript', 'HTML']
for l in languages:
print("I want to learn {}".format(l))
I want to learn Python I want to learn Java I want to learn Javascript I want to learn HTML
They support "list comprehensions"
numbers = range(10)
[i + 10 for i in numbers]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Lists (and other structures) support the in operator.
names
['Morris', 'Bockarie', 'Mariatu', 'Fodi', 'Cass']
"Morris" in names
True
if "Morris" in names:
say_hello("Morris")
Kushe-o, Morris
Dictionaries allow you to put multiple pieces of information into a single structure.
{"name": "Cass", "age": 33, "occupation": "programmer"}
{'age': 33, 'name': 'Cass', 'occupation': 'programmer'}
person = _
person['name']
'Cass'
person['occupation']
'programmer'
Tuples are like "lists of fixed size." You cannot add to them or extend them.
point = (1, 2)
print(point)
(1, 2)
Sets are groups of items. A set contains no duplicates.
numbers = {1, 2}
numbers.update({10})
numbers
{1, 2, 10}
numbers.update({10})
numbers.update({1})
numbers
{1, 2, 10}
This is an actual job interview exercise, usually the first one in a round of interviews.
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.
For printing, use print.
For multiples, use n % m == 0. For instance, since 10 is an even number, then
10 % 2 == 0 evaluates to true.
Create a version of the "Caesar Cipher".

The code would be a correspondence between plain letters and encoded letters:
Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Cipher: XYZABCDEFGHIJKLMNOPQRSTUVW
so that a message encoding would be:
Plaintext: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
Ciphertext: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD