Python self study notes -- basic data types

MG_ Lee 2021-10-29 04:59:30
python self study notes basic

Python3 There are six standard data types in :

  • Number( Numbers )
  • String( character string )
  • List( list )
  • Tuple( Tuples )
  • Set( aggregate )
  • Dictionary( Dictionaries )

Python3 Of the six standard data types :

  • Immutable data (3 individual ):Number( Numbers )、String( character string )、Tuple( Tuples );
  • Variable data (3 individual ):List( list )、Dictionary( Dictionaries )、Set( aggregate ).

You can use the built-in type()、isinstance() Function can be used to query the object type of variable

Python Multiple variables can be assigned at the same time

a = b = c = 1
a, b, c = 1, 2, "Python Self study notes "

Number

Python3 Support int、float、bool、complex( The plural ).
stay Python 3 in , There is only one integer type int, Expressed as a long integer , No, python2 Medium Long.

Numerical operation :

5 + 4 # Add 
9
4.3 - 2 # Subtraction 
2.3
3 * 7 # Multiplication 
21
2 / 4 # ▲ division ,/ Get a floating point number 
0.5
2 // 4 # ▲ division ,// Get an integer 
0
17 % 3 # Remainder 
2
2 ** 5 # chengfang 
32

character string

▲Python No separate character type , One character is the length of 1 String
▲ And C The difference between strings is ,Python String cannot be changed . Assign a value to an index location , such as word[0] = 'm' Can cause errors .

string = 'Python Self study notes '
print(string) # Output string
print(string[0:-2]) # Output No 1 One to the bottom 3 All of the characters in
print(string[0]) # Output string No 1 Characters
print(string[6:10]) # Output from 7 From the beginning to the 10 Characters
print(string[6:]) # Output from 6 All characters after the beginning of the first
print(string[0:10:2]) # Output from 1 From the beginning to the 10 And every other character ( In steps of 2)
print(string * 2) # Output string 2 Time
print(string + ' Basic grammar ') # Connection string
print('------------------------------')
print('Hello\nWorld') # Use backslash (\)+n Escapes special characters
print(r'Hello\nWorld') # Add a... Before the string r, Represents the original string , There will be no escape 
Python Self study notes
Python self-taught
P
Self study notes
Self study notes
Pto Self pen
Python Self study notes Python Self study notes
Python Self study notes, basic grammar
------------------------------
Hello
World
Hello\nWorld

List list

List( list ) yes Python The most frequently used data type in . List can complete the data structure implementation of most collection classes . The types of elements in a list can vary , It supports Numbers , Strings can even contain lists ( The so-called nested ).

Just like a string , The list can also be indexed and intercepted , After the list is truncated, a new list containing the required elements is returned .

▲ And Python The difference between strings is , The elements in the list can be changed

The syntax format of list truncation is as follows :

 Variable [ Header subscript : Tail subscript ]

image.png

list = [ 'Python', 123 , 234, ' Self study notes ', 345 ]
tinylist = [123, 'Python']
print (list) # Output complete list
print (list[0]) # The first element of the output list
print (list[1:3]) # Output from the second to the third element
print (list[2:]) # Output all elements starting with the third element
print (tinylist * 2) # Output the list twice
print (list + tinylist) # Connection list 
['Python', 123, 234, ' Self study notes ', 345]
Python
[123, 234]
[234, ' Self study notes ', 345]
[123, 'Python', 123, 'Python']
['Python', 123, 234, ' Self study notes ', 345, 123, 'Python']

Tuple Tuples

Tuples (tuple) Like a list , The difference is that the elements of a tuple cannot be modified . Tuples are written in parentheses () in , The elements are separated by commas .

Tuples can also be indexed and sliced .

▲ Tectonic inclusion 0 Or 1 Tuples of elements are special , So there are some extra grammatical rules :

tup1 = () # An empty tuple
tup2 = (20,) # An element , Comma needs to be added after element 
tuple = ( 'Python', 123 , 234, ' Self study notes ', 345 )
tinytuple = (123, 'runoob')
print (tuple) # Output full tuples
print (tuple[0]) # The first element of the output tuple
print (tuple[1:3]) # The output starts with the second element and goes to the third element
print (tuple[2:]) # Output all elements starting with the third element
print (tinytuple * 2) # Output tuples twice
print (tuple + tinytuple) # Join tuples 
('Python', 123, 234, ' Self study notes ', 345)
Python
(123, 234)
(234, ' Self study notes ', 345)
(123, 'Python', 123, 'Python')
('Python', 123, 234, ' Self study notes ', 345, 123, 'Python')

Set aggregate

aggregate (set) It is composed of one or several different sizes , The things or objects that make up a set are called elements or members .

The basic function is to test membership and remove duplicate 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)
sites = {'Google', 'Taobao', 'Runoob', 'Facebook', 'Zhihu', 'Baidu'}
print(sites) # Output set , Duplicate elements are automatically removed
# Members of the test
if 'Runoob' in sites :
print('Runoob In the assembly ')
else :
print('Runoob Not in the assembly ')
# set You can do set operations
a = set('abracadabra')
b = set('alacazam')
print(a)
print(a - b) # a and b The difference between the set
print(a | b) # a and b Union
print(a & b) # a and b Intersection
print(a ^ b) # a and b Elements that do not exist at the same time in 
{'Zhihu', 'Baidu', 'Taobao', 'Runoob', 'Google', 'Facebook'}
Runoob In the assembly
{'b', 'c', 'a', 'r', 'd'}
{'r', 'b', 'd'}
{'b', 'c', 'a', 'z', 'm', 'r', 'l', 'd'}
{'c', 'a'}
{'z', 'b', 'm', 'r', 'l', 'd'}

Dictionary Dictionaries

A list is an ordered collection of objects , A dictionary is an unordered collection of objects . The difference between the two is : The elements in the dictionary are accessed by keys , Instead of accessing by offset .

A dictionary is a mapping type , Dictionary use { } identification , It's a disorder key (key) : value (value) Set .

key (key) Immutable type must be used .

In the same dictionary , key (key) Must be unique .

dict = {}
dict['one'] = "Python"
dict[2] = " Self study notes "
tinydict = {'name': 'Python','code':1, 'content': ' Self study notes '}
print (dict['one']) # The output key is 'one' Value
print (dict[2]) # The output key is 2 Value
print (tinydict) # Output complete dictionary
print (tinydict.keys()) # Output all keys
print (tinydict.values()) # Output all values 
Python
Self study notes
{'name': 'Python', 'code': 1, 'content': ' Self study notes '}
dict_keys(['name', 'code', 'content'])
dict_values(['Python', 1, ' Self study notes '])

Reference resources :Python3 Basic data type | Novice tutorial

版权声明
本文为[MG_ Lee]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/10/20211013165939944F.html

  1. OpenCV - Python Real play (14) - face detection details (six lignes de code seulement pour apprendre 4 méthodes de détection de visage)
  2. 你好,python开发mes系统,能分享下吗,我最近也想搞这方面的
  3. 你好,python開發mes系統,能分享下嗎,我最近也想搞這方面的
  4. Bonjour, Python a développé mon système, pouvez - vous le partager?
  5. Introduction to tuples in Python
  6. Introduction to strings in python (Part 2)
  7. Introduction to strings in python (Part 1)
  8. python关于 if 的简单操作时,输出结果不是预期所要的结果 的问题
  9. python關於 if 的簡單操作時,輸出結果不是預期所要的結果 的問題
  10. Lorsque Python fonctionne simplement sur if, la sortie n'est pas le résultat attendu
  11. Python中字典问题请求解惑
  12. Python中字典問題請求解惑
  13. Demande de résolution de problèmes de dictionnaire en python
  14. Python中字典问题请求解惑
  15. Python technique 2: advanced usage of function parameters
  16. Demande de résolution de problèmes de dictionnaire en python
  17. Preliminary Knowledge - Python Core use Common Data Analysis Library (ⅱ)
  18. 关于python的代码问题,终端打印为什么会起飞
  19. En ce qui concerne les problèmes de code Python, pourquoi l'impression du terminal décolle - t - elle?
  20. Python中种子seed的运用问题
  21. L'application de Seed en python
  22. Python functional programming series 008: Testability
  23. [must see for getting started with Python] the difference and connection between cookie and session in Python!
  24. Python Xiaobai from scratch pyqt5 project actual combat (4) basic controls
  25. Python Xiaobai starts the pyqt5 project from scratch (3) connection between signal and slot
  26. Echarts ne peut pas afficher le HTML en PDF en utilisant le pdfkit de Python
  27. 一只Python 小white 的日常提问(づ ●─● )づ
  28. 2021 tutoriel complet d'automatisation des tests d'interface python [matériel d'apprentissage joint]
  29. Décrivez ce que les connaissances pertinentes jouent dans votre travail en utilisant arduino ou Python, y compris les bibliothèques pertinentes, en conjonction avec votre travail quotidien.
  30. Une question quotidienne d'un petit morceau de Python (づ● - ●)
  31. Python中字典问题请求解惑
  32. 一只Python 小white 的日常提問(づ ●─● )づ
  33. 在python中的问题,请问如何解决
  34. Only 10 questions are needed to easily master Matplotlib graphics processing | Python skill tree
  35. 在python中的問題,請問如何解决
  36. Comment résoudre le problème en python
  37. Demande de résolution de problèmes de dictionnaire en python
  38. 使用python,在一个命名为.txt文本文档写入n m乘法表。
  39. En utilisant Python, écrivez une table de multiplication n m dans un document texte nommé.txt.
  40. 使用python,在一個命名為.txt文本文檔寫入n m乘法錶。
  41. Python,前缀后缀相同时合并
  42. 关于#python#的问题:python
  43. 關於#python#的問題:python
  44. Python,前綴後綴相同時合並
  45. Questions sur # # Python #: Python
  46. Python, préfixe et suffixe combinés en même temps
  47. python manage.py shell无法运行,
  48. python manage.py shell無法運行,
  49. Le shell Python manage.py ne fonctionne pas,
  50. python中使用vscode Import 'matplotlib.pyplot' could not be resolved from source 问题
  51. [Chapter 11 of the full version] Python advanced crawler practice - system master Po anti climbing skills challenge high salary
  52. L'utilisation de vscode Import 'matplotlib.pyplot' en python ne peut pas être résolue à partir du problème source
  53. Python fusionne les deux listes et supprime les éléments dupliqués lors de la fusion
  54. [JS Reverse AES Reverse Encryption] python crawler combat, les jours sont de plus en plus décisifs
  55. 30 jeux Python. Je peux jouer à la pêche au travail pendant une journée.
  56. J'a i collecté un nouveau hit de liste en python, donc c'est un secret que quelqu'un d'autre peut devenir un magnat des médias!
  57. J'a i utilisé Python pour ramper à travers 5000 belles photos de papier peint, un jour oublié Premier amour!
  58. [Python planting system] the best green plant for your girlfriend. Girls love it when they see it! Attachment: should be able to feed - right??!
  59. [Python love guide] two small programs for sweetness burst table are released! Afraid you can't find someone?
  60. J'ai utilisé Python pour me connecter à la plus grande plate - forme de jeu au monde, et à quel point le cryptage steam est intelligent [code source inclus]