current position:Home>Python note-day6-tuple dictionary collection
Python note-day6-tuple dictionary collection
2022-05-15 06:03:00【Wandering mage 12】
Preface
python Grammar learning , Leave it to those who need it , Understand everything !!
# coding=utf8
# @time:2022/3/29 20:11
# Author Haoyu
# 1. Tuples - Tuples are ordered and unchangeable sets . stay Python in , Tuples are written in parentheses
'''''''''
Python A tuple of is similar to a list , The difference is that the elements of a tuple cannot be modified .
Tuples use braces ( ), Use square brackets for lists [ ].
Tuples are easy to create , You just need to add elements in parentheses , And separate them with commas .
tup1 = ('Google', 'Runoob', 1997, 2000)
print(tup1)
'''''''''
# 1) Create an empty tuple
'''''''''
tup = ()
print(tup)
'''''''''
# 2) When a tuple contains only one element , You need to add a comma after the element , , Otherwise parentheses will be used as operators :
'''''''''
# No commas
tup = (50)
print(type(tup))
# Output
<class 'int'>
# put a comma
tup = (50,)
print(type(tup))
# Output
<class 'tuple'>
'''''''''
# Be careful : Tuples are similar to strings , Subscript index from 0 Start , You can intercept , Combination etc. .
# Parameter reading of tuple 、 modify 、 Delete 、 Combinations are similar to lists , Not described here one by one , Reference list notes ;
# 2 aggregate - aggregate (set) Is an unordered sequence of non-repeating elements .
# You can use braces { } perhaps set() Function to create a collection ,
# Be careful : To create an empty collection, you must use the set() instead of { }, because { } Is used to create an empty dictionary .
'''''''''
Create format :
parame = {
value01,value02,...}
perhaps
set(value)
'''''''''
# 1) Additive elements
# aggregate .add( Elements )
# Be careful : Add elements to the collection , If the element already exists , Nothing is done .
'''''''''
s = set(("Google", "Runoob", "Taobao"))
print(s)
s.add("Facebook")
print(s)
'''''''''
# There's another way , You can also add elements , And the parameter can be a list , Tuples , Dictionary, etc , The syntax is as follows :
# aggregate .update( Elements )
'''''''''
thisset = set(("Google", "Runoob", "Taobao"))
thisset.update({
1,3})
print(thisset)
{
1, 3, 'Google', 'Taobao', 'Runoob'}
thisset.update([1,4],[5,6])
print(thisset)
{
1, 3, 4, 5, 6, 'Google', 'Taobao', 'Runoob'}
'''''''''
# 2) Remove elements
# grammar : aggregate .remove( Elements )
'''''''''
s = set(("Google", "Runoob", "Taobao"))
s.discard("Facebook") # No, no error occurs
print(s)
'''''''''
# There is also a way to remove elements from the collection , And if the element does not exist , There will be no errors . The format is as follows :
# s.discard( x )
'''''''''
thisset = set(("Google", "Runoob", "Taobao"))
thisset.discard("Facebook") # No, no error occurs
=print(thisset)
{
'Taobao', 'Google', 'Runoob'}
'''''''''
# Randomly delete an element of the set
# aggregate .pop
'''''''''
thisset = set(("Google", "Runoob", "Taobao", "Facebook"))
x = thisset.pop()
print(thisset)
print(x)
'''''''''
# 3. Dictionaries
# Dictionary is another variable container model , And can store any type of object .
# Each key value of the dictionary key=>value Yes, with a colon : Division , Use commas... Between each pair (,) Division , The whole dictionary is enclosed in curly brackets {} in , The format is as follows :
# d = {key1 : value1, key2 : value2, key3 : value3 }
# The key must be unique , But values don't have to be .
# The value can take any data type , But the bond has to be immutable , Such as a string , Numbers .
'''''''''
tinydict = {
'name': 'runoob', 'likes': 123, 'url': 'www.runoob.com'}
'''''''''
# 1) Visit the values in the dictionary
# Put the corresponding key in square brackets
'''''''''
tinydict = {
'Name': 'Runoob', 'Age': 7, 'Class': 'First'}
print("tinydict['Name']: ", tinydict['Name'])
'''''''''
# 2) Revise the dictionary
# The way to add new content to the dictionary is to add new keys / It's worth it , Modify or delete existing keys / The value pairs are as follows :
'''''''''
tinydict = {
'Name': 'Runoob', 'Age': 7, 'Class': 'First'}
tinydict['Age'] = 8 # to update Age
print(tinydict)
tinydict['School'] = " rookie " # Add information
print(tinydict)
'''''''''
# 3) Delete Dictionary
# Can delete a single element can also empty the dictionary , Emptying takes only one operation .
# Explicitly delete a dictionary with del command , The following example :
'''''''''
tinydict = {
'Name': 'Runoob', 'Age': 7, 'Class': 'First'}
del tinydict['Name'] # Delete key 'Name'
print(tinydict)
tinydict.clear() # Empty dictionary
print(tinydict)
del tinydict # Delete Dictionary
# print(tinydict)
'''''''''
More secure sharing , Please pay attention to 【 Security info】 WeChat official account !
copyright notice
author[Wandering mage 12],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/131/202205110607424690.html
The sidebar is recommended
- Introduction to the differences between Python and Java
- Explain Python CONDA in detail
- The pycham downloaded by MAC reports an error as soon as it is opened. The downloaded Python interpreter is also the latest version
- From entry to mastery, python full stack engineers have personally taught Python core technology and practical combat for ten years
- Python is used to detect some problems of word frequency in English text.
- How to choose between excel, database and pandas (Python third-party library)?
- WxPython download has been reporting errors
- Pyside6 UIC and other tools cannot be found in the higher version of pyside6 (QT for Python 6). How to solve it?
- About Python Crawlers
- Successfully imported pandas, unable to use dataframe
guess what you like
How to extract some keywords in the path with Python
Python encountered a problem reading the file!
When Python is packaged into exe, an error is reported when opening assertionerror: C: \ users \ Acer \ appdata \ local \ temp\_ MEI105682\distutils\core. pyc
Eight practical "no code" features of Python
Python meets SQL, so a useful Python third-party library appears
100 Python algorithm super detailed explanation: a hundred dollars and a hundred chickens
[fundamentals of Python] Python code and so on
When Python uses probit regression, the program statement is deleted by mistake, and then it appears_ raise_ linalgerror_ Unrecognized error of singular
Python testing Nicholas theorem
Accelerating parallel computing based on python (BL) 136
Random recommended
- Python dynamic programming (knapsack problem and longest common substring)
- Django uses queryset filter save, and an 'queryset' object has no attribute 'Save' error occurs. Solution?
- Analysis of built-in functions in Python learning
- Python office automation - 90 - file automation management - cleaning up duplicate files and batch modifying file names
- Python office automation - 91 - word file Automation - word operation and reading word files
- After python, go also runs smoothly on the browser
- Self taught Python 26 method
- Summary of Python Tkinter component function examples (code + effect picture) (RadioButton | button | entry | menu | text)
- Python implementation of official selection sorting of Luogu question list
- Application of Django template
- Get project root path and other paths in Python project
- Get, rename, and delete file names in Python projects
- How to set the width and height of Python operation table
- Python string preceded by 'f' R 'B' U '
- JSON and other types convert to each other in Python
- Key value of key combination in pynput in Python
- Conversion of Python PDF file to word file
- Interface testing uses Python decorators
- Get the current time in Python
- Python course notes -- Python string, detailed explanation of related functions
- Python file and folder operations
- Python file content operation
- Three basic data quality evaluation methods and python implementation
- Python data structure and mathematical algorithm examples (continuously updating)
- GUI interface mail sending program based on SMTP protocol server and python language
- Python application tool development: mail sending GUI program
- Python application tool development: PIP instruction GUI program
- Application development based on Python and MySQL database: student information score management system version 1.0
- [Python] sort and sorted
- [Python] create a two-dimensional array with list, which is easy to step on
- Multiply [email protected]
- About creating in Python project folder__ init__. Understanding of PY
- Python, zsbd
- Smplify introduction and in python2 7 operation in environment
- Boost(2):boost. Python library introduction and simple examples
- Boost (3): encapsulate C + + classes into Python classes
- Boost (5): extract the type of C + + language and extract class from Python object
- Boost(6):Boost. How Python converts C + + parameter and return value types
- Boost(7):Boost. Python encapsulates overloaded functions and passes default parameters
- Boost(8):Boost. Python implements Python objects and various types of built-in operations and methods