Chapter 2 - 2.2 In-Depth Control PDF

Title Chapter 2 - 2.2 In-Depth Control
Author Kyle Frudakis
Course Python Programming
Institution Florida Atlantic University
Pages 5
File Size 217.7 KB
File Type PDF
Total Downloads 93
Total Views 137

Summary

Dr. Oge Marques
Full Semester
Learning How to Work with Statements...


Description

Chapter 2 - 2.2 In-Depth Control Friday, May 21, 2021

5:05 PM

• 0 for int, 0.0 for float, and '' for string is considered false ○ All nonempty objects are considered true What does it mean to be equal? • There are two different kinds of equality ○ Two different names are associated with objects that have the same value ○ Two different names are associated with the same object( i.e, objects with the same ID) == to check whether two names refer to objects that have the same value, is to check whether • two names refer the same object (have same ID) a_float = 2.5 b_float = 2.5 c_float = b_float a_float == b_float True a_float is b_float False b_float is c_float True • The equality of floating point values is very finicky • (u + v) + w is not the same as u + (v + w) because of the way a computer rounds it off u = 11111113 v = -11111111 w = 7.51111111 print(u+(v+w)) print((u+v)+w) print((u+v)+w == u+(v+w)) 9.511111110448837 9.51111111 False

• A better way of doing this is to check if the two values are close using the absolute value function x = (u+v)+w y = u+(v+w) print(abs(x - y) < 0.00000001) True • In this case we are checking to see if x and y are "close enough". if the difference x - y happens to be negative (as it is in this case), the expression will always be true even if the values are not close • When comparing for floating-point equality, this is the correct way to do it Chained Relational Operators • Chained relational expressions work just like they do in mathematics ○ which is not true for many programming languages In [1]: a int = 5 In [2]: 0...


Similar Free PDFs