Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Combining conditions | Conditional statements
Learn Python from Scratch
course content

Зміст курсу

Learn Python from Scratch

Learn Python from Scratch

1. The basics
2. Arithmetic operations
3. Common data types
4. Conditional statements
5. Other data types
6. Loops
7. Functions

bookCombining conditions

Often we need to check if multiple conditions hold, and only then decide to continue or not. In boolean algebra, there are two common operators: OR and AND. What do they do?

Operator OR checks if at least one condition holds (i.e. is True) and returns True in that case; otherwise - False.
Operator AND checks if all the conditions hold and returns True in that case; otherwise - False.

For example, imagine that you decide whether go outside or not. If there is good weather AND today is Saturday/Sunday, then you can go; otherwise - not. Here we check if both conditions hold, and only in case when all are True, we will go outside.

In Python, you can use the same words but in lowercase, i.e. condition1 or condition2, condition1, and conditon2. Also, you can combine multiple ands and ors. Parentheses are common there.

For example, let's check the following conditions:

  1. If 2 greater than 1 and "bbb" greater than "aaa"
  2. If the second index symbol in the string "my string" equals "y" or "s"
1234
# check the first two conditions print(2 > 1 and "bbb" > "aaa") # check the next two conditions print("my string"[2] == "y" or "my string"[2] == "s")
copy

Завдання

  1. Given variable t with some string assigned to it. Check the following conditions: if number of characters in this string is less than 15 and number of letter 'n' in this string greater than 2
  2. Given numerical variable a. You need to check if this variable is greater than 9475/37 or the remainder of the division by 14 is less than 7.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 2
toggle bottom row

bookCombining conditions

Often we need to check if multiple conditions hold, and only then decide to continue or not. In boolean algebra, there are two common operators: OR and AND. What do they do?

Operator OR checks if at least one condition holds (i.e. is True) and returns True in that case; otherwise - False.
Operator AND checks if all the conditions hold and returns True in that case; otherwise - False.

For example, imagine that you decide whether go outside or not. If there is good weather AND today is Saturday/Sunday, then you can go; otherwise - not. Here we check if both conditions hold, and only in case when all are True, we will go outside.

In Python, you can use the same words but in lowercase, i.e. condition1 or condition2, condition1, and conditon2. Also, you can combine multiple ands and ors. Parentheses are common there.

For example, let's check the following conditions:

  1. If 2 greater than 1 and "bbb" greater than "aaa"
  2. If the second index symbol in the string "my string" equals "y" or "s"
1234
# check the first two conditions print(2 > 1 and "bbb" > "aaa") # check the next two conditions print("my string"[2] == "y" or "my string"[2] == "s")
copy

Завдання

  1. Given variable t with some string assigned to it. Check the following conditions: if number of characters in this string is less than 15 and number of letter 'n' in this string greater than 2
  2. Given numerical variable a. You need to check if this variable is greater than 9475/37 or the remainder of the division by 14 is less than 7.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 2
toggle bottom row

bookCombining conditions

Often we need to check if multiple conditions hold, and only then decide to continue or not. In boolean algebra, there are two common operators: OR and AND. What do they do?

Operator OR checks if at least one condition holds (i.e. is True) and returns True in that case; otherwise - False.
Operator AND checks if all the conditions hold and returns True in that case; otherwise - False.

For example, imagine that you decide whether go outside or not. If there is good weather AND today is Saturday/Sunday, then you can go; otherwise - not. Here we check if both conditions hold, and only in case when all are True, we will go outside.

In Python, you can use the same words but in lowercase, i.e. condition1 or condition2, condition1, and conditon2. Also, you can combine multiple ands and ors. Parentheses are common there.

For example, let's check the following conditions:

  1. If 2 greater than 1 and "bbb" greater than "aaa"
  2. If the second index symbol in the string "my string" equals "y" or "s"
1234
# check the first two conditions print(2 > 1 and "bbb" > "aaa") # check the next two conditions print("my string"[2] == "y" or "my string"[2] == "s")
copy

Завдання

  1. Given variable t with some string assigned to it. Check the following conditions: if number of characters in this string is less than 15 and number of letter 'n' in this string greater than 2
  2. Given numerical variable a. You need to check if this variable is greater than 9475/37 or the remainder of the division by 14 is less than 7.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Often we need to check if multiple conditions hold, and only then decide to continue or not. In boolean algebra, there are two common operators: OR and AND. What do they do?

Operator OR checks if at least one condition holds (i.e. is True) and returns True in that case; otherwise - False.
Operator AND checks if all the conditions hold and returns True in that case; otherwise - False.

For example, imagine that you decide whether go outside or not. If there is good weather AND today is Saturday/Sunday, then you can go; otherwise - not. Here we check if both conditions hold, and only in case when all are True, we will go outside.

In Python, you can use the same words but in lowercase, i.e. condition1 or condition2, condition1, and conditon2. Also, you can combine multiple ands and ors. Parentheses are common there.

For example, let's check the following conditions:

  1. If 2 greater than 1 and "bbb" greater than "aaa"
  2. If the second index symbol in the string "my string" equals "y" or "s"
1234
# check the first two conditions print(2 > 1 and "bbb" > "aaa") # check the next two conditions print("my string"[2] == "y" or "my string"[2] == "s")
copy

Завдання

  1. Given variable t with some string assigned to it. Check the following conditions: if number of characters in this string is less than 15 and number of letter 'n' in this string greater than 2
  2. Given numerical variable a. You need to check if this variable is greater than 9475/37 or the remainder of the division by 14 is less than 7.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 4. Розділ 2
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
some-alt