Python Fundamentals | Most Easiest and Powerful Language in the Industry of Technology | Python Programming Series-03
Lets move forward towards more interesting topics of the course "python fundamentals". We will cover the following topics in this part of Python Fundamentals course:
- Conditional Statements in Python
- If statement
- Simple if statement
- elif statement
- else statement
- Nested if statements
- While loop
- For loop
- Nested for loop
- Other Statements in Python
- Break statement
- Continue statement
CONDITIONAL STATEMENTS IN PYTHON:
IF STATEMENT
If statement in Python programming language develops logic for the output as all other programming languages do. We can use different variables and operators in if condition , discussed in the previous part 1 and part 2 of this article series, to design our logic and get the appropriate result. Syntax for if condition in Python is a little bit different, actually quite simple.
SYNTAX
- if condition :
- Statement1
- Statement2 ... and so on ...
- #You need to press backspace for exiting from if condition
IF STATEMENT IN PYTHON IDE:
ELSE STATEMENT WITH IF STATEMENT
What if you have to build a logic which contains another logic than if statement? You will simply use else statement with if statement as shown below:
SYNTAX
- if condition :
- Statement
- else :
- Statement
- #You need to press backspace to exit from each block
ELSE STATEMENT IN PYTHON IDE:
OUTPUT:
ELIF STATEMENT WITH IF STATEMENT
Now, as there can be more than just two conditions in a program so we use elif condition for these sort of programs and logic.
SYNTAX
- if condition :
- Statement
- elif :
- Statement
- elif :
- Statement
- else:
- Statement
- #You need to press backspace to exit from each block
- #You can use as many as elif statements in the code.
ELIF STATEMENTS IN PYTHON IDE:
OUTPUT:
NESTED IF-ELSE STATEMENT WITH IF STATEMENT
Finally, in the last part of if statement, we are going to look into the nested if-else statements. It is a bit harder as compared with provides simple statements but it helps you design a perfect and detailed logic for your program.
SYNTAX
- if condition :
- Statement
- if condition:
- Statement
- else:
- Statement
- elif :
- Statement
- if condition:
- Statement
- else:
- Statement
- else:
- Statement
- #You need to press backspace to exit from each block
- #You can use as many as nested if-else statements in the code.
NESTED IF-ELSE STATEMENT IN PYTHON IDE
NOTE:1. input() function takes the user input.
2. Correct in the print function, that letter is an upper case and lower case in other print function.
OUTPUT:
WHILE LOOP
There are different loops used in Python ranging from For Loop to While loop. Loops are responsible for the iterating or repeating values. Syntax for While loop is:
SYNTAX
- #While loop syntax
- while (condition) :
- Statement1
- Statement2 ... and so on ...
WHILE LOOP IN PYTHON IDE
OUTPUT:
FOR LOOP
For loops are executed until the certain conditions are meet. They are the most used loop in all over conditional statements. For loop iterates through all the values and increments them till the required value. The syntax of for loop is quite simple:
- #For loop syntax
- for <variable> in <range>:
- Statement1
- Statement2 ... and so on ...
WHILE LOOP IN PYTHON IDE
OUTPUT:
OTHER STATEMENTS IN PYTHON:
BREAK STATEMENT
Break statement terminates the loop statement and transfer the execution to the statement immediately following the loop:
0 Comments
Your comments and interesting view make our day!