s ='hello'#s = abc 引号
s ='hello'
s ="hello"#单双都可,不要混用
s ="子曰:'学而时习之不亦乐乎!'"#长字符串#不能跨行使用 使用反斜杠
s = '锄禾日当午,\
汗滴禾下土。'
# 三重引号来表示一个长字符串
s ='''锄禾日当午,
汗滴禾下土。'''#使用\ 作为转义字符
s ="子曰:\"学而时习之,\\\\n不亦乐乎!\""
s ='\u0070'
s ='\u16A1'
s ='\u2250'print(s)
三. 格式化字符串
a ='hello'
a ='abc'+'hahah'
a=123#字符串不能和其他的类型进行加法运算# print("a = "+a) 不常见
a =123print('a=',a)#在创建字符串中,可以在字符串中指定占位符# %s 在字符串中表示任意字符# %f 浮点数占位符# %d 整数占位符
b ='hello %s'%'孙悟空'
b ='hello %s 你好 %s'%('tom','孙悟空')
b ='hello%3.5s'%'abdfsfsd'#字符串限制3-5
b ='hello %s'%123.522
b ='hello %.2f'%123.456
b ='hello %d'%123.456
b ='heh'print('a=%s'%a)#格式化字符串,可以通过在字符串前面添加一个F来创建一个格式化字符串#在格式化字符串中可以直接嵌入变量
c =f'helllo{a}{b}'#变量有效print(f'a={a}')
四. 复制字符串
#创建一个变量来保存你的名字
name ='孙悟空'#使用四种方式来输出,欢迎 xxx光临#拼串print('欢迎 '+name+' 光临!')#多个参数print('欢迎',name,'光临!')#占位符print('欢迎 %s 光临!'%name)#格式化字符串print(f'欢迎 {name} 光临!')
a ='abc'
a = a *20#重复二十次print(a)
五. 布尔值(bool)
# 布尔值主要用来做逻辑判断,# 布尔值一共有两个True和False# True表示真False表示假
a =True
a =Falseprint('a=',a)print(1+True)print(1+False)#None(空值)#None 专门用来表示不存在
b =Noneprint(b)
a =10+5
a ='hello'+' '+'world!'
a =0/3
a =0%3
a =4%0.3print('a =',a)
九. 关系运算符
# is 比较ID# == 比较值
a =1==Trueprint(a)
b=1isTrueprint(b)print(id(1),id(True))#Python中的逻辑运算符是短路的 看到该看的就不看后面的了!
十. 逻辑运算符
#Python中的逻辑运算符是短路的 看到该看的就不看后面的了!# 与运算 看见False# 或运算 看见True #非布尔值运算# 当我们对非布尔值进行与或运算时,Python会把它当做布尔值运算,然后返回它的值
result =1and2#2 True and True
result =1and0#0 True and False
result =0and1#0 False and True
result =0andNone#0 False and False
result =Noneand1#None False and False
result =Noneand0#None False and False#找False 同理 或运算找True
result =1or2#1
result =0or1#1
result =0orNone#Noneprint(result)
十一. 条件运算符
# 语法 语句1 if 条件表达式 else 语句2# 条件运算符在执行时,会先进行求值判断# 如果判断结果 True,则执行语句1# 如果判断结果 False,则执行语句2print('你好')ifTrueelseprint('hello')print('你好')ifFalseelseprint('hello')
a =100
b =200print('a的值比较大!')if a>b elseprint('b的值比较大!')max= a if a>b else b
print('最大数的值是:%d'%max)
a=600
b=310
c=320max=(a if(a>b and a>c)else b)if b>c else c
print(max)
十二. 运算符优先级
# and 比 or 的优先级高# result = 1 < 2 < 3# result = 10 < 20 > 15# 以上都是和中间值比较 不是从左到右!