if True:
print("True means do something")
else:
print("Not True means do something else")
True means do something
hot_tea = True
if hot_tea:
print("enjoy some hot tea!")
else:
print("enjoy some tea, and perhaps try hot tea next time.")
enjoy some hot tea!
someone_i_know = False
if someone_i_know:
print("how have you been?")
else:
# use pass if there is no need to execute code
pass
# changed the value of someone_i_know
someone_i_know = True
if someone_i_know:
print("how have you been?")
else:
pass
how have you been?
sunny_today = True
# [ ] test if it is sunny_today and give proper responses using if and else
if sunny_today:
print("Is it sunny_today")
else:
print("Not True means do something else")
Is it sunny_today
sunny_today = False
# [ ] use code you created above and test sunny_today = False
if sunny_today:
print("Is it sunny_today")
else:
print("Not True means do something else")
Not True means do something else
# review code and run cell
favorite_book = input("Enter the title of a favorite book: ")
if favorite_book.istitle():
print(favorite_book, "- nice capitalization in that title!")
else:
print(favorite_book, "- consider capitalization throughout for book titles.")
big red dog - consider capitalization throughout for book titles.
# review code and run cell
a_number = input("enter a positive integer number: ")
if a_number.isdigit():
print(a_number, "is a positive integer")
else:
print(a_number, "is not a positive integer")
# another if
if a_number.isalpha():
print(a_number, "is more like a word")
else:
pass
2 is a positive integer
# review code and run cell
vehicle_type = input('"enter a type of vehicle that starts with "P": ')
if vehicle_type.upper().startswith("P"):
print(vehicle_type, 'starts with "P"')
else:
print(vehicle_type, 'does not start with "P"')
passat starts with "P"
test_string_1 = "welcome"
test_string_2 = "I have $3"
# [ ] use if, else to test for .islower() for the 2 strings
if test_string_1.islower() and test_string_2.islower():
print("are lowercase")
else:
print("are not lowercase")
if test_string_1.islower():
print("are lowercase")
else:
print("are not lowercase")
if test_string_2.islower():
print("are lowercase")
else:
print("are not lowercase")
are not lowercase
are lowercase
are not lowercase
test_string_1 = "welcome"
test_string_2 = "I have $3"
test_string_3 = "With a function it's efficient to repeat code"
def w_start_test():
# [ ] create a function w_start_test() use if & else to test with .startswith('w')
if test_string_1.startswith('w'):
print("starts with w")
else:
print("does not start with w")
if test_string_2.startswith('w'):
print("starts with w")
else:
print("does not start with w")
if test_string_3.startswith('w'):
print("starts with w")
else:
print("does not start with w")
# [ ] Test the 3 string variables provided by calling w_start_test()
w_start_test()
starts with w
does not start with w
does not start with w