Page 197 - Data Science Algorithms in a Week
P. 197

Python Reference


            Output:

                $ python example09_if_else_elif.py
                The variable x is equal to 10.
                The variable x is not greater than 20.
                The variable x is not greater than 10, but greater than 5.


            For loop

            For loop enables the iteration through every element in some set of elements, e.g. range,
            python set, list.


            For loop on range

            Input:

                source_code/appendix_c_python/example10_for_loop_range.py
                print "The first 5 positive integers are:"
                for i in range(1,6):
                        print i
            Output:

                $ python example10_for_loop_range.py
                The first 5 positive integers are:
                1
                2
                3
                4
                5


            For loop on list

            Input:

                source_code/appendix_c_python/example11_for_loop_list.py
                primes = [2, 3, 5, 7, 11, 13]
                print 'The first', len(primes), 'primes are:'
                for prime in primes:
                        print prime







                                                    [ 185 ]
   192   193   194   195   196   197   198   199   200   201   202