python learning note 4

Keywords: Python

Chat 6 set

a = set([1, 2, 3, 1])
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
# Create set
a.union(b)
a | b 
a.intersection(b)
a & b
a.difference(b)
a - b
a.symmetric_difference(b)
a ^ b
a = {1, 2, 3}
b = {1, 2}
b.issubset(a)
b <= a
a.issuperset(b)
a >= b
# In fact, there are > < symbols to use here

Relative to list, append is used to add, set is used to add, and update is used to update the entire file

t = {1, 2, 3}
t.add(5)
t.update([5, 6, 7])

Use remove to delete an element, pop to delete the last element

t.remove(1)
t.remove(10)
# If this does not exist, an error will be reported
t.discard(10)
# Compared with t.remove(), t.discard(), no error will be reported
t.pop()

Next, we will introduce the frozen set. As the name implies, it is an invariant set

s = frozenset([1, 2, 3, 'a', 1])

One of the main applications of invariant sets is to be the key of dictionaries

flight_distance = {}
city_pair = frozenset(['Los Angeles', 'New York'])
flight_distance[city_pair] = 2498
flight_distance[frozenset(['Austin', 'Los Angeles'])] = 1233
flight_distance[frozenset(['Austin', 'New York'])] = 1515
flight_distance

Because sets are not ordered, different orders do not affect the lookup results:

flight_distance[frozenset(['New York','Austin'])]
flight_distance[frozenset(['Austin','New York'])]

This is also the difference between tuple s

Some children's shoes think that the tuple is the same, and why do I need to use the frozen, because the set is not sequential, and the tuple is sequential

Chat 7 control section

python's control statements all end with:

python's tab key indicates whether you belong to this control statement

if statement

x = -0.5
if x > 0:
    print "Hey!"
    print "x is positive"
    print "This is still part of the block"
print "This isn't part of the block, and will always print."
year = 1900
if year % 400 == 0:
    print "This is a leap year!"
# Only when both conditions are met
elif year % 4 == 0 and year % 100 != 0:
    print "This is a leap year!"
else:
    print "This is not a leap year."

while statement

plays = set(['Hamlet', 'Macbeth', 'King Lear'])
while plays:
    play = plays.pop()
    print 'Perform', play

for statement

plays = set(['Hamlet', 'Macbeth', 'King Lear'])
for play in plays:
    print 'Perform', play
total = 0
for i in xrange(100000):
    total += i
print total

# range(x) will generate a temporary table before doing so, which is not good for efficiency and memory

Let's not talk about continue and break

Let's talk about else

Like if, while and for loops can be followed by else statements, but they need to be used with break.

  • When the loop ends normally, the loop condition is not satisfied and else is executed;
  • When the loop is broken, the loop condition is still satisfied and else does not execute.

Here's an example

values = [11, 12, 13, 100]
for x in values:
    if x <= 10:
        print 'Found:', x
        break
else:
    print 'All values greater than 10'

Posted by contex on Mon, 30 Mar 2020 23:21:35 -0700