Conditional statements

0

 

Assignment-2

Conditional statements:

Conditional statements in Python are like decision-making tools for your code. They let your program make choices based on certain conditions. Imagine you're telling your code, "If something is true, do this; otherwise, do that."

For example, think of a weather app. You could write code that says, "If it's sunny, wear sunglasses; if it's rainy, grab an umbrella." These "if-else" statements are a way to make your code flexible and responsive, just like making choices in everyday life. If you have any more questions or want to dive deeper, feel free to ask!

types of conditional statements:

In Python, there are several types of conditional statements that allow you to control the flow of your code based on different conditions. Here are the main types:

1.if Statement:

The basic "if" statement is used to execute a block of code if a specific condition is true.

example:

          if condition:

# code to execute if condition is true

2.if-else Statement:

The "if-else" statement allows you to execute one block of code if a condition is true and another block if the condition is false.

example:

          if condition:

                    # code to execute if condition is true

          else:

                   # code to execute if condition is false

 

program: check wheater a number is positve or negtive:

 

num=int(input("enter your number"))

if(num<0):

          print("number is negtive")

else:

          print("number is positive")

 

3.if-elif-else Statement:

The "if-elif-else" statement is used when you have multiple conditions to check in sequence. It allows you to execute different blocks of code based on which condition is true first.

example:

          if condition1:

                    # code to execute if condition1 is true

          elif condition2:

                    # code to execute if condition2 is true

          else:

                    # code to execute if no conditions are true

 

Program: To show the concept of elif statements:

per=int(int("enter percentage of students))

if per>=80:

          print("grade A")

elif per<80 and per >=60:

          print("grade B")

elif per<60 and per>=33:

          print("grade C")

else:

          print("fail")

4.Nested if Statements:

You can have "if" statements inside other "if" statements, creating nested conditions for more complex scenarios.

example:

if condition1:

    if condition2:

        # code to execute if both conditions are true

    else:

        # code to execute if condition1 is true but condition2 is false

else:

    # code to execute if condition1 is false

 

Program: To check a number is positve, negtive or zero ( using nested if )

 

num= float(input("Enter a number:")

if num>=0

          if num==0

                   print("zero")

          else

                   print("positive number")

          else:

                   print("negtive number")

Post a Comment

0Comments
Post a Comment (0)