基本语法
不需要转换符
//int--整形float--浮点型str--字符型bool--布尔型
1 int float str
a=10 //inta=10.23 //floata="hello" //str
2 bool True和False首字母要大写
a=True
3 查看数据类型
type(a) //输出一个数据的类型
数据类型之间的转换
其他类型转化为字符类型
a=10b=str(a)print(type(b))
其他类型转化为int()—-只能是整数
a="10"b=int(a)print(type(b))
其他类型转化为float
a="10.26"b=float(a)print(type(b))
常量多个变量赋值
常量
- python中没有常量
 - 常量就是声明之后不能改变的
 - javascript中使用const声明的就叫常量
 
//约定常量所有字母多要大写GET=10
给多个变量赋值
a,b,c=1,2,3print(c)
字符串的运算
1.乘法
a="hello"*3print(a) //hellohellohello
2.读取字符串中的某一位
a="hello"print(a[1]) //e
3.截取字符串中的某一段
a="hello"print(a[1:]) //ello 截取第一位之后的所有的字符print(a[1:3]) //el 不包含第三位
4.format拼接字符串
a="http://www.baidu.com/{}"url=a.format("223")print(url) // http://www.baidu.com/223
数组—列表list
1.len()—获取长度
arr=[1,2,3]print(type(arr)) //listprint(len(arr)) //3
2 读取数组的某一段
arr=[1,2,3]print(arr[1:]) //[2,3]
3 数组相加
a=[1,2,3]b=[4,5]c=a+bprint(c) //[1, 2, 3, 4, 5]
4 乘法
b=[4,5]print(b*2) //[4,5,4,5]
5 遍历
arr=['html','javascript','vue']for key in arr:print(key)
6 方法
增加
//向后增加--append()arr=['html','css','react']arr.append("vue") //从后面添加print(arr) //['html', 'css', 'react','vue']
//定点增加--insert()arr=['html','css','react']arr.insert(1,"vue") //['html', 'vue', 'css', 'react']print(arr)
删除
//remove()arr=['html','css','react']arr.remove("css")print(arr)
index()—查找下标
arr=['html','css','react','javascript']print(arr.index('css')) //1
7 元祖
arr=(1,2,3)print(type(arr)) //<class 'tuple'>
8 集合
集合特点:里面的值不能重复,是无序的
arr={1,2,3,4,1}print(arr) //{1, 2, 3, 4}
1. 并集
a={1,2,3,4}b={2,3,5}print(a|b) //{1, 2, 3, 4, 5}
2. 交集
a={1,2,3,4}b={2,3,5}print(a&b) //{2, 3}
3. 差集
a={1,2,3,4}b={2,3,5}print(a-b) //减去前一个相同的部分 {1, 4}
4. 方法
因为集合是无序的,所有对集合进行增删改查的时候,是无序的,排序不唯一
4-1 添加 add() update()
add()
arr={'html','css'}arr.add("js")print(arr) ////{'html', 'js', 'css'}
update()
arr={'html','css','react'}arr.update({"vue","angular"})print(arr) //{'html', 'vue', 'react', 'angular', 'css'}
4-2 删除 pop() remove() clear()
pop()
arr={'html','css','react'}arr.pop()print(arr) //{'html', 'react'}
remove()
arr={'html','css','react'}arr.remove('html')print(arr) //{'css', 'react'}
clear() //清空
arr={'html','css','react'}arr.clear()print(arr) //set()
9 字典和函数
字典相当于obj
obj={"name":"xiang","age":19}print(type(obj)) //<class 'dict'>print(obj["name"]) //xiang
函数
定义一个函数
def test():print("a")test() //adef test():return "hello world" //有返回值的函数print(test()) //hello world
默认参数
def test(a=2,b=3):print(a+b)test() //5test(b=6) //8 对b的值修改过够再执行操作
10 逻辑运算和For
逻辑运算
1 or 逻辑或 满足一个条件即可
age=2flag=Trueif age>18 or flag:print("disco dancing")else:print("home") //disco dancing
2 and 逻辑与条件都要同时满足
age=2flag=Trueif age>18 and flag:print("disco dancing")else:print("home") //home
3 逻辑非
age=2flag=Falseif not flag: //取反操作print("home") //home
For
for value in range(0,5):print(value) //0 1 2 3 4
continue
for value in range(0,5):if(value==3):continueprint(value) //0 1 2 4
break
for value in range(0,5):if(value==3):breakprint(value) //0 1 2
