2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: Range Method
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# Range Function
  
						 
					
						
							
								
									
										
										
										
											2018-12-20 06:07:33 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								If you need to iterate over a sequence of numbers, the built-in function range() comes in handy. It generates arithmetic progressions:
							 
						 
					
						
							
								
									
										
										
										
											2018-10-04 14:47:55 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### Example Usage
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 ```py
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								for i in range(5):
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    print(i)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 ```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 #### Output
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 ```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								0
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								1
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								2
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								3
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								4
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 ```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 14:37:30 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								#### Example with optional additional arguments
  
						 
					
						
							
								
									
										
										
										
											2018-12-20 06:07:33 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								 ```py
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 # A range function call.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								range(start, stop, step)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 ```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-15 20:52:19 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The first argument, *start* , is the starting number of the sequence.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The second argument, *stop* , means to generate numbers up to, but not including this number.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The third argument, *step* , is the amount to increment by. In other words, it's the difference between each number in the sequence. It defaults to 1 if step is not specified.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 14:37:30 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								 ```py
							 
						 
					
						
							
								
									
										
										
										
											2018-10-15 20:52:19 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								for i in range(3, 12, 2):
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 14:37:30 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    print(i)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 ```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 #### Output
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 ```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								3
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								5
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								7
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								9
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								11
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 ```
							 
						 
					
						
							
								
									
										
										
										
											2018-11-01 09:41:43 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 #### Notes
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 In Python 2, there are 2 functions for going through a range of numbers: range() and xrange().
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 Out of these functions, xrange() is the "lazy" function, meaning it generates numbers as necessary instead of actually creating
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 a list of numbers and iterating through them. range(), on the other hand, makes an entire list of numbers and iterates through
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 this list. This makes it a strain on the memory in the case of really long lists.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								 In Python 3, the range() function mimics xrange() as the "lazy" variant, and xrange() itself has been removed.