Skip to content
Home » Blogs » If-Else Loops In Python | Python Tutorial #6

If-Else Loops In Python | Python Tutorial #63 min read

Python Tutorial #6

Hey Programmers, In this Tutorial We we lean about Loops in Python, How to Use Loops And How to Write Them. Different Ways of Writing Loops, What are Loops & More. So Let’s But Before Starting If you have not Read Previous Parts of the You can Access the Here: Python Complete Course

Table of Contents:

  • What are Loops In Python?
  • If Else Loops
  • While Loops
  • For Loops

What are Loops In Python?

Loops In Python are used to Perform some Specific Block of Code, Like For Example If the Statement is True Then if will Continue the Loop And If the Statement is False Then If Will Break the Loop.

If Else Loops

The if-else statement is used to execute both true and false part of a certain condition. If the condition is true, the code in the if block is executed, and if the condition is false, the code in the other block is executed.

Example:

a = 10
b = 5
if a > b:
  print("a is greater than b")

Output:

a is greater than b

Run Code

Indentation

Indent refers to the space at the beginning of a line of code. While in other programming languages, indenting in code is just for readability, indentation in Python is very important. Python uses indentation to indicate a block of code.

a = 10
b = 5
if a > b:
print("a is greater than b")

Output:

 File "/home/jdoodle.py", line 4
    print("a is greater than b")
    ^
IndentationError: expected an indented block

Run Code

Elif

The elif keyword is used in conditional statements (if statements), and is short for else if.

Example:

a = 26
b = 27
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")

Output:

b is greater than a

Run Code

In the Given Above Example, The Condition is that if value of b is greater than a then it will print b is greater than a and we used a elif statement where if a & b are then print a and b are equal.

Else

The else keyword catches anything not caught by the previous conditions.

Example:

a = 50
b = 90
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
else:
  print("a is greater than b")

Output:

b is greater than a

Run Code

Short Hand If

If you only have one statement to execute, you can put it on the same line as the if statement.

a = 120
b = 100

if a > b: print("a is greater than b")

Output:

a is greater than b

Run Code

Short Hand If -Else

If you only have one statement to execute, one if statement, and another, you can put it all on the same line:

a = 90
b = 560

print("A") if a > b else print("B")

Output:

B

Run Code

And

The and keyword is a logical operator and is used to combine conditional statements:

a = 50
b = 33
c = 100
if a > b and c > a:
  print("Yes You are Correct!")

Or

Or Keyword is Logical Operator, It Used to Combine Statements In Python:

a = 90
b = 20
c = 120
if a > b or a > c:
  print("One Statement is Correct!")

Nested If

A Statement in which there if statement inside a if statement is known as Nested If Statements

x = 30

if x > 5:
  print("The Value is Greater than 5")
  if x > 20:
    print("The Value is Greater than 10")
  else:
    print("The Value is not Greater than 10!")

Python Pass Statement

The if statement cannot be empty, but if for some reason you have an if statement with no content, insert a pass statement to avoid an error.

a = 50
b = 145

if b > a:
  pass

Access the Full Python Course:

Here

Tanmay Sinha

Leave a Reply