Print('Python') if a > b else print('Examples') Run Based on the return value of condition, Python executes one of the print statements. In this example, we will write print statements in the ternary operator. The condition would evaluate to True and a would be selected.Įxample 2: Print Statements in Python Ternary Operator You may swap the values of a and b, and run the program. The ternary operator in the following program selects a or b based on the condition a>b evaluating to True or False respectively. In this example, we will find out the maximum of two numbers, using ternary operator. You can either provide a value, variable or statement for the statement_1 and statement_2. Else if the expression evaluates to False, statement_2 is selected. Statement_1 is selected if expression evaluates to True. The syntax of Python Ternary Operator is: if else In this tutorial, we will learn how to use Ternary Operator in Python. It is a miniature of if-else statement that assigns one of the two values to a variable. Python Ternary operator is used to select one of the two values based on a condition.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |