python及go笔记
input()接收数据后类型将变成string类型
三元运算符:变量 = 值1 if 条件 else 值2
format,字符串
eval()用于字符串 脱去外壳
输入"123"字符串,eval("123")结果为123 ,type为int
类型转换:
str ->int int(a)
str -> float float(a)
int -> str str(a)
float -> str str(a)
int -> float float(a)
float -> int int(a)
逻辑运算优先级not> and >or
几种最常用的输出方式:
print("%s跟%s是同一个人" % (name1, name2)) print(f"您的金币数是{coins}个,您共玩了{count}局") print("您的金币数是{}个,您共玩了{}局".float(coins, count))
精确到小数点后一位: print("num1的值是:%.1f" % num1)
随机生成一个1-6之间的数字(1-6,左右都是闭区间) shu1 = random.randint(1, 6)
序列值:
range(9)表示0-8
range(1,9)表示1-8
range(1,9,2)表示1,3,5,7 (start, end,sep)左闭右开
字符串切割:
s[start: end: step]同上,是左闭右开,切出来的是列表
if step是正数 从左向右
if step是负数 从右向左
and or 的细节处理:
print(10 and 20) 输出20
print(0 and 20) 输出0
print(20 and 0) 输出0
print(10 or 20) 输出10
print(0 or 20) 输出 20
print(10 or 0) 输出 10
在python中,转换成布尔值时,只有0,“”,“ ”,None,(),[],{}会转换成False
len(xx)测字符串长度
字符串索引机制:
-
0 - (len(s) -1)
-
-len(s) - (-1)
字符串的常见操作:
长度:len
查找内容:find,index,rfind,rindex
判断:startswith,endswith,isalpha,isdigit,isalnum,isspace
计算出现次数:count
替换内容:replace
切割字符串:split,rsplit,splitlines,partition,rpartition
修改大小写:capitalize,title,upper,lower
空格处理:ljust,rjust,center,lstrip,rstrip,strip
字符串拼接:join
(注:python中字符串时不可变的,所有的字符串相关方法都不会改变原有的字符串)
path = "https://www.bilibili.com/video/BV1R7411F7JV/?p=38&spm_id_from=pageDriver&vd_source=52f414724e7733e9ae21b977293e6179" print(len(path)) # 115 i = path.find('?') print(i) # 44 sql = path[i+1:] print(sql) # p=38&spm_id_from=pageDriver&vd_source=52f414724e7733e9ae21b977293e6179 path = "https://www.bilibili.com" i = path.rfind(".") bath = path[i:] print(bath) # .com i = path.find(".") fore = path[:i] print(fore) # https://www print(path.count('.')) # 2 print(path.startswith('h')) # True print(path.startswith('a')) # False print(path.isalnum()) # False print(path.isalpha()) # False print(path.isdigit()) # False a = " " print(a.isspace()) # True s = 'hello' print(s.upper()) # HELLO print(s.lower()) # hello print(s.title()) # Hello print(s.isupper()) # False #islower() s = "dsj说:阿牛,你来唱歌吧,阿牛,你来跳舞吧, 阿牛,你来打篮球吧" print(s.replace('阿牛', 'xx', 1)) # dsj说:xx,你来唱歌吧,阿牛,你来跳舞吧, 阿牛,你来打篮球吧 print(s.replace('阿牛', 'xx', 2)) # dsj说:xx,你来唱歌吧,xx,你来跳舞吧, 阿牛,你来打篮球吧 print(s.replace('阿牛', 'xx')) # dsj说:xx,你来唱歌吧,xx,你来跳舞吧, xx,你来打篮球吧 # replace(old, new, count),count代表次数,默认全部 s = 'dgfhSFDG' print(s.capitalize()) # Dgfhsfdg s = "sgd.dgd.sg.wt.et.f" print(s.split('.')) # ['sgd', 'dgd', 'sg', 'wt', 'et', 'f'] print(s.split('.', maxsplit=2)) # ['sgd', 'dgd', 'sg.wt.et.f'] # 返回的是列表 s = " fasf adfa gg g " print(s.strip()) # fasf adfa gg g # 去除左右两侧的空格 print(len(s)) # 21 print(s.center(10)) # fasf adfa gg g print(len(s.center(10))) #21 print(s.center(40)) # fasf adfa gg g print(len(s.center(40))) # 40 print(s.rjust(30)) # fasf adfa gg g print(len(s.rjust(30))) # 30 print(s.ljust(30)) # fasf adfa gg g
go语言的打印是fmt.Println()
而不是fmt.println()
for i :=0;i<10;i++ {
fmt.Print("hello,world!")
}
go = python + c
go 语言特点:
go语言继承了C语言的指针
引入了包的概念,go语言的每个文件都要归属一个包,不能单独存在
垃圾回收机制,内存自动回收
天然并发
注:go语言包中同一层文件不能同时拥有两个main()函数,程序的入口必须唯一
go文件的执行方式:
1.go build xxx.go
xxx.exe(先编译成可执行文件再运行,可以在没有go开发环境的机器上运行,但是文件会变大很多,因为编译器会把程序运行依赖的库文件全部打包在编译文件中)
2.go run xxx.go(需要依赖go开发环境,但是占用空间小)
go程序的注意事项:
1.以.go为扩展名
2.go程序的执行入口是main()
3.go语言严格区分大小写
4.go语言会自动在每行后自动加上分号,所以不必画蛇添足,当然加上不会报错
5.go编译器是一行一行进行编译的,所以每一行只能写一条语句,一行写多条语句的话会报错
6.go语言严格控制定义的变量以及import的包,如果定义了或者引用了就必须用到,否则编译不能通过
7.括号成对出现
介绍一下极个别转义字符:
\t:制表符 \r:回车符(使用时注意:fmt.Print("天龙八部雪山飞狐\r张飞"),输出结果是:张飞八部雪山飞狐。 因为回车符是把光标定位到当前行的行首,所以张飞覆盖了天龙) \n:换行符