== Concepts == Programming: instructions to follow - writing a recipe - writing assembly instructions for a toy or ikea furniture - except actions are things like multiply, display a picture, retrieve a webpage rather than whip egg whites, insert tab A into slot C - ideally, English/Spanish/whatever - QU: why can't computers handle that? - so far, all we have are artificial languages like Python - human language is unambiguous "I saw the girl with the telescope" "I saw the girl with the dog" [just say it, don't try to draw it] - computer languages are NOT ambiguous a line of code means one thing and one thing only == Click on IDLE This is a Python shell. *** I will *save* the session - what I'm typing in - edit it (to remove my mistakes) - and post it on the webpage. So, you don't have to try to write everything down. >>> The prompt. instruction to Python Python reads it, executes it (does what you ask it to do), shows the result. We can use Python as a calculator + / Python is just calculating these numbers and displaying them for you. But we *often* use values more than once. variables reference values students = 90 lab_capacity = 30 labs_needed = students / lab_capacity #"=" not as we use it in Math. # Variable = value assigned to that variable #In Math: 4 + 5 = 11 - 2; doesn't make sense in programming # Let's see what happens 4 + 5 = 11 - 2 oops! # Variables in math and programming languages are very different! SO: consider this line labs_needed = students / lab_capacity What happens? students - get 90 lab_capacity - get 30 divide 90 by 30, to get 3 assign 3 to labs_needed # How do we calculate an average? my_points = 95 total_points = 112 my_score = my_points/total_points print ("My score on the BIO test was", my_score) if my_score >= .90: print ("Awesome!") elif my_score >= .80: print ("Doing fine") elif my_score >= .70: print ("So so") elif my_score < .70: print ("Uh oh!!") # We reference score several times; we only needed to calculate it once # Suppose the test has 120 points, rather than 100 total_points = 120 my_points = 93 my_score = my_points / total_points [So, the score is, out of all the total points, how many did I earn?] [But, you usually don't brag that we got a .93 on a test! Sounds like you didn't even get ONE POINT] [Percentages - we usually say, 93 percent, not .93.] [We need to move the decimal point 2 places to the right] # Move the decimal point 2 places to the right by ... # multiplying the result by 100! [Two 0's; moves the decimal point 2 places to the right!] [NOW: I don't want to have to type all the code in again.] [Go to File, Open - I have the file stored on my computer.] [New Window - would give you a fresh file.] [Run Module] Let's change that code: my_score = my_points / total_points * 100 change the .90 to 90, and so on Put in different values save, save as Now, let's have a storewide sale. # Storewide sale - 20% off. 20% is .2 20/100 20/100 original_price = 100 discount_percentage = .2 discount = original_price * discount_percentage sale_price = original_price - discount sale_price print(sale_price) # How else could we have solved this problem? Still needs a * and a - # If the dress is 20% off, then the new price is 80% of the original # price. Or, .8 # If it is 30% off, then the new price is 70% of the original. Or, # .7 #SUBTRACT THE DISCOUNT PERCENTAGE FROM 1, and multiple the original # price by that! sale_price = original_price * (1 - discount_percentage) sale_price Let's create a program, and see what else IDLE can do. File, new window Type this in students = 90 lab_capacity = 30 labs_needed = students / lab_capacity print("We need",labs_needed,"labs) Save it Run it [phew, that's enough for the first day. Let's go back over things. [read chapter 1 and appendix B for Wednesday. We *are* having labs this week - W and F. The UG TAs will be there, and I'll stop in. You will work in pairs in the labs You get full credit if you make an effort. The work in this class is steady - no huge project or paper dumped on you BUT: it is STEADY! If you haven't programmed before - *this class is for people who haven't* There is no way to cram at the last minute. Need to build up over the semester. Download Python 3* on your own computers, so you can try things yourself. [If you are using a MAC, and get a message that Tcl is not up to date, just go to the website, and download and install the version listed in the table. You don't have to do anything else.]