Skip to content

Latest commit

 

History

History
221 lines (171 loc) · 3.45 KB

File metadata and controls

221 lines (171 loc) · 3.45 KB

Lecture-11 If, Elif in Python

Comparison/Relational Operators

Comparison operators are to test the condition. The conditional operator result as boolean True or False.

equals to (==)

This operator returns True if both the operands are equal.if both the left and the right operand are equal to each other.

a=10
b=10
result= a==b
print(result)

⚙️ Output:

True

less than ( < )

This operator returns True if the left operand is less than the right operand.

a=10
b=20
result= a<b
print(result)

⚙️ Output:

True

greater than ( > )

This operator returns True if the left operand is greater than the right operand.

a=30
b=20
result= a>b
print(result)

⚙️ Output:

True

less than or equal to ( <= )

This operator returns True if the left operand is less than or equal to the right operand.

a=10
b=20
result= a<=b
print(result)

⚙️ Output:

True

greater than or equal to ( >= )

This operator returns True if the left operand is greater than or equal to the right operand.

a=50
b=20
result= a>=b
print(result)

⚙️ Output:

True

not equal to (!=)

This operator returns True if both the operands are not equal.

a=50
b=20
result= a!=b
print(result)

⚙️ Output:

True

a=20
b=20
result= a!=b
print(result)

⚙️ Output:

False

Indentation

Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose.

Conditional Statements

if Statement

Syntax:

if condition :
    statements-1
    statements-2
    .....
    .....
    statements-n

Example:

a=20
b=20
if a==b:
  print("A is Equals to B ")

⚙️ Output:

A is Equals to B

if--else Statements

Syntax:

if condition :
     statements-1
     statements-2
     .....
     .....
     statements-n
else:
    statements-1
     statements-2
     .....
     .....
     statements-n

Example:

a=20
b=30
if a==b:
  print("A is Equals to B ")
else :
   print("B is Unequals to A ")

⚙️ Output:

B is Unequals to A

a=50
b=30
if a>b:
  print("A is greater than B")
else :
  print("B is greater than to A ")

⚙️ Output:

A is greater than B

💡Difference between = and == operators in Python

The “ = ” is an assignment operator is used to assign the value on the right to the variable on the left.

The ‘==’ operator checks whether the two given operands are equal or not.

🏠 HomeWork

1️⃣ Write a program to check whether applicant is eligible for voting or not using if statement.

👁 Show Answer

num=int(input("Enter Number :"))
rem=num%2
if rem==0:
  print("Given Number is Even")
else:
  print("Given Number is Odd")

2️⃣ Write a program to check whether entered number is even ir odd by using if-else statements.

👁 Show Answer

age=int(input("Enter Your age :"))
if age>=18:
  print("You are eligible for Voting")
else:
  print("You are not eligible for Voting")

🔗 Some Useful Links

📖 References