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: 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: 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 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” 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):
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: 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:
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).
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:
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”
creates a new variable named “testArray” of type array that holds 4 separate variables within it. 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:
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 should get an output that reads “first”.
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:
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:
we will get an output “25”. 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: This should output “5” as there are 5 items in this array -----------------------------------------------------------------------------------------------------LESSON 1 TASKS:
lesson1_tasks.py |