Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lesson 3 Variables – How our Brains Work - Variables in Python."— Presentation transcript:

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

2 Ever wondered how our brains work?!?! http://www.youtube.com/watch?v=wmcHH4wPpMU

3 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].Reber

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

5 HOW OUR BRAINS WORK Algorithms – (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

6 How our brains work We 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

7 And this is where “Variables” come in. Variables are about allocating a little memory space to store a value. Inputs need to be “stored”

8 When I give you two numbers your brain stores it automatically… 15 47 Number 1 Number 2 You don’t consciously say – “right, now I am going to store number 1 and assign it a value of 15 and number 2 and assign it a value of 47…” it just happens instantly!”

9 Analogy Variables are like boxes that can hold values in them. Variable Name: NumberToStore FirstName 47Joey

10 In a computer you need to declare variables …. Num1=15 Num2 =47 15 47 Number 1 Number 2 Think of it like a block of space (MEMORY ALLOCATION) for the inputs You are going to be processing Example of declaring variables

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

12 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

13 Assigning a value to a Variable For example: Num1 = 37

14 Now, in the Shell, try this: Create a simple variable called Num1 >>> Num1 = 3 >>> Num1 3 >>> 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 + 5 8 >>> Note that a variable is only a name for a value, we can write expressions with variables like this: >>> Num1 = 15 >>> Num1 + Num1 30 >>> Num1 – Num1 0 >>>

15 Using Multiple Variables

16 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 MumA ge, like so: >>>DadAge = 37 >>> MumAge = 33 Now the DadAge variable has 37 inside it, and MumAge has 33 inside it. DadAge MumAge 37 33

17 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 >>> AgesCombined 70 >>> Try it yourself

18 Variable Names Variable 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

19 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.

20 End of Lesson 3


Download ppt "Lesson 3 Variables – How our Brains Work - Variables in Python."

Similar presentations


Ads by Google