Department of:                                   Subject of:
Information Technology                          Artificial Intelligence
  Quaid-e-Awam University of               Year      4TH     Semester         8TH
   Engineering, Sciences &
          Technology                       Batch     19IT    Duration         03
                                                                             Hours
         Nawabshah
                               PRACTICAL 07
                     TO UNDERSTAND & EXECUTE DIFFERENT
                      STATEMENTS & FUNCTIONS IN PYTHON
----------------------------------------------------------
OBJECTIVES
  • Python conditions
  • Conditional statements
  • Iterative statements
  • Functions
REQUIREMENTS
  • PC with windows
  • Anaconda environment/Online python compiler
7.1   PYTHON CONDITIONS
      Python supports the usual logical conditions from mathematics:
            • Equals: a == b
            • Not Equals: a != b
            • Less than: a < b
            • Less than or equal to: a <= b
            • Greater than: a > b
            • Greater than or equal to: a >= b
      These conditions can be used in several ways, most commonly in "if
      statements" and loops.
7.2   CONDITIONAL STATEMENTS/IF STATEMENTS
      Conditional Statement in Python perform different computations or actions
      depending on whether a specific Boolean constraint evaluates to true or
      false. Conditional statements are handled by IF statements in Python.
      Python if Statement is used for decision-making operations. It contains a body
      of code which runs only when the condition given in if statement is true. If
      the condition is false, then the optional else statement runs which contains
      some code for the else condition.
      When you want to justify one condition while the other condition is not
      true, then you use Python if else statement.
7.2.1 SYNTAX
7.2.2 FLOW CHART
7.2.3 Elif Statement
      The elif keyword is python way of saying "if the previous conditions were
      not true, then try this condition".
7.2.4 EXAMPLE
7.3   ITERATIVE STATEMENTS/PYTHON LOOPS
      Python has two primitive loop commands:
         • while loop
         • for loop
7.3.1 WHILE LOOP
      With the while loop we can execute a set of statements as long as a
      condition is true.
7.3.2 EXAMPLE
7.3.3 FOR LOOP
      A for loop is used for iterating over a sequence (that is either a list, a tuple,
      a dictionary, a set, or a string).
      With for loop we can execute a set of statements, once for each item in a
      list, tuple, set etc.
7.4   FUNCTIONS IN PYTHON
         • A function is a block of code which only runs when it is called.
         • You can pass data, known as parameters, into a function.          A
           function can return data as a result.
7.4.1 CREATING A FUNCTION
     In Python a function is defined using the def keyword:
7.4.2 CALLING A FUNCTION
     To call a function, use the function name followed by parenthesis:
7.4.3 ARGUMENTS IN FUNCTION
     Information can be passed into functions as arguments.
     Arguments are specified after the function name, inside the parentheses.
     You can add as many arguments as you want, just separate them with a
     comma.
     The following example has a function with one argument (fname). When
     the function is called, we pass along a first name, which is used inside the
     function to print the full name:
                               EXERCISE
1. Write a function that returns the maximum of two numbers.
2. Write a function called check_num that takes a number.
      • If the number is divisible by 3, it should return “Divisible by 3”.
      • If it is divisible by 5, it should return “Divisible by 5”.
      • If it is divisible by both 3 and 5, it should return “Divisible by both”.
        Otherwise, it should return the same number.
3. Write a function called showNumbers that takes a parameter called limit. It
   should print all the numbers between 0 and limit.
4. Write a function called show_stars(rows). If rows is 5, it should print the
   following:
   *
   **
   ***
   ****
   *****
5. Write a Python program to test whether a passed letter is a vowel or not.
6. Write a Python program to solve (x + y) ^ (z).
   Test Data: x = 4, y = 3, z=2
   Expected Output: (4 + 3) ^ 2) = 49
7. Write a function that returns the sum of multiples of 3 and 5 between 0 and
   limit (parameter). For example, if limit is 20, it should return the sum of 3, 5,
   6, 9, 10, 12, 15, 18, 20