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 if statement
The easiest method for making a decision in Python is by using the if statement. This enables you to select the actions to perform if the evaluated condition is true. The syntax of the if statement is:
if condition: statements
As you can see, the if statement begins with the keyword if. After the if keyword comes a condition that will be evaluated. The condition must end with a colon (:). After the condition come the statements that will be performed if the condition evaluates to true. Note that the text indentation is important in if statements and affects the meaning of code, so make sure to indent the statements.
Consider the following example:
x = 5 if x == 5: print('Hello world!')
In the example above the value of x is set to 5. The if statement will evaluate whether the x = 5 and print Hello world! if the condition is evaluated to true. Because x indeed is 5, the text Hello world! will be printed.
Here is another example:
x = 5 if x == 6: print('Hello world!')
In the example above the text will not be printed because x is not equal to 6.
One more example:
myName = 'Mark' if myName == 'Mark': print('Hi Mark!')
In this case, the text Hi Mark! will be printed because the variable myName indeed holds the string Mark:
>>> Hi Mark! >>>
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