CARVIEW |
- CCNA online course
- Linux online course
- VMware ESXi online course
- Nmap online course
- MySQL online course
- Raspberry Pi online course
- Apache HTTP Server course
- VMware Player online course
- Splunk online course
- SQL online course
- Oracle VirtualBox online course
- Python online course
- Asterisk course
- VMware Workstation Player course
- Process Explorer course
- Pillow online course
- Create a web crawler in Python
- A short introduction to…
Use for loop with the range() function
The range() function in Python is often used in for statements to define the number of loop iterations. This built-in function creates lists containing arithmetic progressions. The syntax of the range() function is:
range(start, stop [, step])
The start argument is the starting number. The stop argument is the last number (which is not included). The step argument is optional and defines the difference between each number in the sequence.
Here is an example. Let’s say that we want to loop through a for loop 10 times. We can use the range(1, 11) function inside a for statement to do that:
for i in range(1, 11): print('This is the', i, 'iteration.')
When run, the code above gives the following output:
>>> This is the 1 iteration. This is the 2 iteration. This is the 3 iteration. This is the 4 iteration. This is the 5 iteration. This is the 6 iteration. This is the 7 iteration. This is the 8 iteration. This is the 9 iteration. This is the 10 iteration. >>>
Notice how we didn’t specify the step argument in the example above, so it defaulted to 1. Also, notice that the last number is 10, and not 11. This is because the number defined as the stop argument isn’t included in the range.
Here is another example, this time with the step argument defined:
for i in range(1, 11, 2): print('This will print only odd numbers:', i)
The output:
>>> This will print only odd numbers: 1 This will print only odd numbers: 3 This will print only odd numbers: 5 This will print only odd numbers: 7 This will print only odd numbers: 9 >>>
Python course
- Introduction
- Python overview
- Install Python on Windows
- Install Python on Linux
- Add Python to the Windows Path
- Run Python code
- Interactive prompt
- IDLE editor
- Command line
- Help mode
- Basic programs
- Write your first program
- Use comments
- What are variables?
- Variable data types
- Variable names
- Numeric variables
- Strings
- Get the current date and time
- Operators overview
- Arithmetic operators
- Comparison operators
- Logical operators
- Assignment operators
- Membership operators
- Identity operators
- Conditional statements
- The if statement
- Get user input
- The if...else statement
- The if...elif statement
- Nested if statements
- Use logical operators
- Loops
- The for loop
- Use for loop with the range() function
- The break statement
- The continue statement
- The pass statement
- Use else statement in loops
- The while loop
- Nested loop statements
- Errors
- Types of errors
- Syntax and logical errors
- The try...except statements
- The try...except...else statements
- The try...except...finally statements
- Catch specific exceptions
- Raise exception
- Nest exception handling statements
- Modules
- What are modules?
- Import modules
- Find files on disk
- Display module content
- Strings
- What are strings?
- Escape characters
- Access individual characters
- String functions
- Search strings
- Concatenating strings
- Lists, sets, tuples, dictionaries
- What are lists?
- Modify lists
- Loop through a list
- Check whether a value is in a list
- Sorting lists temporarily
- Sorting lists permanently
- Obtaining the list length
- What are sets?
- What are dictionaries?
- Add new key-value pair to a dictionary
- Modify a value in a dictionary
- Delete a key-value pair in a dictionary
- Loop through a dictionary
- What are tuples?
- Looping over a tuple
- Working with files
- How to read and write files
- Read a file
- Read and write – with statement
- Make a list of lines from a file
- Functions
- What are functions?
- Return statement
- Positional arguments
- Keyword arguments
- Default values for parameters
- Flexible number of arguments
- Variable scopes