Application Development Using Python - Unit 2-2
Application Development Using Python - Unit 2-2
tushar.1801@gmail.com
D0OLHR8SGA
Program: MCA
Specialization: Core
Semester: 3
Course Name: Application Development using Python *
Course Code: 21VMT0C301
Unit Name: Flow Control
1. Operators …3
2. Operators precedence …4
3. Logical and Membership operators …5
4. Mixing Boolean and comparison Operator …7
5. Flow control statements … 10
6. Python looping constructs … 15
7. Importing modules … 21
8. The sys module … 23
9. Functions of the sys module
tushar.1801@gmail.com
D0OLHR8SGA
• Operators
• Logical and Membership operators
• Operators precedence
• Mixing Boolean and comparison Operator
tushar.1801@gmail.com
D0OLHR8SGA • Program execution
• Flow control statements
• Python looping constructs
• Importing modules
• The sys module
• Functions of the sys module
Python operators are used to perform calculations on values and variables in general. These
are standard symbols used in logical and arithmetic operations. We further look at various
Python operators.
Operators: These are the unique symbols. Eg- + , * , /, etc.
Operand: The value that the operator is applied to.
Arithmetic Operators: these are basic mathematical operations like addition, multiplication,
subtraction and division. There are 7 arithmetic operators:
Example:
tushar.1801@gmail.com
D0OLHR8SGA
Precedence of operators:
P stands for parentheses.
E stands for exponentiation.
M stands for Multiplication (Multiplication and division have the same precedence)
Division D
A is for addition (Addition and subtraction have the same precedence)
S stands for Subtraction.
The modulus operator assists us 4etermineing the last digit/s of a number. As an example:
x% 10 -> returns the final digit
x% 100 -> return the last two digits
Output:
tushar.1801@gmail.com
D0OLHR8SGA
Logical operators: they are used to compare two conditional statements. Returns Boolean
values.
tushar.1801@gmail.com
D0OLHR8SGA
Boolean Operators:
Python Boolean values have only two possible values, True or False, the operators can be
completely specified in terms of the results they assign to every possible input combination.
Because they are displayed in a table, these specifications are known as truth tables.
True and False can be thought of as Boolean operators with no inputs. One of these
operators is always True, while the other is always False.
It’s sometimes useful to think of Python’s Boolean values as operators. This method, for
example, serves to remind you that they are not variables. For the same reason that you
can’t assign to +, you can’t assign to True or False.
There are only two Python Boolean values. With no inputs, a Boolean operator always
returns the same value. As a result, True and False are the only two Boolean operators that
do not accept inputs.
Not is the only Boolean operator with a single argument. It accepts one argument and
returns the inverse value: False for True and True for False.
A B A and B A or B Not A
False False False False True
True True True True False
False True False True
True False False True
With only one argument, there are only four possible operators. Aside from that, the
remaining three operators all have somewhat wacky names because they don’t exist:
Identity: Because this operator simply returns its input, removing it from your code has no
effect.
Yes, because it does not rely on its argument, this is a short-circuit operator. You could
simply replace it with True to achieve the same result.
No, because it does not rely on its argument, this is another short-circuit operator. You
could simply replace it with False to achieve the same result.
Bitwise Operators:
Bitwise operators are used in Python to perform bitwise calculations on integers. After
converting the integers to binary, operations are performed bit by bit, hence the name
tushar.1801@gmail.com
D0OLHR8SGA
bitwise operators. The result is then returned in decimal format.
a=
Conditionals Statements:
As the name suggests conditional statements are those which are dependent on certain
conditions. Meaning if a person had to choose between two or more options, conditions
would play a role in determining the output of the program. They are used to control the
flow of the program based on certain conditions. To see if the condition is true or not,
comparison operators are used. Keywords in conditional statements are: if, else, and elif.
Indentation in python is important when conditional statements are used.
tushar.1801@gmail.com
D0OLHR8SGA
If-else:
When there are questions(maybe one, two or more) having one solution, and the rest
having another solution, then an if-else statement comes in handy. A block of code is
executed only when the condition is true, otherwise the second block contained inside the
‘else: ‘ is executed. If the condition is false, the interpreter does not enter the specified
block of codes.
For example, if a child is told that he would get a chocolate if he does his homework. Here
the condition is homework, if it is done, then the boy gets a chocolate, otherwise he
doesn’t. Similarly, in the world of coding, if-else statements help to execute programs where
multiple either-or or this-or-that that type of problems exist.
Here are a few more examples of If-else statements in programs:
1. A person must get grade ‘Pass’ if they score above 45 in a 100 marks test. If they
score below it, they must get a ‘Fail’.
2. If a person’s salary is more than Rs. 200000, then display ‘Pay Tax’, if it is less than
that, display ‘Not liable to Pay tax’.
3. For whether a number is positive or negative. If the integer is more than zero, then it
is positive, if it less than zero, then it is negative.
if (condition):
code of statements if the given condition is true
else:
code of statements if the given condition is false.
Example
tushar.1801@gmail.com
D0OLHR8SGA
tushar.1801@gmail.com
D0OLHR8SGA
When the first condition is true, the statements inside it are executed. Sequentially, the if
code inside it is checked. Incase the if within the bigger if is true, the conditions inside the
former are executed, otherwise the else block is executed. The interpreter enters any block
of code if and only if the condition on which its execution is dependent is true.
Example:
tushar.1801@gmail.com
D0OLHR8SGA
In the example, when the first condition is true, the statements inside it are executed.
Sequentially, the if code inside it is checked. Incase the if within the bigger if is true, the
conditions inside the former are executed, otherwise the else block is executed. The
interpreter enters any block of code if and only if the condition on which its execution is
dependent is true.
Pass Statement:
Pass statement can be used when you don’t have statements inside a set of codes. A pass
statement does not have any impact on the program. The interpreter just continues and
goes to the next statement when it reads a pass statement.
Pass is a keyword in Python.
The example shows how pass is used. The program shows no display, meaning it runs
without encountering errors but there are no statements inside the if statements, only an if,
therefore, the output is empty. It is used to execute nothing.
While loop:
In a while loop, the set of statements keeps on repeating till the condition is true. As soon as
the condition is false, the loop ends and the set of codes written after the loop is executed.
It is used in cases when the number of iterations is unknown.
For a while loop, you first initialise a variable (conventionally i=0). Then the while condition
is written. Inside the loop, the set of statements that are to be printed in every iteration are
written. One must note that it is important to increment or decrement the initialised
variable otherwise it will become an infinite loop.
Increment means increasing the value of the variable, decrement means decreasing the
value of a variable.
The above example says that I is initialised with value 0, and n with value 10. The while
condition is then checked. According to the example, if I <= n, which in this case is true, it
enters the loop. It prints Hello for the first time, and goes to the next line. i+=1 means that I
is incremented. Now the value of I changes from 0 to 1. Again, the while loop is checked. If
the condition I <= n is satisfied, then the statements inside the loop are executed again.
Since it is true, hello is printed the second time and I is incremented again. This continues
until the value of I become 11. In that case, the condition I <= n will not be satisfied because
11 is not less than 10. Thus the loop will end there.
Other examples where while loop can be used:
1. To keep printing numbers from 1 to 100.
2. To print prime numbers between 1 and 500.
3. To print multiples for 18 between 200 and 500
4. Printing factorials of numbers in descending order, etc.
tushar.1801@gmail.com
Example:
D0OLHR8SGA
range() function:
before doing for loops, one must know what the range() function do. The range() function is
an in-built python function. In a range(n,m), the code will run from the n th element to the
(m-1)th element.
Break statement:
The break statement is used when the condition of the loop is specified but one wants to
terminate the loop in between.
Syntax:
for I in range/sequence_Name/list_Name:
set of codes
break
statements.
tushar.1801@gmail.com
D0OLHR8SGA
According to the above mentioned syntax, the for loop runs per usual but when it
encounters a break statement then it exits the loop without completing all its iterations and
starts executing the statements mentioned after the loop. Break is a keyword to break the
flow of statements inside a loop.
tushar.1801@gmail.com
D0OLHR8SGA
In the above example, I is initialised at 1. According to the first while loop, 1<=30 is true,
hence the interpreter enters the loop. The second while loop is checked. Since j <= 15, the
block of statements inside the loop are executed. i+2 and j are printed and then both the
variables are incremented. These types of loops- one within another, are called nested
loops.
Speed For loop is faster than while loop. While loop is slower than for loop.
Modules:
Python modules are files that contain Python definitions and statements. Functions, classes,
and variables can all be defined by a module. A module may also contain executable code.
Grouping related code into modules makes it easier to understand and use the code. It also
helps to organise the code logically.
import module
This does not directly import the functions or classes, but rather the module. The dot(.)
operator is used to access the functions within the module.
Example:
tushar.1801@gmail.com
D0OLHR8SGA
Lets consider a module called math which can be used to perform math problems. Sqrt
returns the square-root of a number and pi returns . We get the following output.
tushar.1801@gmail.com
D0OLHR8SGA
Output:
The from import statement’s * symbol is used to import all the names from a module into
the current namespace.
Locating Python Modules:
To get the location of a particular module, one has to use the syntax help(moduleName).
In the preceding example, sys.version is used to return a string containing the Python
Interpreter version along with some additional information. This demonstrates how the sys
module communicates with the interpreter.
tushar.1801@gmail.com
D0OLHR8SGA
tushar.1801@gmail.com
D0OLHR8SGA
To exit the programme, use sys.exit([arg]). Optionally, arg can be an integer indicating the
exit or another type of object. If it’s an integer, zero means “successful termination.”
It should be noted that a string can also be passed to the sys.exit() method.
tushar.1801@gmail.com
D0OLHR8SGA
tushar.1801@gmail.com
D0OLHR8SGA
tushar.1801@gmail.com
D0OLHR8SGA
5. sys.maxsize() – It returns the largest value that a Py ssize t variable can store.
The maximum size of lists and strings in Python is determined by the Python
platform's pointer. The size returned by maxsize is determined by the platform
architecture:
32-bit: the value is 231 - 1, or 2147483647.
64-bit: the value will be 9223372036854775807, which is 263 - 1.
As an example, we first import the module and fetch the max value. We then create
a list with the maximum size and display the length of the list and print that the list is
created successfully.
tushar.1801@gmail.com
D0OLHR8SGA