Range( )+FOR





IMPORTANT!!

Please read carefully

*We at St Columba's School, are ready to take the next step - and move towards using Google Classroom as a tool to enhance the teaching learning environment.

*This shift involves the school giving each student a unique email address, which he will need to setup ( the teachers will assist him in doing the same). This email will facilitate each student to interact with his teachers.

* The student should use this email ID only for:

-interacting with  teachers on school /subject related matters
-keep the words and expressions as he would- if he  were interacting with us as a Columban student
-use the facilities connected to this email ID only for google meets organised with the permission of a teacher 
-he should have a copy of the permission given ( when and by which teacher and for what purpose)
-do not access any of the other facilities connected with this email address unless he first seeks the permission of a teacher
(He should have a copy of the permission given - when and by which teacher and for what purpose).

*Please note that Google and St Columba's have legal obligations by giving you access to this email address and hence,
you will need to sign an agreement when creating this email ID. 

*Be informed that the history of use of this ID shall have a digital footprint !

----------------------------------------------------------------------------
The Google Meet Link for today is :


------------------------------------------------------------------------------------------------------------
By the end of this session you will be able to:

  • understand the working of the variety of range( ) functions
  • Apply the concept learnt to to create range to solve the given problem
  • To be able to create for loop to solve the problem at hand. 
------------------------------------------------------------------------------------------------------- 
The range( ) function-
  • The range( ) function of Python is used with the FOR Loop.
  • The range( ) function generates a LIST which is a special sequence type.
  • Let us now see how the range function works.


A) The common use of range( ) is in the form given below:
   range(<lower limit> ,<upper limit>)      # both limits should be integer

For example
1)   range(0,5)
will produce list as [0,1,2,3,4]
2) range (12,18)
will produce list as [12,13,1415,16,17]
B) The other use of range( ) is in the form given below:
   range(<lower limit> ,<upper limit>,< step value>)
For example
1)   range(0,5,1)
will produce list as [0,1,2,3,4]
2) range (0,5,2)
will produce list as [0,2,4]
C) The other use of range( ) is in the form given below:
 range(<number>) 
This creates a list from 0(Zero) to number>-1
For example
1)   range(5)
will produce list as [0,1,2,3,4]

Let us also look at the Operators in and not in (MEMBERSHIP OPERATORS)
A) in Operator:
  • used with range( ) in for loop
  • it is used to check whether a value is contained inside a list.
  • Example-
    • 3 in [1,2,3,4]     will return True as the value 3 is contained in the list[12,3,4]
    • 5 in [1,2,3,4]     will return False as the value 5 is not contained in the list[12,3,4]    
    • 'a' in "trade"     will return True as the value 'a'  is contained in the string (list of characters)-"trade".          
B)   not in Operator:  

  • used with range( ) in for loop

    • it is used to check whether a value is not contained inside a list.
    • it is the opposite of in operator 
    • Example-
      • 3 not in [1,2,3,4]     will return False as the value 3 is contained in the list[12,3,4]
      • 5 not in [1,2,3,4]     will return True as the value 5 is not contained in the list[12,3,4] 
      • 'b' not in "trade"     will return True as the value 'b'  is  not contained in the string (list of characters)-"trade".    
    Let us look at a few examples of FOR loop:

    1.
                   for v in [1,2,3]:
                             print(v*v)

    The above program will produce the following output:
    1
    4
    9
    -----------------------------------------------------------------------------------
    2.             for ch in 'columbas'
                               print(ch)
    The above program will produce the following output:      
    c
    o
    l
    u
    m
    b
    a
    s
    --------------------------------------------------------------------------------
    3.                 for val  in range(3,9):
                             print(val)
    The above program will produce the following output:    
    3
    4
    5
    6
    7
    8

    -----------------------------------------------------------------------------------

    PROGRAM TO PRINT FIRST 10 NATURAL NUMBERS USING FOR LOOP

    for num in range(1,10):
          print(num)

    The OUTPUT will be:



    NOTE:
    If the FOR loop is 
          for n in range(1,num) 
    Then the FOR loop continues till num-1 
    Example 
          for num in range(1,10)     ,    will end at 9

    ------------------------------------------------------------------------------------------------------------
    EXERCISE

    Write a program in Python to print all even numbers till 20
    ---------------------------------------------------------------------------------------------------------

    Comments

    Post a Comment