Python Tutorial #6

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

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

Author

  • Tanmay Sinha

    Founder & CEO of MrProgrammer.in | I help Learning Freaks to Step into the World of Programming and Our Mission is to Provide High Quality and Valuable Content to Our Users! We believe that everyone has the potential to learn to code, and we are committed to making that happen. Our courses are designed to be engaging and interactive, and we offer a variety of learning paths to meet the needs of all learners. We are also committed to providing our learners with the support they need to succeed, and we offer a variety of resources to help learners learn at their own pace. If you are interested in learning to code, we encourage you to check out our courses. We are confident that you will find our content to be high-quality, valuable, and makes sense.

Leave a Reply