Presentation is loading. Please wait.

Presentation is loading. Please wait.

Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Similar presentations


Presentation on theme: "Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg."— Presentation transcript:

1 Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

2 What type(s) would we use To represent your:  Weight  Height  Age  Name  Class status (freshman, sophomore, etc)  Gender  If you owe the university money  If you are taking classes this semester

3 Yesterday’s Lab – Main Concept Review First we looked at the idea of Conditionals and the basic six operators:  Less than: <  Greater than: >  Equal to: == (Not the same as =)  Not equal to: !=  Less than or equal to: <=  Greater than or equal to: >=

4 Yesterday’s Lab – Main Concept Review The results of a conditional operator is always a Boolean value.  Meaning ???  Either True or False

5 Boolean Expressions George Boole’s (mid-1800’s) mathematics of logical expressions Boolean expressions (conditions) have a value of True or False Conditions are the basis of choices in a computer, and, hence, are the basis of the appearance of intelligence in them.

6 What is True, and What is False True: any nonzero number or nonempty object. 1, 100, “hello”, [a,b] False: zero number or empty object. 0, “”,[ ] Special values called “True” and “False”, which are just stand-ins for 1 and 0. However, they print nicely (True or False)

7 Boolean Expression Every boolean expression has the form:  expression booleanOperator expression The result of evaluating something like the above is just True or False. Evaluating a boolean operation is a binary operation “if (a<b)” has two operands in expression However, remember what constitutes True or False in Python! “If (a)” will evaluate to True if a is non-zero!

8 Relational Operators Relational Operators have low preference 5 + 3 < 3 – 2 (5 + 3) < (3 - 2) 8 < 1 False

9 Selection Then we looked at how conditionals are applied in selection statements. Selection is how programs make choices, and it is the process of making choices that provides a lot of the power of computing

10

11

12 Python if Statement if boolean_expression : suite evaluate the boolean_expression (True or False) if True, execute all statements in the suite

13 Warning About Indentation Elements of the “suite” must all be indented the same number of spaces/tabs Python only recognizes suites when they are indented the same “distance” You must be careful to get the indentation right to get suites right.

14 Python Selection, Round 2 if boolean expression: suite1 else: suite2 The process is: evaluate the boolean if True, run suite1 if False, run suite2

15 Chained Comparisons In python, chained comparisons work just like you would expect in a mathematical expression: Given myInt has the value 5  0 <= myInt <= 5 True  0 10 False

16 Compound Expressions Logically 0 < X < 3 is actually (0 < X) and (X < 3) Logical Operators (lower case)  and  or  not


Download ppt "Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg."

Similar presentations


Ads by Google