Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Оператори Порівняння | Оператор if
Умовні Оператори в Python

Свайпніть щоб показати меню

book
Оператори Порівняння

Давайте нарешті поговоримо більш детально про те, що ви можете написати в умовах.

Ви вже навчилися писати найпростіші умови, тобто умови, в яких ви берете будь-які два вирази і якось порівнюєте їх. Для порівняння можна використовувати спеціальні оператори, швидше за все, ви знаєте їх усі, оскільки ми вже працювали з ними в попередніх розділах, але давайте згадаємо.

You can compare complex mathematical expressions, elements of different data structures, strings, and even boolean values. As we already know how to compare numbers from previous chapters, let's explore other types of comparisons:

Example 1: Comparing strings to determine workout types

12345
favorite_activity = "Yoga" current_activity = "Running" if favorite_activity != current_activity: print("This is not your favorite activity, but keep going!")
copy

This checks if two strings are not equal (exact match, case sensitive).

Example 2: Comparing boolean values to check activity completion

12345
# Check if the user completed their morning routine morning_run_completed = True if morning_run_completed: print("Great job on completing your morning run!")
copy

This example evaluates whether a specific activity was completed (in this case, a morning run). Instead of writing if morning_run_completed == True, we use if morning_run_completed because the value of morning_run_completed is already a boolean (True or False). Writing == True is redundant and less concise. Python allows such simplifications to make the code cleaner and easier to read.

Example 3: Checking if a number falls within a range

Your app can even provide weather-based recommendations. This example checks if the street temperature is ideal for running and gives timely suggestions to users.

1234
street_temperature = 15 if 10 <= street_temperature <= 20: print("Best conditions for running now!")
copy
Завдання

Swipe to start coding

You are building a basic fitness tracker. Your task is to write code that checks various conditions and provides personalized feedback based on the user's activities. This will test your understanding of comparison operators.

  1. Check if the user's steps are in the range of 5,000 to 10,000 and print: "You're on track with your step count!" (use less than or equal operator).
  2. Add a boolean variable hydration_goal_met and use it to check if the hydration goal was met. Print a congratulatory message if true.

Рішення

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

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

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

Секція 1. Розділ 3

Запитати АІ

expand
ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

book
Оператори Порівняння

Давайте нарешті поговоримо більш детально про те, що ви можете написати в умовах.

Ви вже навчилися писати найпростіші умови, тобто умови, в яких ви берете будь-які два вирази і якось порівнюєте їх. Для порівняння можна використовувати спеціальні оператори, швидше за все, ви знаєте їх усі, оскільки ми вже працювали з ними в попередніх розділах, але давайте згадаємо.

You can compare complex mathematical expressions, elements of different data structures, strings, and even boolean values. As we already know how to compare numbers from previous chapters, let's explore other types of comparisons:

Example 1: Comparing strings to determine workout types

12345
favorite_activity = "Yoga" current_activity = "Running" if favorite_activity != current_activity: print("This is not your favorite activity, but keep going!")
copy

This checks if two strings are not equal (exact match, case sensitive).

Example 2: Comparing boolean values to check activity completion

12345
# Check if the user completed their morning routine morning_run_completed = True if morning_run_completed: print("Great job on completing your morning run!")
copy

This example evaluates whether a specific activity was completed (in this case, a morning run). Instead of writing if morning_run_completed == True, we use if morning_run_completed because the value of morning_run_completed is already a boolean (True or False). Writing == True is redundant and less concise. Python allows such simplifications to make the code cleaner and easier to read.

Example 3: Checking if a number falls within a range

Your app can even provide weather-based recommendations. This example checks if the street temperature is ideal for running and gives timely suggestions to users.

1234
street_temperature = 15 if 10 <= street_temperature <= 20: print("Best conditions for running now!")
copy
Завдання

Swipe to start coding

You are building a basic fitness tracker. Your task is to write code that checks various conditions and provides personalized feedback based on the user's activities. This will test your understanding of comparison operators.

  1. Check if the user's steps are in the range of 5,000 to 10,000 and print: "You're on track with your step count!" (use less than or equal operator).
  2. Add a boolean variable hydration_goal_met and use it to check if the hydration goal was met. Print a congratulatory message if true.

Рішення

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

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

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

Секція 1. Розділ 3
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Ми дуже хвилюємося, що щось пішло не так. Що трапилося?
some-alt