Вход/Регистрация
Python Handbook For Beginners
вернуться

Gurbanov Roman

Шрифт:

See that? This is how Boolean logic and values work. But what if we try comparing strings? Try comparing apples to oranges using the following:

"apples" == "oranges"

See the output? Now, try changing comparison operators and values to your liking. See what happens? Now you know that Boolean logic works not only with numeric but with other data formats in programming.

3 True and False in Variables

Let us see how we can use True and False values in variables. We can store results from comparisons that return True or False in variables. This is how it works:

result = "apples" == "oranges"

print(result)

Run the above code in the console and see what it returns. Let's break it down.

I've created a variable named result in this code and assigned it a Boolean value from comparing the two strings "apples" and "oranges ".

Then I went down one line and printed the value of the "result" variable on the screen, passing the variable name in parentheses to the print function.

Does it make sense?) Now, alter the code by indicating that apples and oranges are not equal. What does the program return?

4 Comparing Variables

Not only can we compare numbers and strings, but the entire variables too! See how we can do it:

fruit = "Kiwi"

result = fruit == "Apple"

print(result)

Before you write the code into the console and hit enter, think of what output it will return? Let's break it down:

Конец ознакомительного фрагмента.

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Ебукер (ebooker) – онлайн-библиотека на русском языке. Книги доступны онлайн, без утомительной регистрации. Огромный выбор и удобный дизайн, позволяющий читать без проблем. Добавляйте сайт в закладки! Все произведения загружаются пользователями: если считаете, что ваши авторские права нарушены – используйте форму обратной связи.

Полезные ссылки

  • Моя полка

Контакты

  • chitat.ebooker@gmail.com

Подпишитесь на рассылку: