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…
The while loop
The while loop in Python is used to repeatedly execute statements as long as the given condition is true. When the condition becomes false, the program execution is resumed at the next statement after the body of the while loop. The syntax of the while loop is:
while condition: statements
Here is a simple example of the while loop in action:
x = 0 while x < 10: print(x) x += 1
The code above will print the value of the variable x as long as x is less than 10. The output:
>>> 0 1 2 3 4 5 6 7 8 9 >>>
Notice how we have added 1 to x in each loop iteration (the x += 1 statement). This is important because without it, the loop would be an endless loop, meaning that it would never end – the value of x would never increase and the loop would execute forever. Try removing the x+=1 line and see what happens.
Here is another example:
x = int(input('Enter a number: ')) while x != 0: y = x*10 print(x, 'times 10 equals', y) x = int(input('Enter a number. Press 0 to quit: '))
The code above asks the user to enter a number and it multiply it by 10. If the user enters 0, the program will terminate:
Enter a number: 14 14 times 10 equals 140 Enter a number. Press 0 to quit: 54 54 times 10 equals 540 Enter a number. Press 0 to quit: 47 47 times 10 equals 470 Enter a number. Press 0 to quit: 11 11 times 10 equals 110 Enter a number. Press 0 to quit: 2 2 times 10 equals 20 Enter a number. Press 0 to quit: 14 14 times 10 equals 140 Enter a number. Press 0 to quit: 0 >>>
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