设为首页 加入收藏

TOP

Python官方文档学习笔记(一)
2023-07-23 13:43:50 】 浏览:24
Tags:Python 方文档 习笔记

原文:https://docs.python.org/3/tutorial/introduction.html
版本:3.11.2

Using Python as a Calculator

Numbers

  • Division (/) always returns a float.
  • To do floor division and get an integer result you can use the // operator;
  • To calculate the remainder you can use %
  • With Python, it is possible to use the ** operator to calculate powers
4/2
Out[2]: 2.0
4//2
Out[3]: 2
5 % 2
Out[4]: 1
2 ** 3
Out[5]: 8
  • In interactive mode, the last printed expression is assigned to the variable _. This means that when you are using Python as a desk calculator, it is somewhat easier to continue calculations, for example:
>>> 4/2
2.0
>>> _ + 1
3.0

Strings

  • \ can be used to escape quotes 转义字符: \ !
>>> "doesn\'t"
"doesn't"
>>> '"doesn\'t"'
'"doesn\'t"'
  • ''单引号内套双引号,可以使转义字符失效;但print()会删掉外侧的单引号,又使得转义字符生效
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
>>> print('"Isn\'t," they said.')
"Isn't," they said.
  • Raw strings
>>> print('C:\some\name')  # \n被识别为换行符
C:\some
ame
>>> print(r'C:\some\name')  # 在括号外面加上r可以打印出原字符
C:\some\name
>>> print("""\
... Usage: thingy [OPTIONS]
...      -h                        Display this usage message
...      -H hostname               Hostname to connect to
... """)
Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to

>>> print("""\
    Usage: thingy [OPTIONS]
         -h                        Display this usage message
         -H hostname               Hostname to connect to
    \""")

Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to

  • Python strings cannot be changed — they are immutable.
    如果需要更改字符串,应该使其赋值等于一个新的变量。

Lists

  • All slice operations return a new list containing the requested elements. This means that the slice returns a shallow copy of the list.
    对列表的切片会返回一个原列表的浅复制
  • Unlike strings, which are immutable, lists are a mutable type

First Steps Towards Programming

  • 非零整数的布尔值为真,零为False。
  • 长度不为零的Sequence对象为True,空的sequence为False。

More Control Flow Tools

  • for Statements
    Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.
    对一个sequence循环时,如果还需要对sequence本身进行修改,最好对sequence的copy进行循环
# Create a sample collection
users = {'Hans': 'active', 'Éléonore': 'inactive', '景太郎': 'active'}

# Strategy:  对sequence本身循环
for user, status in users.items():
    if status == 'inactive':
        del users[user]
# 上述循环也可以正常返回删除inactive后的字典。

# Strategy:  Iterate over a copy
for user, status in users.copy().items():
    if status == 'inactive':
        del users[user]

# Strategy:  Create a new collection
active_users = {}
for user, status in users.items():
    if status == 'active':
        active_users[user] = status

上述例子中,如果直接对users本身循环,则默认是对key循环,所以不能添加两个参数。同时对key和value循环,必须得是对users.items()

  • break and continue Statements
    break结束最内侧的循环,continue直接开始下一次循环
  • match Statements (released in python3.10)
    Only the first pattern that matches gets executed and it can also extract components (sequence elements or object attributes) from the value into variables.
def http_error(status):
    match status:
        case 400:
            return "Bad request"
        case 404:
            return "Not found"
        case 418:
            return "I'm a teapot"
        case _:
            return "Something's wrong wi
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Django笔记十之values_list指定字.. 下一篇PyQt5学习 (5)--QPushButton、Q..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目