EE 194 / Bio 196: Modeling biological systems

Slides:



Advertisements
Similar presentations
Intro to Robots Robots are Everywhere. Intro to Robots Various robots in use today lawnmower pet baby seal for convalescents gutter cleaner home security.
Advertisements

Lecture 2: Variables and Expressions Yoni Fridman 6/29/01 6/29/01.
Python Programming Chapter 2: Variables, expressions, and statements Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Python.
Introduction to Python
General Programming Introduction to Computing Science and Programming I.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Python Let’s get started!.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Welcome to the Basic Microsoft Word Guide. Before you start this Guide, you will need to complete “Basic Computer”; “Basic Windows” and know how to type.
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
JavaScript: Conditionals contd.
Math operations 9/19/16.
Any questions on today’s homework. (Sections 1. 6/1
10th World Studies Turn in: Take out: Today’s objective:
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Introduction to Computing Science and Programming I
CMPT 120 Topic: Python’s building blocks -> More Statements
Whatcha doin'? Aims: To start using Python. To understand loops.
Topic: Python’s building blocks -> Variables, Values, and Types
Topics Designing a Program Input, Processing, and Output
A Playful Introduction to Programming by Jason R. Briggs
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Python Let’s get started!.
Introduction to Python
Introduction to Python
Loops BIS1523 – Lecture 10.
Introduction To Repetition The for loop
Government Research Project compare and contrast essay
Topic: Python’s building blocks -> Variables, Values, and Types
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
Variables, Expressions, and IO
Thinking about programming
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
CS 240 – Lecture 11 Pseudocode.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Lesson 2: Building Blocks of Programming
CompSci 101 Introduction to Computer Science
CSE 341: Programming Langs
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Number and String Operations
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Teaching London Computing
Java Programming Arrays
Hello World! Syntax.
Fundamentals of Data Representation
Testing and Repetition
Introduction In today’s lesson we will look at: why Python?
Recursion Taken from notes by Dr. Neil Moore
CISC101 Reminders All assignments are now posted.
Topics Designing a Program Input, Processing, and Output
Tonga Institute of Higher Education IT 141: Information Systems
Fundamental Programming
Topics Designing a Program Input, Processing, and Output
Tonga Institute of Higher Education IT 141: Information Systems
EE 194/BIO 196: Modeling,simulating and optimizing biological systems
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Running a Java Program using Blue Jay.
EE 194/BIO 196: Modeling,simulating and optimizing biological systems
EE 155 / COMP 122: Parallel Computing
EE 194/Bio 196: Modeling biological systems
EE 194/BIO 196: Modeling biological systems
Class code for pythonroom.com cchsp2cs
Lecture 3 More on Flow Control, More on Functions,
Software Development Techniques
Week 1 - Friday COMP 1600.
Presentation transcript:

EE 194 / Bio 196: Modeling biological systems Spring 2018 Tufts University Instructor: Joel Grodstein joel.grodstein@tufts.edu Variables and such EE 194/Bio 196 Joel Grodstein

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

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

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

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

Variables save computation sum100_plus_2 = 1+2+3+4+5+…+100 + 2 sum100_plus_3 = 1+2+3+4+5+…+100 + 3 slow sum100 = 1+2+3+4+5+…+100 sum100_plus_2 = sum100 + 2 sum100_plus_3 = sum100 + 3 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

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 = 1 + 2 + 3 + 4 + 5 sum5_plus_1 = sm5 + 1 EE 194/Bio 196 Joel Grodstein

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

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

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

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

Comments What's with all of the '#' characters? PI = 3.14159 # 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

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

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

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

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

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

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

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

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

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

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

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 http://usingpython.com/python-programming-challenges (the variable-length banner problem) Read a bit from any of the Python resources in the syllabus EE 194/Bio 196 Joel Grodstein