You can learn Python articles without reading online classes (day 4)

Why_ does_ it_ work 2021-11-25 15:31:37
learn python articles reading online


Number( Numbers )

Python  character string

Python  list (List)

Python  Tuples

Python  Dictionaries (Dictionary)

Number( Numbers )

python Number Data types are used to store values .

Data type cannot be changed , That means if it changes Number Value of data type , Memory space will be reallocated

Python  character string

The string is Python The most commonly used data type in . We can use quotation marks (' or ") To create a string .

Creating a string is simple , Just assign a value to the variable

Python  list (List)

Sequence is Python The most basic data structure in . Each element in the sequence is assigned a number - Its location , Or index , The first index is 0, The second index is 1, And so on .

Python Yes 6 Built in types of sequences , But the most common are lists and tuples .

The operations that can be performed by a sequence include indexing , section , Add , ride , Check members .

Besides ,Python There's a built-in way to determine the length of the sequence and determine the maximum and minimum elements .

Lists are the most commonly used Python data type , It can appear as a comma separated value in square brackets .

The data items of a list do not need to have the same type

Create a list , Just enclose the different data items separated by commas in square brackets

list1 = ['physics', 'chemistry', 1997, 2000]

list2 = [1, 2, 3, 4, 5 ]

list3 = ["a", "b", "c", "d"]

list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5, 6, 7 ]
print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]

The output of the above example :

list1[0]: physics
list2[1:5]: [2, 3, 4, 5]

Delete list elements

have access to del Statement to delete the elements of the list

del list1[2]

Key functions in the list

Serial number function
1 cmp(list1, list2)
Compare the elements of two lists
2 len(list)
Number of list elements
3 max(list)
Returns the maximum value of a list element
4 min(list)
Returns the minimum value of a list element
5 list(seq)
Converts a tuple to a list

Python  Tuples

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 = ('physics', 'chemistry', 1997, 2000)

tup2 = (1, 2, 3, 4, 5 )

tup3 = "a", "b", "c", "d"

Create an empty tuple

tup1 = ()

When a tuple contains only one element , You need to add a comma after the element

tup1 = (50,)
tup1 = ('physics', 'chemistry', 1997, 2000)
tup2 = (1, 2, 3, 4, 5, 6, 7 )
print "tup1[0]: ", tup1[0]
print "tup2[1:5]: ", tup2[1:5]

The output of the above example :

tup1[0]: physics
tup2[1:5]: (2, 3, 4, 5)

Modify tuple

Element values in tuples are not allowed to be modified , But we can join tuples

tup1 = (12, 34.56)
tup2 = ('abc', 'xyz')
tup3 = tup1 + tup2
print tup3

The output of the above example :

(12, 34.56, 'abc', 'xyz')

Delete tuples

Element values in tuples are not allowed to be deleted , But we can use del Statement to delete the entire tuple

del tup

Tuple operators

Same as string , You can use... Between tuples + Number and * Number to calculate . That means they can combine and copy , A new tuple is generated after the operation

Python  Dictionaries (Dictionary)

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 , Comma between each key value pair  ,  Division , The whole dictionary is enclosed in curly brackets  {}  in , The format is as follows :

d = {key1 : value1, key2 : value2 }

Keys are usually the only , If you repeat the last key value pair, the previous one will be replaced , Value doesn't need to be unique .

dict = {'a': 1, 'b': 2, 'b': '3'}

Will be output :3

The value can take any data type , But the bond has to be immutable , Such as a string , A number or tuple .

A simple dictionary example :

dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}

You can also create a dictionary :

dict1 = { 'abc': 456 }

dict2 = { 'abc': 123, 98.6: 37 }

Visit the values in the dictionary

Put the corresponding key in the familiar square brackets , The following example :


 dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
print "dict['Name']: ", dict['Name']
print "dict['Age']: ", dict['Age']

The output of the above example :

dict['Name']: Zara
dict['Age']: 7

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 :

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
dict['Age'] = 8 # to update
dict['School'] = "RUNOOB" # add to
print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']

The output of the above example :

dict['Age']: 8
dict['School']: RUNOOB

Delete dictionary elements

Can delete a single element can also empty the dictionary , Emptying takes only one operation .

Show delete a dictionary with del command , The following example :

del dict['Name'] # Delete key is 'Name' The entry of

dict.clear() # Empty all dictionary entries

del dict # Delete Dictionary

Dictionary key features

The dictionary value can take any python object , It can be a standard object , It can also be user-defined , But the key doesn't work .

Two important points to remember :

1) The same key is not allowed to appear twice . When creating, if the same key is assigned twice , The latter value will be remembered , The following example :2) The key must be immutable , So you can use numbers , A string or tuple acts as , So using lists doesn't work , The following example

dict = {['Name']: 'Zara', 'Age': 7}
print "dict['Name']: ", dict['Name']

In short, the dictionary repeats and remembers the following , The dictionary cannot appear in the list .

python It is expected to end tomorrow , But the article will continue to share ,c Language and python The details of , Turn on c++ Algorithm

The third language !


Function and code reuse
One , Definition and use of functions

(1) Definition of function :

        a, General functions

         def< Function name >( Parameters (0 One or more )):

               < The body of the function >

              return < Return value >

      b, lambda function

        < Function name >=lambda< Parameters >:< expression >

(2) The return value of the function

        The function can return 0 One or more results

(3) Local and global variables

     #1: Local variables and global variables are different variables . Can be renamed ; At the end of the function operation , Local variables are released ; It can be done by global Reserved words use global variables within functions

    #2: The local variable is a composite data type and has not been created , Equal to global variable


本文为[Why_ does_ it_ work]所创,转载请带上原文链接,感谢

  1. Python爬虫编程思想(85):在Python中使用非关系型数据库
  2. Volume de réglage audio Python (avec Code) | outils Python
  3. Python crawler programming idea (59): get attribute value and text with beautiful soup CSS selector
  4. Python crawler programming idea (57): basic usage of CSS selector in beautiful soup
  5. Python crawler programming idea (55): find of beautiful soup method selector_ All method
  6. Python crawler programming idea (54): use beautiful soup to select sibling nodes
  7. Python crawler programming idea (53): use beautiful soup to select the parent node
  8. Django3.0 solves the problem of error reporting in reverse parsing
  9. Precautions for Python crawler
  10. Python 3 crawler series (1) -- climbing blind date websites
  11. Python到底是什么?为什么要学Python?
  12. #yyds干货盘点#Pandas数据清洗实用指南
  13. Python打包exe文件无法运行
  14. Two common ways to save files in Python
  15. #yyds幹貨盤點#Pandas數據清洗實用指南
  16. Yyds Dry Inventory pandas Data Cleaning Practical Guide
  17. PYTHON用LSTM长短期记忆神经网络的参数优化方法预测时间序列洗发水销售数据
  18. Python集成学习:自己编写构建AdaBoost分类模型可视化决策边界及sklearn包调用比较
  19. Python 3 makes a search software
  20. Python 3 simulated microblog login
  21. Using Python 3 to make practical software for drawing modification
  22. About HTML (acceptable to Python)
  23. Python集成學習:自己編寫構建AdaBoost分類模型可視化决策邊界及sklearn包調用比較
  24. PYTHON用LSTM長短期記憶神經網絡的參數優化方法預測時間序列洗發水銷售數據
  25. Python Integrated Learning: Writing and Constructing adaboost Classification Model Visualized decision Boundary and sklearn package Calling Comparison
  26. Python prédit les données de vente de shampooing de séries chronologiques en utilisant la méthode d'optimisation des paramètres du réseau neuronal de mémoire à court et à long terme lstm
  27. [zero basics of Python to introduction] a prerequisite for Python preparatory knowledge -- basic coding specification of Python
  28. OpenCV对比度亮度变换竟能用来去水印(附Python/C++源码)
  29. [zero basics of Python to getting started] a prerequisite for Python preparatory knowledge -- installing the visualization tool pycharm
  30. The test modifies in micro python
  31. Microphoton experimental circuit board based on mm32f3273 - does not work normally
  32. Run micropathon on mm32f3273 to test performance
  33. Design mm32f3277 micro Python experimental board with SD card
  34. Mm32f3277 corresponding interface files during microphoton migration
  35. Mm32f3277 microphoton experimental board design and software testing
  36. Making and testing mm32f3277 microphoton minimum circuit board
  37. Download mm32-link program automatically with Python simulated mouse
  38. A curriculum of "artificial intelligence Python machine learning and deep learning"
  39. Test the basic functions of mm32 microphoton test circuit board
  40. Test the basic functions of the mm32f3277 micro Python development board flying one by one
  41. Debugging mm32f3277 from zhufei, transplanted with micro Python development board
  42. Recognizing numbers using OpenCV and python
  43. 真的太香了,Python速查表终于出中文版了
  44. Django REST Framework(DRF)教程:快速入门
  45. python django objects filter过滤查询:startswith year
  46. Data encapsulation of Excel test cases in Python
  47. 学员管理系统python
  48. 肝了一夜,8000字概括精髓,pandas必知必会50例!
  49. Python竟然可以画漫画!漫画版的故宫导游图,来袭!
  50. Networkx graph theory Dijkstra algorithm shortest path implementation, Python
  51. Draw a HelloWorld with Python turtle Library
  52. 运行django的服务器内存使用偶尔跳高一点,重启服务马上下来,是内存泄露吧
  53. Basic usage of Python tqdm module in machine learning training
  54. Python -- basic usage method and basic template of argparse module
  55. python-sockertserver并发编程思想
  56. Programmation simultanée du serveur de sockets Python
  57. Vue+Django 旅游网项目 首页前端实现
  58. Classic usage of pandas: iloc and LOC of data filtering
  59. Calendar module of Python time series
  60. A super easy to use Python standard library. It's great to operate directories and files