基础
数和表达式
- 整除:
1 // 2
(0) - 取模:
10%-3
(-2)原因是python整数除法是向下取圆整(10/-3 == -3.333… 向数轴负方向取整得到-4) - 乘方:
2 ** 3
(8) - 十六进制:
0xAF
, 八进制:010
, 二进制:0b101010
模块
import math
math.round //圆整
math.floor
math.ceil
from math import sqrt
sqrt(10)
海龟绘图
from turtle import *
forward(100)
left(120)
penup()
pendown()
直接运行python脚本
首行添加:
#!/usr/bin/env python
将其变成可执行的
chmod a+x hello.py
字符串
连续的字符串会被拼接:
"let's say" '"hello word!"'
str (类型)展示用户能看懂的字符串, repr(函数)展示合法Python表达式表示
>>> s = "Hello \nworld"
>>> print(s)
Hello
world
>>> print(repr(s))
'Hello \nworld'
>>> print(str(s))
Hello
world
用三个单引号或双引号表示长字符串, 可以在中间换行
用反斜杠’\’可跨行书写语句
原始字符串
r'c:\fold\file'
r'abc\'ed'
# 引号需要转义, 但执行转义的反斜杠依旧会被包含在实际的字符串中, 最终字符串为: abc\'ed
r'c:\fold\file' '\\'
# 原始字符串结尾不能是\
Unicode(P18)
"Hello".encode("UTF-8")
# UTF-8 UTF-32 ASCII
# 第二个参数可以在使用ASCII编码时候, 但有字符没有对应的ASCII码时, 如何处理("ignore", "replace", "backslashreplace", "xmlcharrefreplace")