Programming

For non-programmers

Bem vindo!

Available online

Feel free to click along!

http://tinyurl.com/programming-part1

Programming basics

First part of 'Programming for non-programmers'


October 13, 2017

Mathijs de Bruin / @mathijsfietst

Goal

Demystifying technology

“Any sufficiently advanced technology is indistinguishable from magic.”
— Arthur C. Clarke

Today's topics

Now to tell you what I'm going to tell you.


  1. What are computers?
  2. How are they programmed?
  3. ‘Hello World’: your first program
  4. Variables, conditionals, loops and functions
  5. Hands on!

Today's topics

Now to tell you what I'm going to tell you.


  1. What are computers?
  2. How are they programmed?
  3. ‘Hello World’: your first program
  4. Variables, conditionals, loops and functions
  5. Hands on!

What are computers?

Computers are

Universal Machines

  • Any computer can emulate any other computer.
  • Can do any kind of information processing.

Computers are

Huge amounts of electronic switches

(and some memory)

  • Switches are either ON or OFF (1 or 0)
  • Logical gates (AND, OR, …) perform boolean logic
  • Boolean logic allows for advanced behaviour

Today's topics

Now to tell you what I'm going to tell you.


  1. What are computers?
  2. How are they programmed?
  3. ‘Hello World’: your first program
  4. Variables, conditionals, loops and functions
  5. Hands on!

How they are

programmed

Switches, logical gates…
where does my cursor come from!?

Components

Switches

form

Components

logical gates

form

Components

Integrated Circuits (IC's/chips)

form

Modular components

  • RAM (Random Access Memory)
  • persistent storage (disk, flash memory/SSD)
  • CPU (Central Processing Unit)
  • BIOS (Basic Input Output System)

Meet

RAM

Random Access Memory

Temporary storage for current state of computer

Meet

persistent storage

hard disks, SSD's (flash), …

Meet

persistent storage

hard disks, SSD's (flash), …

stores data when computer is off

Meet

The CPU

Central Processing Unit

Executes programs from RAM

Meet

The CPU

Central Processing Unit

Executes programs from RAM

Meet

The BIOS

Basic Input Output System

Initial program: loads system from persistent storage

Meet

The BIOS

User Interaction

Console output

Printer, screen, graphics, …

User Interaction

Console input

Punch cards, keyboard, mouse, touch, …

How does it work?

Switching on the computer

  • CPU loads BIOS from harcoded memory address
  • BIOS tests system and parts (RAM, disks, screen, keyboard, …)
  • BIOS looks on disks for Operating System
  • BIOS bootstraps (loads) and boots (starts) Operating System

How does it work?

The Operating System (OS)

  • Sits between applications (user programs) and hardware
  • Abstracts from network (internet), files, screen, keyboard, …
  • Loads apps from disk into RAM and starts them
  • Prioritizes between simultaneous apps (multitasking)

Operating System

Windows

Operating System

UNIX

Operating System

OS X

Operating System

Ubuntu (Linux)

Operating System

Android

Operating System

iOS

Programming languages

Low-level to high level

Machine language

Hardwired in CPU

  • 0's and 1's
  • Memory addresses all over
  • 'Textual form': assembly

High-level languages

‘Readable’ source code converted to (binary) machine language

  • Compiled languages: read whole source, generate binary once
  • Interpreted languages: generate machine code 'line by line' while program runs

Today's topics

Now to tell you what I'm going to tell you.


  1. What are computers?
  2. How are they programmed?
  3. ‘Hello World’: your first program
  4. Variables, conditionals, loops and functions
  5. Hands on!

Hello World

Program starts up, says: 'Hello World'

Most common demo app*

*List of Hello world program examples

Example

Code: Python


#!/usr/bin/env python3

print('Hello World!')
					

Code: PHP



					

Code: C


#include <stdio.h>

int main() {
    printf("Hello World!\n");
}
				

Code: C++


#include <iostream>

int main() {
	std::cout << "Hello World!" << std::endl;
}
					

Code: Java


public class HelloWorld {
   public static void main(String[] args) {
       System.out.println("Hello, world!");
   }
}
					

Code: Brainfuck


+++++ +++++             initialize counter (cell #0) to 10
[                       use loop to set the next four cells to 70/100/30/10/40
    > +++++ ++              add  7 to cell #1
    > +++++ +++++           add 10 to cell #2
    > +++                   add  3 to cell #3
    > +                     add  1 to cell #4
    > ++++                  add 4 to cell #5
    <<<<< -                  decrement counter (cell #0)
]
> ++ .                  print 'H'
> + .                   print 'e'
+++++ ++ .              print 'l'
.                       print 'l'
+++ .                   print 'o'
>>> ++++ .              print ','
<< ++ .                 print ' '

< +++++ +++ .           print 'w'
 ----- --- .            print 'o'
+++ .                   print 'r'
----- - .               print 'l'
----- --- .             print 'd'
> + .                   print '!'
> .
					

Today's topics

Now to tell you what I'm going to tell you.


  1. What are computers?
  2. How are they programmed?
  3. ‘Hello World’: your first program
  4. Variables, conditionals, loops and functions
  5. Hands on!

Python

From now, we'll use Python.

It's simple, powerful, runs everywhere and…

I know this shit like laden swallows.

Python

Interpreted language

No compilers, no binaries

Python

Runs everywhere

Windows, Mac, Linux, Android, name it…

Python

Real world applications

Webapps (Google anyone?), science, art

Python

so f*cking simple

Never worry about:

  • Memory
  • Types (binary representation)
  • Machine language
  • Bits'n bytes

Python

Readable code

Only programming language designed to be readable.

Python

Open Source

Everyone can use, distribute and modify it.

Real programming

Structures:

  • Variables: information referrable by name
  • Conditionals: do stuff only when conditions are met
  • Loops: repeatedly doing stuff
  • Functions: actions referrable by name
  • Libraries: functions you don't have to write anymore

Variables

Remember stuff for later use

Example


birth_year = 1985
current_year = 2014
age = current_year - birth_year
print(age)
					

Output

29

Conditionals

When condition is met, do stuff


if age < 30:
	print('Still young!')
else:
	print('You dinosaur!')
					

Output

Age: 29

Still young!

Age: 30

You dinosaur!

Loops

Do stuff while condition is true



age = 28

while age < 30:
	print('Go on for another year!')
	print(age)

	# Increase value in age by 1, store new value as age
	age = age + 1

print('30? You might as well die!')
					

Output


Go on for another year!
28
Go on for another year!
29
30? You might as well die!
					

Functions

Actions with names, arguments and return values


def calculate_age(birth_year, current_year):
	return current_year - birth_year

print(calculate_age(1985, 2014))
					

Output

29

Libraries

Collections of functions and objects

Work you don't have to do: use them!

Libraries

Example


# From datetime import datetime object which has the now() function
from datetime import datetime

# now() returns a 'datetime' object
# Using <object>.<property> we can grab a property of object
current_year = datetime.now().year

# Calculate age and display result
print(current_year - birth_year)
					

Output

Whatever age this dude from '85 is right now.

Full example

Ask birthdate, calculate age.


# Import datetime object to work with dates and times
import datetime

# Ask user for some text
birth_year = input('In what year were you born?')

# Turn text into integer (whole) number
birth_year = int(birth_year)

# From the datetime module, call the now() function of the datetime object,
# returning current date and time from the computer's clock
current_year = datetime.datetime.now().year

# Calculate age and display result
print('Your age is:', current_year - birth_year)

					

Today's topics

Now to tell you what I'm going to tell you.


  1. What are computers?
  2. How are they programmed?
  3. ‘Hello World’: your first program
  4. Variables, conditionals, loops and functions
  5. Hands on!

Hands on!

A little less talking
a lot more action!

Questions so far?

Step 1

Install Python

They downloadeth from python.org

Step 2

Install text editor

'Code' is text files, you're going to edit them

Step 3

Write code

Create a .py (Python) file, write code

Step 4

Run code

Try and execute your code.

Step 5

Fix code

Your code is probably broken. Welcome to…

Debugging

Next step:

Interactive Python, with help!


$ pip install ipython
$ ipython
...
In [1]: l = 5

In [2]: 2*l
Out[2]: 10
In [3]: import datetime

In [4]: help(datetime)
					

Thanks!

Mathijs de Bruin / @mathijsfietst