1、变量是可以变化的,a=[-5,256] b=[-5,256] a is b 此时都返回True,这些都是小整数,超过这个区间都会返回False;还有就是字符串,中间不可以出现空格或者其他特殊的符号时,最多缓存256个字符串,最后也会返回True,其余的都返回False, 因为他获得的是变量的内存地址;区分大小写 Python爬虫人工智能学习教程
2、格式化的四种方式
name = '小明'
age = 18.1
#用%做占位符 %d整数 %s字符串 %f小数
print('你的名字是:%s,你的年龄是:%d' %(name,age))
#用{}做占位符
print('你的名字是:{},你的年龄是:{}' .format(name,age))
#用(索引做占位符)
print('你的名字是:{0},你的年龄是:{1}' .format(name,age))
#用名字做占位符
print('你的名字是:{xxx},你的年龄是:{yyx}' .format(xxx=name,yyx=age))
3、寻找保存路径
import os
os.getcwd()
4、数字:int、float、bool、complex,数字是不可变类型 range就是数字类型
for i in range(2,10,2):
print(i)#2到10前开后闭,间隔为2的数字
print(1==1.0)#返回True
print(1=='1')#返回False
print(1=True)#返回True
print(0=False)#返回True
5、字符串的连接,索引,运算
print('atbnc')#字符串的转义
print(r'atbnc')#字符串的转义
print('qwe'+'123')#相加时必须是相同的类型
s[i:j:k]#包头不包尾,步长为k
print(str[-1:-5])#返回空值
print(str[2:0])#返回空值
最后一个元素的偏移是-1,第一个元素的偏移是0
6、函数
s = '123qwe'
print(type(s))
print(len(s))
print(min(s))
print(max(s))
7、方法:字符串是不可变得类型
'acb123'.index('1')#返回索引
'acb123'.find('2')#返回位置
'acb123'.find('c2')#不没找到返回-1
'acb123'.count('2')#计数
'@'.join('abc')#'分隔符'.join(字符串) 'a@b@c’
(' a sd ').strip(' ')#删除两边空格,不指定就是空格
(' a sd ').lstrip(' ')#删除左边空格
(' a sd ').rstrip(' ')#删除右边空格
('#a#sd ').strip('#')#删除两边指定的符号
'QWE'.lower()#大写变小写
'qwe'.upper()#小写变大写
'qwe'.capitalize()#首字母大写
'qWa'.swapcase()#交换大小写
'i love you'.split()#按空格分开,返回元素的列表
'i*love*you'.split('*')#按*分开,返回元素的列表
8、数字和字符串之间的转换
float 将其他类型转换成字符串
float(1)#1.0
float('1.23')#1.23
float(1.0e-5)
str 将其他类型转换成字符串
str(1)
str(1.0)
int 将其他类型转换成整数型
int(3.14)#3
int(3.5)#3
int(True)#1
#int('3.5')#报错
int(float('3.5'))#先转化为3
bool 将其他类型转化成布尔型
bool(0)#False
bool(-1)#所有非0的数都返回真
bool('a')#True
bool('')#False
9、运算符
#算术运算符
#+ - * / // %
a = 7
b = 2
a+b#9
a - b#5
a * b#14
a/b#除,得到浮点数 3.5
a % b#取余 1
a // b#取整 3
b ** 3 #8
#python内置的函数
#abs取绝对值
#divmod取模 divmod(5,2)#(2,1)#返回商和余
#sum 求和
#round 四舍六入 round(2.5)#2 round(1.5)#2
#math模块的内置的函数 先倒包 import math
# math.sqrt math.exp math.log math.factorial阶乘 math.ceil取上 math.floor取下
#math模块的内置常量
#math.pi math.e
#赋值运算符
#= += -= *= /= %= //= **=
#a += 1
a,b,c=1,2,3#序列赋值
print(a,b,c)
a,b,c='1','2','3'
print(a,b,c)
a,b,c='123'
print(a,b,c)
#比较运算符 返回的都是bool值
#== != <= >= <> < >
a = 1
b = 2
a==b
a!=b
a>b
a<b
a<=b
a>=b
#逻辑运算符
and:两边为真才是真
or:一边为真就为真
not:对原值取反
#成员运算符
#in not in
'h' in 'hello'
#身份运算符
#is not is
a = 1
b = 2
a is b
10、条件控制语句
#单元
age = 19
if age >18:
print(age)
#二元
age = 20
if age <18:
print("青年")
else:
print("壮年")
#多元
age = 20
if age <18:
print("青年")
elif age <30:
print("壮年")
elif age <45:
print("中年")
else:
print('老年')
11、循环控制语句
while语句
#输出1到10
i = 1
while i <11:
print(i)
i = i + 1
#输出1到10的偶数
i = 1
while i<11:
if i % 2 == 0:#求余数
print(i)
i = i + 1
i = 1
while i <5:
if i == 4:
break#跳出循环
print(i)
i = i + 1
i = 1
while i <5:
i = i + 1
if i == 4:
continue#这个循环到此为止,到下一个循环
print(i)
for语句
for i in range(2,10):
print(i)#从2开始到9
for i in range(10):
print(i)#从0开始到9