current position：Home>Stroke tuple in Python
Stroke tuple in Python
2022-01-30 19:12:33 【Lei Xuewei】
Little knowledge , Great challenge ！ This article is participating in 「 A programmer must have a little knowledge 」 Creative activities
This article has participated in 「 Digging force Star Program 」 , Win a creative gift bag , Challenge creation incentive fund .
ceremonial Python Column No 24 piece , Classmate, stop , Don't miss this from 0 The beginning of the article ！
Today, the school committee is writing code , Wrote a lot of , This time we'll talk about tuples Tuple, Don't miss ！
Tuple yes Python A unique type , It's a bit like a list, but it's not exactly .
If you understand it simply and roughly , So let's call it tuple Of limited length list, Not too much ！
This is a tuple
obtain tuple Elements of type data ：
tup = (1,2,3)
tup # Get the element corresponding to the first subscript
tup[0:2] # Get sub tuple
No more operation variants , This is in the School Committee Python On the basis, the first part tells .
tuple Add, delete, check and modify
Basically don't think about it ,Python Tuples in are not allowed to be tampered with .
however , There's a premise ,tuple If the element in is not the basic data type , That is, reference types .
Then we have a way to modify its internal state , Here's the picture ：
We can view and try to modify 't' When Python The interpreter immediately threw an error ！
So what can be done on tuples ？
Basically, there are only addition and multiplication , The effect is to generate an extended new meta group .
It's kind of like 1+1 = 11 That's what I mean .
The school committee has prepared the following code ：
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/10/30 6:39 Afternoon # @Author : LeiXueWei # @CSDN/Juejin/Wechat: Lei Xuewei # @XueWeiTag: CodingDemo # @File : tupledemo2.py # @Project : hello tup = (3, 3, 3) # new_tup = tup - tup #TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple' new_tup = tup + tup print(new_tup) # The academic committee still likes the three companies ,666 new_tup = tup * 3 print(" Three times in a row tuple：", new_tup) # new_tup = tup / 3 # I don't support it. It must be next time ！ print("'666' in new_tuple ? ", '666' in new_tup) Copy code
Readers can directly copy the running code , The school committee added the operation effect diagram ：
list turn tuple
In front of us, we roughly put tuple As a limited length list 了 （ Some scenes can be very useful , For example, fixed length list）.
So we sometimes put list Turn into tuple, Still get the elements for business operations .
Next, the school committee has prepared some code displays ：
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/10/30 6:39 Afternoon # @Author : LeiXueWei # @CSDN/Juejin/Wechat: Lei Xuewei # @XueWeiTag: CodingDemo # @File : tupledemo3.py # @Project : hello tup = (3, 3, 3) new_tup = ([x * 2 for x in tup]) print(" Doubled tuple：", new_tup) list = [6, 6, 6] new_tup = tuple(list) print(" hold list turn tuple： ", new_tup) if 6 in new_tup: print("new_tup has 6 !") Copy code
The operation effect is as follows ：
In addition to some of the functions described above , We can also use some built-in functions （max/min etc. ） Yes tuple To operate , These functions can also be used for string, list And so on , You can follow the subscription and summarize it separately later .
by the way , like Python Friend, , Please pay attention to Python Basic column or Python From getting started to mastering the big column
Continuous learning and continuous development , I'm Lei Xuewei ！
Programming is fun , The key is to understand the technology thoroughly .
Welcome to wechat , Like support collection !
author[Lei Xuewei],Please bring the original link to reprint, thank you.
The sidebar is recommended
- Exploratory data analysis (EDA) in Python using SQL and Seaborn (SNS).
- Turn audio into shareable video with Python and ffmpeg
- Using rbind in python (equivalent to R)
- Pandas: how to create an empty data frame with column names
- Talk about quantifying investment using Python
- Python, image restoration in opencv - CV2 inpaint
- Python notes (14): advanced technologies such as object-oriented programming
- Python notes (13): operations such as object-oriented programming
- Python notes (12): inheritance such as object-oriented programming
- Chapter 2: Fundamentals of python-5 Boolean
guess what you like
Python notes (11): encapsulation such as object-oriented programming
Python notes (10): concepts such as object-oriented programming
Gradient lifting method and its implementation in Python
Van * Python | simple crawling of a site course
Chapter 1 preliminary knowledge of pandas (list derivation and conditional assignment, anonymous function and map method, zip object and enumerate method, NP basis)
Nanny tutorial! Build VIM into an IDE (Python)
Fourier transform of Python OpenCV image processing, lesson 52
Introduction to python (III) network request and analysis
China Merchants Bank credit card number recognition project (Part I), python OpenCV image processing journey, Part 53
Introduction to python (IV) dynamic web page analysis and capture
- Python practice - capture 58 rental information and store it in MySQL database
- leetcode 119. Pascal's Triangle II（python）
- leetcode 31. Next Permutation（python）
- [algorithm learning] 807 Maintain the city skyline (Java / C / C + + / Python / go / trust)
- The rich woman's best friend asked me to write her a Taobao double 11 rush purchase script in Python, which can only be arranged
- Glom module of Python data analysis module (1)
- Python crawler actual combat, requests module, python realizes the full set of skin to capture the glory of the king
- Summarize some common mistakes of novices in Python development
- Python libraries you may not know
- [Python crawler] detailed explanation of selenium from introduction to actual combat 
- This is what you should do to quickly create a list in Python
- On the 55th day of the journey, python opencv perspective transformation front knowledge contour coordinate points
- Python OpenCV image area contour mark, which can be used to frame various small notes
- How to set up an asgi Django application with Postgres, nginx and uvicorn on Ubuntu 20.04
- Initial Python tuple
- Introduction to Python urllib module
- Advanced Python Basics: from functions to advanced magic methods
- Python Foundation: data structure summary
- Python Basics: from variables to exception handling
- Python notes (22): time module and calendar module
- Python notes (20): built in high-order functions
- Python notes (17): closure
- Python notes (18): decorator
- Python notes (16): generators and iterators
- Python notes (XV): List derivation
- Python tells you what timing attacks are
- Python -- file and exception
- [Python from introduction to mastery] (IV) what are the built-in data types of Python? Figure out
- Python code to scan code to pay attention to official account login
- [algorithm learning] 1221 Split balanced string (Java / C / C + + / Python / go / trust)
- Python notes (22): errors and exceptions
- Python has been hidden for ten years, and once image recognition is heard all over the world
- Python notes (21): random number module
- Python notes (19): anonymous functions
- Use Python and OpenCV to calculate and draw two-dimensional histogram
- Python, Hough circle transformation in opencv
- A library for reading and writing markdown in Python: mdutils
- Datetime of Python time operation (Part I)
- The most useful decorator in the python standard library
- Python iterators and generators