一.基本知识
1.vs stdio交互式python
按下 ctrl+enther
2.运算
指数 ______________________ ** _______ 2**3 = 8
取模/取余数_________________% ________ 22%8=6
整除/商数取整_______________ // __________ 22//8 = 2
除法 _____________________ / ____________ 22/8 = 2.75
乘法 _____________________ * ____________ 2*3 = 6
减法 _____________________ - ____________ 2-3 = -1
加法 _____________________ + ____________ 2+3 = 5
3.基本数据类型
整型
-2, -1, 0, 1, 2, 3, 4, 5
浮点型
-1.25, -1.0, - -0.5, 0.0, 0.5, 1.0, 1.25
字符串
‘a’, ‘aa’, ‘aaa’, ‘Hello!’, ‘11 cats’
4.字符串的连接和复制
>>> 'bat' + 'man'
'batman'
在用于两个字符串时,它将字符串连接起来,成为“字符串连接”操作符。
错误示范
>>> 'batman' +21
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
>>
*操作符只能用于两个数字(作为乘法),或一个字符串和一个整型(作为字符
串复制操作符)。
>>> 'bat' * 'man'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'str'
>>>
>>> 'bat' * 5.0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'
>>>
变量名
1.只能是一个词。
2.只能包含字母、数字和下划线。
3.不能以数字开头。
基本函数
print()
input()
len()
str(),int(),float()
二.控制流
1.布尔值
True
Flase
2.比较操作符
等于 ———————— ==
不等于 ——————— !=
小于 _______________ <
大于 _______________ >
小于等于 ___________ <=
大于等于 ___________ >=
“比较操作符”比较两个值,求值为一个布尔值
==和!=操作符实际上可以用于所有数据类型的值。
>>> 42 == 42
True
>>> 42 > 43
False
>>> 42 != '42'
True
3.布尔操作符
1.二元布尔操作符
and 和 or 操作符总是接受两个布尔值(或表达式),所以它们被认为是“二元”
操作符
2.not操作符
not 操作符只作用于一个布尔值(或表达式)。not 操作符求值为相反的布尔值。
>>> not True
False
>>> not not not not True
True
4.控制流语句
if
else
elif
while
break
continue
for循环和range()
print('My name is')
for i in range(5):
print('Jimmy Five Times (' + str(i) + ')')
>>>
My name is
Jimmy Five Times (0)
Jimmy Five Times (1)
Jimmy Five Times (2)
Jimmy Five Times (3)
Jimmy Five Times (4)
等价的while循环
做类似for循环的事
print('My name is')
i = 0
while i < 5:
print('Jimmy Five Times (' + str(i) + ')')
i = i + 1
range()的开始、停止、步长参数
#开始到截止
for i in range(12, 16):
print(i)
>>>
12
13
14
15
#步长2
for i in range(0, 10, 2):
print(i)
>>>
0
2
4
6
8
三.模块
import
import 关键字;
模块的名称;
可选的更多模块名称,之间用逗号隔开。
from import
例如 from random import *
用 sys.exit()提前结束程序
import sys
while True:
print('Type exit to exit.')
response = input()
if response == 'exit':
sys.exit()
print('You typed ' + response + '.')
python编程-让繁琐的工作自动化