unit 2 python
unit 2 python
Conditional statements
In Python, condition statements act depending on whether a given condition is true or false. You
can execute different blocks of codes depending on the outcome of a condition. Condition
statements always evaluate to either True or False.
There are three types of conditional statements.
1. if statement
2. if-else
3. if-elif-else
4. nested if-else
Iterative statements
In Python, iterative statements allow us to execute a block of code repeatedly as long as the
condition is True. We also call it a loop statements.
Python provides us the following two loop statement to perform some actions repeatedly
1. for loop
2. while loop
Transfer statements
In Python, transfer statements are used to alter the program’s way of execution in a certain
manner. For this purpose, we use three types of transfer statements.
1. break statement
2. continue statement
3. pass statements
1.If statement in Python
4. In control statements, The if statement is the simplest form. It takes a condition and
evaluates to either True or False.
5. If the condition is True, then the True block of code will be executed, and if the condition
is False, then the block of code is skipped, and The controller moves to the next line
6. Syntax of the if statement
7. if condition:
8. statement 1
9. statement 2
10. statement n
If – else statement
The if-else statement checks the condition and executes the if block of code when the condition
is True, and if the condition is False, it will execute the else block of code.
Syntax of the if-else statement
if condition:
statement 1
else:
statement 2
If the condition is True, then statement 1 will be executed If the condition is False, statement 2
will be executed. See the following flowchart for more detail.
Python Functions is a block of statements that return the specific task. The idea is to put some
commonly or repeatedly done tasks together and make a function so that instead of writing the
same code again and again for different inputs, we can do the function calls to reuse code
contained in it over and over again.
Some Benefits of Using Functions
• Increase Code Readability
• Increase Code Reusability
Keyword Arguments
The idea is to allow the caller to specify the argument name with values so that the caller does
not need to remember the order of parameters.
# Python program to demonstrate Keyword Arguments
def student(firstname, lastname):
print(firstname, lastname)
# Keyword arguments
student(firstname='ram', lastname='sham')
student(lastname=’sham’, firstname='ram')
Positional Arguments
We used the Position argument during the function call so that the first argument (or value) is
assigned to name and the second argument (or value) is assigned to age. By changing the
position, or if you forget the order of the positions, the values can be used in the wrong places,
as shown in the Case-2 example below, where 27 is assigned to the name and Suraj is assigned
to the age.
def nameAge(name, age):
print("Hi, I am", name)
print("My age is ", age)
Python lambda
In Python, an anonymous function means that a function is without a name. As we already
know that def keyword is used to define the normal functions and the lambda keyword is used
to create anonymous functions.
Python lambda Syntax:
lambda arguments : expression