1 / 30

300 likes | 373 Vues

Random , if , elseif , logical operators. random.randint (a, b). A program can decide which statements to execute based on a condition. if radius < 0 : print ( " Incorrect input " ) else : area = radius * radius * math.pi print ( " Area is" , area ).

Télécharger la présentation
## Random , if , elseif , logical operators

**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

**Random, if, elseif,logicaloperators**random.randint(a, b)**A program can decide which statements to execute based on a**condition ifradius < 0: print("Incorrectinput") else: area= radius * radius * math.pi print("Area is", area)**Boolean Types, Values, and Expressions**A Boolean expression is an expression that evaluates to a Boolean value True orFalse.**Example**radius = 1 Print(radius> 0) print(int(True)) print(bool(0)) You can also use the boolfunction to convert a numeric value to a Boolean value. Thefunction returns False if the value is 0; otherwise, it always returns True.**GeneratingRandomNumbers**Python also provides another function, andrange(a, b), for generating a random integerbetween a and b – 1, which is equivalent to randint(a, b – 1). For example,randrange(0, 10) and randint(0, 9) are the same. Since randintis more intuitive, thebook generally uses randintin the examples. You can also use the random() function to generate a random float r such that 0 <= r < 1.0.**Exampleif-else**ifradius >= 0: area= radius * radius * math.pi print("The area for the circle of radius", radius, "is", area) else: print("Negative input")**if-else**ifnumber % 2 == 0: print(number, "is even.") else: print(number, "is odd.")**LogicalOperators**The logical operators not, and, and or can be used to create a composite condition. Sometimes, a combination of several conditions determines whether a statement is executed. You can use logical operators to combine these conditions to form a compound expression.**ConditionalExpressions**A conditionalexpressionevaluatesanexpressionbased on a condition. You might want to assign a value to a variable that is restricted by certain conditions. For example, the following statement assigns 1 to y if x is greater than 0, and -1 to y if x is less than or equal to 0. ifx > 0: y = 1 else: y = -1 y = 1 if x > 0 else -1

More Related