filmov
tv
Boolean 'in' keyword - Introduction to Python: Absolute Beginner Module 1 Video 23

Показать описание
Concept: Boolean in keyword
Using the in keyword to return a Boolean
the in keyword can be used as a simple search returning True or False indication if a string is included in a target sequence.
comparing strings is case sensitive
'Hello' is not the same as 'hello'
Example
# review and run code to test if a string is to be found in another string
menu = "salad, pasta, sandwich, pizza, drinks, dessert"
print('pizza' in menu)
# review and run code to test case sensitive examples
greeting = "Hello World!"
print("'hello' in greeting = ",'hello' in greeting)
print("'Hello' in greeting = ", 'Hello' in greeting)
example below: remove case sensitivity from a string comparison
# review and run code to test removing case sensitivity from a string comparison
greeting = "Hello World!"
print("'hello' in greeting = ",'hello' in greeting)
print("'Hello' in greeting = ", 'Hello' in greeting)
Task 3: multi-part
[ ] add code below testing the menu string variable for 'pizza', 'soup', and 'dessert' using keyword in
print each test on a separate line
print a description for each test (e.g. - "Pizza in menu = True")
# [] print 3 tests, with description text, testing the menu variable for 'pizza', 'soup' and 'dessert'
menu = "salad, pasta, sandwich, pizza, drinks, dessert"
Program: What is on the menu
[ ] Create a program where a user can check if an item is on the menu
store the user response in a variable menu_ask
use the menu from above and add some additional items
the program should be able to ignore case mismatch so that "hello" is found in "Hello World!"
# Create a program where the user supplies input to search the menu
[ ] Challenge: Add to the menu
print the current menu
get user input for add_item variable
new_menu use string addition to add add_item to menu
print the new_menu
testing
check if an item is on the menu, check for previous items and the item you added
# add to menu
# Testing Add to Menu - create user input to search for an item on the new menu
Task 4
Fix The Error
# [ ] fix the error
paint_colors = "red, blue, green, black, orange, pink"
print('Red in paint colors = ',red in paint_colors)
Using the in keyword to return a Boolean
the in keyword can be used as a simple search returning True or False indication if a string is included in a target sequence.
comparing strings is case sensitive
'Hello' is not the same as 'hello'
Example
# review and run code to test if a string is to be found in another string
menu = "salad, pasta, sandwich, pizza, drinks, dessert"
print('pizza' in menu)
# review and run code to test case sensitive examples
greeting = "Hello World!"
print("'hello' in greeting = ",'hello' in greeting)
print("'Hello' in greeting = ", 'Hello' in greeting)
example below: remove case sensitivity from a string comparison
# review and run code to test removing case sensitivity from a string comparison
greeting = "Hello World!"
print("'hello' in greeting = ",'hello' in greeting)
print("'Hello' in greeting = ", 'Hello' in greeting)
Task 3: multi-part
[ ] add code below testing the menu string variable for 'pizza', 'soup', and 'dessert' using keyword in
print each test on a separate line
print a description for each test (e.g. - "Pizza in menu = True")
# [] print 3 tests, with description text, testing the menu variable for 'pizza', 'soup' and 'dessert'
menu = "salad, pasta, sandwich, pizza, drinks, dessert"
Program: What is on the menu
[ ] Create a program where a user can check if an item is on the menu
store the user response in a variable menu_ask
use the menu from above and add some additional items
the program should be able to ignore case mismatch so that "hello" is found in "Hello World!"
# Create a program where the user supplies input to search the menu
[ ] Challenge: Add to the menu
print the current menu
get user input for add_item variable
new_menu use string addition to add add_item to menu
print the new_menu
testing
check if an item is on the menu, check for previous items and the item you added
# add to menu
# Testing Add to Menu - create user input to search for an item on the new menu
Task 4
Fix The Error
# [ ] fix the error
paint_colors = "red, blue, green, black, orange, pink"
print('Red in paint colors = ',red in paint_colors)