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 logical operators
The logical operators in Python (and, or, not) are often used in the if, if…else, and if…elif statements. They enable you to make multiple comparisons inside a single statement, such as to determine whether a value is within a certain range. Consider the following example:
x = int(input('Enter your age: ')) if x < 21 or x > 100: print('You are too young or too old, go away!') else: print('Welcome, you are of the right age!')
Here is the output of the various inputs:
Enter your age: 14 You are too young or too old, go away! >>> Enter your age: 101 You are too young or too old, go away! >>> Enter your age: 25 Welcome, you are of the right age! >>>
The code above checks to see if the user has entered a value less than 21 or greater than 101 (if x < 21 or x > 100) and if that is the case, the user is informed that she is too young or too old. If the user enters any other number between 21 and 100, the Welcome, you are of the right age! message will be displayed.
Here is another example:
x = int(input('Enter your age: ')) if x > 0 and x < 21: print('Oww, you are still a child!') elif x > 21 and x < 40: print('You are so young!') elif x > 40 and x < 60: print('You are not that young, but not that old also!') elif x > 60 and x < 100: print ('Golden ages!') elif x < 0 or x > 100: print ('I do not really believe you that you are younger than 0 or older than 100!') else: print ('Invalid selection.')
The code above will check the user input and print the corresponding message. For example, if the user enters the value of 45, the elif x > 40 and x < 60 statement will evaluate to True and the You are not that young, but not that old also! message will be printed.
Here is the output:
Enter your age: 58 You are not that young, but not that old also! >>> Enter your age: 12 Oww, you are still a child! >>> Enter your age: 105 I do not really believe you that you are younger than 0 or older than 100! >>> Enter your age: -6 I do not really believe you that you are younger than 0 or older than 100! >>> Enter your age: 25 You are so young! >>> Enter your age: 68 Golden ages!
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