python programming from entry to practice
# -*- coding: utf-8 -*-
"" ""
chapter5 if statement
"" "
# 5.1 A simple example
cars = ['audi', 'Oil Benz', 'bmw', 'toyota']
for car in cars:
if car == 'bmw': # 6 6 The string of the string is equal
print(car.upper())
else:
print(car.title())
# 5.2 Condition test: The core of each if statement is an expression with a value or false. This expression is called conditional test.
# 5.2.1 Check whether it is equal
car = 'bmw'
print(car == 'bmw') # true
# 5.2.2 Check whether the case is not considered when equal
# Check whether it is equal when checking in python
car = 'Audi'
print(car == 'audi') # False
# 5.2.3 Check whether it is not equal: to determine whether the two values are different, it can be combined with the exclamation mark and equal number (! =)
name = 'Li Si'
if name == 'Zhang San':
print('You are Zhang San')
else:
print('You are not Zhang San')
# 5.2.4 Comparison numbers
answer = 17
if answer != 42:
print('Your answer is wrong, the ultimate secret of the universe is 42, not%s!' % answer)
else:
print('You saw the universe !!!')
# 5.2.5 Check multiple conditions
# 1. Use AND to check multiple conditions
age = 10
if age < 30 and age < 40:
print("aa")
else:
print("bb")
if (age < 50) & (age < 60):
print("cc")
else:
print("dd")
# 2. Use or check multiple conditions
age_1 = 20
if (age_1 > 10) | (age_1 > 15):
print("ee")
else:
print("ff")
# 5.2.6 Check whether the specific value is included in the list
grade = ['Jia', 'B', 'C']
mina = 'Ding'
if min in grade:
print('good-level')
else:
print('bad-level')
# 5.2.7 Check whether the specific value is not included in the list in the list
grade = ['Jia', 'B', 'C']
zhao_level = 'Ding'
if zhao_level not in grade:
print('bad-level')
else:
print('good-level')
# 5.2.8 Boolean expression
# True/False
# 5.3 If statement
# 5.3.1 Simple IF statement
age = 19
if age >= 18:
print("You are old enough to vote!")
# 5.3.2 If-Art Sentence
age = 17
if age >= 18:
print("You are old enough to vote!")
print("Have you registered to vote yet?")
else:
print("Sorry, you are too young to vote.")
print("Please register to vote as soon as you turn 18!")
# 5.3.3 If-Elif-Else structure
age = 12
if age < 4:
print("Your admission cost is $0.")
elif age < 18:
print("Your admission cost is $5.")
else:
print("Your admission cost is $10.")
age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
else:
price = 10
print("Your admission cost is $" + str(price) + ".")
# 5.3.4 Use multiple ELIF code blocks
age = 12
price
if age < 4:
price = 0
elif age < 18:
price = 10
elif age < 65:
price = 20
else:
price = 10
print("Your admission cost is $" + str(price) + ".")
# 5.3.5 omit omitted ELSE code block
# python does not require if-Elif structures must have ELSE code blocks: ELSE is an all-inclusive sentence. As long as it is not met in any IF or Elif conditional test, the code will be executed.
# This may introduce invalid or even malicious data. If you know the condition to be tested in the end, you should consider using an Elif code block to replace the ELSE code block.
# 5.3.6 Test multiple conditions
request_word = ['Flower', 'OK', 'month', 'circle']
need_words = ['Flower', 'OK', 'month', 'circle']
finish_words = []
for need_word in need_words:
if need_word in request_word:
print("need %s" % need_word)
finish_words.append(need_word)
print("GOOD" if len(finish_words) > 2 else "BAD") # python does not have a ternary computing symbol, you can use this format to replace
# 5.4 Use IF statement processing list
# 5.4.1 Check special elements
requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
for requested_topping in requested_toppings:
print("Adding " + requested_topping + ".")
print("\nFinished making your pizza!")
requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
for requested_topping in requested_toppings:
if requested_topping == 'green peppers':
print("Sorry, we are out of green peppers right now.")
else:
print("Adding " + requested_topping + ".")
print("\nFinished making your pizza!")
# 5.4.2 The list is not empty
requested_toppings = []
if requested_toppings:
for requested_topping in requested_toppings:
print("Adding " + requested_topping + ".")
print("\nFinished making your pizza!")
else:
print("Are you sure you want a plain pizza?")
# 5.4.3 Use multiple lists
available_words = ['Small', 'English', 'male', 'Rain', 'Lai']
request_words = ['Small', 'English', 'male', 'Where', '6']
for request_word in request_words:
if request_word in available_words:
print('We have this word:%s' % request_word)
else:
print('We have this word:%s' % request_word)
# 5.5 Set the format of if statement
The only suggestion provided by
# PEP 8 is to add a space on both sides such as ==,> =, and <= and other comparative operators. For example, if Age <4: