python学习/2021/1/1_day1_zhou125disorder

python注释

单行注释
#这是单行注释
多行注释
‘’’ 这是多行注释 ‘’’

在IDLE中输出全部的关键字—>>>!在这里插入图片描述

help()--->回车
help>keywords--->回车

退出help模式

quit--->>>回车

在IDLE中使用海龟画图画一个奥运五环

#引进海龟画图
import turtle
turtle.width(10)
turtle.color("#096")
turtle.circle(49)
turtle.penup()
turtle.goto(60,0)
turtle.pendown()
turtle.color("#ddd")
turtle.circle(49)
turtle.penup()
turtle.goto(120,0)
turtle.pendown()
turtle.color("#242424")
turtle.circle(49)
turtle.penup()
turtle.goto(30,-60)
turtle.pendown()
turtle.color("red")
turtle.circle(49)
turtle.penup()
turtle.goto(90,-60)
turtle.pendown()
turtle.color("#717")
turtle.circle(49)

python快捷键

Ctrl+F6 重启 shell,以前定义的变量全部失效
 F1 打开帮助文档 
Alt+/ 自动补全前面曾经出现过的单词  
(Ctrl + [)or(Ctrl + ]) 缩进代码和取消缩进   Alt+M
打开模块代码,先选中模块,然后按下此快捷键,会帮你 打开改模块的 py 源码供浏览   
Alt+C 打开类浏览器,方便在源码文件中的各个方法体之间切换  
F5 运行程序
-------------------------------------------------------------
## del

```python
>>> a=1
>>> a 1
>>> del a
>>> a Traceback (most recent call last):   File "<pyshell#242>", line 1, in <module>
a NameError: name 'a' is not defined

换行

>>> a='123456789'
>>> a
'123456789'
>>> a='123\
456789'
>>> a
'123456789'
>>> 
>>> a=[1,2,3,4,5,6]
>>> a
[1, 2, 3, 4, 5, 6]
>>> a=[1,2,3\
   ,4,5,6]
>>> a
[1, 2, 3, 4, 5, 6]
>>> 

数字和基本运算符

/ 浮点数除法 8/2=4.0
// 整数除法 6//4=1
% 模(取余 8%3=2
** 幂 2**4 =16

<<, >> (移位)

1. 数左移相当于乘以二
>>> 4<<1
8
>>> 4<<2
16

2. 数右移相当于除以二
>>> 8>>1
4
>>> 8>>2
2

| ^ & 按位或,按位异或、按位与

&:按位与操作,只有 1 &1 为1,其他情况为0。可用于进位运算.
|:按位或操作,只有 0|0为0,其他情况为1.
^:异或,相同为0,相异为1。可用于加操作(不包括进位项).

divmod函数

使用 divmod()函数同时得到商和余数:—>>> divmod(12,4) (3, 0);
int()转换成整型
float()转换成浮点型
round()转换成四舍五入型

获取时间


>>> import time
>>> time.time()
1609505390.9070842
>>> 

此时间是从“1970 年 1 月 1 日 00:00:00”开始计算的,以毫秒(1/1000 秒) 进行计算,到现在的所经过的时间.

逻辑运算符

or 逻辑或 x or y x 为 true,则不计算 y,直接返回 true x 为 false,则返回 y and 逻辑与 x and
y x 为 true,则返回 y 的值 x 为 false,则不计算 y,直接返回 false not 逻辑非 not x x 为
true,返回 false x 为 false,返回 true

同一运算符

is (is 是判断两个标识符是不是引用同一个对象)
is not (is not 是判断两个标识符是不是引用不同对象)

整数缓存问题

Python 仅仅对比较小的整数对象进行缓存(范围为[-5, 256])缓存起来,而并非是所有整数对 象。
需要注意的是,这仅仅是在命令行中执行,而在 Pycharm 或者保存为文件执行,结果是不一样 的,
这是因为解释器做了一部分优化(范围是[-5,任意正整数])。

字符串的编码

使用内置函数 ord()可以把字符转换成对应的 Unicode 码;
使用内置函数 chr()可以把十进制数字转换成对应的字符.

>>> ord("周")
21608
>>> chr(21608)
'周'
>>> 

空标题字符串和len()

>>> a=''
>>> len(a)
0

len()用于计算字符串有多少字符

>>> a="我的愿望是世界和平"
>>> len(a)
9

转义字符

\(在行尾时) 续行符
\\ 反斜杠符号
\’ 单引号
\" 双引号
\b 退格(Backspace)
\n 换行
\t 横向制表符
\r 回车

不换行打印

print("我是",end='1') print("一个",end='2') print("大帅逼")
>>>我是1一个2大帅逼

从控制台读取字符串

>>> myname = input("请输入名字:")
 请输入名字:卡卡西 
 >>> myname '卡卡西'

猜你喜欢

转载自blog.csdn.net/ZHOU125disorder/article/details/112062318