当前位置:网站首页>tuple and point
tuple and point
2022-07-05 02:24:00 【Incoming brother】
Tuples
About tuples : Tuples are in Python of use ()
Express , Unlike lists, tuples cannot be modified
If the tuple has only one value , We can't just write a value , This will turn Python The interpreter is confused
We're going to write ( value ,)
Form like this
The purpose of tuples is to tell people who read code , You don't want to change this thing
hello = [1,2,3]
hello[1] = 4
# Wrong writing
tuple() and list()
We can convert tuple type to list type , You can also convert a list type to a tuple type
a = tuple([1,2,4])
print(type(a))
# Output is tuple type
a = list((1,2,3,4))
print(tupe(a))
# Output is list type
About quoting
Before you know about references , Let's compare the two programs first
a = 1
b = a
b = 2
print(a) #1
print(b) #2
We found this a The value of
a = [1,2,3]
b = a
b[0] = 0
print(a) # [0,2,3]
print(b) # [0,2,3]
We found out here a[1] The value of has changed , Mainly because of a It's a reference ,a Point to the list
b = a, It means to make b Also points to the list
Pass on references
def add(p):
a.append(4)
a = [1,2,3]
add(p)
print(a)
# [1,2,3,4]
This happens to lists and dictionaries
If you don't want this quote to happen , You can use full assignment
import copy
a = [1,2,3,4]
b = copy.copy(a)
b[1] = 0
print(a) #[1,2,3,4]
print(b) #[0,2,3,4]
print('========')
# If a list is nested in the list
c = [1,[1,2,3]]
d = copy.deepcopy(c)
d[1] = 0
print(c)
print(d)
summary :
1). The list is variable , Tuples are immutable
2). The assignment of a list is a reference
3). The complete assignment is copy.copy(list),copy.deepcopy(list)
边栏推荐
- [Yu Yue education] National Open University spring 2019 0505-22t basic nursing reference questions
- Icu4c 70 source code download and compilation (win10, vs2022)
- One click generation and conversion of markdown directory to word format
- Yolov5 model training and detection
- Talk about the things that must be paid attention to when interviewing programmers
- Practical case of SQL optimization: speed up your database
- Vulnstack3
- [technology development-26]: data security of new information and communication networks
- Pgadmin 4 V6.5 release, PostgreSQL open source graphical management tool
- Numpy library introductory tutorial: basic knowledge summary
猜你喜欢
Visual studio 2019 set transparent background (fool teaching)
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience
Binary tree traversal - middle order traversal (golang)
A label making navigation bar
Runc hang causes the kubernetes node notready
【LeetCode】111. Minimum depth of binary tree (2 brushes of wrong questions)
【附源码】基于知识图谱的智能推荐系统-Sylvie小兔
[source code attached] Intelligent Recommendation System Based on knowledge map -sylvie rabbit
如何搭建一支搞垮公司的技術團隊?
官宣!第三届云原生编程挑战赛正式启动!
随机推荐
数据库和充值都没有了
Use the difference between "Chmod a + X" and "Chmod 755" [closed] - difference between using "Chmod a + X" and "Chmod 755" [closed]
Erreur de type de datagramme MySQL en utilisant Druid
【LeetCode】110. Balanced binary tree (2 brushes of wrong questions)
openresty ngx_ Lua execution phase
RichView TRVStyle MainRVStyle
The phenomenology of crypto world: Pioneer entropy
Word processing software
R language uses logistic regression and afrima, ARIMA time series models to predict world population
Prometheus monitors the correct posture of redis cluster
Yolov5 model training and detection
Grub 2.12 will be released this year to continue to improve boot security
172. Zero after factorial
Timescaledb 2.5.2 release, time series database based on PostgreSQL
Pytorch common code snippet collection
Restful fast request 2022.2.1 release, support curl import
RichView TRVStyle MainRVStyle
Official announcement! The third cloud native programming challenge is officially launched!
RichView TRVUnits 图像显示单位
How to find hot projects in 2022? Dena community project progress follow-up, there is always a dish for you (1)