Page 127 - TechPluse_C8_Flipbook
P. 127

A data type specifies the type of value a variable can contain. Python supports various data
                 types. Integers (int) are whole numbers, such as a = 10. Floating-point numbers (float) include
                 decimals, such as b = 3.5. Strings (string) are sequences of characters enclosed in quotes, such
                 as c = "Hello".

                 Comments in Python can be used to explain parts of the code. It can also be used to hide the code
                 as well. Comments enable us to understand the way a program works. In Python, any statement
                 starting with the # symbol is known as a comment. Python does not execute comments.
                 In Python, operators can be defined as special symbols which perform arithmetic and logical
                 computation. The values which the operators use to get the output are called operands. Python
                 includes arithmetic operators such as +, -, *, /, %, **, and //, assignment operators like =, +=,
                 -=, *=, /=, %=, //= and **=, relational operators including ==, !=, >, <, >= and <=, and logical
                 operators such as and, or, and not. Precedence of operators determines the order in which the
                 operators are executed.

                 There are situations in life when we need to decide based on a situation. For example, if it rains,
                 I will stay at home or else I shall visit the market. Similar situations arise in programming as
                 well, where we need to make some decisions and based on these decisions, execute a set of
                 commands. We need to determine which action is to be taken and which statement is to be
                 executed, if the outcome is true or false. In this chapter, we will learn about decision making
                 statements of Python.

                     CONDITIONAL STATEMENTS

                 Decision making in Python is done by using conditional statements which decide the flow of
                 program execution. The following conditional statements are available in Python:
                    if statement
                    if...else statement
                    Nested if statement
                    if…elif…else ladder                                                  Test Expression   False

                 The if Statement

                 The if statement is the simplest conditional statement. Here,                   True
                 a statement or a collection of statements within the if block
                 is executed only if a certain condition or expression evaluates           Body of if
                 to True. If the condition evaluates to False, then the control
                 of execution is passed to the next statement after the if block.

                 The syntax of the if statement is given below:
                                                                                     Statement just below if
                 Syntax:
                 if (Test Expression):
                     Indented statement block
                 # if block ends here







                                                                Conditional and Looping Statements in Python         125
   122   123   124   125   126   127   128   129   130   131   132