Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 112 - 2615_KEC
P. 112

    To call a function

                                      def my_function()                                   Name of a function

                                            print (“Hello”)                                Body of a function

                                      my_function()                                        Function call

                      Program 1: To handle string using user-defined function
                          Program1.py
                       File  Edit  Format   Run    Options   Window    Help

                       #Function definition
                       def hello (name):
                           print('Hello ' +name+ '! How do you do?')

                       #Calling function by passing parameter
                       hello('Yash')

                      Program 2: To add two numbers using user-defined function

                          Program2.py

                       File  Edit  Format   Run    Options   Window    Help

                       #Program that demonstrate to create a user-defined function
                       def add (a, b):
                           c = a+b
                           print("The sum of both the numbers is", c)
                       add (12, 16)

                                STRING

                      A sequence of characters which is enclosed or surrounded by single (' '), double (" "), or Triple
                      ('''  ''') quotes is known as a string. The sequence may include a letter, number, special characters
                      or a backslash. Python treats single quotes as double quotes.

                      Program 3: To print a string

                          Program3.py
                       File  Edit  Format   Run    Options   Window    Help

                       str = "This is Shweta's pen."
                       print(str)

                      110   Premium Edition-VIII

| 107 108 109 110 111 112 113 114 115 116 117 |