lesson 3 variables – how our brains work - variables in python

20
Lesson 3 Variables – How our Brains Work - Variables in Python

Upload: kristin-dean

Post on 23-Dec-2015

236 views

Category:

Documents


2 download

TRANSCRIPT

Lesson 3

Variables – How our Brains Work - Variables in Python

Ever wondered how our brains work?!?!

http://www.youtube.com/watch?v=wmcHH4wPpMU

The Human Brain! They might not look much like each other, but at first blush computers have several similarities with brains. For one

thing, they can store information, just the same as our gray matter. For another, computers too have memory, though their use of memory is a bit different from ours, and their idea of "bad" memory is far different from ours as well. (It would be nice if we could simply swap out our bad memories in the same way a computer just needs a new memory module to be swapped in whenever bad memory arises.) In a sense, a computer's memory, depending on the type -- for example, RAM -- also "forgets" things when it releases information it no longer needs. Their forgetting is a bit more purposeful than ours, however. We don't plan to lose our car keys, wallets and remote controls.

Next, computers can process a whole lot of information, really fast. Relatively speaking, we can too. (Though for sheer, absolute number-crunching speed we can't possibly outperform computers with just our naked brains.)

Once we get past the storage, memory and processing similarities, though, the comparison begins to take on water. Beyond the superficial resemblances, the two aren't really much alike. The human brain can innovate -- humans can come up with ideas from out of the proverbial clear-blue sky. We're free thinkers, and self aware. Computers, though, must follow sets of instructions to accomplish tasks. They can't do anything they haven't already been given the tools, or information, to accomplish. These tasks may be complex and might otherwise take a human being thousands of years to complete, but the computer isn't able to jump outside the relatively narrow list of instructions it receives. While there have been great strides in artificial intelligence, computers are still a long way from replicating the human brain.

The numbers don't help with the comparison either. The human brain has an estimated storage capacity of some 2.5 petabytes of information (two and a half million gigabytes) -- not exactly the kind of storage you'll see in a single computer at your local box store outlet

[source: Reber].

And what, really, is a program?-A Sequence of Instructions

HOW OUR BRAINS WORKAlgorithms – (in our brain) are inbuilt

We eat, drink, speak, respond and appear to have been pre-programmed to do so. There is ‘Free Will’ too, but that’s a more philosophical discussion.

DEFINITION: Algorithm = sequence of instructions which performs a meaningful task

How our brains workWe don’t often realize it but our brains

are pretty awesome. The brain is storing values and processing data (as well as producing outputs) all the time

Think of any system as comprising of these 3 parts

InputProcessing

Output

And this is where “Variables” come in.

Variables are about allocating a little memory space to store a value.

Inputs need to be “stored”

When I give you two numbersyour brain stores it automatically…

15 47Number 1 Number 2

You don’t consciously say – “right, now I am going to store number 1 and assign ita value of 15 and number 2 and assign it a value of 47…” it just happens instantly!”

AnalogyVariables are like boxes that can hold values in them.

Variable Name: NumberToStore FirstName

47 Joey

In a computer you need to declare variables ….

Num1=15Num2 =47

15 47Number 1 Number 2

Think of it like a block of space (MEMORY ALLOCATION) for the inputsYou are going to be processing

Example of declaring variables

We’ll be looking at Variables (how to implement these in Python) next …

So, like we’ve been saying – as programmers, we will need to save the values that our expressions evaluate so we can use them later.

We can store values in variables.

Think of variables like a box that can hold values. You can store values inside variables with the = sign (called the assignment operator).

For example, to store the value 15 in a variable named "spam", enter MyAge = 21 into the shell:

>>> MyAge = 21

MyAge

21

Assigning a value to a VariableFor example: Num1 = 37

Now, in the Shell, try this:Create a simple variable called Num1>>> Num1 = 3>>> Num13>>>

Now, Num1 evaluates to the value inside the variable, 3

Now Try this: If we type “Num1 + 5 into the shell, we get the integer 8, like so.>>> Num1=3>>> Num1 + 58>>>

Note that a variable is only a name for a value, we can write expressions with variables like this:>>> Num1 = 15>>> Num1 + Num130>>> Num1 – Num10>>>

Using Multiple Variables

It goes without saying that in the course of our

programming we will need to use

multiple variables.

Let’s try this: Assign different values to two

variables named DadAge and MumAg

e, like so:>>>DadAge = 37

>>> MumAge = 33

Now the DadAge variable has 37  inside it,

and MumAge has 33 inside it.

DadAge

MumAge

37

33

Without changing the value in our spam variable, let's try assigning a new value to the spam variable. Enter spam = fizz + eggs into the shell then enter spam into the shell to see the new value of spam. Can you guess what it will be?>>> DadAge = 37>>> MumAge = 33>>> AgesCombined = DadAge + MumAge>>> AgesCombined70>>>

Try it yourself

Variable NamesVariable names (as well as everything else in Python) are case-

sensitive.

Case-sensitive means the same variable name in a different case is considered to be an entirely separate variable name. So DadAge, dadAge, DAdAGe, and dadage will be considered 4 different variables in Python.

Naming convention = an optional but standard and accepted way of doing things) Capitalizing the first letter of each word (when you have two words) in variable names makes the program more readable.

DadAge rather than dadage

What have you learned?

Hopefully, you now understand that you can store values inside of variables so that your program can remember them in order to use them later on.

End of Lesson 3