Teachnique
      CourseRoadmaps
      Login

      OverviewCommentsUser InputNumbersBooleansHistoryHello World ProgramEnvironment SetupSyntaxVariablesData TypesType CastingUnicode SystemLiteralsOperators

      Control FlowBreak StatementContinue StatementPass StatementNested LoopsDecision MakingIf StatementIf-else StatementNested IF StatementMatch-Case StatementLoopsFor LoopsFor-else LoopsWhile Loops

      FunctionsBuilt-in FunctionsDefault ArgumentsKeyword ArgumentsKeyword-Only ArgumentsPositional ArgumentsPositional-Only ArgumentsArbitrary ArgumentsVariable ScopeFunction AnnotationsModules

      StringSlicing StringsModify StringsString ConcatenationString FormattingEscape CharactersString MethodsString Exercises

      ListsList ExercisesAccess List ItemsChange List ItemsAdd List ItemsRemove List ItemsLoop ListsList ComprehensionSort ListsCopy ListsJoin ListsList Methods

      TuplesAccess Tuple ItemsUpdate TuplesUnpack Tuple ItemsLoop TuplesJoin TuplesTuple MethodsTuple Exercises

      SetsAccess Set ItemsAdd Set ItemsRemove Set ItemsLoop SetsJoin SetsCopy SetsSet OperatorsSet MethodsSet Exercises

      DictionariesDictionary ExercisesAccess Dictionary ItemsChange Dictionary ItemsAdd Dictionary ItemsRemove Dictionary ItemsDictionary View ObjectsLoop DictionariesCopy DictionariesNested DictionariesDictionary Methods

      ArraysAccess Array ItemsAdd Array ItemsRemove Array ItemsLoop ArraysCopy ArraysReverse ArraysSort ArraysJoin ArraysArray MethodsArray Exercises

      File HandlingWrite to FileRead FilesRenaming and Deleting FilesDirectoriesFile Methods

      OOP ConceptsDynamic BindingDynamic TypingAbstractionObject and ClassesEncapsulationInterfacesPackagesInner ClassesAnonymous Class and ObjectsSingleton ClassWrapper ClassesEnumsReflectionClass AttributesClass MethodsStatic MethodsConstructorsAccess ModifiersInheritancePolymorphismMethod OverridingMethod Overloading

      Feedback

      Submit request if you have any questions.

      Course
      Nested IF Statement

      Python Tutorial

      This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.

      Nested IF Statements

      Python supports nested if statements which means we can use a conditional if of else…if statement inside an existing if statement.
      There may be a situation when you want to check for another condition after a condition resolves to true. In such a situation, you can use the nested if construct.
      In a nested if construct, you can have an if…elif…else construct inside another if…elif…else construct.

      Syntax

      The syntax of the nested if…elif…else construct will be like this
      if expression1:
      statement(s)
      if expression2:
      statement(s)
      elif expression3:
      statement(s)3
      else
      statement(s)
      elif expression4:
      statement(s)
      else:
      statement(s)

      Example

      Now let's take a Python code to understand how it works
      num=8
      print ("num = ",num)
      if num%2==0:
      if num%3==0:
      print ("Divisible by 3 and 2")
      else:
      print ("divisible by 2 not divisible by 3")
      else:
      if num%3==0:
      print ("divisible by 3 not divisible by 2")
      else:
      print ("not Divisible by 2 not divisible by 3")
      When the above code is executed, it produces the following output
      num = 8
      divisible by 2 not divisible by 3
      num = 15
      divisible by 3 not divisible by 2
      num = 12
      Divisible by 3 and 2
      num = 5
      not Divisible by 2 not divisible by 3

      Practice with Online Editor

      Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
      Remember to save code(Ctrl + S Or Command + S) before run it.