Tuesday, July 27, 2021

Python - Day 12 - condition - if else

 Day 12. Python

Write a python program to accept 2 numbers from the user and perform division and display division result?

Step1:

Step

vi test.py or open with idle

a=ent(input("Enter first number:"))
b=ent(input("Enter 2nd number:"))
c=a/b
print("Divison result is:", c)

run module

Enter first number:
enter 2nd number:


10 will be storing in a and so on


what happens if user enters 10 and 0?

error: run time error - zeroDivisionError: division by zero
value can not be divide by 0.


observation:
-----------
in the program above, when user entered first number 10 and second number 0, it will throw divide by zero error (run time error). Because of that program execution will be terminated abnormally.

run the program

enter first no: 10
enter 2nd no: 0

error:



Control Statement
in every programming language, control statement is very importand
how to handle run time errors?
- We can handle run time errors in 2 ways.
1. By using login
2. By using exception handling mechanism

1. By using login
- for this, we use control statement

2. Using exception hadling mechanism
-

What can we do using control statement?
- Using comtrol statement, we can control the program control execution flow according to our requirement.


When do we use control statement?
-> When we want to control the program execution flow as per our requirement.

We will go for control statements.


In python, we have 3 types of control statements.
1. Conditional statements
2. Loops
3. Transfer Statements

1. Conditional statements

Lets look at 4 scenarios

---------------------------------

statement 1
statement 2
statement 3
statement 4
statement 5

you have 5 lines of code and execution in order. We don't need any control statement


2nd scenario

---------------------------------

statement 1
statement 2
statement 3 ***
statement 4
statement 5


here staement3 may execute or may not execute. In this type of condition weneed conditional stement

---------------------------------

3nd
statement 1
statement 2
statement 3 ***
statement 4
statement 5


here statement 3 executing multiple time so we use look


---------------------------------
4th
statement 1
statement 2
statement 3  ***
statement 4
statement 5  ***


here, executing 1 and jumping to stetmenet 3 and jumping to 5, we use transfer statement.

as per the situation, we use one of the condition.


=============================

1. Conditional statements
What can we do with conditional statement?
- Using conditional statements, we can execute single statement or multiple statements based on condition.

When do we use conditional statements?
-> When you want to execute single statement or multiple statements based on condition, we will use conditional statements.

Types of conditional statements?
- In python, we have following conditional statements

1. Simple if
2. if else
3. if elif
4. multiple if
5. nested if


HW
Write a program using simple if, if else, if elif, multiple if, and nested if.


==============================

xcellis:


QXS-4 series chassis overview and qxs-456 chassis overview

xcellis I artico documentation center





No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...