• Skip to content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Educational Article
  • Technical Articles
  • Engineering Physics
  • English Grammar
  • Engineering Chemistry

Latest News on Educational and Technical Era

Latest News, Multiple choice Quiz, Technical, educational articles.

[03] Python Conditional Statements

February 20, 2023 by Dr. IM Leave a Comment

Download

Python Conditional Statements

Conditional Statements

In Python, conditional statements allow you to execute specific code based on whether a certain condition is true or false. The most common conditional statements in Python are if, if-else, if-elif-else, and the ternary operator. Here is an explanation of each of these:

if statement: This statement is used to execute a block of code only if a condition is true. The syntax for an if  statement is as follows:

Python Code:

x = 10

if x > 5:
print(“x is greater than 5”)

 

a = 10
if a == 10 :
    print("Hi")

if-else statement: This statement is used to execute one block of code if a condition is true, and another block of code if the state is false. The syntax for an if-else statement is as follows:

Python Code:

a = 10

if a == 11:
    print("Hi")
else:
    print("Bye")
conditional statements

if-elif-else statement: This statement is used to execute different blocks of code based 
on multiple conditions. The syntax for an if-elif-else statement is as follows:
Python Code:
a = 8

if a < 5:
    print(1)
elif a == 10:
    print(2)
elif a > 7:
    print(3)
else:
    print(4)

Python Code:

x = 3

Download

if x > 5:
print(“x is greater than 5”)
elif x > 0:
print(“x is greater than 0 but less than or equal to 5”)
else:
print(“x is less than or equal to 0”)

Nested if else:

Python Code:

a = 5

if a == 5:
    print(1)
    if a > 3:
        print(2)
    else:
        print(3)
elif a == 7:
    print(4)
    if a > 5:
        print(5)
else:
    print("Finish")

Ternary operator: This is a shorthand way of writing an if-else statement. The syntax for a ternary operator is as follows:

Python Code:

a = 5

b = 10 if a > 2 else 45

print(b)

Python Code:

x = 3

print(“x is greater than 5” if x > 5 else “x is less than or equal to 5”)

 

 

Questions

  • WAP to enter a number from the user and check if it is even or odd
n = int(input(“Enter a number: “))
if n%2 == 0:
    print("Even")
else:
    print("Odd")

  • WAP to enter a number from the user and print its character format.

1 -> “One”

2 -> “Two”

n = int(input("Enter a number: "))

if n == 1:
    print("One")
elif n == 2:
    print("Two")
elif n == 0:
    print("Zero")
else:
    print("Invalid Input")
  • WAP to enter a number from the user and print its absolute value
n = int(input("Enter a number: "))

if n < 0:
    print(n * (-1) )
else:
    print(n)

  • WAP to enter a character from the user and check if it is a vowel or consonant
  • WAP to enter a character from the user and check if it is a vowel or consonant
  • WAP to enter length and width and check it it is a square or rectangle
a = int(input("Enter length: "))
b = int(input("Enter width: "))

if a == b:
    print("Square")
else:
    print("Rectangle")
 

Download

Filed Under: IM-Blog-Data Science Tagged With: Pyhton, Python Conditional Statements

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Posts

  • Clever Preparation Tips for CUET Entrance Test 2023
  • CUET UG Exam 2023: Exam Dates, Syllabus, Pattern, Question Papers, Result, Cut off
  • The difference Between ChatGpt-3.5 and GPT-4 Everything you should know Big News
  • Breaking News: 1 बार बुरे फसे : कपिल शर्मा अभी भी दीपिका को पसंद करते है ?
  • 8 Tips for success in exams
  • [04] Python While Loops
  • [03] Python Conditional Statements
  • [02] Python Operators
  • [01] About Python
  • Migraine Cure effective Treatment | Home remedies माइग्रेन जड़ से खत्म

Categories

m

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

DMCA.com Protection Status

Copyright © 2023 · Magazine Pro on Genesis Framework · WordPress · Log in