Public Approaches to Python Foundation

Posted by flashbot on Wed, 08 May 2019 11:30:03 +0200

Common methods: Lists, tuples, dictionaries and strings can be used together.

Python built-in functions

Listing of built-in functions

function describe Remarks
len(item) Calculate the number of elements in the container
del(item) Delete variables del has two methods
max(item) Returns the maximum value of the element in the container If it's a dictionary, it's only for key comparisons
min(item) Returns the minimum value of the element in the container If it's a dictionary, it's only for key comparisons
cmp(item1,item2) Comparing two values, -1 less than/0 equals/1 greater than Python 3 cancels the cmp function

Note: Strings have the following rules: "0"< "A"< "a"

Use of built-in functions

len() calculates the number of elements in the container;

Del uses keywords and functions, and the results are the same in both ways; del variable; del (variable);

```list = [1, 2,4]
del list[1]
list
[1, 4]
del(list[0])
list
[4]
del(list)
# After deleting the list, an error will be reported and undefined```

max and min functions

```test_str = "qeqfeijovodmbahidkolmc"
max(test_str)
'v'
min(test_str)
'a'
# If it's a dictionary, it's only for key comparisons
test_list = [3, 9, 0, 1]
max(test_list)
9
min(test_list)
tset_dict = {"a": "3", "b": "1", "c": "2"}
max(tset_dict)
'c'
min(tset_dict)
'a'```

In Python 3, the cmp comparison operator is cancelled, but we can directly compare by comparison operator <>.
Numbers can be compared, strings can be compared, tuples, lists can be compared, but dictionaries can not compare sizes.

Section

• Slices use index values to limit ranges and cut small strings from a large string.
• Strings, lists, tuples can be sliced because they are ordered, but dictionaries cannot slice because dictionaries are disordered.

Examples of slices are as follows:

```t_list = [3,1,2,5,7]
t_list[0:3]
[3, 1, 2]
t_tuple = (1,2,3,4,5)
t_tuple[0:3]
(1, 2, 3)```

operator

Operator listing

operator Python expression Result describe Supported data types
+ [1,2]+[3,4] [1,2,3,4] merge Strings, lists, tuples
* ["hi"]*4 ["hi","hi","hi","hi"] repeat Strings, lists, tuples
in 3 in (1,2,3) True Does an element exist? Strings, lists, tuples, Dictionaries
not in 4 not in(1,2,3) True Is the element non-existent? Strings, lists, tuples, Dictionaries
> >= == < <= (1,2,3)<(2,2,3) TRue Element comparison Strings, lists, tuples
• When in operates on a dictionary, it judges the key of the dictionary.
• In and not in are called member operators

Operator usage

* It can be used for list tuples, but not dictionaries, because dictionary key s must be unique;

```[1,2]*5
[1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
(1,2)*5
(1, 2, 1, 2, 1, 2, 1, 2, 1, 2)```

+ Operators represent merges and can be used to merge lists, tuples and strings; different types cannot be merged, such as tuples and lists;

```"hello" + "world"
'helloworld'
(1,2) + (3,4)
(1, 2, 3, 4)
[1,2] + [3,4]
[1, 2, 3, 4]```

Note that when we use + merge lists, we generate a new list

The difference between append and extend methods is that extend combines a specified list parameter into a list, i. e. scattered addition, while append combines a specified list as a whole element into a list.
Using append and extend is to extend elements on the original list.

Use of in and not in

• Inclusion and non-inclusion;
• It can be used for strings, lists, tuples, dictionaries; however, it should be noted that in the dictionary, in only checks for key s in the dictionary.

The use of in and not in is as follows:

```"a" in "asdfg"
True
"a" not in "asdfg"
False
2 in (1,2,3,4)
True
2 not in [1,2,3,4]
False
"name" in {"name": "zhangsan"}
True
"zhangsan" in {"name": "zhangsan"}
False```

Complete for loop

Complete for loop grammar

```for xx in Collection:
xxx
else:
xxx```

This is the grammatical structure of the complete for loop.

• As long as the for loop is not interrupted by break, the code in else will execute after the for loop ends.
• If the for loop is interrupted, subsequent code outside the for loop is executed directly

Complete for loop demonstration

```students = [{"name": "Hanxin"}, {"name": "Li Bai"}, {"name": "Liu Bei"}]

for stu in students:
print(stu)
else:
print("I am for At the end of the loop traversal else Sentence")
print("for The cycle is over")

# {name':'hanxin'}
# {name':'Li Bai'}
# {name':'Liu Bei'}
# I'm the else statement after the for loop traversal
# The for loop is over```

break interrupts for loop demonstration

```students = [{"name": "Hanxin"}, {"name": "Li Bai"}, {"name": "Liu Bei"}]

for stu in students:
print(stu)
if stu["name"] == "Li Bai":
print("Li Bai, your mother called you home for dinner.")
break
else:
print("I am for At the end of the loop traversal else Sentence")
print("for The cycle is over")

# {name':'hanxin'}
# {name':'Li Bai'}
# Li Bai, your mother called you home for dinner.
# The for loop is over```

Application scenarios for else

• When iterating over nested data types, for example, a list contains multiple dictionaries

Requirement: Need to determine whether a specified value exists in a dictionary
If it exists, prompt and exit the loop
If it does not exist, after the whole cycle is over, we hope to get a unified reminder.

Find out the situation

```students = [{"name": "Hanxin"}, {"name": "Li Bai"}, {"name": "Liu Bei"}]

find_stu = "Li Bai"

for stu in students:
print("Looking for...", stu["name"])
if stu["name"] == find_stu:
print("%s，Your mother told you to go home for dinner." % find_stu)
break
else:
print("Excuse me, your home%s Not in this class" % find_stu)
print("for The cycle is over")

# Looking for Han Xin
# Looking for... Li Bai
# Li Bai, your mother called you home for dinner.
# The for loop is over```

```students = [{"name": "Hanxin"}, {"name": "Li Bai"}, {"name": "Liu Bei"}]

find_stu = "Zhang Fei"

for stu in students:
print("Looking for...", stu["name"])
if stu["name"] == find_stu:
print("%s，Your mother told you to go home for dinner." % find_stu)
break
else:
print("Excuse me, your home%s Not in this class" % find_stu)
print("for The cycle is over")

# Looking for Han Xin
# Looking for... Li Bai
# Looking for Liu Bei
# Sorry, Zhang Fei is not in this class.
# The for loop is over```

Topics: Python less