Page 76 - Code & Click - 8
P. 76

Experiential Learning
              Lab Activity 3
             To create a user-defined function that takes arguments, returns a value and call it.

             1. Type the code as shown in the image in the Python Editor.














             2. Press the F5 key to execute the code. Sample output of the program is given below.
















            WORKING WITH STRINGS
            A String in Python is a sequence of characters (digits, alphabets, spaces, or symbols) enclosed within
            single quotes (‘ ’) or double quotes (“ ”). Examples of strings in Python are “Python”, ‘P’, “1234”, and
            “I Love Programming”.

            Strings can be assigned to a variable in two ways:
              1.  Enclosing the value in double quotes.
              2.  Accepting values from the user using the input( ) function.
                  For example,

                          Name = “DataMine”
                          Name = input(“Enter the name of the student : ”)

             Knowledge Discovery                                                                 Subject Enrichment

             The input( ) function converts any input value typed by the user into a string. Thus, the number 1234
             entered by the user will be converted to the string value “1234”. A string that has no characters between
             the double quotes, such as “ ”, is considered an Empty string.


            Traversing a String
            Traversing a string is the process used to access the characters in a string as required. Each character
            in a string is assigned a position called its Index. The leftmost character is assigned the index 0 and
            the remaining characters are assigned an index number in sequence. For example, in the string
            “Python” the indexes assigned to various characters are:

                                                       P   y   t   h    o   n
                                                       0   1   2   3    4   5

            74
   71   72   73   74   75   76   77   78   79   80   81