1.运算符
(1)算术运算符:
+ - * / % ** //
= (赋值)
(2)成员运算符:
in作用:判断子字符串在不在某个里面
if "a" in name:
print("OK")
else:
print("ERROR")
扩展:not in
if "文" not in "建文帝":
print("xx")
else:
print("yy")
扩展2:
v = "文" not in "建文帝"
布尔值: True False
(3)比较运算符:
== > < >= <=
!= <> (都是不等于)
not
(4)逻辑运算符:
and or ()
优先级和C语言一样;
简化写法:
count += 1
count *= 1
这个也类似C语言,省略写法
2.基本数据类型
说明:每种数据类型有自己的方法; (int,ctrl + 鼠标单击这个类型名字,可以查看其对应的方法)
类型:整型 字符串 列表 元祖 字典 布尔值
int str list tuple dict bool
(1)数字 int
转化类型:
a = “123”
b = int(a) (转化为数字了)
type(a) (可以获取数据类型)
b = int(a, base=16) (指定转化为多少进制的,默认是转化为十进制)
(2)字符串 str
test = "alex name"
用法 功能
test.center(20,"*") (总长20,中间放字符串,空白用*填充,默认不填写为空白)
test.count('ex') (字符串中寻找子序列出现的次数,可设置起始和结束位置)
test.endwith('a') (判断是不是以 什么 结尾的,可设置位置,返回bool类型)
test.startwith('b') (判断是不是以 什么 开始的)
test.find('ex') (找到第一个,获取位置,返回,同事可设置查找起始和结束位置)
test.format(name='aaa') (格式化,替换) (还可以替换数字,从0开始,0,1,2 依次替换)
test.format_map({'name':'aaa', 'a':19}) (和上面一个类似,格式不一样)
test.index() (类似find,但是这个找不到会报错,不如用find)
test.isalnum() (判断字符串中,是不是只包含字母和数字,是返回True)