Presentation is loading. Please wait.

Presentation is loading. Please wait.

EE 194 / Bio 196: Modeling biological systems

Similar presentations


Presentation on theme: "EE 194 / Bio 196: Modeling biological systems"— Presentation transcript:

1 EE 194 / Bio 196: Modeling biological systems
Spring 2018 Tufts University Instructor: Joel Grodstein Variables and such EE 194/Bio 196 Joel Grodstein

2 Variables as little plastic buckets
Do a demo with little named buckets as variables EE 194/Bio 196 Joel Grodstein

3 Variables One of the most basic concepts in programming
But easy to get very confused for a newcomer. Why do we care? What problem(s) do variables solve? They are a place to keep a number (or a string, or most anything) They let you give an intuitive name to a number EE 194/Bio 196 Joel Grodstein

4 The basics sum = 7 sum = 8 sum = 3 + 4 Observations:
# Now sum is the number 7. U 8 7 # Now it’s not 7 any more! sum # Now it is 7 again Observations: This is not like algebra. A variable can change its value! Statements come in an order. If we change the order of the statements above, we can leave “sum” at 8 rather than at 7. “sum” just holds a number. Assigning it 7 or 3+4 are indistinguishable after the fact. Terminology: what we’re doing above is called assigning a value to a variable. The first assignment is often called initialization. By the way, everything after a # sign is just a comment in Python. EE 194/Bio 196 Joel Grodstein

5 It’s really not like algebra
Consider this code: i=5 i=i+2 # Pretty obvious 7 9 U 5 # Now i=7 i # Now i=9 In algebra i=i+2 would just be impossible. Here, it says to First, compute i+2 (i.e., 5+2); it’s 7 Next, assign 7 to i. The next time, we get 7+2=9. This will turn out to be really useful… now computers can count! EE 194/Bio 196 Joel Grodstein

6 Variables save computation
sum100_plus_2 = … sum100_plus_3 = … slow sum100 = …+100 sum100_plus_2 = sum sum100_plus_3 = sum fast and more clear Reusing a variable means the big long sum is only computed once. And it’s more clear (to most people). Terminology: the 2nd and 3rd lines are accessing the variable “sum100.” Any advantages of the bottom method over the top one? EE 194/Bio 196 Joel Grodstein

7 Computers are very picky!
What's wrong with this code? sm5 is uninitialized What happens? In some languages (including Python), accessing an uninitialized variable is illegal In some languages (e.g., C++) it’s unpredictable. Some languages start all variables with some default (e.g., 0). sum5 = sum5_plus_1 = sm5 + 1 EE 194/Bio 196 Joel Grodstein

8 Variables help clarity
x=19 y=x*12 z=x*365 What does this code do? age_in_years = 19 age_in_months = age_in_years*12 age_in_days = age_in_years*365 Which version is more clear? age_in_years = 19 age_in_days = age_in_years*12 age_in_months = age_in_years*365 Nice descriptive names are not real helpful if they’re wrong! EE 194/Bio 196 Joel Grodstein

9 Another benefit of variables
age_in_years = age_in_months = age_in_years*12 age_in_days = age_in_years*365 19 18 age_in_years = 19 age_in_months = 19*12 age_in_days = 19*365 Any benefits of the top version over the other one? With just a few keystrokes, you can change “19” to 18 or 20, and re-run the program. EE 194/Bio 196 Joel Grodstein

10 Strings Variables do not have to just be numbers. Strings work too.
prints out Hello world (just like you did in the lab) Basically, a string is any collection of characters (letters, numbers, punctuation, etc). message = 'Hello world' print(message) EE 194/Bio 196 Joel Grodstein

11 Fun with strings You can do lots of things with strings
name = 'jiminy cricket' name = "jiminy cricket" name = "jiminy cricket' name = "O'hare airport" len ('jiminy') len (name) # No different # Not legal! # Works as expected # 6 # 14 ("O'hare airport") len() is a function More on that later EE 194/Bio 196 Joel Grodstein

12 Comments What's with all of the '#' characters?
PI = # March 14 is pie day Computer programs can get big. And complicated. And confusing Trying to understand somebody else’s program can be difficult, and frustrating. You could just ask them for help They might not like that They may have graduated 5 years ago When you write code, sometimes a word (or sentence, or paragraph) of explanation for the uninitiated is a really good idea Comments let you do that In some of our HWs, you will have to take code that I've written and add to it. My code is liberally commented (If you still don't understand it, it's OK to ask me). EE 194/Bio 196 Joel Grodstein

13 Comments What do these do?
PI = # This one assigns to PI # PI = # This one does nothing at all! EE 194/Bio 196 Joel Grodstein

14 Expressions a = 2+3*5 # yields 17 a = (2+3)*5 a =7 % 4 # yields 25
a=pow(2,3) b = 'ab'+'cd'+'ef' c='12'+b+'gh' b = 3 * 'ab' b = 3 + 'ab' # yields 17 # yields 25 # yields 3 (modulus, or remainder) # yields 8 # also yields 8 # yields 'abcdef' # yields '12abcdefgh' # yields 'ababab' # Illegal EE 194/Bio 196 Joel Grodstein

15 More picky rules Names of variables are case sensitive
Size=4 a = size*2 # error Names cannot begin with numbers or special characters 3squared = 3*3 # Illegal Squared3 = 3*3 # Legal Names can never have a special character three^2 = 3*3 # Illegal three_sqr = 3*3 # Legal. “_” is OK. threeSqr = 3*3 # Another common usage EE 194/Bio 196 Joel Grodstein

16 So many little rules If you don’t learn all of these picayune little rules now, you’ll discover them on your own (one by one) when you try to write your programs. Your programs will break for inexplicable reasons (well, inexplicable to you, anyway) You might suffer from death by 1000 paper cuts Or you might just google these rules and find them out yourselves Or you might even get motivated to read the documentation! (see the syllabus for details) EE 194/Bio 196 Joel Grodstein

17 Printing All of this computation is only useful if you can see what it did . So let’s talk about printing. Python can print in several ways. EE 194/Bio 196 Joel Grodstein

18 print print() is a function that prints things. print(3) # prints 3
print('a=',a) # prints a= 3 (or whatever the print (3, 5) # 3 5 Python can print with fancy formatting (e.g., remove the space, centered in an 8-wide field, …) Not needed in this course Google Python string.format() for details variable a actually is) EE 194/Bio 196 Joel Grodstein

19 Python statement syntax
More detail when we talk about loops. For now… No spaces at the beginning of a line before a statement One statement cannot span multiple lines Except if you end a line with '\' Or after a comma inside parentheses bad a=3 b=4 a=3 + 4 bad OK OK a=3 + \ 4 print ('a=', a) EE 194/Bio 196 Joel Grodstein

20 Python statement syntax
Most languages are free form; they ignore spaces and newlines. Except Python. High on the list of the best things about Python Also high on the list of the worst things about Python – sort of a religious dispute. EE 194/Bio 196 Joel Grodstein

21 Recitation The recitation for HW1 is today or tomorrow (room 122, the PC lab). HW1 uses what we learned today. It also uses loops, which we won’t cover until Jan 30! Well, yeah, this is part of life HW1 isn’t due until Feb 8 Some options to help you: Don’t do that part of the HW until after we cover loops Read the lecture on loops (it’s on the class web page) Read the documentation Google “python help for statement" Ask for help Just wing it EE 194/Bio 196 Joel Grodstein

22 Group activity What do these do? a = 4+3*5 a = (2+4)*5 # sets a to 19
b = 'ab'+'cd'+'ef' b*2 print (b,b) print (b, 'x'+'y') h=2; print (h, 'heads is',h*'head') # sets a to 19 # sets a to 30 # sets b to 'abcdef' # yields 'abcdefabcdef ' # prints abcdef abcdef # prints abcdef xy # prints 2 heads is headhead EE 194/Bio 196 Joel Grodstein

23 Follow-up activities Try the examples from this lecture yourself
Vary them, or even mis-type some to see what happens More exercises. Write a program that… takes the radius r of a circle and computes the area takes a number n and computes the value of n+ n*n + n*n*n. Start with a variable name and print a banner as in (the variable-length banner problem) Read a bit from any of the Python resources in the syllabus EE 194/Bio 196 Joel Grodstein


Download ppt "EE 194 / Bio 196: Modeling biological systems"

Similar presentations


Ads by Google