cisc101 reminders notes a minor in computing
play

CISC101 Reminders & Notes A Minor in Computing? Test 1 this - PowerPoint PPT Presentation

CISC101 Reminders & Notes A Minor in Computing? Test 1 this week Are you interested in finding out what is required for a Minor in Computing? Will take place in your tutorial Lab will still follow Please contact our


  1. CISC101 Reminders & Notes A Minor in Computing? • Test 1 this week • Are you interested in finding out what is required for a Minor in Computing? – Will take place in your tutorial – Lab will still follow – Please contact our Undergraduate Chair – Dr. Bob Tennent ( rdt@cs.queensu.ca) • Assignment 1 should be marked in two weeks • Assignment 1 should be marked in two weeks – Look for your grade in Moodle – Contact your TA if you have any questions or concerns • Assignment 2 will be posted next week Winter 2011 CISC101 - Whittaker 1 Winter 2011 CISC101 - Whittaker 2 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod From Last Time … Documentation Strings • Put a string literal right after the def … statement • Supplemental notes from Jan. 25 th – This is a documentation string – Functions • It will be regurgitated by the help system – Variable scope – Can also be accessed using special variables – Demo: WindowWeight.py • Notes from Jan. 25 th • Try typing help(main) at the >>> prompt • Try typing help(main) at the >>> prompt – Global variable demo • Use triple quotes to place a large document • Slides 28 • Typically used when creating object definitions – Programming style – Classes (which we won’t be doing) • Slides 29-35 • Not a bad habit to get into … Winter 2011 CISC101 - Whittaker 3 Winter 2011 CISC101 - Whittaker 4 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  2. So Far … Branching Algorithms • So far, all our programs have been linear • If a program could test a condition, then it would have a choice as to its path of execution Obtain data from user if true if true if false if false Work with data Work with data (e.g., calculations) Display results etc. etc. Winter 2011 CISC101 - Whittaker 5 Winter 2011 CISC101 - Whittaker 6 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod if-else Statement Conditionals or “Selection Statements” • Syntax of if-else statement • Python has if , if-else and chained if ( if- elif-else ) statements if boolean_expression : • if statement syntax statement(s)_when_true else : if boolean_expression : statement(s)_when_false statement1_when_true statement2_when_true • Example statement3_when_true if num < 0 : … print(“Negative number – using absolute value") num = abs(num) • Example else : if num < 0 : print(“Non-negative number - OK") print(“Negative number") Winter 2011 CISC101 - Whittaker 7 Winter 2011 CISC101 - Whittaker 8 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  3. Boolean Expressions Unary Boolean Logical Operators • We need to know how to construct expressions • Python has a Boolean logical operator not that evaluate to a bool – Must be used on a single bool value – a bool in Python is either True or False – Sets False to True • Use Boolean operators to create expressions – Sets True to False • Just like the Boolean logical operator NOT (!) – Work like mathematical operators, but they produce Booleans instead of numbers Booleans instead of numbers • Boolean operators are either unary or binary a (! a ) – Unary requires one data value 0 1 • e.g. , NOT a 1 0 – Binary requires two data values • e.g. , a < b, a OR b Winter 2011 CISC101 - Whittaker 9 Winter 2011 CISC101 - Whittaker 10 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod Binary Boolean Logical Operators Binary Boolean Operators • Python has Boolean logical operators and and or > greater than – Must be used on two bool values < less than – Work just like the Boolean logical operators AND ( ∧ ) and OR ( ∨ ) >= greater than or equal to <= less than or equal to All of these result in All of these result in a a b b ( a ∧ b ) ( a ∧ b ) a a b b ( a ∨ b ) ( a ∨ b ) a True or False == equals 0 0 0 0 0 0 bool value != not equals 0 1 0 0 1 1 1 0 0 1 0 1 and Boolean logical AND 1 1 1 1 1 1 or Boolean logical OR Winter 2011 CISC101 - Whittaker 11 Winter 2011 CISC101 - Whittaker 12 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  4. Binary Boolean Operators - Cont. Binary Boolean Operators - Cont. • The Python interpreter wants both sides to be the > greater than same data type < less than These must have a – Two numeric values number on both >= greater than or equal to – Two string values sides or a string on <= less than or equal to both sides – do not both sides – do not • You will get an odd result if you try to compare a • You will get an odd result if you try to compare a == equals mix types string to a number != not equals These must have and Boolean logical AND a bool value on or Boolean logical OR both sides Winter 2011 CISC101 - Whittaker 13 Winter 2011 CISC101 - Whittaker 14 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod Boolean Operators - Cont. Boolean Expression Examples Precedence rules are now expanded! 5 > 2 4 < 3 or 7 > 1 1) Math operators in the usual order: ** 7 != 8 - (numeric negation) 6 + 2 > 9 or 4 == 3 + 1 * / // % 7 == 7 and 5 > 2 and 6 != 3 7 == 7 and 5 > 2 and 6 != 3 + - (subtraction) + - (subtraction) 2) Binary Boolean comparison operators: 5 * 5 >= 5 ** 2 > >= < <= == != 128 % 2 == 0 3) Logical Boolean operators: not and or 4) Assignment operator: = Winter 2011 CISC101 - Whittaker 15 Winter 2011 CISC101 - Whittaker 16 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  5. Evaluating Another Example Yet Another Example • Of course, these expressions can be used with not(5 * 4 > 3) or 2 + 3 <= 5 and 6 == 2 variables not(20 > 3) or 2 + 3 <= 5 and 6 == 2 – The values of the variables are used by the operators not(True) or 2 + 3 <= 5 and 6 == 2 • What is printed out in this case? False or 2 + 3 <= 5 and 6 == 2 a = 12 a = 12 False or 5 <= 5 and 6 == 2 False or 5 <= 5 and 6 == 2 b = 5 False or True and 6 == 2 c = 2 False or True and False print(a > b and b * c < a) True and False False Winter 2011 CISC101 - Whittaker 17 Winter 2011 CISC101 - Whittaker 18 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod if Statements - Cont. Nested if Statements • You will often have to nest if statements • For example, what if you have three numbers a , b & c and you want to print them in order? – Demo: SortThree.py T F a < b T T F F b < c T T F F b < c T F T F a < c a < c abc cba acb cab bac bca etc. Winter 2011 CISC101 - Whittaker 19 Winter 2011 CISC101 - Whittaker 20 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

  6. if-elif Statements if-elif Statements - Cont. • In the code in SortThree.py, you might have • This leads to a common construct often called a chained if construct noticed this construct: • You can have as many else : if condition1 : elif s as you want statement(s ) if a < c : • The else is optional elif condition2 : statement(s) if condition1 : elif condition3 : • This kind of thing occurs so often that it can be statement(s ) statement(s) shortened to: elif condition2 : else : statement(s) elif a < c : statement(s) elif condition3 : statement(s) Winter 2011 CISC101 - Whittaker 21 Winter 2011 CISC101 - Whittaker 22 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod if-elif Statements - Cont. if-elif Statements - Cont. • There is nothing in this construct that you could • Good lab exercises! not make with normal if-else statements – Rewrite GradeCodes.py using a nested if construct • For some kinds of conditionals, the if-elif – Rewrite SortThree.py with a chained if construct might be easier to put together • For example, consider letter grades • In both cases, compare the versions • In both cases, compare the versions – Demo: GradeCodes.py – Is one more efficient than the other? • 0 to 50 is F • Count the maximum number of comparisons that are made • 50 to 70 is C – Which version is easier to write and debug? • 70 to 85 is B • 85 to 100 is A • Anything else is an illegal grade Winter 2011 CISC101 - Whittaker 23 Winter 2011 CISC101 - Whittaker 24 Slides courtesy of Dr. Alan McLeod Slides courtesy of Dr. Alan McLeod

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend