前言
最适用于蓝桥杯,仅含笔者认为关键的部分!
编程基础
python开发环境
目前笔者使用的是pycharm
python输入输出
print函数
print(*objects,sep=' ',end='\n')
默认以空格间隔,结尾换行\n
print('Hello world')
print('Hello world',end='2')
print('Hello world')
print('Hello world','3',sep='+')
print('Hello world''3',sep='+')
print('22''33')
print('22','33')
input函数
input([prompt提示内容])
格式转换函数
int()
str()
float()
bool()
eg:a=int(input())
type函数
type()
eg:print(type(a))
常量、变量、运算符
变量命名
和C语言一样
可以由数字,字母,下划线组成
开头不能是数字,只能是字母,下划线
不能使用保留字关键字
基础数据结构
int
float
str
bool(非0true 0false)
数据结构转化
float->int 舍去小数
int->bool 非0ture 0false
bool->int true->1 false->0
注意str->bool 全是1true
基础运算符
/(除法)
//(整除)
%(求余数)
**(幂)(不用pow了耶)
and(C的&&)
or(C的||)
not(C的!)
高级一点的运算符
in / not in成员运算符
is / is not身份运算符
程序结构之选择结构
条件表达式
>,<,>=,<=,==,!=
逻辑表达式
前面提过的and or not
比较方向
比C智能很多,可以一列比下去
好玩的套娃
a='xx'由于非空str默认为true
print(print(a))
得到内层的xx
外层的None(此时里面print完已经空了无返回)
and or的运算规则
and要看完整
or只要一个行就停
print(4 and 3)得到3
print(4 or 3)得到4
print(False or print(a))
or前面不可以就看后面,等价于print(print(a))
得到内层的xx和外层的None
print(False and print(a))
and前面不可以不看后面,等价于print(False)得到False
换成True或者其他非0数或者非空str
print(1 or print(a))
or只要前面可以就输出,得到1
print(1 and print(a))
and看后面,得里面的xx和外面后面的None
if
if xxx:
xxx
elif xxx:
xxx
elif xxx:
xxx
else:
xxx
程序结构之循环结构
range函数
range(start,stop,step)用于生成一个范围的数字(专业名叫可迭代的对象,或者可遍历的对象),用于遍历,计数
注意:不含stop的数字,步长step默认为1,负数的step是从大往小数;只输入一个数字默认是stop
range(4,-4,-2)
得到4,2,0,-2
range(3)得到0,1,2
for
for<循环变量> in <sequence>:
xxx
sequence:序列(后面的数据结构?)或可以迭代的对象(比如range())
eg:for i in range(3):
while
while xxx:
xxx
循环控制(跟C一样)
break:可以跳出当前该层循环(一次只能跳出一层(不论该层里面有没有别的结构))
continue:跳过本次循环
若要跳出多重循环,可以设置一个跳出循环的标记,跳出时每层判断一遍依次放行跳出
循环嵌套
和C一样,套就完了