Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 103 - 2629_Devagiri_C-6
P. 103

    21 st
     INTERDISCIPLINARY LEARNING  Century   #Critical Thinking  SOME MORE PROGRAMS
     Skills

     Write a Python program to calculate the area of a square.  Program 7  To multiply two numbers entered by the user.

                          Program7.py
                       File  Edit  Format   Run   Options   Window    Help

                       num1 = float(input("Enter the first number: "))
                       num2 = float(input("Enter the second number: "))
                       print("The product of the two numbers is:", num1 * num2)

                          Output
     MATHEMATICS
                       Enter the first number: 23
                       Enter the second number: 17
                       The product of the two numbers is: 391.0

     PRECEDENCE OF OPERATORS

                      Program 8    To calculate the perimeter of a rectangle.
     Operator precedence determines  the order in which operations  are performed.  Operators
     with higher precedence are evaluated first. When operators have the same precedence, Python   Program8.py
     evaluates them from left to right (associativity). The following table shows the operator precedence,   File  Edit  Format  Run  Options  Window  Help
     starting with the highest precedence at the top:
                       length = float(input("Enter the length of the rectangle: "))
     Operator  Name    width = float(input("Enter the width of the rectangle: "))
     ()  Parentheses   perimeter = 2 * (length + width)
                       print("The perimeter of the rectangle is:", perimeter)
     **  Exponentiation
     *, /, //, %  Multiplication, Division, Floor Division, Modulus
     +, –  Addition, Subtraction  Output
      <, <=, >, >=, !=, ==  Relational operators  Enter the length of the rectangle: 20

     not               Enter the width of the rectangle: 40
     and  Logical operators  The perimeter of the rectangle is: 120.0
     or

     =, %=, /=, //=, -=, +=, *=, **=  Assignment operators  Program 9 To calculate the average of three numbers.

                          Program9.py
     RAPID RECALL  Tick ( ) if you know this.
                       File  Edit  Format   Run   Options   Window    Help

     1.  A single-line comment begins with the hash symbol (#).   num1 = float(input("Enter the first number: "))
                       num2 = float(input("Enter the second number: "))
     2.  Keywords are reserved, so you cannot use them as variable names or identifiers.   num3 = float(input("Enter the third number: "))
                       average = (num1 + num2 + num3) / 3
                       print("The average of the three numbers is:", average)

                                                                                                                      101
                                                                                                  Python–Start to Code

| 98 99 100 101 102 103 104 105 106 107 108 |