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…
Nested if statements
You can place an if (or if…else, if…elif) statement inside another statement. This process is called nesting and enables you to make complex decisions based on different inputs. Here is a sample syntax:
if condition: if condition: statements else: statements else: statements
Note how the lines above are indented. The indentation is important because it informs Python that the indented statement is the second-level statement.
Consider the following example:
x = int(input('Enter your age: ')) if x > 21: if x > 100: print('You are too old, go away!') else: print('Welcome, you are of the right age!') else: print('You are too young, go away!')
The program above prompts the user to enter his age. The program will first determine if the user is older than 21. If that is the case, the program will additionally check if the user is older than 100. If that is also the case, the program will print the corresponding message, indicating that the user is too old. If the user entered a number between 21 and 100, the Welcome, you are of the right age! message will be printed. If the user has entered a number less than 21, he will be informed that he is too young. Note that the nested if…else statement will not be executed at all if the user has entered a value less than 21.
The output of the example above:
Enter your age: 55 Welcome, you are of the right age! >>> Enter your age: 13 You are too young, go away! >>> Enter your age: 101 You are too old, go away! >>>
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