Goals for toda Lecture 2 Variables Assignment Sequential Steps Sequential Steps, Variables, Assignment Computer Memor If we can t remember things, we can t actuall do ver much Computers have memor the abilit to remember information Computer Memor Computer memor is organied in a hierarch As ou go higher in the hierarch, the memor becomes slower to access, but ou get more, and it is more permanent Offline Memor (e.g. Cloud) Secondar Memor (Files) Main Memor Cache (near CPU) CPU - registers Slower to Access More total data More long-lasting Faster to Access Less total data Less permanent Computer Memor When we sa memor, we ll be referring to main memor Offline Memor (e.g. Cloud) Secondar Memor (Files) Main Memor Cache (near CPU) CPU - registers Slower to Access More total data More long-lasting Faster to Access Less total data Less permanent Computer Memor Later in the course, we ll talk about handling files Offline Memor (e.g. Cloud) Secondar Memor (Files) Main Memor Cache (near CPU) CPU - registers Slower to Access More total data More long-lasting Faster to Access Less total data Less permanent 1
Computer Memor We ll ignore the lowest levels of memor in this class. Offline Memor (e.g. Cloud) Secondar Memor (Files) Main Memor Cache (near CPU) CPU - registers Slower to Access More total data More long-lasting Faster to Access Less total data Less permanent Main Memor Think of main memor as being a bunch of boes that hold information. We will call these boes of memor variables Main Memor Variables and Names The (Pthon) rules for naming A variable (a bo of memor) can hold some unit of information e.g. a number, like the number 19 We need a wa to refer to a particular variable, to distinguish it from the other boes of memor. This will be the variable name. e.g. Think of the name as the label for the bo 19 Names can start with a letter (lowercase or uppercase) or an underscore (_). But, ou generall shouldn t start them with an _ since this tends to impl certain things about the variable. The name can contain letters (lowercase or uppercase), numbers, and underscore characters. The name cannot be a reserved keword These are special commands reserved for the langu itself. The include words like for and while. You will learn these as ou learn to program. Naming Variables and Constants The importance of names So ou could name all our variables to minimie tping e.g. a, b, c, But this would result in confusion later what do these mean? A better wa of thinking about variable (and other) names ou choose in code is to think of communicating with other people You are communicating with anone ou are sharing the code with, including anone who has to take over our code But most importantl to ou, ou are communicating with the future ou the ou that will have forgotten man of the details of the code ou are going back to after doing other things Names help us understand the world, identif particular things, and so on. In computers, the name of a variable distinguishes one particular bo of memor. But, a good name can give more information about the purpose for that piece of memor. Just because a name is valid, doesn t mean it s good. 2
Are these names valid and good? abcde Age m_name M_Name MName 2nd_name name_2nd Winner! F0rTheW1n _densit Gig Em Gig Em Gig-Em Gig_Em Valid but not good (meaningless) Valid and good Valid and good Valid and good (and different from previous) Valid and good (and different from previous) Not valid (starts with a number) Valid, and probabl good Not valid (contains!) Valid, but not good (digits are confusing) Valid, probabl not good (begins with _) Not valid (contains ) Not valid (contains a space) Not valid (contains a -) Valid, but probabl not good (not clear what it contains) More about naming Generall, pick descriptive names Volume might be a better choice than V Not too long, though Volume_of_the_sphere is probabl too long V_sphere, or VolSphere, or Vsphere, etc. might be better names There are a few conventions people use: Constants (that never change) often in ALL_CAPITALS Variables i, j, and k often used for counting or indeing Variables tpicall start with lower-case letters Determining the Variables You Need: Mapping Problems to Software Mapping problems to software representations involves answering a number of questions. What question are ou tring to answer using the software? What are the characteristics of the problem that affect the outcome? How do the affect the outcome and how do the interact? Who is using the software ou re creating? Answers to the second question are likel to help determining what variables our software will have The other questions affect the design and the individual operations Problem Taken from Your Own Life One of the most important skills engineering students need is to be able to effectivel allocate their time to their studies. What questions do we want to answer? How effective are man short, a middle number of medium-length, or a few long stud sessions? How might other aspects of ourselves affect the outcome? What characteristics affect the outcome? The length of stud sessions The number of stud sessions How tired, stressed, hungr, etc. ou are Names for our Stud Session Problem Important concepts/values Time spent studing in a session t advant is it is short, disadvant is it is ambiguous with lots of other concepts time better, but this could be time of starting stud session, time of da, etc. length more accurate, but could be confusing because most often associated with phsical dimensions session_length even better, but what is the units (seconds, minutes, hours) session_length_min good tr, but could be read as the minimum length of a session session_length_minutes prett likel to be understood, of course, it is now a long name Rate of learning concepts/skills concepts_per_minute Number of concepts learned in a session concepts_learned_in_session Writing software requires trade-offs One that will be apparent throughout this course is the trade-off between ease of comprehension and compactness Variable Values When we see a variable name in a program, that is a placeholder for the value contained in that variable s bo of memor. : sa the variable holds value 19. What would be the value of +? + = 19+ = 22 19
Assignment The process of sticking a value into the bo of memor is called assignment. Can assign to an variable new or old When a new value is assigned to a variable, the old value disappears it is replaced b the new one Can (and often does) happen man times for the same variable Assignment statements Pthon assignment statements have the form: <Variable name> = <Value to assign> 19 Assign 0 to 0 Assignment statements Pthon assignment statements have the form: <Variable name> = <Value to assign> The left hand side is the variable name for the bo in memor that will hold the value. Assignment statements Pthon assignment statements have the form: <Variable name> = <Value to assign> The = is the assignment operator. It is NOT the equals sign, though we still often read it aloud as equals. We can read it aloud as gets or is assigned. 19 19 Assignment statements Pthon assignment statements have the form: <Variable name> = <Value to assign> The right hand side of the assignment operator is the value that will be assigned to the bo Assignment statements Pthon assignment statements have the form: <Variable name> = <Value to assign> When this statement is eecuted: We FIRST evaluate the right hand side Then, we assign that value to the left hand side Some eamples follow 19 4
=+1 10 =+1 1. We first find what is the value in variable, which is. 2. Then, we add +1 to get 4.. Then, we assign the value of 4 to variable, replacing the value previousl stored there. 4 =+2* =+2* 1. We first find what is the value in variables and, which are and. 10 2. Then, we multipl 2 times (order of operations!), to get 10.. Then, we add to 10, to get 1 1 4. Then, we assign that value, 1, to variable, replacing the value previousl stored there. =+1 10 =+1 1. We first find what is the value in variable, which is 10. 2. Then, we add 10+1 to get 11.. Then, we assign the value of 11 to variable, replacing the value previousl stored there. 11
=+1 Notice: the use of the same variable on the right and left side is not a problem! While this statement would make no sense if the = reall meant equals, it makes perfect sense since = is actuall an assignment operation. 11 Giving Instructions If ou were asked to give a stranger directions from one place to another, how would ou do it? Drive straight to the net light Turn right Drive 1 mile Take the entrance ramp to the highwa Drive until eit 91 This is a sequence of steps that the person should follow Sequential Steps Sequential Steps We are used to giving and receiving instructions as a sequential series of steps. This is a ver important process learning how to break up a comple task in various was is critical to programming, and to dealing with an large project Including man parts of engineering One of the main was this is done is to create a sequence of steps You ll get some practice with this in lab We are used to giving and receiving instructions as a sequential series of steps. When we give instructions to a computer, we ll do the same thing Our instructions will be a sequence of steps that we want the computer to do We can think of the computer as mindlessl following those instructions, in the order the re specified Later, we will see was of giving instructions that aren t sequential! Assignment sequences Assignment sequences What is the value of at the end? (i.e. what is the value stored in the variable named = = =1 What is the value of at the end? (i.e. what is the value stored in the variable named Net = = =1 It can be helpful to keep track of the instruction pointer that is, what the net instruction to be eecuted is. We can think of the program as being at these points between statements. This starts out in front of the first statement. 6
Assignment sequences Assignment sequences What is the value of at the end? (i.e. what is the value stored in the variable named What is the value of at the end? (i.e. what is the value stored in the variable named Net = = =1 After the first statement is eecuted, the variable has the value stored inside. Net = = =1 After the second statement, has the value. Assignment sequences Assignment sequences (2) Net What is the value of at the end? (i.e. what is the value stored in the variable named = After the third statement, is 1. = =1 1 What is the value of at the end? (i.e. what is the value stored in the variable named = = = =1 Assignment sequences (2) Assignment sequences (2) Net What is the value of at the end? (i.e. what is the value stored in the variable named = = = =1 Net What is the value of at the end? (i.e. what is the value stored in the variable named = = = =1 7
Assignment sequences (2) Assignment sequences (2) Net What is the value of at the end? (i.e. what is the value stored in the variable named = = = =1 Net What is the value of at the end? (i.e. what is the value stored in the variable named = = = =1 Notice that the value of changed, but NOT the value of. The third statement, assigning = was just assigning the current value of to, not making forever equivalent to. 1 Assignment sequences () Assignment sequences () PI.1419 PI PI=.1419 r=. volume=pi*r*r*r*(4/) r Net PI=.1419 r=. volume=pi*r*r*r*(4/) r volume volume Assignment sequences () Assignment sequences ().1419 PI.1419 PI Net PI=.1419 r=. volume=pi*r*r*r*(4/) r. Net PI=.1419 r=. volume=pi*r*r*r*(4/) r. volume 88.977 volume 8
2 =2 = =* =+2 =* =+1 Net =2 = =* =+2 =* =+1 2 4 Net =2 = =* =+2 =* =+1 Net =2 = =* =+2 =* =+1 4 4 Net =2 = =* =+2 =* =+1 Net =2 = =* =+2 =* =+1 20 9
Net =2 = =* =+2 =* =+1 4 21 Special Assignment Operators Certain tpes of operations are ver common. Because of this, some special assignment operators have been defined. = + c Can be written: += c Means, for the variable on the left, increment b the amount on right Similarl for: -=, *=, /=, etc. e.g. a-= means a=a- Assignment sequences () Assignment sequences () 2 =2 = *=4 +=7* =+ -=+1 Net =2 = *=4 +=7* =+ -=+1 Assignment sequences () 2 Assignment sequences () 8 Net =2 = *=4 +=7* =+ -=+1 Net =2 = *=4 +=7* =+ -=+1 The variable gets its prior value,, multiplied b 4. This was equivalent to: =*4 10
Assignment sequences () Assignment sequences () 8 8 Net =2 = *=4 +=7* =+ -=+1 The variable has its value incremented b 7 times the value of, or 6. This was equivalent to: =+7* 9 Net =2 = *=4 +=7* =+ -=+1 9 67 Assignment sequences () A reminder of output Net =2 = *=4 +=7* =+ -=+1 The variable has its value decremented b the value of plus one. This was equivalent to: =-(+1) 8 9-1 Basic output is to the console a window or the screen that shows the output of the program. To show the value of a variable, we print it Command is: print() where is the thing ou want to be printed. The can be: a constant fied value (a literal ) a variable or an epression a combination of literals, constants, variables, functions, etc. that compute to a single value Output Output To print the value of a variable, then, we will write print(), where is the variable name. : The following program a = 10 b = 2**4 b -= a c = b//2 print(c) To print the value of a variable, then, we will write print(), where is the variable name. : The following program a = 10 b = 2**4 b -= a c = b//2 print(c) Outputs:???? Outputs: 11
This week s Lab and Assignment Goal is to practice variable manipulation and sequential statements 12