Anaconda
官网下载(Windows下载64位)
百度Anaconda官网下载
1.滑到最下面点get started
2.再点Download Anaconda installers
3.该页面往下滑找到相应的系统版本下载并安装,这里是Windows64位
基本类型
例子:
#### 多少个人
# 100 # int整数类型
# type(100) ## type查看数据类型的接口
# 100.1 ## float 浮点数类型
type(100.1)
# '张三'
# "张三" ,推荐这种方式
# """张三"""
# 'what's wrong',错误
# "what's wrong", 正确
# """what's worng""", 正确
## 表示字符串,推荐用"张三"表示,字符串是str类型
type("张三")
# type(True), # boolean 布尔类型
False
# type(None), # NoneType缺失值类型
变量的定义:
举例:
a = 100 # 把100赋值给变量a
a = "张三"
# a = True
print(a) #print把传入的东西打印出来
id(a) # id把传入的变量所指向的值的内存地址打印出来
#a = 100
#b = "张三"
a, b = 100, "张三" ## 多变量赋值写法
print(a)
print(b)
# 2022年1月的结余
income = 30000 # income表示收入
rent_cost = 5000 # rent_cost表示房租水电
food_cost = 2000 # food_cost表示伙食费
other_cost = 1800 # other_cost表示其他开销
print(income - rent_cost - food_cost - other_cost)
# 2022年2月的结余
food_cost = 1200
other_cost = 4000
print(income - rent_cost - food_cost - other_cost)
字符串操作
字符串拼接
求字符串长度
字符串索引和切片
字符串编辑方法(replace,split,join)
字符串查找方法(startwith,endwith,find,rfind,count)
举例:
字符串拼接
## 方法1
print("my name is " + "zhangsan")
## 方法2
name = "zhangsan"
age = 22
height = 1.80
print("my name is %s, my age is %d, my height is %.2f(m)" % (name, age, height)) ## %s是占位符,%s代表字符串,%d代表整数,%f代表浮点数(.2f表示保留小数点后两位)
## 方法3
name = "zhangsan"
age = 22
height = 1.80
print("my name is {}, my age is {}, my height is {}".format(name, age, height))
求字符串长度
## 字符长度
name = "zhangsan320"
print(len(name)) # len()是求长度的函数
字符串索引和切片
## 字符串的索引
name = "zhangsan"
print(name[2]) ## 索引是从0开始的
print(name[len(name)-2])## 表示倒数第二个个字符
print(name[-3])## 表示倒数第三个字符
## 字符串的切片 [start:end],start是包括的,end是不包括的
name = "zhangsan"
print(name[0:3])##切出第一到第三
print(name[1:3])
print(name[2:5])
print(name[:]) # 切出整个字符串
print(name[:-1])
字符串编辑方法(replace,split,join)
# replace,split,join
## replace表示替换
name = "zhangsan"
print(name.replace("san", "250")) ##把第一参数代表字符串,替换为第二个参数代表的字符串
## split表示切割
name = "zhangsan"
print(name.split("zh")) ## 把参数代表的字符串当成切割符,如果前或者后面没有东西,切出来就是空串
a = "my name is zhangsan, my age is 22"
print(a.split(","))
## join表示拼接,跟split相反操作
print("*".join(["my name is zhangsan", "my age is 22"])) ## 把join前面的表示拼接符,然后传入的参数表示需要拼接的序列
字符串查找方法(startswith,endswith,find,rfind,count)
##startwith
name = "zhangsan"
# startwith,是否以参数作为开头
print(name.startswith("zn")) ##False
print(name.startswith("zhan")) ##True
## endwith,是否以参数作为结尾
print(name.endswith("xn")) ##False
#find,返回第一次出现的下标,没有找到就返回-1
print(name.find("an"))
print(name.find("xn"))
print(name.find("gs"))
## rfind,返回最后一次出现的下标,没有找到就返回-1
print(name.find("ax"))
print(name.rfind("gn"))
# count,表示包含参数内容的有多少个
print(name.count("zh"))
运算符:
算术运算符
##算术运算符
a = 5
b = 4
print(a + b) # 加
print(-a)
print(a - b) # 减
print(a * b) # 乘
print(a / b) # a除以b
print(a % b) # 取余
print(a ** 3) # 平方
print(a // b) # 取整
比较运算符
#比较运算符,返回结果是bool类型
a = 5
b = 4
print(a == b)
print((a == b) + 1)
print(int(False)) ## bool转换为int类型
print(int(True)) ## bool转换为int类型
print(a != b)
print(a > b)
print(a < b)
print(a >= b)
print(a <= b)
赋值运算符
# 赋值运算
a = 5
b = 4
#a += b # a = a + b
#a -= b # a = a - b
#a *= b # a = a * b
#a /= b # a = a / b
#a %= b # a = a % b
#a **= 2 # a = a ** 2
a //= b # a = a // b
print(a)
逻辑运算符
# 逻辑运算符,and or not
a = True
b = False
print(a and b) ## and必须要两个都是True,结果才为True
print(a or b) ## or必须至少有个True,结果才为True
print(not b) ## not结果取反就可以
位运算符
# 位运算,&,|,^
a = 2 ## 二进制10
b = 1 ## 二进制01
print(a & b) ## 二进制00,从低位到高位分别按位与
print(a | b) ## 二进制11(3),从低位到高位分别按位或
print(a ^ b) ## 二进制11(3),从低位到高位分别按位亦或
成员运算符
##成员运算符,in,not in
a = "bc"
b = "abc"
c = [1, 2, 3, 4, 5]
print(a in c) ## in表示左边的数是否在右边的序列里面
print(a not in c) ## in表示左边的数是否不在右边的序列里面
运算优先级:
运算符 | 描述 |
---|---|
** | 指数幂 |
+,- | 一元加号减号 |
*,/,%,// | 乘,除,求余数和取整数 |
& | 位运算符 |
^,| | 位运算符 |
<=,<,>,>= | 比较运算符 |
==,!= | 等于运算符 |
=,%=,/=,//=,-=,+=,*=,**= | 赋值运算符 |
in,not in | 成员运算符 |
not,and,or | 逻辑运算符 |
运算优先级由高到低
例子:
# answer = 3 // 2 + (6 > 4) * 5 + (“a” in “abc”)
# answer = 3 // 2 + (True) * 5 + (True)
answer = 3 // 2 + True * 5 + True
answer = 1 + True * 5 + True
answer = 1 + 5 + 1
answer = 7
所以:
answer = 3 // 2 + (6 > 4) * 5 + ("a" in "abc") = 7
条件控制
关键字:if else
if condition(成立的条件):
(缩进)do something1
else:
(缩进)do something2
例子:
if a > b:
print('变量a是大于b的')
else:
print('变量a是不大于b的')
age = 22
if age < 22:
print('未满22岁,禁止访问!')
elif age == 22:
print('现在刚好22岁,再等一年吧!')
else:
print('欢迎访问!')
age = 22
if age < 22:
print('未满22岁,禁止访问!')
#else:
# print('欢迎访问!')
print('hello') ##有if的判断,并不是一定要有else成对
条件判断场景:
值比较-使用比较运算符:
a = 5
b = 4
if a > b:
print('变量a是大于变量b')
else:
print('变量a是不大于变量b')
看结果-调用函数方法:
##看结果-调用函数方法
name = "张某某"
a = "李"
if name.startswith(a):
print("是{}老师".format(a))
else:
print("不是{}老师".format(a))
猜有无-使用成员运算符:
## 猜有无-使用成员运算符,in,not in
1.
name = "张某某"
a = "张"
if a in name:
print("找到了")
else:
print("没找到")
2.
name = "张某某"
if name in ["张三","李四","王五"]:
print("找到了")
else:
print("没找到")
##注意:not in跟in取反
if嵌套结构
a = 5
b = 4
if a > 3:
if b > 3:
print("变量a和b都大于3")
name = "张三"
name_list1 = ["张三","李四","王五"]
name_list2 = ["张三","赵六","狗七"]
if name in name_list1:
if name in name_list2:
print("属于间谍")
a = 5
b = 4
if a > 3 and b > 3:
print("变量a和b都大于3")
多条件判断
##多条件判断
1.
name = "张三"
name_list1 = ["张三","李四","王五"]
name_list2 = ["张四","赵六","狗七"]
if name in name_list1 and name in name_list2:
print("属于间谍")
else:
print("不是间谍")
2.重点!
name = "张三"
name_list1 = ["张三","李四","王五"]
name_list2 = ["张四","赵六","狗七"]
if name in name_list1:
if name in name_list2:
print("属于间谍")
else:
print("属于阵营1")
else:
print("属于阵营2")
语法规则
for(关键字) item(元素) in(关键字) iterable(集合):
(缩进)do something
for name in ["张三","李四"]:
print(name)
for循环应用
name_list = ["张三","李四","王五"]
for name in name_list:
print("欢迎你,{}".format(name))
s = "zhangsan"
for c in s:
print(c)
# range(start,end,step) start是起始值,end是终值,step表示增加步长,step没有就默认为1
for i in range(0,101):
print(i)
## 执行一个变量的加1,执行100次
a = 1
for i in range(100):
a += 1
print(a)
## 计算1-100的整数和
sum = 0
for i in range(1,101):
sum = sum + i
print(sum)
使用可迭代对象中的数据
## 可迭代对象
name_list = ["张三","李四","王五"]
for name in name_list:
print("欢迎你,{}".format(name))
## 判断是否可迭代的方法
hasattr([1,2],'__iter__') ##第二个参数固定,第一个参数是我们需要判断的对象
构造容器内的数据
## 构造容器内的数据
name_list = ["张三","李四","王五"]
name_list2 = ["赵六"]
for name in name_list:
name_list2.append(name)
print(name_list2)
循环内条件判断
## 1~100所有偶数和
sum = 0
for i in range(1,101):
if i % 2 == 0: ## 用%2来判断偶数
sum +=i
print(sum)
嵌套多重循环
## 输出9*9乘法表
for i in range(1,10):
for j in range(1,i+1):
print("{}*{}={}\t".format(j,i,i*j),end='')
print()
## 注意:通过指定end参数的值,可以取消在末尾输出回车符,实现不换行。而\t是水平制表(跳到下一个tab位置)
语法规则
while(关键字) condition(成立的条件):
(缩进)do something
即,只要…条件成立,就一直做
while循环应用:
## 求1~100的整数和
sum = 0
count = 1
while count < 101: ##while循环注意避免陷入死循环
sum += count
count = count + 1 ##更新操作
print(sum)
## 猜数字
answer = 6
num = int(input("请输入一个数字:"))
while num != answer:
print("你猜错了,请重新猜:")
num = int(input("请输入一个数字:"))
print("恭喜你,猜对了,有奖")
while循环和for循环的区别:一般循环的判断条件带有不确定性,就会用while循环实现
for循环
name_list = ["李四","王五","赵六","赵七"]
for name in name_list:
print(name)
if name == "赵六":
break ##跳出循环
print("ok")
for name in name_list:
if name == "赵六":
continue ##直接进入for的下一次循环
print(name)
## 1~100之间的偶数和
sum = 0
for i in range(1,101):
if i % 2 != 0:
continue
sum += i
print(sum)
while循环
## 猜数字
answer = 6
while True:
num = int(input("请输入一个数字:"))
if num == answer:
break
print("你猜错了,请重新猜:")
print("恭喜你,猜对了,有奖")
## 1~100的偶数和
sum = 0
count = 1
while count <=100:
if count % 2 != 0:
count += 1
continue
sum += count
count += 1
print(sum)