Python how to do if statements

Python If . Else

Python supports the usual logical conditions from mathematics:

  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a
  • Greater than: a > b
  • Greater than or equal to: a >= b

These conditions can be used in several ways, most commonly in «if statements» and loops.

An «if statement» is written by using the if keyword.

Example

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».

Indentation

Python 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.

Example

If statement, without indentation (will raise an error):

Elif

The elif keyword is Python’s way of saying «if the previous conditions were not true, then try this condition».

Example

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».

Else

The else keyword catches anything which isn’t caught by the preceding conditions.

Example

a = 200
b = 33
if b > a:
print(«b is greater than a»)
elif a == b:
print(«a and b are equal»)
else:
print(«a is greater than b»)

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 else without the elif :

Example

Short Hand If

If you have only one statement to execute, you can put it on the same line as the if statement.

Example

Short Hand If . Else

If you have only one statement to execute, one for if, and one for else, you can put it all on the same line:

Example

One line if else statement:

This technique is known as Ternary Operators, or Conditional Expressions.

You can also have multiple else statements on the same line:

Example

One line if else statement, with 3 conditions:

And

The and keyword is a logical operator, and is used to combine conditional statements:

Example

Test if a is greater than b , AND if c is greater than a :

Or

The or keyword is a logical operator, and is used to combine conditional statements:

Example

Test if a is greater than b , OR if a is greater than c :

Not

The not keyword is a logical operator, and is used to reverse the result of the conditional statement:

Example

Test if a is NOT greater than b :

Nested If

You can have if statements inside if statements, this is called nested if statements.

Example

if x > 10:
print(«Above ten,»)
if x > 20:
print(«and also above 20!»)
else:
print(«but not above 20.»)

The pass Statement

if statements cannot be empty, but if you for some reason have an if statement with no content, put in the pass statement to avoid getting an error.

Источник

Python if Statement

Summary: in this tutorial, you’ll learn how to use the Python if statement to execute a block of code based on a condition.

The simple Python if statement

You use the if statement to execute a block of code based on a specified condition.

The syntax of the if statement is as follows:

if condition: if-blockCode language: Python (python)

The if statement checks the condition first.

If the condition evaluates to True , it executes the statements in the if-block. Otherwise, it ignores the statements.

Note that the colon ( : ) that follows the condition is very important. If you forget it, you’ll get a syntax error.

The following flowchart illustrates the if statement:

age = input('Enter your age:') if int(age) >= 18: print("You're eligible to vote.")Code language: Python (python)

This example prompts you to input your age. If you enter a number that is greater than or equal to 18 , it’ll show a message «You’re eligible to vote» on the screen. Otherwise, it does nothing.

The condition int(age) >= 18 converts the input string to an integer and compares it with 18.

Enter your age:18 You're eligible to vote.Code language: Python (python)

See the following example:

age = input('Enter your age:') if int(age) >= 18: print("You're eligible to vote.") print("Let's go and vote.")Code language: Python (python)

In this example, if you enter a number that is greater than or equal to 18 , you’ll see two messages.

In this example, indentation is very important. Any statement that follows the if statement needs to have four spaces.

If you don’t use the indentation correctly, the program will work differently. For example:

age = input('Enter your age:') if int(age) >= 18: print("You're eligible to vote.") print("Let's go and vote.") Code language: Python (python)

In this example, the final statement always executes regardless of the condition in the if statement. The reason is that it doesn’t belong to the if block:

Enter your age:11 Let's go and vote.Code language: Python (python)

Python if…else statement

Typically, you want to perform an action when a condition is True and another action when the condition is False .

To do so, you use the if. else statement.

The following shows the syntax of the if. else statement:

if condition: if-block; else: else-block;Code language: Python (python)

In this syntax, the if. else will execute the if-block if the condition evaluates to True . Otherwise, it’ll execute the else-block .

The following flowchart illustrates the if..else statement:

The following example illustrates you how to use the if. else statement:

age = input('Enter your age:') if int(age) >= 18: print("You're eligible to vote.") else: print("You're not eligible to vote.")Code language: Python (python)

In this example, if you enter your age with a number less than 18, you’ll see the message «You’re not eligible to vote.» like this:

Enter your age:11 You're not eligible to vote.Code language: Python (python)

Python if…elif…else statement

If you want to check multiple conditions and perform an action accordingly, you can use the if. elif. else statement. The elif stands for else if .

Here is the syntax if the if. elif. else statement:

if if-condition: if-block elif elif-condition1: elif-block1 elif elif-condition2: elif-block2 . else: else-blockCode language: Python (python)

The if. elif. else statement checks each condition ( if-condition , elif-condition1 , elif-condition2 , …) in the order that they appear in the statement until it finds the one that evaluates to True .

When the if. elif. else statement finds one, it executes the statement that follows the condition and skips testing the remaining conditions.

If no condition evaluates to True , the if. elif. else statement executes the statement in the else branch.

Note that the else block is optional. If you omit it and no condition is True , the statement does nothing.

The following flowchart illustrates the if. elif. else statement:

The following example uses the if. elif..else statement to determine the ticket price based on the age:

age = input('Enter your age:') # convert the string to int your_age = int(age) # determine the ticket price if your_age < 5: ticket_price = 5 elif your_age < 16: ticket_price = 10 else: ticket_price = 18 # show the ticket price print(f"You'll pay $ for the ticket") Code language: Python (python)
  • If the input age is less than 5, the ticket price will be $5.
  • If the input age is greater than or equal to 5 and less than 16, the ticket price is $10.
  • Otherwise, the ticket price is $18.

Summary

  • Use the if statement when you want to run a code block based on a condition.
  • Use the if. else statement when you want to run another code block if the condition is not True .
  • Use the if. elif. else statement when you want to check multiple conditions and run the corresponding code block that follows the condition that evaluates to True .

Источник

Читайте также:  Background solid colors css
Оцените статью