We won't talk about the basic functions of if. In this article, we will focus on the advanced features of if in Python
1. About if xxx
1.1 if xxx introduction
if xxx
It is a conditional statement in Python, used to judge variablesxxx
Whether it is true. ifxxx
If true, executeif
Code in statement block; otherwise, skipif
Statement block, execute the following code.
In Python, xxx is considered false as follows:
`False` `None` `0`(include `0.0`) Empty string `''`(include `""`) Empty list `[]` Empty tuple `()` Empty Dictionary `{}`
All values except the above values are considered true.
1.2 if xxx example
The following is a useif a
Example:
a = 10 if a: print("a is the true value") else: print("a is a false value")
Output
a is true value
In this example, the variablea
The value of10
,because10
It is a non-zero number, soif a
The conditions of are true, executeif
Code in statement block, outputa is true value
。
2. About if xxx==xxx
2.1 if xxx==xxx Introduction
In Python,==
The operator is used to compare whether the values of two objects are equal. If the values of the two strings are equal, it returnsTrue
, otherwise returnFalse
. For example:
2.2 if xxx==xxx Example
str1 = "hello" str2 = "world" str3 = "hello" if str1 == str2: print("str1 and str2 are the same") else: print("Str1 and str2 are not the same") if str1 == str3: print("Str1 and str3 are the same") else: print("Str1 and str3 are not the same")
Output
str1 and str2 are different
str1 and str3 are the same
In this example,str1
The value of"hello"
,str2
The value of"world"
,str3
The value of"hello"
. becausestr1
andstr3
The values of are equal, so the outputs str1 and str2 are not the same asstr1 and str3 are the same
。
3. About if xxx is xxx
3.1 About if xxx is xxx Introduction
In Python,is
The operator is used to compare whether two objects are the same object. If the two strings are the same object, it returnsTrue
, otherwise returnFalse
. For example:
3.2 About if xxx is xxx example
str1 = "hello" str2 = "world" str3 = "hello" if str1 is str2: print("str1 and str2 are the same object") else: print("Str1 and str2 are not the same object") if str1 is str3: print("str1 and str3 are the same object") else: print("Str1 and str3 are not the same object")
Output
str1 and str2 are not the same object
str1 and str3 are the same objects
In this example,str1
The value of"hello"
,str2
The value of"world"
,str3
The value of"hello"
. becausestr1
andstr3
It is the same object, so the output isstr1 and str3 are the same objects
。
3.3 Extensions
In Python, a string is an immutable object, that is, once a string object is created, its value cannot be modified.
Therefore, when you create a new string, the Python interpreter allocates a new piece of space for it in memory and then stores the value of the string in this space.
In this code, str1 and str3 are both string constants, and their values are "hello".
When the Python interpreter executes this code, it will first create a string object and then assign a reference to str1.
Then, it will find that the value of str3 is also "hello", so there is no need to create a new string object, but instead point the reference to str3 to the string object that already exists.
Therefore, str1 and str3 refer to the same string object, and their addresses in memory are the same.
4. Summary
At this point, the advanced functions of Python if have been introduced. I believe that the subsequent problems with Python if will definitely not be difficult for everyone.
This is all about this article about the advanced features of Python if. For more related Python if content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!