557 statement 3 #outside the while block. |
557 statement 3 #outside the while block. |
558 |
558 |
559 Let us look at an example: |
559 Let us look at an example: |
560 |
560 |
561 :: |
561 :: |
562 |
562 |
563 |
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 |
|
691 |
|
692 Let us look at an example. |