Address reference and garbage collection
The variable’s == address reference ==
a = "jack"
b = a
a = "tom"
What's the value of b?
Copy the code
== Delete reference ==
a = "jack"
b = a
del a
del b
Copy the code
When an object is not referenced, Python’s garbage collection mechanism destroys the current object, freeing memory.
View object memory number, id(obj)
Determine the type of the object
In Python programs, you often need to determine the type of obj. Common methods:
type
#type
type(obj) is int
type(obj) is float
type(obj) is str
type(obj) is bool
type(obj) is None #None is a unique object representing a null value
type(obj) is list
type(obj) is tuple
type(obj) is dict
type(obj) is set
type(obj) is not int
Copy the code
Where ==is== determines whether the object is the same; == Check whether the values are the same.
isinstance
a = 24
isinstance(a,int)
isinstance(a,(int.str.bool)) # satisfy is an instance of one of these classes and returns True
issubclass(int.object) Determine if it is a subclass
Copy the code
Ternary operator
a = 1
b = "jack"
c = a if a else b
Copy the code
List of additions, deletions, checks, sharding
alist = ["jack"."tom"."lucy"."jack"]
alist.tab # query method
alist[1:3] #["tom","lucy"]
alist[::-1] # reverse
alist[::2] # specify interval ["jack"," Lucy "]
Copy the code
Practice:
- Find index value of second “jack” on alist and change jack to jack2.
- Fragmentation implements the reverse order of alist
- Prints the value with an odd index in Alist
- The reverse sort of Alist itself
The answer:
# 1.
alist.index("jack",alist.index("jack") +1)
# 2.
alist[::-1]
# 3.
for i in range(len(alist)):
if i%2! =0:
print(alist[i])
#4. Sort by itself
alist.sort() # returns None
alist.reverse()
Copy the code
Tuples are used and immutable
Reference addresses are immutable and generally store read-only data
string
s = R "Raw string, unescaped"
s.tab # query method
s.capitalize() # capitalize the first character
s.title() Capitalize the first letter of each word
s.center(100."*") # center
s.ljust(100."*") # length 100, left aligned, not enough *
s.rjust(100."*")
s.upper() # uppercase
s.lower()
s.islower() # all lowercase
s.isupper()
s.startswith("xxx",start,end)
s.endswith("xxx",start,end)
# String formatting
"name is {0} , age is {1}, this name {0}".format("jack".25)
"name is {name} , age is {age}, this name {name}".format(name="jack",age=25)
"name is %s,age is %d"% ("jack".25)
# to find
s.find("xxx",start,end) # return index /-1
s.index("xxx",start,end) Return index/exception ValueError
s.isdigit() # if number, (integer)
s.isspace()
s.join(["jack"."tom"]) # use s to concatenate items in the list
s.strip() # Remove the Spaces on both sides
s.strip("xx")# Remove xx from both sides
s.rstrip("xx")
s.lstrip("xx")
s.replace("old"."new",count=num) # replace several times, default all
s.split("xx",maxsplit=1) # split string with xx at most once; The default all
Copy the code
Practice: pending
The dictionary
Key-value storage, unordered container data structure key unique and immutable type, such as “name”/ 1/1.0/(1,2,3) Fast query, time complexity O(1)
d = {"name":"jack"}
d1 = dict(name="tom")
d2 = {}.fromkeys([1.2.3].None)
# insert
d["age"] = 25
d.setdefault(key,value) If there is no such key, return value. Otherwise, nothing is written and only the original value is returned
# remove
d.pop(key)
d.popitem()
del d[key]
# update
d.update(key=value) # with a new key
d.update({"name":"jack"."age":25}) # overwrites the original key
# query
if key in d:
d[key]
d.get(key,None) Return None/ other if no
d.keys()
d.values()
d.items()
Copy the code
A collection of
Unordered, deduplicated container data structures store immutable data types
set1 = set()
set1.add(5)
set1.add([1.2.3]) #error
# remove
set1.discard(5) # Delete if it exists, do nothing if it does not exist
set1.pop() # random delete
set1.remove(5) # Do not exist, error
5 in set1 # Whether to include
Collections cannot be indexed
# Set relational operations
# intersection
s1 & s2
# difference set
s1 - s2
# and set
s1 | s2
# xor to s1 does not belong to s2 | to do not belong to the s1 s2
s1 ^ s2
s1.issubset(s2)
s2.issuperset(s1)
s1.isdisjoint(s2) # Whether there is no intersection
Copy the code
Previous: Basics of Python —- Data types and Structures Next: Python and binary