Home Lesson 2 Lesson 3 Lesson 4 Lesson 5 Lesson H

Lesson 1 - Variables and Functions

Python is a simple programming language. It is used in visual fx, web design, game design and much more . It is an ideal language to begin learning as it is very 'stripped down' of formal jargon.

Programming requires a different way of thinking about language in comparison to how most people normally think about it. This means it will probably throw up some confusions and seem alienating at first. If it does, it helps to take each word, or each line, at a time and ask the question "what is this doing"

-----------------------------------------------------------------------------------------------------

Two initial concepts to understand are VARIABLES and FUNCTIONS

A variable is a word/symbol that references some piece of data. It can be thought of as a container for that data. In python, a variable is declared as follows:

someVariable = 45

The above code has the syntax:

name of the variable (someVariable in this case))

assignment operator (=)

value held by that variable (the number 45 in this case)

-----------------------------------------------------------------------------------------------------

After this line of code, the number 45 has been stored in a variable named “someVariable” and every time we write the word “someVariable” the program will substitute the value stored in it (in this case the number 45)

For example, if we execute the following code:

print someVariable+7

we should get an output that reads “52”

A valid question would be “what kind of animal is that word 'print'?. 'print' is an in built function (or command) that takes an input (in this case someVariable+7) and outputs that input to the user in the form of text the user can read. We'll talk about functions in a sec.

Variables can be re-assigned after their initial instantiation (new data can be stored in it after it is created). For example if we execute the following code after someVariable = 45

someVariable = someVariable + 45

The number stored in someVariable will now be 90

Variables need to be named as a single word starting with a letter (I.e “some variable” and “2variable” are not acceptable names but “some” and “some2” and “some_2” are all fine).

It is common for variables to be named in “camelCase” structure, meaning multiple words are concatenated and each word after the first begins with a capital letter.

-----------------------------------------------------------------------------------------------------

The value stored in a variable need not be a number. It can be a different type. For example, the following line of code declares a variable called “mattyVar” which stores the characters “hello there”

mattyVar = “helloThere”

-----------------------------------------------------------------------------------------------------

So now we've encountered two distinct types of variables: int and string. The following are the main types you will encounter (there are plenty more):



Integers are whole numbers, Floats are decimal numbers.

Sometimes it may seem like there isn't much difference between floats and ints, but it is often important to remember which type your variable is. To illustrate this consider the following: 

newNumber1 = 23.0

newNumber2 = 23

Here we have created 2 new variables. They both hold a value we would think of as 23.

newNumber1 has been created as an integer variable and newNumber2 has been created as a float variable. This difference will be evident if we run the line:

1/newNumber1 or

1/newNumber2

The first line will print the value “0.043478260869565216” while the second will print “0”.

Q: Why is this?

A: In both examples, the operation that is taking place (the action, function, command, however you want to think of it) is the mathematical operation of division.

This function takes two variables as input (the numerator and the denominator).

In the first example, the numerator is the integer value 1 and the denominator is the float value 23.0. Because one of the variables is a float, the output will also be a float (the decimal fraction 1/23 which is 0.043478260869565216).


In the second example, because both numerator and denominator are integers, the division operation will output

an integer, specifically the integer “below” the decimal value (this is technically called the “floor” of the value). Floor(0.043478260869565216) = 0

Strings are very important to understand. The data a string variable stores is a series of characters. It can be thought of as writing or text. String variables are declared using either double or single quotation marks. (“word” or 'word'). Nothing more about the data is stored apart from the characters. For example if I write the following line of code:

print '57' + '3'

We might expect to get the value '60' as we would with print 57 + 3 but because we are using string variables, the “+” symbol has an alternate meaning to mathematical addition. It means string concatenation. In other words, it means “add one set of characters to another”. So we get an output: “573”
Arrays are lists of things. You could think of them as groups of items. They are declared using square brackets and separate “items” are demarcated using commas. For example, the code:

testArray = ['first', 'second', 3, 4]

creates a new variable named “testArray” of type array that holds 4 separate variables within it.

One of the nice things about python is that single arrays can hold different data types. The above testArray holds 2 strings and 2 integers. It could, if we wanted, hold any kind of data type we can think of (integer, float, string, even other arrays or dictionaries). This makes arrays in python a very quick and dynamic way to reference data. 

We can add new items to the array by using the function .append(). The following code will add a float value 0.5 to our testArray:

testArray.append(0.5)

This syntax will become familiar when dealing with functions. First we have the function name (in this case “append” followed by curly brackets containing all arguments that are the input to that function. The append function will, in this case, attach the single argument (0.5) to the variable “testArray”. It will leave us with a testArray that looks like: ['first', 'second', 3, 4, 0.5]

We can access individual items by their position within the array (often called the index of the item). The first item in the array is positioned at index=0. The second item has index=1 and so on. So if we want to return the first item in our array, we can enter the following:

testArray[0]

We should get an output that reads “first”.

We are also able to index from the end of the array. To do this we use negative indices. The item at the end of an array has index=-1. The penultimate item has index=-2 and so on. For example, to return the item 3 from the end of our array (which is the number 3 in this case), we can run the following code:

testArray[-2]

Dictionaries are a little like arrays, with the main difference being that you retrieve each item in a dictionary by a specific “key” rather than by it's position in the array. For example, consider the following line of code:

ageDic = {'Mat':27, 'Jane':27, 'Eliott':25}

Here we have created a dictionary with 3 string keys ('Mat', 'Jane', 'Eliott') and 3 integer values (27, 27, 25). If we excecute the following code:

ageDic['Eliott']

we will get an output “25”.
We'll leave dictionaries for now, but they are also extremely versatile. You may mix up types for keys and items and there are many built in functions available to manipulate them. -----------------------------------------------------------------------------------------------------

Introduction to Functions 

Variables are passive things. They hold data, are manipulated and retrieved. They are like objects. Functions are active. Functions change things.

We have already seen a few functions, the built in function “print” for example.

By built-in, I mean that Python has access to this function by default. It is part of Python's standard library. Not all functions you use will be part of this standard library. Some functions you will import from other libraries, some you will write yourself.

Let's have a look at another built in function “len”:

This function takes as input either a string, array or dictionary and outputs the number of items in that object. In the case of a string, it outputs the number of characters in the string.

Try entering the following:

len(testArray)

This should output “5” as there are 5 items in this array

-----------------------------------------------------------------------------------------------------

LESSON 1 TASKS:

  • create a variable named “myName” that stores a string of your first name.

  • create another variable named “nameLength” that stores an integer of the number of letters in that variable

  • print the last letter in myName using negative indices

  • print the last letter in myName using the variable nameLength

-----------------------------------------------------------------------------------------------------

lesson1_tasks.py