407 later stage be used as a float variable as well. |
413 later stage be used as a float variable as well. |
408 |
414 |
409 Strings |
415 Strings |
410 ~~~~~~~ |
416 ~~~~~~~ |
411 |
417 |
|
418 Strings are one of the essential data structures of any programming language. |
|
419 The ``print "Hello, World!"`` program was introduced in the earlier section, and |
|
420 the *"Hello, World!"* in the print statement is a string. A string is basically |
|
421 a set of characters. Strings can be represented in various ways shown below: |
|
422 |
|
423 :: |
|
424 |
|
425 s = 'this is a string' # a string variable can be represented using single quotes |
|
426 s = 'This one has "quotes" inside!' # The string can have quotes inside it as shown |
|
427 s = "I have 'single-quotes' inside!" |
|
428 l = "A string spanning many lines\ |
|
429 one more line\ |
|
430 yet another" # a string can span more than a single line. |
|
431 t = """A triple quoted string does # another way of representing multiline strings. |
|
432 not need to be escaped at the end and |
|
433 "can have nested quotes" etc.""" |
|
434 |
|
435 Try the following on the interpreter: |
|
436 ``s = 'this is a string with 'quotes' of similar kind'`` |
|
437 |
|
438 **Exercise: How to use single quotes within single quotes in a string as shown |
|
439 in the above example without getting an error?** |
|
440 |
|
441 String operations |
|
442 ----------------- |
|
443 |
|
444 A few basic string operations are presented here. |
|
445 |
|
446 **String concatenation** |
|
447 String concatenation is done by simple addition of two strings. |
|
448 |
|
449 :: |
|
450 |
|
451 >>> x = 'Hello' |
|
452 >>> y = ' Python' |
|
453 >>> print x+y |
|
454 Hello Python |
|
455 |
|
456 *Try this yourself:* |
|
457 |
|
458 :: |
|
459 |
|
460 >>> somenum = 13 |
|
461 >>> print x+somenum |
|
462 |
|
463 The problem with the above example is that here a string variable and an integer |
|
464 variable are trying to be concantenated. To obtain the desired result from the |
|
465 above example the str(), repr() and the `` can be used. |
|
466 |
|
467 **str()** simply converts a value to a string in a reasonable form. |
|
468 **repr()** creates a string that is a representation of the value. |
|
469 |
|
470 The difference can be seen in the example shown below: |
|
471 |
|
472 :: |
|
473 |
|
474 >>> str(1000000000000000000000000000000000000000000000000L) |
|
475 '1000000000000000000000000000000000000000000000000' |
|
476 >>> repr(1000000000000000000000000000000000000000000000000L) |
|
477 '1000000000000000000000000000000000000000000000000L' |
|
478 |
|
479 It can be observed that the 'L' in the long value shown was omitted by str(), |
|
480 whereas repr() converted that into a string too. An alternative way of using |
|
481 repr(value) is ```value```. |
|
482 |
|
483 A few more examples: |
|
484 :: |
|
485 |
|
486 >>> x = "Let's go \nto Pycon" |
|
487 >>> print x |
|
488 Let's go |
|
489 to Pycon |
|
490 |
|
491 In the above example, notice that the '\n'(newline) character is formatted and |
|
492 the string is printed on two lines. The strings discussed until now were normal |
|
493 strings. Other than these there are two other types of strings namely, raw strings |
|
494 and unicode strings. |
|
495 |
|
496 **Raw strings** are strings which are unformatted, that is the backslashes(\) are |
|
497 not parsed and are left as it is in the string. Raw strings are represented with |
|
498 an 'r' at the start of a string. |
|
499 Let us look at an example |
|
500 |
|
501 :: |
|
502 |
|
503 >>> x = r"Let's go \nto Pycon" |
|
504 >>> print x |
|
505 Let's go \nto Pycon |
|
506 |
|
507 Note: The '\n' is not being parsed into a new line and is left as it is. |
|
508 |
|
509 *Try this yourself:* |
|
510 |
|
511 :: |
|
512 |
|
513 >>> x = r"Let's go to Pycon\" |
|
514 |
|
515 **Unicode strings** are strings where the characters are Unicode characters as |
|
516 opposed to ASCII characters. Unicode strings are represented with a 'u' at the |
|
517 start of the string. |
|
518 Let us look at an example: |
|
519 |
|
520 :: |
|
521 |
|
522 >>> x = u"Let's go to Pycon!" |
|
523 >>> print x |
|
524 Let's go to Pycon! |
|
525 |
|
526 Boolean |
|
527 ~~~~~~~ |
|
528 |
|
529 Python also provides special Boolean datatype. A boolean variable can assume a |
|
530 value of either *True* or *False* (Note the capitalizations). |
|
531 |
|
532 Let us look at examples: |
|
533 |
|
534 :: |
|
535 |
|
536 >>> t = True |
|
537 >>> f = not t |
|
538 >>> print f |
|
539 False |
|
540 >>> f or t |
|
541 True |
|
542 >>> f and t |
|
543 False |
|
544 |
|
545 The **while** loop |
|
546 ~~~~~~~~~~~~~~~~~~ |
|
547 |
|
548 The Python **while** loop is similar to the C/C++ while loop. The syntax is as |
|
549 follows: |
|
550 |
|
551 :: |
|
552 |
|
553 statement 0 |
|
554 while condition: |
|
555 statement 1 #while block |
|
556 statement 2 #while block |
|
557 statement 3 #outside the while block. |
|
558 |
|
559 Let us look at an example: |
|
560 |
|
561 :: |
|
562 |
|
563 >>> x = 1 |
|
564 >>> while x <= 5: |
|
565 ... print x |
|
566 ... x += 1 |
|
567 ... |
|
568 1 |
|
569 2 |
|
570 3 |
|
571 4 |
|
572 5 |
|
573 |
|
574 The **if** conditional |
|
575 ~~~~~~~~~~~~~~~~~~~~~~ |
|
576 |
|
577 The Python **if** block provides the conditional execution of statements. |
|
578 If the condition evaluates as true the block of statements defined under the if |
|
579 block are executed. |
|
580 |
|
581 If the first block is not executed on account of the condition not being satisfied, |
|
582 the set of statements in the **else** block are executed. |
|
583 |
|
584 The **elif** block provides the functionality of evaluation of multiple conditions |
|
585 as shown in the example. |
|
586 |
|
587 The syntax is as follows: |
|
588 |
|
589 :: |
|
590 |
|
591 if condition : |
|
592 statement_1 |
|
593 statement_2 |
|
594 |
|
595 elif condition: |
|
596 statement_3 |
|
597 statement_4 |
|
598 else: |
|
599 statement_5 |
|
600 statement_6 |
|
601 |
|
602 Let us look at an example: |
|
603 |
|
604 :: |
|
605 |
|
606 >>> n = raw_input("Input a number:") |
|
607 >>> if n < 0: |
|
608 print n," is negative" |
|
609 elif n > 0: |
|
610 print n," is positive" |
|
611 else: |
|
612 print n, " is 0" |
|
613 |
|
614 **raw_input()** |
|
615 ~~~~~~~~~~~~~~~ |
|
616 |
|
617 In the previous example we saw the call to the raw_input() subroutine. |
|
618 The **raw_input()** method is used to take user inputs through the console. |
|
619 Unlike **input()** which assumes the data entered by the user as a standard python |
|
620 expression, **raw_input()** treats all the input data as raw data and converts |
|
621 everything into a string. To illustrate this let us look at an example. |
|
622 |
|
623 :: |
|
624 |
|
625 >>> input("Enter a number thats a palindrome:") |
|
626 Enter a number thats a palindrome:121 |
|
627 121 |
|
628 |
|
629 >>> input("Enter your name:") |
|
630 Enter your name:PythonFreak |
|
631 Traceback (most recent call last): |
|
632 File "<stdin>", line 1, in <module> |
|
633 File "<string>", line 1, in <module> |
|
634 NameError: name 'PythonFreak' is not defined |
|
635 |
|
636 As shown above the **input()** assumes that the data entered is a valid Python |
|
637 expression. In the first call it prompts for an integer input and when entered |
|
638 it accepts the integer as an integer, whereas in the second call, when the string |
|
639 is entered without the quotes, **input()** assumes that the entered data is a valid |
|
640 Python expression and hence it raises and exception saying PythonFreak is not |
|
641 defined. |
|
642 |
|
643 :: |
|
644 |
|
645 >>> input("Enter your name:") |
|
646 Enter your name:'PythonFreak' |
|
647 'PythonFreak' |
|
648 >>> |
|
649 |
|
650 Here the name is accepted because its entered as a string (within quotes). But |
|
651 its unreasonable to go on using quotes each time a string is entered. Hence the |
|
652 alternative is to use **raw_input()**. |
|
653 |
|
654 Let us now look at how **raw_input()** operates with an example. |
|
655 |
|
656 :: |
|
657 |
|
658 >>> raw_input("Enter your name:") |
|
659 Enter your name:PythonFreak |
|
660 'PythonFreak' |
|
661 |
|
662 Observe that the **raw_input()** is converting it into a string all by itself. |
|
663 |
|
664 :: |
|
665 |
|
666 >>> pal = raw_input("Enter a number thats a palindrome:") |
|
667 Enter a number thats a palindrome:121 |
|
668 '121' |
|
669 |
|
670 Observe that **raw_input()** is converting the integer 121 also to a string as |
|
671 '121'. Let us look at another example: |
|
672 |
|
673 :: |
|
674 |
|
675 >>> pal = raw_input("Enter a number thats a palindrome:") |
|
676 Enter a number thats a palindrome:121 |
|
677 >>> pal + 2 |
|
678 Traceback (most recent call last): |
|
679 File "<stdin>", line 1, in <module> |
|
680 TypeError: cannot concatenate 'str' and 'int' objects |
|
681 >>> pal |
|
682 '121' |
|
683 |
|
684 Observe here that the variable *pal* is a string and hence integer operations |
|
685 cannot be performed on it. Hence the exception is raised. |
|
686 |
|
687 **int()** method |
|
688 ~~~~~~~~~~~~~~~~ |
|
689 |
|
690 Generally for computing purposes, the data used is not strings or raw data but |
|
691 on integers, floats and similar mathematical data structures. The data obtained |
|
692 from **raw_input()** is raw data in the form of strings. In order to obtain integers |
|
693 from strings we use the method **int()**. |
|
694 |
|
695 Let us look at an example. |
|
696 |
|
697 :: |
|
698 |
|
699 >>> intpal = int(pal) |
|
700 >>> intpal |
|
701 121 |
|
702 |
|
703 In the previous example it was observed that *pal* was a string variable. Here |
|
704 using the **int()** method the string *pal* was converted to an integer variable. |
|
705 |
|
706 *Try This Yourself:* |
|
707 |
|
708 :: |
|
709 |
|
710 >>> stringvar = raw_input("Enter a name:") |
|
711 Enter a name:Guido Van Rossum |
|
712 >>> stringvar |
|
713 'Guido Van Rossum' |
|
714 >>> numvar = int(stringvar) |
|
715 |
|
716 |
|
717 Functions in Python: **def** |
|
718 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
719 |
|
720 *Functions* allow us to enclose a set of statements and call the function again |
|
721 and again instead of repeating the group of statements everytime. Functions also |
|
722 allow us to isolate a piece of code from all the other code and provides the |
|
723 convenience of not polluting the global variables. |
|
724 |
|
725 *Function* in python is defined with the keyword **def** followed by the name |
|
726 of the function, in turn followed by a pair of parenthesis which encloses the |
|
727 list of parameters to the function. The definition line ends with a ':'. The |
|
728 definition line is followed by the body of the function intended by one block. |
|
729 The *Function* must return a value:: |
|
730 |
|
731 def factorial(n): |
|
732 fact = 1 |
|
733 for i in range(2, n): |
|
734 fact *= i |
|
735 |
|
736 return fact |
|
737 |
|
738 The code snippet above defines a function with the name factorial, takes the |
|
739 number for which the factorial must be computed, computes the factorial and |
|
740 returns the value. |
|
741 |
|
742 A *Function* once defined can be used or called anywhere else in the program. We |
|
743 call a fucntion with its name followed by a pair of parenthesis which encloses |
|
744 the arguments to the function. |
|
745 |
|
746 The value that function returns can be assigned to a variable. Let's call the |
|
747 above function and store the factorial in a variable:: |
|
748 |
|
749 fact5 = factorial(5) |
|
750 |
|
751 The value of fact5 will now be 120, which is the factorial of 5. Note that we |
|
752 passed 5 as the argument to the function. |
|
753 |
|
754 It may be necessary to document what the function does, for each of the function |
|
755 to help the person who reads our code to understand it better. In order to do |
|
756 this Python allows the first line of the function body to be a string. This |
|
757 string is called as *Documentation String* or *docstring*. *docstrings* prove |
|
758 to be very handy since there are number of tools which can pull out all the |
|
759 docstrings from Python functions and generate the documentation automatically |
|
760 from it. *docstrings* for functions can be written as follows:: |
|
761 |
|
762 def factorial(n): |
|
763 'Returns the factorial for the number n.' |
|
764 fact = 1 |
|
765 for i in range(2, n): |
|
766 fact *= i |
|
767 |
|
768 return fact |
|
769 |
|
770 An important point to note at this point is that, a function can return any |
|
771 Python value or a Python object, which also includes a *Tuple*. A *Tuple* is |
|
772 just a collection of values and those values themselves can be of any other |
|
773 valid Python datatypes, including *Lists*, *Tuples*, *Dictionaries* among other |
|
774 things. So effectively, if a function can return a tuple, it can return any |
|
775 number of values through a tuple |
|
776 |
|
777 Let us write a small function to swap two values:: |
|
778 |
|
779 def swap(a, b): |
|
780 return b, a |
|
781 |
|
782 c, d = swap(a, b) |
|
783 |
|
784 Function scope |
|
785 --------------- |
|
786 The variables used inside the function are confined to the function's scope |
|
787 and doesn't pollute the variables of the same name outside the scope of the |
|
788 function. Also the arguments passed to the function are passed by-value if |
|
789 it is of basic Python data type:: |
|
790 |
|
791 def cant_change(n): |
|
792 n = 10 |
|
793 |
|
794 n = 5 |
|
795 cant_change(n) |
|
796 |
|
797 Upon running this code, what do you think would have happened to value of n |
|
798 which was assigned 5 before the function call? If you have already tried out |
|
799 that snippet on the interpreter you already know that the value of n is not |
|
800 changed. This is true of any immutable types of Python like *Numbers*, *Strings* |
|
801 and *Tuples*. But when you pass mutable objects like *Lists* and *Dictionaries* |
|
802 the values are manipulated even outside the function:: |
|
803 |
|
804 >>> def can_change(n): |
|
805 ... n[1] = James |
|
806 ... |
|
807 |
|
808 >>> name = ['Mr.', 'Steve', 'Gosling'] |
|
809 >>> can_change(name) |
|
810 >>> name |
|
811 ['Mr.', 'James', 'Gosling'] |
|
812 |
|
813 If nothing is returned by the function explicitly, Python takes care to return |
|
814 None when the funnction is called. |
|
815 |
|
816 Default Arguments |
|
817 ----------------- |
|
818 |
|
819 There may be situations where we need to allow the functions to take the |
|
820 arguments optionally. Python allows us to define function this way by providing |
|
821 a facility called *Default Arguments*. For example, we need to write a function |
|
822 that returns a list of fibonacci numbers. Since our function cannot generate an |
|
823 infinite list of fibonacci numbers, we need to specify the number of elements |
|
824 that the fibonacci sequence must contain. Suppose, additionally, we want to the |
|
825 function to return 10 numbers in the sequence if no option is specified we can |
|
826 define the function as follows:: |
|
827 |
|
828 def fib(n=10): |
|
829 fib_list = [0, 1] |
|
830 for i in range(n - 2): |
|
831 next = fib_list[-2] + fib_list[-1] |
|
832 fib_list.append(next) |
|
833 return fib_list |
|
834 |
|
835 When we call this function, we can optionally specify the value for the |
|
836 parameter n, during the call as an argument. Calling with no argument and |
|
837 argument with n=5 returns the following fibonacci sequences:: |
|
838 |
|
839 fib() |
|
840 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] |
|
841 fib(5) |
|
842 [0, 1, 1, 2, 3] |
|
843 |
|
844 Keyword Arguments |
|
845 ----------------- |
|
846 |
|
847 When a function takes a large number of arguments, it may be difficult to |
|
848 remember the order of the parameters in the function definition or it may |
|
849 be necessary to pass values to only certain parameters since others take |
|
850 the default value. In either of these cases, Python provides the facility |
|
851 of passing arguments by specifying the name of the parameter as defined in |
|
852 the function definition. This is known as *Keyword Arguments*. |
|
853 |
|
854 In a function call, *Keyword arguments* can be used for each argument, in the |
|
855 following fashion:: |
|
856 |
|
857 argument_name=argument_value |
|
858 Also denoted as: keyword=argument |
|
859 |
|
860 def wish(name='World', greetings='Hello'): |
|
861 print "%s, %s!" % (greetings, name) |
|
862 |
|
863 This function can be called in one of the following ways. It is important to |
|
864 note that no restriction is imposed in the order in which *Keyword arguments* |
|
865 can be specified. Also note, that we have combined *Keyword arguments* with |
|
866 *Default arguments* in this example, however it is not necessary:: |
|
867 |
|
868 wish(name='Guido', greetings='Hey') |
|
869 wish(greetings='Hey', name='Guido') |
|
870 |
|
871 Calling functions by specifying arguments in the order of parameters specified |
|
872 in the function definition is called as *Positional arguments*, as opposed to |
|
873 *Keyword arguments*. It is possible to use both *Positional arguments* and |
|
874 *Keyword arguments* in a single function call. But Python doesn't allow us to |
|
875 bungle up both of them. The arguments to the function, in the call, must always |
|
876 start with *Positional arguments* which is in turn followed by *Keyword |
|
877 arguments*:: |
|
878 |
|
879 def my_func(x, y, z, u, v, w): |
|
880 # initialize variables. |
|
881 ... |
|
882 # do some stuff |
|
883 ... |
|
884 # return the value |
|
885 |
|
886 It is valid to call the above functions in the following ways:: |
|
887 |
|
888 my_func(10, 20, 30, u=1.0, v=2.0, w=3.0) |
|
889 my_func(10, 20, 30, 1.0, 2.0, w=3.0) |
|
890 my_func(10, 20, z=30, u=1.0, v=2.0, w=3.0) |
|
891 my_func(x=10, y=20, z=30, u=1.0, v=2.0, w=3.0) |
|
892 |
|
893 Following lists some of the invalid calls:: |
|
894 |
|
895 my_func(10, 20, z=30, 1.0, 2.0, 3.0) |
|
896 my_func(x=10, 20, z=30, 1.0, 2.0, 3.0) |
|
897 my_func(x=10, y=20, z=30, u=1.0, v=2.0, 3.0) |
|
898 |
|
899 Parameter Packing and Unpacking |
|
900 ------------------------------- |
|
901 |
|
902 The positional arguments passed to a function can be collected in a tuple |
|
903 parameter and keyword arguments can be collected in a dictionary. Since keyword |
|
904 arguments must always be the last set of arguments passed to a function, the |
|
905 keyword dictionary parameter must be the last parameter. The function definition |
|
906 must include a list explicit parameters, followed by tuple paramter collecting |
|
907 parameter, whose name is preceded by a *****, for collecting positional |
|
908 parameters, in turn followed by the dictionary collecting parameter, whose name |
|
909 is preceded by a ****** :: |
|
910 |
|
911 def print_report(title, *args, **name): |
|
912 """Structure of *args* |
|
913 (age, email-id) |
|
914 Structure of *name* |
|
915 { |
|
916 'first': First Name |
|
917 'middle': Middle Name |
|
918 'last': Last Name |
|
919 } |
|
920 """ |
|
921 |
|
922 print "Title: %s" % (title) |
|
923 print "Full name: %(first)s %(middle)s %(last)s" % name |
|
924 print "Age: %d\nEmail-ID: %s" % args |
|
925 |
|
926 The above function can be called as. Note, the order of keyword parameters can |
|
927 be interchanged:: |
|
928 |
|
929 >>> print_report('Employee Report', 29, 'johny@example.com', first='Johny', |
|
930 last='Charles', middle='Douglas') |
|
931 Title: Employee Report |
|
932 Full name: Johny Douglas Charles |
|
933 Age: 29 |
|
934 Email-ID: johny@example.com |
|
935 |
|
936 The reverse of this can also be achieved by using a very identical syntax while |
|
937 calling the function. A tuple or a dictionary can be passed as arguments in |
|
938 place of a list of *Positional arguments* or *Keyword arguments* respectively |
|
939 using ***** or ****** :: |
|
940 |
|
941 def print_report(title, age, email, first, middle, last): |
|
942 print "Title: %s" % (title) |
|
943 print "Full name: %s %s %s" % (first, middle, last) |
|
944 print "Age: %d\nEmail-ID: %s" % (age, email) |
|
945 |
|
946 >>> args = (29, 'johny@example.com') |
|
947 >>> name = { |
|
948 'first': 'Johny', |
|
949 'middle': 'Charles', |
|
950 'last': 'Douglas' |
|
951 } |
|
952 >>> print_report('Employee Report', *args, **name) |
|
953 Title: Employee Report |
|
954 Full name: Johny Charles Douglas |
|
955 Age: 29 |
|
956 Email-ID: johny@example.com |
|
957 |
|
958 Nested Functions and Scopes |
|
959 --------------------------- |
|
960 |
|
961 Python allows nesting one function inside another. This style of programming |
|
962 turns out to be extremely flexible and powerful features when we use *Python |
|
963 decorators*. We will not talk about decorators is beyond the scope of this |
|
964 course. If you are interested in knowing more about *decorator programming* in |
|
965 Python you are suggested to read: |
|
966 |
|
967 | http://avinashv.net/2008/04/python-decorators-syntactic-sugar/ |
|
968 | http://personalpages.tds.net/~kent37/kk/00001.html |
|
969 |
|
970 However, the following is an example for nested functions in Python:: |
|
971 |
|
972 def outer(): |
|
973 print "Outer..." |
|
974 def inner(): |
|
975 print "Inner..." |
|
976 print "Outer..." |
|
977 inner() |
|
978 |
|
979 >>> outer() |
|
980 |