美文网首页
python常识(一)

python常识(一)

作者: Brucezen | 来源:发表于2020-11-11 01:01 被阅读0次

声明文件所使用编码

# -*- coding: encoding -*-
# -*- coding: utf-8 -*-

计算乘方使用**,比如5**2表示5的平方

在交互模式下,上一次打印出来的表达式被赋值给变量 _

>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06

字符串可以用+拼接, 也可以用*重复

>>> # 3 times 'un', followed by 'ium'
>>> 3 * 'un' + 'ium'
'unununium'

相邻的两个或多个 字符串字面值 (引号引起来的字符)将会自动连接到一起. 把很长的字符串拆开分别输入的时候尤其有用:

>>> 'Py' 'thon'
'Python'
>>> text = ('Put several strings within parentheses '
...         'to have them joined together.')
>>> text
'Put several strings within parentheses to have them joined together.'

使用过大的索引会产生一个IndexError错误,但是,切片中的越界索引会被自动处理

>>> word = 'Python'
>>> word[42]  # the word only has 6 characters
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> word[4:42]
'on'
>>> word[42:]
''

在列表中,给切片赋值也是可以的,这样甚至可以改变列表大小,或者把列表整个清空:

>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> # replace some values
>>> letters[2:5] = ['C', 'D', 'E']
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>> # now remove them
>>> letters[2:5] = []
>>> letters
['a', 'b', 'f', 'g']
>>> # clear the list by replacing all the elements with an empty list
>>> letters[:] = []
>>> letters
[]

**print(\*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
将 objects 打印到 file 指定的文本流,以 sep 分隔并在末尾加上 end。 sep, end, file 和 flush 如果存在,它们必须以关键字参数的形式给出. **

enumerate(iterable, start=0)
返回一个枚举对象。
iterable
必须是一个序列,或 iterator或其他支持迭代的对象。enumerate()返回的迭代器的 __next__()方法返回一个元组,里面包含一个计数值(从 start 开始,默认为 0)和通过迭代 iterable 获得的值。
**

>>>seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>>list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>>list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]

等价于:

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1

** sum()是接受可迭代对象为参数的函数**

>>> sum(range(4))  # 0 + 1 + 2 + 3
6

官方文档看第4.4(breakcontinue语句,以及循环中的 else子句)

相关文章

  • 6.Python简介与语法常识

    Python简介与语法常识 本文章将简单介绍Python的发展历史和语言特点、语法常识。熟知这些语法常识、语法特点...

  • python常识(一)

    声明文件所使用编码 计算乘方使用**,比如5**2表示5的平方 在交互模式下,上一次打印出来的表达式被赋值给变量 ...

  • Python相关文章索引(11)

    基本常识 python 使用set对列表去重,并保持列表原来顺序 python基础-python函数any()与a...

  • Python第一课

    学习重点:Python概述、计算机常识、编程语言、搭建Python开发环境、Python程序的编辑和执行、Pyth...

  • Python相关文章索引(12)

    基本常识 Python多级排序(多属性排序)csv文件 python re的findall和finditer py...

  • python的30个冷知识

    目录 基本常识 数据结构部分 输入输出函数部分 基本常识 Python没有switch/ case 语句。 如何中...

  • Python相关文章索引(14)

    基本常识 python中的左位移和右位移 利用Python在一个文件的头部插入数据 Debug xpath中遇到<...

  • Python相关文章索引(13)

    基本常识 Python-去除字符串中不想要的字符 Python split()方法 strip()默认去除的空白字...

  • 一篇文章学习 Python 网络爬虫

    一、爬虫开发基础 爬虫基础分为 Python 基础,网页常识和网页分析三部分。 学习爬虫需要有简单的 Python...

  • Python for Windows 中文编码汇总

    关于python编码的基本常识在python里面 "明文"是unicode类型 "密文"是其他的编码格式 如gbk...

网友评论

      本文标题:python常识(一)

      本文链接:https://www.haomeiwen.com/subject/meisbktx.html