Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 116 - 2611_SmartGPT Pro V(5.0) C-7
P. 116

    Output

                       Enter your marks: 90
                       Excellent
                       Enter your marks: 75
                       Very Good
                       Enter your marks: 51
                       Fairly Good
                       Enter your marks: 40
                       Need more effort

                      The number of elif statements used in this construct can vary depending on the problem and
                      there is no fixed upper limit for it. However, else can be used only once for each if statement.

                           Tick ( ) if you know this.
                           ▶  The if…else… statement lets your program choose between two options.
                           ▶  The nested if structure allows you to place one if statement inside another.

                                TERNARY OPERATOR IN PYTHON

                      The ternary operator allows you to perform conditional checks and assign values or execute
                      expressions  in a single line. It is also called a  conditional  expression because  it  evaluates  a
                      condition and returns one value if the condition is True and another value if the condition is False.

                      Syntax of the ternary operator:

                      <value_if_true> if (condition) else <value_if_false>
                      Program 7. To print “Eligible to vote” if the age is greater than or equal to 18, else “Not eligible to
                      vote”

                          Program7.py
                       File  Edit  Format   Run    Options   Window    Help

                       age = int(input("Enter your age: "))
                       print("Eligible to vote" if age >= 18 else "Not eligible to vote")

                          Output

                       Enter your age: 15
                       Not eligible to vote

                       114  Computer Science (V5.0)-VII

| 111 112 113 114 115 116 117 118 119 120 121 |