所有的变量无需声明即可使用;变量的数据类型可以随时变更。
/* 多行 */
a=1 b=2 print(a+b) print(type(a)) c=1.23 print(type(c)) d="hello" print(type(d))
标识符可以由字母、数字、下画线()组成,其中数字不能打头。 标识符不能是Python 关键字,但可以包含关键字。
image.png
标识符不能包含空格。
数值类型(空值None):整型 浮点型 整型(二进制、八进制、十进制、十六进制) hex valuel = Ox13 hex value2 = OXaF print ( ” hexValuel 的值为:”, hex_valuel) print (” hexValue2 的值为:”, hex_value2) #以Ob 或OB 开头的整型数值是二进制形式的整数 bin val = Oblll print ( ' bin_val 的值为: ', bin_ val) bin val = OB101 print ( ’ bin_val 的值为:’, bin_val) # 以Oo 或00 开头的整型数值是八进制形式的整数 oct val = Oo54 print (’ oct_val 的值为:’, oct_val) oct val= 0017 print ( ’ 。ct_val 的值为:’, oct_val)
浮点型 a=2.33
复数:有个cmath模块可以处理
字符串:可以用双引号、单引号 str="sdfsdf" print(type(str)) str="abc'ddd" str="abc'ddd" ("\) 字符串拼接: str1+str2
a=""" sdfdsfsd sdfsdfds sdfsdf """
str="123" a=int(str) b=str(a) (float)
r ’ G: \publish\codes\02\2 . 4 ’ s2 = r ’”Let\’s q。”, sa 工d Charlie '
True False "" None 都是False and or not
image.png
简便写法: st="a" if 1>2 else "bd"
b=bytes() b2=b'xxx' print(b3) print (b3 [0]) pr 工nt(b3[2 : 4]) #调用bytes 方法将字符席转换成bytes 对象 b4 =bytes (’我爱Python 编程’, enc。ding= ’ utf-8 ’) bS = ”学习Pyth on 很有趣” .en code {『utf-8 ’) print (bS)
input("请输入") 任何输入都会被当作string
\n
image.png
print (”the book ’ s price is %s ” %price) print("aa{}{}".format(a,b))
image.png
image.png
image.png
image.png
dir(str) help(str)
str.lower() str.upper() str.title()
str.strip() str.lstrip() str.rstrip()
str.strip(".") str.strip("abc")
startswith() endswith() find()
str.find("abc") str.find("abc",8) index() replace("")
abc,def,ghi s.split(",") x.join(",")
& | ^ ~ << >>
+=
image.png
image.png
abc in s abc not in s