Python Lab Manual (22616)
Practical 4 : Write simple Python program to demonstrate use of conditional statements: if’ statement, ‘if … else’ statement, Nested ‘if’ statement.
* Practical Related Question.1. List operators used in if conditional statementAns => - Equals: a == b.
- Not Equal: a != b.
- Less than: a < b.
- Less than or equal to: a <= b.
- Greater than: a > b.
- Greater than or equal to: a >= b.
- Modulus : %
2. Differentiate between if-else and nested-if statementAns => *If Else 1) We use If else when we have to check only one condition . 2) Syntax : if (Condition): #Statement to be executed else: #Statement to be executed Nested If Else 1) We use If else when we have to check Multiple conditions . 2) Syntax : if (Condition): if (Condition): #Statement to be executed else: #Statement to be executed else: #Statement to be executed
* Exercise :
1. Write a program to check whether a number is even or oddAns=>
2. Write a program to find out absolute value of an input numberAns=>
3. Write a program to check the largest number among the three numbersAns=>
4. Write a program to check if the input year is a leap year of not
Ans=>
5. Write a program to check if a Number is Positive, Negative or ZeroAns=>
6. Write a program that takes the marks of 5 subjects and displays the grade. Ans=>
1. List operators used in if conditional statement
Ans =>
- Equals: a == b.
- Not Equal: a != b.
- Less than: a < b.
- Less than or equal to: a <= b.
- Greater than: a > b.
- Greater than or equal to: a >= b.
- Modulus : %
2. Differentiate between if-else and nested-if statement
Ans => *If Else
1) We use If else when we have to check only one condition .
2) Syntax : if (Condition):
#Statement to be executed
else:
#Statement to be executed
Nested If Else
1) We use If else when we have to check Multiple conditions .
2) Syntax : if (Condition):
if (Condition):
#Statement to be executed
else:
#Statement to be executed
else:
#Statement to be executed
* Exercise :
1. Write a program to check whether a number is even or odd
Ans=>
2. Write a program to find out absolute value of an input number
Ans=>
3. Write a program to check the largest number among the three numbers
Ans=>
4. Write a program to check if the input year is a leap year of not
Ans=>
5. Write a program to check if a Number is Positive, Negative or Zero
Ans=>
6. Write a program that takes the marks of 5 subjects and displays the grade.
Ans=>
0 Comments