2. Conditional Statements
There comes situations in real life when we need to
do some specific task and based on some specific
conditions and, we decide what should we do next.
Similarly there comes a situation in programming
where a specific task is to be performed if a specific
condition is True. In such cases, conditional
statements can be used.
The following are the conditional statements
provided by Python.
if
If else
Nested if
3. If Statement
If the simple code of block is to be performed
if the condition holds true then the if statement is
used. Here the condition mentioned holds true
then the code of the block runs otherwise not.
Syntax:
if condition:
# Statements to execute if
# condition is true
4. If else Statement
In conditional if Statement the additional
block of code is merged as else statement which
is performed when if condition is false.
Syntax:
if (condition):
# Executes this block if
# condition is true
else:
# Executes this block if
# condition is false
5. Nested if Statement
if statement can also be checked inside other if
statement. This conditional statement is called a
nested if statement. This means that inner if
condition will be checked only if outer if condition is
true and by this, we can see multiple conditions to
be satisfied.
Syntax:
if (condition1):
# Executes when condition1 is true
if (condition2):
# Executes when condition2 is true
# if Block is end here
# if Block is end here
6. Loops
Python programming language provides the
following types of loops to handle looping
requirements. Python provides three ways for
executing the loops. While all the ways provide
similar basic functionality, they differ in their syntax
and condition-checking time.
7. While Loop in Python
In python, a while loop is used to execute a
block of statements repeatedly until a given
condition is satisfied. And when the condition
becomes false, the line immediately after the
loop in the program is executed.
Syntax:
while expression: statement(s)
8. For Loop
For loops are used for sequential traversal. For
example: traversing a list or string or array etc. In
Python, there is “for in” loop which is similar to for
each loop in other languages. Let us learn how to
use for in loop for sequential traversals.
Syntax:
for iterator_var in sequence: statements(s)
9. Loop Control Statements
Loop control statements change execution from
their normal sequence. When execution leaves a
scope, all automatic objects that were created in
that scope are destroyed. Python supports the
following control statements.
Continue Statement
the continue statement
in Python returns the
control to the beginning
of the loop.
11. Functions
A function is a block of code which only runs
when it is called.
You can pass data, known as parameters, into a
function.
A function can return data as a result.
12. Invoking Built In Functions
Python has various functions which are readily
available. These functions are termed built in functions. On
this reference page, you will be able to see all the built-in
functions available in Python:
Python abs()
It returns the absolute and exact value of a given number
Python all()
It returns true when all the elements in iterable are true
Python chr()
It returns a Character from an Integer
Python classmethod()
It returns the class method for the provided function
13. random() Function in
Python
random(), simply speaking, is an inbuilt function of
the random module in Python3 and it helps in the
derivation of the python random list. Furthermore,
the random module in Python provides access to
some useful functions and one of them is able to
facilitate the generation of random floating
numbers, which is random(). Also, its syntax is
random. random() python.
Syntax: random.random()
Parameters: Any parameter is not accepted by
this method.
Returns: Random floating number between 0
and 1 is returned by this method.
15. randint() Function in
Python
randint() refers to an inbuilt function belonging to
the random module in Python3. Furthermore,
access is provided by random module provides
access to some beneficial functions.
Moreover, one of the functions is to generate
random numbers, which is randint().
Syntax : randint(start, end)
Parameters : (start, end) : Both of them have to
be integer-type values.
Returns : A random integer in the range [start,
end] consisting of the endpoints.
17. Errors and Exceptions :
ValueError: Returns a ValueError when floating-
point values are passed as parameters.
TypeError: TypeError is returned when anything
other than numeric values is passed as
parameters.