Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 123 - 2615_KEC
P. 123

    SOME MORE PROGRAMS

                     Program 19: To calculate the area of different shapes

                         Program19.py
                      File  Edit  Format   Run    Options   Window    Help

                      def calculate_area(name):
                          name = name.lower()
                          if(name == 'rectangle'):
                              l = float(input('Enter the length of the rectangle: '))
                              b = float (input('Enter the breadth of the rectangle: '))
                              rect_area = 1 * b
                              print('The area of the rectangle is: ', rect_area)
                          elif(name == 'square'):
                              s = float(input('Enter the side of the square: '))
                              sqr_area = s * s
                              print('The area of the square is:', sqr_area)
                          elif (name == 'circle'):
                              pi = 3.14
                              r = float(input('Enter the radius of the circle: '))
                              circle_area = pi * r**2
                              print('The area of the circle is: ', circle_area)
                          else:
                              print('sorry! This shape is not available')
                      print('Calculate Area for: Rectangle, Square, or Circle')
                      shape_name = input('Enter the name of the shape whose area you want to find: ')
                      calculate_area(shape_name)

                         Output
                      Calculate Area for: Rectangle, Square, or Circle
                      Enter the name of the shape whose area you want to find: Circle
                      Enter the radius of the circle: 4.5
                      The area of the circle is:  63.585

                     Program 20: To access string characters
                         Program20.py

                      File  Edit  Format   Run    Options   Window    Help

                      #Accessing string characters in Python
                      str = 'Orange Education'
                      print('str = ', str)
                      print('str[0] = ', str[0])                 #First character
                      print('str[-1] = ', str[-1])               #Last character

                                                                                       Functions, String and List in Python  121

| 118 119 120 121 122 123 124 125 126 127 128 |