Python learning journal Day5 if statement

Today, February 14, 2020, overcast to light rain, 11-17 ℃

Single dogs can't celebrate Valentine's day, so happy birthday to ENIAC, 74 A kind of

1, Condition test

A conditional test is an expression with a value of True or False. An expression can be a variable of type bool or a comparison expression of other types.

1. Comparison value

● string comparison

To determine whether two strings are equal, use "=".

"name" == "name"	#True
"Name" == "name"	#False

To determine whether two strings are unequal, you can use "! =".

"name" == "name"	#False
"name" == "age"		#True

Different case, different string value.

● digital comparison

For numbers, you can use ">", "<", "= =", "!", "" > to compare them.

my_age = 20
my_age > 19		#True
my_age >= 20	#True
my_age < 20		#False
my_age <= 21	#True
my_age == 20	#True
my_age != 18	#True

These symbols can also be used for strings, with the size being compared bit by bit based on ASCII code.

● judge whether the value is in the list

To determine whether a value is in the list, you can use the in keyword. If you want to determine whether it is no longer in the list, you can add the not keyword before in.

number_list = [1,2,3]
1 in number_list	#True
number_list = [ [1,2] , 3]
1 in number_list	#False
1 not in number_list	#True

If an element in a list also contains an iterative object such as a list or a tuple, it will no longer be judged whether the value is in the object.

● check multiple conditions

To check multiple conditional tests, you can use the and keyword and the or keyword.
And keyword and or keyword are equivalent to logical and or.

my_age = 20
my_age >= 20 and my_age <= 20	#True
my_age > 19 and my_age < 20		#False
my_age < 21	or my_age >22		#True
my_age <19 or my_age > 21		#False

2. Boolean expression

A Boolean expression is another name for a conditional test. It is an expression with a value of True or False. The above check and judgment is actually a kind of Boolean expression. It can also be a variable that holds a value of type bool.

only_read = True	#Boolean expression true
can_edit = False	#False Boolean expression

2, if statement

When you need to check a series of conditional tests and decide what to do, you can use the if statement.

if conditional_test:
	do something
if true:
	print("Yes!")		#Output:Yes!

Similar to for loop statements, if statements can also be judged to be true by increasing indentation.

age = 20
if age >= 18:
	print("You are " + str(age) + "!")
	print("Congratulations on being an adult")
print("End if.")
You are 20!
Congratulations on being an adult
End if.

When the condition test is false, you also want to operate. You can use the if else statement.

#Adult judgement
if age >= 18:
	print("You are an adult!")
	print("You are not an adult!")

If elif else statement can be used when the conditional test needs to judge multiple times.

#Judging age
if age >= 18:
	print("You are an adult!")
elif age >= 15:
	print("You are a High School student!")
elif age >= 12:
	print("You are a Middle School student!")
elif age >= 6:
	print("You are a pupil!")
	print("You are a baby!")
print("You are " + str(age) + " years old!")

If elif else structure when an if condition is tested as true, the statement indented below the statement will be executed, and all subsequent elifs and else will be skipped. If you want to check every conditional test, you should continue to use if statements separately.

plant = ["rose","cactus","tomato"]
food = ["beef","bread","tomato"]
red_color = ["rose","apple","tomato"]
if "tomato" in plant:
	print("Tomato is a plant.")
if "tomato" in food:
	print("Tomato is food.")
if "tomato" in red_color:
	print("Tomato is red.")

3, Use if statement to process list

1. Check special elements

When you need to do different operations on special elements in the list, you can use for loop statements and if statements to nest.

name_list = ["Alex","Blair","Chris"]
for name in name_list:
	if name == "Alex":
		print("My name is " + name)
		print("His name is " + name)
My name is Alex
His name is Blair
His name is Chris

2. Judge whether the list is empty

If you don't want to do wrong operation or no operation when the list is empty, you can nest the if statement and for loop statement.

name_list = []
if name_list:
	for name in name_list:
		print("He is " + name)
	print("Empty list!")
Empty list!

In Python's if statement, the number 0, empty object and none object are considered False; the non-zero number and non empty object are considered True.

3. Use multiple lists

If you want to process multiple lists, you can use for loop statements and if statements to nest flexibly according to the situation.

searching_person_list = ["Bulk","Micheal","Chris"]
person_list = ["Alex","Bulk","Chris"]
for searching_person in searching_person_list:
	if searching_person in person_list:
		print("Please wait, " + searching_person + " is coming.")
		print("Sorry, " + searching_person + " is not here.")
Please wait, Bulk is coming.
Sorry, Micheal is not here.
Please wait, Chris is coming.

I would rather be dangerous and free than enslaved in ease.

Published 5 original articles, praised 0, visited 106
Private letter follow

Tags: ascii Python

Posted on Fri, 14 Feb 2020 00:43:40 -0800 by mr_zhang