Jason Thomas

I like to make stuff

December 29, 2016 @ 11:32

A really basic introduction to Python - in plain English

Recently my partner asked me how to code, so I thought I'd have a crack at teaching her Python. I wrote this before showing her the different elements of Python programming.

I wanted to do my very best to show her programming in a way that was easy to understand, so I spent some time thinking about getting the explanation right.

Here's what I wrote.

Processes and data

If this part doesn't make sense to you, don't worry about that, you'll pick programming up as you go.

In computer programming there's three things - the environment, the process(es) and data. The environment is where your programs run. A process manipulates data in the environment.

The book SICP defines the nature of a process and what it does in greater detail. It's hard to beat the wealth of knowledge in that book, if you want to take programming seriously, just read that.

Python

Python is a language that's interpreted. That means, that instead of compiling a program's source code into 0's and 1's, the program interprets the source code as it goes.

There are two ways to run Python programs - 1) loading files from your file system (what you'll want to do for serious projects) and 2) - the interactive interpreter.

A simple way to start coding is to use the interactive interpreter. We're using Linux, so we go to the terminal and type python. You will see >>> and Python is now listening to our input.

To load a program, save the code in a .py file and load it from the terminal, like:

python my_program.py

Data types

Data types in Python are important because it is a strongly typed language; you can't add two types that can not be added, like a string and an int (like you can in JavaScript, often with undesired results).

Some basic data types are:

Int - A whole number, can be negative

-100

0

1

1000

Float - A decimal number, can be negative

-5.1

0.0

3.1415

String - One or more text characters

"A"

'S@llyGr1m$hAw'

"Farmer Bob likes the beer"

'I like to walk after meals'

Boolean - A True or False binary variable

True

False

List - Called an array in other languages.

This is a list of things - things can include any other type of thing, including other lists, and even lists of lists of lists of ...

[1, 2, 3]

['Jack', 'Mandy', 'Marty']

[True, False, True]

[True, 'Jack', 1, 3.4]

[[1,2], ['Bill', True], [1.3, 'Jill']]

Examples of incorrect uses of variable types:

int_example = '1'
# This is a string because it has speech marks, but can look like an int

float_example = 1.5.0
# A float can have one decimal place only

string_example = Brown
# Without quote marks, Python will read Brown as being a variable name 

boolean_example = 'True' 
# Because of the quote marks, this is a string

array_example = ['string', 1, False 
# Arrays require two opposing brackets, like [ ]

Operators

If data types define the things a process can work on, than operators are the things a process can do to the data.

In Python, there are mathematical operators:

+

-

/

* 

Note, the * is multiply, don't use x.

And there are basic logic operators:

== 
# Equal to

!= 
# Not equal to

> 
# Larger than

< 
# Smaller than

>= 
#Larger than or equal to

<= 
#Smaller than or not equal to

So this is a valid line in Python:

1 + 1 == 2

If you type that into the interpreter, it will return True, because the logic is valid.

Note though, the use of double == is different to =. = is for assigning values and == is for comparing.

Variables

As mentioned, we use a single = to define a variable, like this:

my_variable = 1

Python has put a nametag of my_variable on the value of 1, so we can retrieve that data later on.

So now we can use the print statement (Python2), or the print() function (Python3):

print(variable_1)

Gives us:

1

Defining data, and calling the data back, in the enviroment is great... but that by itself will not create a useful program. To do anything useful we need to control the process and how it manipulates data.

Control flow

If statement

One way to instruct the process to manipulate data is to use an if statement:

if something == 1:
    message = 'something is equal to 1'
else:
    message = 'something is not equal to 1'

print(message)

So the above evaluates the value of something, and then prints the result.

Indentation

Note the use of indentation above. In Python, indentation is how we tell the computer that those lines belong in the same code block.

It is common, and therefore advisable, to use four spaces for all indentation. Text programs like Sublime Text, Visual Studio Code and even Vim (with some work) can be set up to do four space tabs on default.

Loops

Another element of programming is to use a loop. A loop will repeat a process several times. There are different kinds of loops in Python.

Here is a while loop:

counter = 0
while counter < 10:
    counter = counter + 1
    print(counter)

That program will print the numbers 0 to 9. The following program produces the same result, but in a different way:

for i in range(10):
    print(i)

Both of these loops are useful, but for this example the for loop is more effective. However, while loops are effective when you need to perform a loop but you don't know how many times the loop should occur.

The following loop continues only if a condition is met, which in this case, can not be predicted:

message = 'Type "stop" to stop program: '
listening = True
while listening:
    user_input = input(message)
    if user_input == 'stop':
        listening = False
        print('Ending')
    else:
        print('Still listening')

Functions

Good code is reusable code, an experienced programmer once told me, so we want to reuse our code if we can. We can use functions to reuse code while only defining it once. We can wrap one of our loops above in a function.

def my_loop():
    for i in range(10):
        print(i)

So the code above has defined a function, with the name my_loop(). You can call the function anytime by stating its name. I could call the same function ten or more times in a program, and only define it once. Beauty.

Functions can also accept inputs, and that's where the parentheses in our function definition come into use. This function will check your age:

def age_checker(age):
    if age < 18:
        message = 'You are not an adult in Australian age standards'
    else:
        message = 'You would be considered an adult in Australia'
    print(message)

age_checker(43)

So the function, when called with the parameter of 43, can determine this person is an adult by their age.

Where to now?

There are more elements of programming, and Python has its own conventions and features that Pythonistas would want to look into, but these basic elements should be sufficient to building some kind of basic program, like a simple text game.

Larger applications can be made more manageable with classes and modules, but that's a bit more advanced than what I'm trying to do here.

Consider learning Python3 over Python2, as this website points out, Python2 is legacy and won't be supported post-2020 (hopefully). The changes to the language were not massive, but there were some big improvements.

Also try and do personal projects, to keep programming interesting. As SICP says, the most important thing is to have fun. Don't let programming become a boring way to get stuff done.

log in