In this example we use two variables, a and b, which are used as part of the if statement to test whether b is greater than a. As a is 33, and b is 200, we know that 200 is greater than 33, and so we print to screen that "b is greater than a". Show IndentationPython relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose. ExampleIf statement, without indentation (will raise an error): a = 33 ElifThe elif keyword is pythons way of saying "if the previous conditions were not true, then try this condition". Examplea = 33 In this example a is equal to b, so the first condition is not true, but the elif condition is true, so we print to screen that "a and b are equal". ElseThe else keyword catches anything which isn't caught by the preceding conditions. Examplea = 200 In this example a is greater than b, so the first condition is not true, also the elif condition is not true, so we go to the else condition and print to screen that "a is greater than b". You can also have an Examplea = 200 Short Hand IfIf you have only one statement to execute, you can put it on the same line as the if statement. Short Hand If ... ElseIf you have only one statement to execute, one for if, and one for else, you can put it all on the same line: ExampleOne line if else statement: a = 2 This technique is known as Ternary Operators, or Conditional Expressions. You can also have multiple else statements on the same line: ExampleOne line if else statement, with 3 conditions: a = 330 AndThe and keyword is a logical operator, and is used to combine conditional statements: ExampleTest if a = 200 OrThe ExampleTest if a = 200 Nested IfYou can have Examplex = 41 if x > 10: The pass Statement
Use the 3.Syntax
Note that In the example below, we test two values to find out if 20 is greater than 18. If the condition is 3, print some text:Example
Try it Yourself » We can also test variables: Example
Try it Yourself » Example explainedIn the example above we use two variables, x and y, to test whether x is greater than y (using the 6 operator). As x is 20, and y is 18, and we know that 20 is greater than 18, we print to the screen that "x is greater than y".The else StatementUse the 8.Syntax
Example
Try it Yourself » Example explainedIn the example above, time (20) is greater than 18, so the condition is 8. Because of this, we move on to the else condition and print to the screen "Good evening". If the time was less than 18, the program would print "Good day".The else if StatementUse the 0 statement to specify a new condition if the first condition is 8.Syntax
Example
Try it Yourself » Example explainedIn the example above, time (22) is greater than 10, so the first condition is 8. The next condition, in the 0 statement, is also 8, so we move on to the else condition since condition1 and condition2 is both 8 - and print to the screen "Good evening".
Did Beyonce write if I were a boy?"If I Were a Boy" is a song written by BC Jean and Toby Gad and originally performed by Jean in 2008. The song gained international attention the same year in a version by the American singer Beyoncé, from her third studio album I Am... Sasha Fierce (2008).
When did Beyonce release if I were a boy?2008If I Were a Boy / Releasednull
Who sang the cowboy song?"Cowboy Song" is a song by hard rock band Thin Lizzy that originally appeared on their 1976 album Jailbreak.
|