Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Booleans | Section
Python Basics for Data Analysis
Секція 1. Розділ 11
single

single

bookBooleans

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

In this chapter, we'll dive into the Boolean data type. Booleans are simple yet powerful, they allow us to handle True or False values, enabling our programs to react to different situations effectively. In the context of managing a grocery store, Booleans can help us determine whether certain conditions are met, such as whether an item is in stock or if a sale is active.

Watch as Alex uses Boolean data types to compare prices and names:

Understanding Boolean Data Types

A boolean data type has only two possible values: True and False. These values are often the result of comparison operations and are fundamental in controlling the flow of our programs. By understanding booleans, you'll be able to write code that can make decisions based on various conditions.

The following operations are common comparison operators that result in boolean values:

  • Equal to: ==;
  • Not equal to: !=;
  • Greater than: >;
  • Less than: <;
  • Greater than or equal to: >=;
  • Less than or equal to: <=.

Example Application

Let's check if an item (milk) is low in stock by comparing its quantity to a predefined threshold for low stock:

123456789
# Define the quantity of the item and the low stock threshold milk_quantity = 12 low_stock_threshold = 10 # Check if the item quantity is below the low stock threshold low_stock = milk_quantity <= low_stock_threshold # Print the result print("Is the item low in stock?", low_stock)
copy

Now it's your turn to practice using booleans. In this challenge, you will check if the total cost of a purchase is eligible for a discount.

Завдання

Swipe to start coding

You need to define a variable for the total cost, create a boolean variable to check for discount eligibility, and print the result.

  • Define a variable named total_cost and assign it the value 25.00 to represent the total cost of a grocery bill.
  • Create a boolean variable named discountEligible by comparing the total_cost variable to the discount threshold of 20.00 using the greater than or equal to (>=) operator.
  • Print the value of the discountEligible variable to indicate whether the purchase is eligible for a discount.

Output Requirements

  • Print the message: Is the purchase eligible for a discount? <discountEligible>.

Рішення

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

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

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

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

single

Запитати АІ

expand

Запитати АІ

ChatGPT

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

some-alt