Translation: practical Python Programming 02_ 01_ Datatypes

itread01 2021-02-22 12:01:47
translation practical python programming 02_


[ Catalog ](https://github.com/codists/practical-python-zh/blob/main/Notes/Contents.md) \| [ Last section (1.7 Function )](https://www.cnblogs.com/codists/p/14415553.html) \| [ Next section (2.2 Containers )](02_Containers.md)# 2.1 Data types and data structures this section introduces data structures represented by tuples and dictionaries .### Source data type Python There are some primitive data types :* Integers * Floating point numbers * String ( written words )### Empty type ```pythonemail_address = None````None` Often used as a place holder for optional or missing values . It evaluates to... In a conditional statement `False`.```pythonif email_address: send_email(email_address, msg)```### Data structure the actual program has more complex data . for example , Information about the holding of shares :```code100 shares of GOOG at $490.10``` This is a three part “ thing ”:* The name or symbol of a stock ("GOOG", String )* Number of shares (100, Integers )* Price (490.10, Floating point numbers )### A tuple is a collection of values grouped together . Example :```pythons = ('GOOG', 100, 490.1)``` Sometimes it's grammatically omitted `()` .```pythons = 'GOOG', 100, 490.1``` Special circumstances (0 Tuple ,1 Tuple ).```pythont = () # An empty tuplew = ('GOOG', ) # A 1-item tuple``` Tuples are generally used to represent simple records or structures . Usually , It's a single object made up of multiple parts . There's a good analogy : Tuples are like a row in a database table . The contents of tuples are ordered ( Similar to an array ).```pythons = ('GOOG', 100, 490.1)name = s[0] # 'GOOG'shares = s[1] # 100price = s[2] # 490.1``` however , The contents of tuples cannot be modified .```python>>> s[1] = 75TypeError: object does not support item assignment``` You can create a new metagroup based on the current tuple .```pythons = (s[0], 75, s[2])```### Tuple packing tuples are more about packing related items into an entity (entity) in .```pythons = ('GOOG', 100, 490.1)``` And then , The tuple is easily passed as a single object to the rest of the program .### Tuple unpacking uses tuples elsewhere , You can unpack parts of a tuple into variables .```pythonname, shares, price = sprint('Cost', shares * price)``` The number of variables on the left must match the structure of the tuple .```pythonname, shares = s # ERRORTraceback (most recent call last):...ValueError: too many values to unpack```### Tuples and lists tuples look like read-only lists . however , Tuples are most commonly used for single items that consist of multiple parts . A list is usually a collection of items of the same type ,```pythonrecord = ('GOOG', 100, 490.1) # A tuple representing a record in a portfoliosymbols = [ 'GOOG', 'AAPL', 'IBM' ] # A List representing three stock symbols```### A dictionary is a key to value mapping . Sometimes , Dictionaries are also called hash tables (hash table) Or associative arrays (associative array). The key is used as an index to access the value .```pythons = { 'name': 'GOOG', 'shares': 100, 'price': 490.1}```### The common operation is to get the value from the dictionary , Please use key name .```python>>> print(s['name'], s['shares'])GOOG 100>>> s['price']490.10>>>``` To add or modify values , Please use key name to assign .```python>>> s['shares'] = 75>>> s['date'] = '6/6/2007'>>>``` To delete a value , Please use `del` Sentence .```python>>> del s['date']>>>```### Why use a dictionary ? When there are many different values and it is possible to modify or manipulate them , Dictionaries are very useful . Dictionaries make code more readable .```pythons['price']# vss[2]```## Practice in the last few exercises , I've compiled a data access file `Data/portfolio.csv` The program . Use `csv` Module , It's easy to read files line by line .```python>>> import csv>>> f = open('Data/portfolio.csv')>>> rows = csv.reader(f)>>> next(rows)['name', 'shares', 'price']>>> row = next(rows)>>> row['AA', '100', '32.20']>>>``` Although it's easy to read files , But compared to reading data , Usually use data to do more . for example , Maybe you want to store it and perform some calculations on it . Unfortunately , Original information “ That's ok ” It can't be done . for example , Not even simple mathematical calculations .```python>>> row = ['AA', '100', '32.20']>>> cost = row[1] * row[2]Traceback (most recent call last):
版权声明
本文为[itread01]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/02/20210221122430803q.html

  1. 27000 stars! The most comprehensive collection of Python design patterns
  2. python day3
  3. python day3
  4. Commonly used data operation functions of Python
  5. (数据科学学习手札108)Python+Dash快速web应用开发——静态部件篇(上)
  6. (learning notes of data science 108) Python + dash rapid web application development -- static components (I)
  7. (数据科学学习手札108)Python+Dash快速web应用开发——静态部件篇(上)
  8. (learning notes of data science 108) Python + dash rapid web application development -- static components (I)
  9. [Python] Matplotlib 图表的绘制和美化技巧
  10. Drawing and beautifying skills of [Python] Matplotlib chart
  11. [Python] Matplotlib 图表的绘制和美化技巧
  12. Drawing and beautifying skills of [Python] Matplotlib chart
  13. Virtual environment of Python project
  14. 翻译:《实用的Python编程》02_01_Datatypes
  15. Translation: practical Python Programming 02_ 01_ Datatypes
  16. 翻译:《实用的Python编程》02_01_Datatypes
  17. 翻译:《实用的Python编程》02_01_Datatypes
  18. Translation: practical Python Programming 02_ 01_ Datatypes
  19. Translation: practical Python Programming 02_ 01_ Datatypes
  20. Python 3 入门,看这篇就够了
  21. Python 3 entry, see this is enough
  22. 华为大佬打造的400集Python视频学起来,学完万物皆可爬
  23. 400 episodes of Python video created by Huawei boss
  24. django之csrf_exempt解决跨域请求的问题
  25. CSRF of Django_ Exempt solves the problem of cross domain requests
  26. 1.7 万 Star!一个简单实用的 Python 进度条库
  27. 17000 stars! A simple and practical Python progress bar library
  28. Python爬虫:设置Cookie解决网站拦截并爬取蚂蚁短租
  29. Python crawler: setting cookie to solve website interception and crawling ant short rent
  30. Python-Net编程
  31. Python net programming
  32. 学习Python数学英语基础重要吗?Python教程!
  33. Is it important to learn the basics of math and English in Python!
  34. Python数据分析常用库有哪些?Python学习!
  35. What are the common libraries for Python data analysis? Learn Python!
  36. win 创建python虚拟环境
  37. Creating Python virtual environment with win
  38. In order to automatically collect B station barrage, I developed a tool in Python
  39. 用Python编程语言来实现阿姆斯特朗数的检查
  40. Using python programming language to check Armstrong number
  41. Python中的解决中文字符编码的问题
  42. Solving the problem of Chinese character coding in Python
  43. Translation: practical Python Programming 02_ 01_ Datatypes
  44. Installation and use of Python and tensorflow in win10 environment (Python version 3.6, tensorflow version 1.6)
  45. Python series 46
  46. Linux安装Python3
  47. 【python接口自动化】- 正则用例参数化
  48. Python RestFul Api 设计
  49. filecmp --- 文件及目录的比较│Python标准库
  50. Installing python3 on Linux
  51. [Python] Matplotlib 圖表的繪製和美化技巧
  52. (資料科學學習手札108)Python+Dash快速web應用開發——靜態部件篇(上)
  53. 翻譯:《實用的Python程式設計》02_01_Datatypes
  54. 【python接口自动化】- 正则用例参数化
  55. 翻译:《实用的Python编程》02_02_Containers
  56. 两年Java,去字节跳动写Python和Go
  57. [Python interface automation] - regular use case parameterization
  58. Python restful API design
  59. 翻译:《实用的Python编程》02_02_Containers
  60. 两年Java,去字节跳动写Python和Go