Last Zero basics python programming thinking (I) | mathematical operation A small tail is left, that is, logical operation, which is supplemented and updated here first:

# 4: Logical operation

a = 10 b = 20 if (a and b): #Boolean and: returns the value of a if a is False, otherwise returns the calculated value of b print("variable a and b All for True") else: print("variable a and b One is not True") if (a or b): #Boolean or: returns the value of a if a is True, otherwise returns the calculated value of b print("variable a and b At least one variable is True") else: print("variable a and b Not for True") a = 0 #At this time, a is False if (a and b): print("variable a and b All for True") else: print("variable a and b One is not True") if (a or b): print("variable a and b At least one variable is True") else: print("variable a and b Not for True") if not(a and b): #Boolean "no": Returns False if a is True, otherwise returns True print("variable a and b At least one is False") else: print("variable a and b All for True")

There are two things that have been confused before: one is why the assignment operation of a and b can be used for logical judgment; The second is why the value 20 of b is output when "a and b" in the third line of code is run alone. The first problem is that in python, non-zero integer values represent True, so these two variables can be logically operated. When the value of a is changed to 0 later, a is False at this time. For the second question, we can try to exchange the positions of a and b to get the answer:

a and b Out[21]: 20 b and a Out[22]: 10 a or b Out[23]: 10 b or a Out[24]: 20 a and b Out[25]: 20 b and a Out[26]: 10 a or b Out[27]: 10 b or a Out[28]: 20 a = 0 a and b Out[30]: 0

It can be seen from the above operation results that when a and b are True, the output result of and operation is the last value (the same is True with another variable c), and the output result of or operation is the first value; When a is False, the output of and operation is the value of a, i.e. 0, representing False.

The above is my own thinking. If there is anything wrong, please correct it.

The following is the content of this blog:

# 1, String

Reference to string:

my_name = 'Vince' my_age = 23 my_height = 157 my_weight = 46.5 my_eyes = 'Brown' my_teeth = 'White' my_hair = 'Brown' print(f"Let's talk about {my_name}.") print(f"She's {my_height} inches tall.") print(f"She's {my_weight} kilograms heavy.") print("Actually that's not heavy.") print(f"She's got {my_eyes} eyes and {my_hair} hair.") print(f"Her teeth are actually {my_teeth} depending on the coffee.") total = my_age + my_height + my_weight print(f"If I add {my_age}, {my_height}, and {my_weight} I get {total}.")

# 2, List

## list

List is an ordered set, and classmates here is a list:

classmates=['Michael','Bob','Tracy']

len() gets the number of list elements:

len(classmates)

Use the index to access the elements at each position in the list. The positive order of the index starts from 0 and the reverse order starts from - 1.

When the index exceeds the range, an IndexError error is reported. The index of the last element is len(list)-1. You can also use - 1 as the index to get the last element directly. By analogy, you can also get the penultimate 2 and 3 elements, and an error will be reported when it crosses the boundary.

You can append elements to the end of the list or insert them into the specified position:

classmates.append('Adam') classmates.insert(1,'Visstin')

Delete the element at the end, directly use pop() to delete the element at the specified position, and write the index position in parentheses:

classmates.pop(1)

Replacing an element with another element can be directly assigned to the corresponding index position:

classmates[1]='Sarah'

Sort the elements in the list:

classmates.sort()

The data type of the element in the list can be different, or even another lsit:

L=['Apple',123,True] s=['python','java',['asp','php'],'scheme']

There are only four elements in s, of which s[2] is a list. To get php, you can write s[2][1], so s is a two-dimensional array.

If a list is an empty list, its length is 0:

Z=[] len(Z)

## tuple

classmates2=('Michael','Bob','Tracy')

Tuple is also an ordered list. When a tuple is defined, the elements must be determined, but cannot be changed once initialized (elements cannot be added / deleted, and cannot be assigned to other elements). However, because tuples are immutable, the code is safer. If possible, try to use tuples instead of lists.

To define an empty tuole, you can write it as ():

t = ()

But to define a tuple with only one element, write it as (1) defines only the number 1, because () can represent both tuples and parentheses in mathematical formulas. To disambiguate, a tuple definition with only one element must be added, including:

t=(1) t=(1,)

Let's look at the following example:

t=('a','b',['A','B']) t[2][0]='X' t[2][1]='Y'

On the surface, the elements of tuple have indeed changed, but in fact, the elements of list have changed. The list pointed to by tuple at the beginning does not become another list, so the invariability of tuple means that the point will never change.