to Programming 
in Python

J rogel-Salazar

@quantum_tunnel / @dt_science

General Assembly London - 2020



A bit about me...

@quantum_tunnel / @dt_science

How about you?

I use Python

for rapid prototyping 
and data science projects

to analyze data like user transactions
 & natural language processing

to run simulations as part of my stack

to talk to robots and humans :)

This Workshop







These Slides



Hammurabi Code Photo   by Gabriele B






 fundamental equation 

computer = powerful + stupid 
Computers are very powerful, looking at volumes of data very quickly. Computers can perform billions of operations per second, where each operation is pretty simple. Computers are also shockingly stupid and fragile. The operations that they can do are extremely rigid, simple, and mechanical. The computer lacks anything like real insight ... it's nothing like the HAL 9000 from the movies. If nothing else, you should not be intimidated by the computer as if it's some sort of brain. It's very mechanical underneath it all. Programming is about a person using their real insight to build something useful, constructed out of these teeny, simple little operations that the computer can do.
- Francisco Cai and Nick Parlante, Stanford CS101



Top 10 programming 

languages - IEEE


Over time

Powered by python

Talking Python

One of Python's most attractive features


Human readable 

High level & general
Cross platform
Free & open source
Strong & friendly community

Two Modes of Operation

Scripting Mode

  • Code in file
  • Save & load programs
  • More control  

Interactive Mode

  • Terminal-based
  • Direct feedback
  • Try out code






JUPyTER notebook

setting up

Download and install the Anaconda 
distribution (Python 3) from 

Open terminal / cmd window

Type “python3” to start Python in interactive mode

type "exit()" or ctrl+Z to exit


Download python 3 from

Install Python

Open terminal / cmd window

Type “python3” to start Python in interactive mode

type "exit()" or ctrl+Z to exit

Use notebooks

I recommend using Jupyter Notebooks

They are a great way to code

If you are using Anaconda you are sorted!

If not, you can install it with 

 > pip3 install jupyter
If you are using Python 2
 > pip install jupiter

If you have installed JUPyTER

Open terminal / cmd window

Type the following:

> jupyter notebook  

This will open up an interface in your browser. 

You can now  create new notebook by clicking

on the "New" button on the upper-right-hand corner

Getting help

>>> help()
(then Q for quit)

Search Online

Documentation at



Train Parts Photo by Môsieur J. [version 8.0]

syntax You already know

Python as a calculator







Earthshaker Pinball Photo   by Chase N.


J   Rogel

Variables  store  values  under a specified name


J   Rogel

name = 'J Rogel' 

Other information

Num_Pets = 1
Height = 1.72


Variables store values of different types:

string - a sequence of characters, comprising text [”a”, “London”, “X”]

int - an integer, or whole number [1,5,9999, ...]

float - a floating point number (using a decimal point) [3.14, 1.68, 1.0, ...]

bool - boolean; binary true or false values [True, False]

Changing types

'Casting' a variable to another type




assigning Variables

name = "J"
gender = "male"
height = 1.72 


Look around

Choose 4 things in this room 
to assign to 4 different types of variables




Think of them as a bit of text 
You may want to display it for example
Python knows you want 
something to be a string 
when you put either 
" (double-quotes) or 
' (single-quotes)
 around the text

 'A bit of text'
"or some more text"




They are often called just integers or ints, are positive or negative whole numbers with no decimal point

1, 8, -10, -97, 42




Floats represent real numbers and are written with a decimal point dividing the integer and fractional parts

Floats may also be in scientific notation, 
with E or e indicating the power of 10 

2.5e2 = 2.5 x 10^2 = 250




Boolean values are the two constant objects False and True

 They are used to represent truth values 
(other values can also be considered false or true).

 Boolean logic is used everywhere in programming


You can process the values in your variables by operators :

= assignment: assign a value to a variable
== comparison: are two variables equal?
!= comparison: are two variables unequal?

<, >, <=, >=

less-than, greater-than, less or equal, greater or equal
+, -, *, / mathematical operators
and, or logical operators

behaviour depends on data type

>>> start = "Lon"
>>> start
>>> end = "don"
>>> start + end
>>> start + start + end
>>> town = 3 * start + end
>>> town



>>> 1 + 1
>>> cats = 2
>>> cats
>>> dogs = 3
>>> cats == dogs
>>> cats < dogs
>>> dogs + 1
>>> dogs
3>>> dogs = dogs + 1
>>> dogs
4>>> pets = cats + dogs
>>> pets 6




Create two string variables:

first for your first name and last for your last name

Can you make your full name by combining first and last?


What happens if we compare first and last 

with the ‘<‘ and ‘>operators?


(Cheating encouraged)


Collections of Values

Values can also be stored in a collection





Todo Photo   by  purpleslog



Long Queue Photo   by gadl



Recipe Photo   by sleepyneko


We can store multiple values in a list:

>>> l = [1,3,9,4,884328881]
>>> n = ['sex', 'drugs', 'rock', 'roll']
>>> m = l + n
>>> m
[1, 3, 9, 4, 884328881, 'sex', 'drugs', 'rock', 'roll']

A list is an ordered sequence of items (between [...]
each with their own index:
>>> m[0]
>>> m[8]


cool things you can do with lists

>>> l = list(range(10)) >>> l [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> l[1:5] [1, 2, 3, 4] >>> l[:5] [0, 1, 2, 3, 4] >>> del l[5:] >>> l [0, 1, 2, 3, 4] >>> l.append(5) >>> l [0, 1, 2, 3, 4, 5] >>> l.reverse() >>> l [5, 4, 3, 2, 1, 0]
>>> 5 in l


Lists are sequences, and so are Strings

>>> s = "General Assembly"
>>> s[0]
'G' >>> s[:7] 'General'



Todo Photo   by  greeblie



Chinese Menu Photo   by avlxyz



Animal Sign Photo   by cortneymartin82


Dictionaries store key:value pairs associatively.

personX = {
>>> personX = {'first':'J', 'last':'Rogel', 'twitter':'@quantum_tunnel'}
>>> personX['first']
>>> personX['id'] = 31415
>>> personX
{'twitter': '@quantum_tunnel', 'last': 'Rogel', 'id': 31415, 'first': 'J'}

some things you can do with Dictionaries

python 3
>>> list(personX.keys())
['twitter', 'last', 'first']
>>> list( personX.values())
['@quantum_tunnel', 'Rogel', 'J']

python 2
>>> personX.keys()
['twitter', 'last', 'first']
>>> personX.values()
['@quantum_tunnel', 'Rogel', 'J']




Form pairs

Create a dictionary to represent your neighbour in some attributes
(for example home town, hair colour, favourite film).


Add a list of interests to the dictionary . What do you use as key , what do you use as value ? 

(Cheating encouraged)



Pinball Photo by Chase N.



Train Loop Photo  by  Minnesota Historical Society

For Loops

You use loops to repeat a statement.

A for-loop is useful when you know how many times you want to repeat an action (e.g. for every item in a list)

for item in sequence:
     do someting with item

Mind the Indentation

For Loops

For Loops

for example

>>> ages = [18, 21, 16, 12, 90]
>>> for age in ages:
...     print(age)

Visualise this code in action

While Loops

A while-loop  is useful when you don’t know when you want to stop looping yet.

A while-loop statement checks a condition and loops until the condition is no longer satisfied.

while condition true:
     do something

While loops

While Loops

for example

>>> gas = 42
>>> while gas > 0:
...     print("Vroom!")
...     gas = gas - 10
Visualise this code in action

 Conditional Statements 


Rail Split Photo by  Leonid Mamchenkov

Conditional statements

Conditional statements enable you to deal with multiple options. 

A part of your code is executed based on the truth value of a condition. 

You perform conditional checks with: if, (elif), (else)

if condition:
elif other condition:  # optional
   other action
else:                  # optional
   final action
>>> age = 17
>>> if age < 18:
...     print("no drinks for you")
no drinks for you

conditional statements

Conditional statements

>>> ages = [18, 21, 16, 12, 90]
>>> for age in ages:
...     if age >= 75:
...         print("the usual?")
...     elif age <= 74 and age >= 18:
...         print("come on in")
...     else:
...         print("get outta here")
come on in
come on in
get outta here
get outta here
the usual?
Visualise this code in action



Train Station Photo by luke.baldacchino


Functions perform a collections of tasks, bundled under a specific name

Take input argument(s), execute statement(s), return output

Input and output can be of all different types

>>> name = "J Rogel"
>>> length = len(name)
>>> print(length)
>>> type(length)
<type 'int'>

Built in functions


You can define your own functions like this:

def function_name( argument(s) ):
   action with argument(s)

>>> def multiply(a, b):
...     return a * b
>>> multiply(3,4)

>>> def greet(name):
... print("hello "+ name)
>>> greet('J')
hello J

Mind the Indentation



Train Crash Photo  by chrislstubbs


Write a function with an appropriate name that: 

  • takes as input a person (represented as dictionary)
  • prints out information about the person

(Cheating encouraged)



Earthshaker Pinball Photo   by Chase N.


A module is a package of code that extends the 
native functionality of Python.
Use modules to:
plot graphs
download web pages 
read and write .csv files
Importing a modules into your script is simple
>>> import random
>>> random.randint(0,10)
>>> random.randint(0,10)
>>> random.randint(0,10)


You can also write your code conveniently in a 

file using your favourite text editor

Such a file is a program or script and can look as simple as this:

print("Hello World")  

Save your script as “[a_descriptive_name].py”

Navigate in terminal to the location of your file

Run “python [a_descriptive_name].py”


Or this:

# this is a comment, use them!
Comments can also 
span multiple lines
# print() is a very useful function
# mind the quotes
print("Hello World")

# don't forget to greet the Sun
print("Hello Sun")




Implement your function from the previous exercise 

in a script, run it to let it print output

(Cheating encouraged)


Install the Beautiful Soup module using PIP:

Run in terminal / cmd prompt


 >>> pip3 install beautifulsoup4

If you are using Anaconda, the module is already installed!
More info & help on installing modules:

on windows

If windows can't find the pip command, make sure your Python installation folder & Scripts folder are added to your 'PATH'

Follow these instructions:

Alternatively reinstall with this option checked:

 Thank you 

J Rogel-Salazar

Useful Resources

google; “python” + your problem / question; official python documentation, useful to find which functions are available; huge gamified help forum with discussions on all sorts of programming questions, answers are ranked by community; interactive exercises that teach you coding by doing; tools, lessons and tutorials

Introduction to Programming in Python

By J Rogel

Introduction to Programming in Python

A practical introduction to programming through the Python programming language

  • 1,244
Loading comments...

More from J Rogel