Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# How to Use Conditional Statements in Python?

Last Updated on November 30, 2022 by Prepbytes

Conditional statements in python are used to execute a block of code based on the evolution of a specific constraint is true or false. If the statement is used to perform conditional statements in python. In this article, we will see how to use conditional statements in python.

## If conditional statements in Python:

If statement in python is used to make true or false decisions. It executes particular code based on a result of the logical condition. Letâ€™s see what types of logical conditions we can use with the If statement.

• Equals ( A == B )
• Not Equals ( A != B )
• Less than ( A < B )
• Greater Than ( A > B )
• Less than or equal to ( A <= B )
• Greater than or equal to ( A >= B )
• Logical and ( A and B )
• Logical or ( A or B )
• Logical not ( not A )
• In ( A in B)
• Not in ( A not in B )

Based on the output of the above python operators true or false, if statement will execute a block of code. Letâ€™s see how the flow chart of the if statement will work.
How to write if conditional statements in python. Syntax of the if statement is given below.

``````if condition:
# statement
# statement
â€¦
# statement
# following statment``````

In the above syntax, first, if the condition will be checked if it is true then statements in the block will be run and if the condition is false then statements in the block will be not run. After that, it will directly go to the following statement. Letâ€™s understand this with an example.

``````A=10
B=5

if A>B:
print("The condition is true")
print("Following statement 1")

if A``````
``` In the above program, we checked the first condition ( A>B ) which is true so if block code will be executed. After that following statement will be executed. The second condition ( A< B ) is false so if the block code will not be executed and direct the following statement will be executed. If else conditional statements in Python: We already learned how to use the if statement in python. We already know that if the condition is true then if block will be executed else it will not be executed. Now we add one more block which is the else block. Now, if the if condition will be true then it will execute only the if block, and if the condition is not true then it will execute the else block. The syntax of the if-else conditional statement in python is given below. if condition: #statement #statement â€¦ #statement else: #statement #statement â€¦ #statement In the above syntax, first, the if condition will be checked. Now, if the condition is true then the if block will be executed, and if the condition is false then the else block will be executed. Letâ€™s understand the if-else statement with help of an example. A=10 B=5 # check A is greater than B (True) if A>B: print("If block is executed because the condition is true") else: print("Else block is executed because the condition is false") print() # check A is less than B (False) if A In the above program, we created two variables A and B. After that first, we checked if A > B, it is true thus the if block will be executed after that we checked A< B which is false so in this case else block will be executed. If elif conditional statements in Python:- We already know the use of the if statement. Now, letâ€™s understand how to use elif in python. Elif is used to check another condition and if that condition is true then the elif block will be executed. Thus, first, if condition is checked and if it is false then the elif condition will be checked. Syntax of the if elif statement is given below. if condition: #statement #statement â€¦ #statement elif condition: #statement #statement â€¦ #statement In the above syntax, first, the if condition will be checked if it is true then the if block will be executed otherwise elif condition will be checked and if it is true then the elif block will be executed. Letâ€™s understand this with help of an example. A=10 B=5 if A>B: print("A is greater than B") elif AB: print("A is greater than B") elif A In the above program, we created two variables A and B. First, we checked if A is greater than B and it is true so in the first case, the if block will be executed. Secondly, we changed the values of A and B, and then we again check if A is greater than B and it is false so now the elif condition will be checked and it is true so in the second case, the elif block will be executed. If elif else conditional statements in Python: We already know how to use if, elif, and else all three but now letâ€™s try to use all three together. First of all, if the condition will be checked and if it is true then if block will be executed otherwise elif condition will be checked and if it is true then elif block will be checked otherwise else block will be executed. Syntax of the if elif else statement is given below. if condition: # statement # statement â€¦ # statement elif condition: # statement # statement â€¦ # statement else: # statement # statement â€¦ # statement Letâ€™s understand how to use the above syntax to implement if elif else conditional statements in python. A=10 B=5 if A>B: print("A is greater than B") elif AB: print("A is greater than B") elif AB: print("A is greater than B") elif A In the above example, we created two variable A and B. In the first case, A is greater than B thus the if block will be executed. In the second case, A is less than B thus the elif block will be executed. In the third case, A is equal to B thus else block will be executed. Conditional Expression in Python: Conditional Expression in Python is similar to ternary operators in other languages like C++, and Java. The syntax of the conditional expression is given below. [expression 1] if [condition] else [expression 2] In the above syntax, if the condition is true then our answer will be expression 1 else our answer will be expression 2. Letâ€™s understand this in more detail with help of an example. Vote_A=10 Vote_B=17 winner= "Winner A" if Vote_A>Vote_B else "Winner B" print(winner) Output: Winner B In the above program, we have created two variables Vote_A and Vote_B. If Vote_A is greater than Vote_B then our winner will be A otherwise B. ```
``` ```
``` ```
``` ```
``` Post navigation Previous Previous post: What is Normalisation (1NF 2NF 3NF & BCNF) in DBMS?Next Next post: Difference Between List and Tuple in Python Leave a Reply Your email address will not be published. Required fields are marked *Comment * Name * Email * Website Save my name, email, and website in this browser for the next time I comment. ```
``` ```
``` Search for: Pages ALGORITHMS ARRAY BACKTRACKING C PROGRAMMING LANGUAGE C++ PROGRAMMING LANGUAGE CAPGEMINI CIRCULAR LINKED LIST COMPANY PLACEMENT PROCEDURE COMPETITIVE CODING COMPUTATIONAL GEOMETRY CSE SUBJECTS DATA STRUCTURE DOUBLY LINKED LIST DYNAMIC PROGRAMMING GAME THEORY GRAPHS GREEDY ALGORITHM HASHING HEAP INTERVIEW PREPARATION INTERVIEW TIPS JAVA PROGRAMMING LANGUAGE JAVASCRIPT PROGRAMMING LANGUAGE Languages LINKED LIST LINKED LIST USING C MATHEMATICS OPERATING SYSTEM POINTERS PYTHON PROGRAMMING LANGUAGE QUEUE RECURSION SEARCHING SEGMENT TREE SORTING STACK STRING TREES Recent Articles 1 July 24, 2024 Different Types of Data in Data Mining 2 July 24, 2024 The Architecture of Data Mining 3 July 23, 2024 What is Data Mining? 4 July 23, 2024 Introduction to Data Mining 5 July 22, 2024 Seaborn Kdeplot 6 July 19, 2024 Three-dimensional Plotting using Matplotlib ```
``` Close Search for: Menu Language C C++ Java Javascript Python Data Structure Array Graphs Heap Linked List Segment Tree Stack Trees Queue Algorithm Backtracking Dynamic Programming Greedy Algorithm RECURSION Searching Sorting CSE Subjects Operating System Company Placement Interview Interview Tips General Interview Questions Algorithms Data Structure Languages Other Topics Competitive Data Structure Array Graph Heap Segment Tree Stack Queue Tree Tries Algorithm Backtracking Dynamic Programming Greedy Algorithm Searching Sorting Other Topics Computational Geometry Game Theory Maths Pointers Recursion String Others Computational Geometry Game Theory Hashing Maths Pointers Recursion String Related Post Python List Functions & Python List Methods October 30, 2023 Python Interview Questions September 18, 2023 Namespaces and Scope in Python August 30, 2023 What is the Difference Between append and extend in Python? August 10, 2023 Python Program to Check For the Perfect Square July 4, 2023 Python Program To Find The Sum Of First N Natural Numbers July 4, 2023 FOLLOW US CONTACT US +91-7969002111 contact@prepbytes.com QUICK LINKS Interview NotesMock TestsPlacement ProgrammeCoding CoursesMock InterviewAbout UsBlog ```
``` /** * Core styles: block-supports */ /* <![CDATA[ */ var utils_data = {"serverUrl":"https:\/\/server.prepbytes.com"}; /* ]]> */ /* <![CDATA[ */ var login_data = {"serverUrl":"https:\/\/server.prepbytes.com"}; var webinar_data = {"webinarUrl":"https:\/\/webinar.prepbytes.com\/"}; /* ]]> */ /* <![CDATA[ */ var login_data = {"serverUrl":"https:\/\/server.prepbytes.com"}; /* ]]> */ /* <![CDATA[ */ var infinity_news_custom_script = {"daymod":"Light Mode","nightmod":"Dark Mode"}; /* ]]> */ /* <![CDATA[ */ var infinity_news_ajax = {"ajax_url":"https:\/\/blog.prepbytes.com\/wp-admin\/admin-ajax.php","loadmore":"Load More","nomore":"No More Posts","loading":"Loading..."}; /* ]]> */ !function(e,n){if("undefined"!=typeof EnlighterJS){var o={"selectors":{"block":"pre.EnlighterJSRAW","inline":"code.EnlighterJSRAW"},"options":{"indent":4,"ampersandCleanup":true,"linehover":true,"rawcodeDbclick":false,"textOverflow":"break","linenumbers":false,"theme":"enlighter","language":"generic","retainCssClasses":false,"collapse":false,"toolbarOuter":"","toolbarTop":"{BTN_RAW}{BTN_COPY}{BTN_WINDOW}{BTN_WEBSITE}","toolbarBottom":""}};(e.EnlighterJSINIT=function(){EnlighterJS.init(o.selectors.block,o.selectors.inline,o.options)})()}else{(n&&(n.error||n.log)||function(){})("Error: EnlighterJS resources not loaded yet!")}}(window,console); ```