Tuesday, July 27, 2021

Python - day13 - if else

 Day 13 - Python 7-13-2021

1. Simple if
<syntax1>
if <condition>:
  st1
st2


by using relational operators

> -> Greater than
For eg,
-> 10>5  -> true,
   10>20 -> false
   10<20 -> true
   10<5  -> False
   10>=10 -> Yes -> True
   10 <=20 -> True
   10 <=10 -> True
   10==10  -> True
   10!=20  -> True
   10!=10  -> False

<  -> Less than
>=
<=
==
!=



coding

if 10>5:
  print("Hi")
print("Hello")

we have two statements.
Which statement is conditional dependent statement?
the first one..

what is the output of this code?
Since first condition is true, it will print hi and hello is regular statement, so it will also prints the output

first condition 10>5 is checked and its true so print Hi is printed
the control goes to next statement, and prints hello.



next,
if 10>20:
  print("Hi"
print("Hello")

here you will get only Hello outout

10>20 is false so it skips the statement. goes to next statement and reads it. It prints Hello.


<syntax2>
if <condition>:
  statement1
  st2
st3

we have two consitional statement. and one regular statement.

First condition is checked and if it is true, following two statements are printed.

lets say if condition is false, it will skip these two statements and go to next.

we have two statement conditional dependent and we have one conditional indenendent.


what happens when condition is false,
if condition is false, it will skip st1 and st2 and following statement is always executed.

if 10>5:
  print("Hi")
  print("Hello")

print("Bye")

what is the output?
->
Hi
Hello
Bye

10>10 true,
control will enter next statement and print Hi, Hello and goes to regular statement and prints bye.

if 10>50:
  print("Hi")
  print("Hello")
print("Bye")

what is the output?

o/p
--------
bye


10>50 will check and it false, it skips that section and goes to next section and prints bye.

eg,
Implement divide by zero error program by using simple if to handle run time error.

Design with output(o/p)
------------------------
Enter first number: 10
10 i sstored into a variable a.
Enter 2nd number: 0
user enter 0 and stored it into a variable b.

Please enter 2nd number other than zero: 5
user enter 5 and stored in c.
Division result is: 2

third statement is condition dependence. if user enter b==0:
checking the condition b.
3rd is condition accepting number other than 0.

So, we have to write a program. How to write a prigarm?

a=int(input("Enter first number:"))
b=int(input("Enter 2nd number:"))
if b==0:
  b=int(input("Please enter 2nd number other than 0"))
c=a/b
print("Division result is:", c)

here, user enters first number 10 stored in a
user enters 0 and stored on b
it will prompt user to enter a number other than zero.

and result will be printed.

lets say frist user enter 10 and stored in a
2nd user enter 5 and stored in b.


review
o/p
---------------
first
enter first number executes and user enters 10 and stored in a
control goes to 2nd line and user enter 0 and 0 is stoed in b.
after that control will check the condition

0=0 which is true and executes next staetmetn.
please enter 2nd number other than zero.
lets assume, user enters 5 and stored in c.
now, control goes to next statement

c=10/5-2.0
now, print
Divison result is 2.0.

what happens

simple if is only verifies only once so we have to use loops if you have to verify multiple condition.
our user may not be gentle man who enter only one right answer.




Review yesterday's 5 home work.








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. ...