Python Lab for Week 2

Write the code using the Basic If condition in Python:

Examples of using the if-conditional check alone

Example 1: Using only If

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

Example 2: Using only If


if temperature > 30:
    print("It's a hot day")

Example 3: Using only If


if len(name) > 5:
    print("Name is longer than 5 characters")

Example 4: Using If and Else


if score >= 50:
    print("You passed")
else:
    print("You failed")

Example 5: Using If and Else


if is_raining:
    print("Take an umbrella")
else:
    print("You don't need an umbrella")

Examples of If-Elif Conditions in Python

Example 1: Using If-Elif

      
      if grade >= 90:
          print("Grade: A")
      elif grade >= 80:
          print("Grade: B")
      elif grade >= 70:
          print("Grade: C")
      elif grade >= 60:
          print("Grade: D")
      else:
          print("Grade: F")
      
      

Example 2: Using If-Elif


if age < 13:
    print("Child")
elif age < 20:
    print("Teenager")
elif age < 65:
    print("Adult")
else:
    print("Senior")

Example 3: Using If-Elif


if response == 'y':
    print("Yes")
elif response == 'n':
    print("No")
else:
    print("Invalid input")

Example 4: Using If-Elif


if season == 'Spring':
    print("Plant the garden!")
elif season == 'Summer':
    print("Water the garden!")
elif season == 'Fall':
    print("Harvest the garden!")
elif season == 'Winter':
    print("Read a book!")

Example 5: Using If-Elif


if score < 50:
    print("Fail")
elif score < 60:
    print("D Grade")
elif score < 70:
    print("C Grade")
elif score < 80:
    print("B Grade")
else:
    print("A Grade")

Example:Building basic Apps: Build a Basic Calculator

  
  operation = input("Choose an operation (+, -, *, /): ")
  a = float(input("Enter first number: "))
  b = float(input("Enter second number: "))
  
  if operation == '+':
      print("Result:", a + b)
  elif operation == '-':
      print("Result:", a - b)
  elif operation == '*':
      print("Result:", a * b)
  elif operation == '/':
      if b != 0:
          print("Result:", a / b)
      else:
          print("Error: Division by zero.")
  else:
      print("Invalid operation")
  
  

Example 2: Age Category Classifier App

  
  age = int(input("Enter your age: "))
  
  if age < 13:
      print("You are a child.")
  elif age >= 13 and age < 18:
      print("You are a teenager.")
  elif age >= 18 and age < 60:
      print("You are an adult.")
  elif age >= 60:
      print("You are a senior.")
  else:
      print("Invalid age.")
  
  

Example:How to use Nested if Control structures

Example 1: Nested If


if a > b:
    if c > d:
        print("Both conditions are true")

Example 2: Nested If

  
  if year >= 2000:
      if year <= 2020:
          print("Year is in the 21st century")
  
  

Example 3: Nested If with Else

  
  if score > 50:
      if score >= 90:
          print("Grade A")
      else:
          print("Grade B")
  else:
      print("Failed")
  
  

Example 4: Multiple Nested If

  
  if temperature > 0:
      if temperature < 20:
          print("Cold")
      elif temperature < 30:
          print("Warm")
      else:
          print("Hot")
  else:
      print("Freezing")
  
  

Example 5: Nested If with Logical Operators

  
  if x > 10:
      if x < 20:
          if y > 100 and y < 200:
              print("x is between 10 and 20, and y is between 100 and 200")
  
  

Example:5

# I will get the user name and user score and user telephone and get it displayed as a output
useName = input("Please Enter student Name : ")
useScore = input("Please Enter student Score : ")
useTelephone = input("Please Enter student Contact : ")
print("*"*70)
print("Name : "+useName)
print("Grade : "+useScore)
print("Contact Phone : "+useTelephone)
print()
print("*"*70)

Example:How to use Single line if statement

Example 1: Single Line If Statement


print("Welcome to the world of Intelligence-Pycent.com") if a > b else None

Example 2: Single Line If Statement


x = 5 if a > 10 else 0

Example 3: Single Line If Statement


print("Yes") if True else print("No")

Example 4: Single Line If Statement


status = "Adult" if age >= 18 else "Minor"

Example 5: Single Line If Statement


color = "Red" if choice == 1 else "Blue" if choice == 2 else "Green"

Advanced Examples of single line if-statement

Example 1: Complex Conditional with Function Call


result = function1() if a > b else function2()

Example 2: Nested Single Line If


x = 10 if a > b else 20 if a == b else 30

Example 3: Single Line If with List Comprehension

    
    even_numbers = [i for i in range(10) if i % 2 == 0]
    
    

Example 4: Single Line If with Multiple Conditions

    
    status = "High" if score > 90 else "Medium" if score > 50 else "Low"
    
    

Example 5: Single Line If in Function Arguments

    
    print("Yes") if condition1 and condition2 else print("Maybe") if condition3 else print("No")