1 / 12

Course A201: Introduction to Programming

Course A201: Introduction to Programming. 09/09/2010. Recap. [variable] a name that carries/represents some value You should give them values, before you use them because IDLE does not know what it is and what value it carries ex: service_rate = ‘good’ tip_percent = 0.15. Recap.

bowen
Download Presentation

Course A201: Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Course A201:Introduction to Programming 09/09/2010

  2. Recap [variable] • a name that carries/represents some value • You should give them values, before you use them because IDLE does not know what it is and what value it carries ex: service_rate = ‘good’ tip_percent = 0.15

  3. Recap [types] integer • ex: 1; 3; 999; 8019 floating point (float) • ex: 0.8; 87.467; 3.14159 String • ex: “Hi”, “you name is: ”, “@$%^%$”

  4. Recap [types] string: • consists of characters(letters, numbers, anything you can type in from keyboard) • always within single/double quotes • escape sequences: using back slash • ex: ‘good’; “Please enter: ”; ‘812000000’

  5. Recap [types] string: • strings cannot be convert or compared to integers/floats unless they only contains numbers and are converted using type conversion functions • Ex: float(service) -> ERROR! Ex: ‘1’ == 1, result is False Ex: a = ‘1’;a + 2 -> ERROR!

  6. Recap Type conversion functions: int(), str(), float() • Variables/Values on both side of “+”(concatenation) should be in the same type Ex: num1 = 2, num2 = 3 “Sum of ” + num2 + “ and ” + num2 -> ERROR! “Sum of ” + str(num2) + “ and ” + str(num2) • Be careful about logical errors

  7. Recap [functions] • input1, input2, input3, … • You give me some inputs I am a function. I will perform a certain job. • I give you some outputs back, explicitly or implicitly • output1, output2, output3, …

  8. Recap [functions: print] print: output things on the screen • Ex: date = ‘Sep. 23th’ print(‘Today is’,date) • All variables/values you put between parentheses are ARGUMENTS. (Important concept!!) Arguments are separated by comma -> , In the example above, you passes TWO arguments into the function print() • Here

  9. Recap [functions: print] print(“Sum of ” + num2 + “ and ” + num2) -> ERROR! print(“Sum of ”,num2, “ and ”, num2) -> CORRECT! • In the first line, the concatenation will be executed first, that’s when error happens. Variables/values on both sides of + should be in the same type • In the second line, there are 3 commas. So, you actually passed 4 arguments into function print. Different arguments can be in different types.

  10. Recap [functions: raw_input] raw_input: reads anything the user input from keyboard, and returns the value (store it in a variable you named) as a STRING • Ex: service_rate = raw_input(‘Please enter the service rate: ’) After this, no matter what the user entered, service_rate would be a string. • It is a prompt message

  11. Recap [Arithmetic operations] • % : called modulo/modulus, returns remainder • / : division • // : floor division, returns the quotient • ** : exponent, 2**3 is 8 • Due to version conflict, the safest way to perform any division, convert the integers to floating numbers first. • a += 1 is short for a = a + 1

  12. Have a nice weekend!

More Related