1 / 26

COMPSCI 101 S1 2014 Principles of Programming

COMPSCI 101 S1 2014 Principles of Programming. 13 Revision. Agenda. The Three Rules Name Conversion Types Variables Statements Operators selections Loops Lists. The Three Rules. Rule 1: Think before you program Rule 2:

jersey
Download Presentation

COMPSCI 101 S1 2014 Principles of 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. COMPSCI 101 S1 2014Principles of Programming 13 Revision

  2. Agenda • The Three Rules • Name Conversion • Types • Variables • Statements • Operators • selections • Loops • Lists COMPSCI101

  3. The Three Rules • Rule 1: • Think before you program • Rule 2: • A program is a human-readable essay on problem solving that also happens to execute on a computer • Rule 3: • The best way to improve your programming and problem solving skills is to practice. COMPSCI101

  4. Python Name Conventions • must begin with a letter or underscore _ • Ab_123 is OK, but 123_ABC is not. • may contain letters, digits, and underscores • this_is_an_identifier_123 • may be of any length • upper and lower case letters are different • Length_Of_Rope is not length_of_rope • the standard way for most things named in python is lower case with separate words joined by an underline: • my_list NameError: name 'length_of_rope' is not defined COMPSCI101

  5. Python “Types” • integers: 5 • floats: 1.2 • booleans: True/False • strings: "anything" or 'something' • lists: [] ['a',1,1.3] • … • What is a type? • a type in Python essentially defines two things: • the internal structure of the type (what is contains) • the kinds of operations you can perform • 'abc'.isalpha()is a method you can call on strings, but not integers COMPSCI101

  6. Type Conversion • int(some_var)returns an integer • float(some_var)returns a float • str(some_var) returns a string ERROR int(2.1) int('2') int('2.1') float(2) float('2.0') float('2') float(2.0) str(2) str(2.0) str('a') COMPSCI101

  7. Operators • Integer • addition and subtraction: +, - • multiplication: * • division • quotient: / • integer quotient: // • remainder: % • exponentiation: ** • Floating point • add, subtract, multiply, divide: +, -, *, / COMPSCI101

  8. Two Types of Division • The standard division operator (/) yields a floating point result no matter the type of its operands: • Integer division (//) yields only the integer part of the divide (its type depends on its operands): 2 / 3 4.4 / 2 5 / 2 0.6666666666666666 2.2 2.5 0 2.0 2 2.0 2 // 3 4.4 // 2 5 // 2 5 // 2.0 COMPSCI101

  9. Modulus & Exponentiation • The modulus operator (%) give the integer remainder of division: • Again, the type of the result depends on the type of the operands. • The exponentiation operator (**) 2 1.0 5 % 3 7.0 % 3 2 ** 3 4.0 ** 3 4.0 * 3 8 64.0 12.0 COMPSCI101

  10. Order of operations and parentheses • Remember, parentheses always takes precedence COMPSCI101

  11. Exercises • What is the result of the following expression? 25 / 4 + 4 * 10 % 3) 25 - 7 * 3 + 12 / 3 17 % 3 * 2 - 12 + 15 COMPSCI101

  12. Rounding • The round(x) function returns the value x rounded to an integer • Note: The round(x) function rounds 'halfway' numbers (e.g. 2.5, 4.5, -0.5) to the nearest eveninteger print (round(2.3), round(3.2), round(3.9)) print (round(2.5), round(3.5), round(4.5)) COMPSCI101

  13. Variable & Statements • Variable • A variable is a name we designate to represent an object (number, data structure, function, etc.) in our program • We use names to make our program more readable, so that the object is easily understood in the program • Statements • Statements are commands in Python. • They perform some action, often called a side effect, but they do not return any values COMPSCI101

  14. Printing output • The print() function takes a list of elements in parentheses separated by commas • if the element is a string, prints it as is • if the element is a variable, prints the value associated with the variable • The print statement inserts a blank between objects when objects are separated by “,” • after printing, moves on to a new line of output my_var = 12 print('My var has a value of: ', my_var) a = 1 b = 2 c = 3 print(a, b, c) COMPSCI101

  15. Assignment Statement "=" • In many computer languages, = means assignment. • my_int = my_int + 7 • lhs = rhs • What assignment means is: • evaluate the rhs of the = • take the resulting value and associate it with the name on the lhs • It does not stand for equality! • What does the following code print to standard output? x = 9 y = 4 s = " + " print(x - y , s , y + y) a = 5 b = 10 c = 0 c = a a = b b = c print (a, b, c)) COMPSCI101

  16. Exercise • What does the following code print to standard output? a = 5 b = 10 a = a + b b = a - b a = a - b print (a, b) a = 5 b = a + 2 c = b + 2 d = c + 2 c = c // 2 * 2 b = b // 2 * 2 print (a, b, c, d) a = 3 b = 12 c = 6 d = 1 d = d * a c = c + 2 * a d = d - b // c c = c * b % c b = b // 2 print (a, b, c, d) a = 9 b = a % 11 c = a / 2 d = a % 2 print (a, b, c, d) COMPSCI101

  17. Modules • Modules are files that can be imported into your Python program. • Example is the math module • we import a module to use its contents • we use the name of the module as part of the content we imported • Example: import math print(math.pi) COMPSCI101

  18. Selection • Selection is how programs make choices, and it is the process of making choices that provides a lot of the power of computing if condition : indented_Code_Block if condition : indented_Code_Block_For_True_Condition else: indented_Code_Block_For_False_Condition COMPSCI101

  19. if, elif, else, the process • evaluate Boolean expressions until: • the Boolean expression returns True • none of the Boolean expressions return True • if a boolean returns True, run the corresponding block. Skip the rest of the if • if no boolean returns True, run the else block if condition1 : indented_Code_Block_For_True_Condition_1 elif condition2 : indented_Code_Block_For_True_Condition_2 elif condition3 : indented_Code_Block_For_True_Condition_3 else: indented_Code_Block_ForEachCondition_False COMPSCI101

  20. Exercises • What does the following code print to standard output? x = 10 y = 5 if x < 10: if y != 5: print ("a") else: print ("b") elif y > 10: print ("c") else: if y != 5: print ("d") else: print ("e")) a = 4 b = 12 c = 37 d = 51 if a < b : print( "a < b" ) if a > b : print( "a > b" ) if d <= c : print( "d <= c" ) if c != d : print( "c != d" ) COMPSCI101

  21. Repeating statements • Besides selecting which statements to execute, a fundamental need in a program is repetition • repeat a set of statements under some conditions • The for statement is useful for iteration, moving through all the elements of data structure, one at a time. for item in sequence: indented_Code_Block COMPSCI101

  22. Exercises • What does the following code print to standard output? my_list = [1,2,3] b = 0 for y in my_list: b = y print (b) my_list = [1,2,3] a = 0 for y in my_list: a = a + y print (a) COMPSCI101

  23. Terms Test • Section A (75 marks) • 20 MCQ (2.25 minutes per question) • What is the output of the following code… • The following code should…. Which expression should be used in …? • Given the following code… which one of the following function calls will… when executed? • Section B (25 marks) • 3 questions (15 minutes for 3 questions) • Tracing Code • Write a Function • Understanding Python code COMPSCI101

  24. Tuesday 1st April 7:30pm - 8:30pm. • The test is worth 15% of your final mark. • Please bring your Student Id card, a pencil and an eraser • Material to be examined: Lectures 1 - 9 and Labs 1 - 4 • The test is a closed book test, so you cannot refer to any material during the test. • Calculators are also not permitted • Please notify Angela Chang if you have a test clash • Please arrive by 7:15pm as you will be given 5 minutes' reading time. • Please read the instructions on filling out a Teleform sheet before you go to the test COMPSCI101

  25. Teleforms • Fill in your Student ID Number in the STUDENT ID# section. • Also fill in one column for each digit of your Student ID Number. • The example below shows a Student ID Number of 8677777 (with 7 digits) COMPSCI101

  26. Room Allocations • Based on the first letter of your surname: • A - C: Room LgeChem/301-G050 • D - J: Room LibB28/109-B28 • K - L: Room MLT1/303-G23 • M - P Room PLT1/303-G20 • Q - T Room HSB1/201N-346 • U - Z: Room LibB15/109-B15 COMPSCI101

More Related