#使用场景为打印字符串
print("hallo word")
#1.这里的value,如果输入一个或者多个就打印出一个或者多个
print('i love you')
print('i','love',"you")
#2.sep代表value之间的符合默认为空格
print("1","love","you",sep="-")
#3.end作为结尾一般默认为\n
print("hello word",end=".")
#注释是为了代码的方便理解
#使用场景一般以单行代码或者代码片段做注释
print('hello word')#这段代码讲述的是向屏幕输出hello word
#使用场景一般以多行代码注释为主要描述,或者描述整段代码
"""
"""
'''
'''
'''
以下翻译为:
这是《十六世纪改革史》的第十卷,
也是第二辑的第五卷。
第一个系列描述了这个伟大时期的历史,
从开始到奥格斯堡the悔(1530)
'''
print('This is the tenth volume of the History of the Reformation of the Sixteenth Century, ')
print(' and the fifth of the Second Series.')
print(' The first series described the history of that great epoch from its commencement down to the Confession of Augsburg (1530).')
#当代码过长需要换行编写可以使用\+回车,主要为了方便阅读代码
print('This is the tenth volume of the History of the Reformation of the Sixteenth Century, \
and the fifth of the Second Series. The first series described the history of that great\
epoch from its commencement down to the Confession of Augsburg (1530).')
#单引号或者双引号适合处理结构检查的字符串
print('hello')
print("hello")
#如果字符中包含’或者”
print('i'm') #这样就会报错
#如果字符串内容比较复杂需要使用''' '''或者""" """ 注意中间需要空开三个空格再空格中编写字符
print(''' i'm 飞行D小胖 ''')
#使用反斜杠(\)转义字符 \n为换行
print('\n')
#如果我们需要输出\n我们就需要在前面再加入一个\
print('\\n')
#如果我们需要\\n那我们在前面就需要再加两个反斜杠以此类推,那如果我们需要100个反斜杠呢?那我们就需要使用原始字符串
#我们再字符串前加入R或者r就可以现在字符串内的本意不会有改便
print(r"\\\\\\\\\\\\\\\n")
1+1
1-1
1*4
'''
注意在python中*是不能省略的列如
4*(6-2)不可以写成4(6-2)
'''
4*(6-2)
6/2
4**4
20%6
20//6
round(8.9) #默认四舍五入保留到整数位
round(8.99,1) #四舍五入保留到小数点后一位
round(8.99,2) #四舍五入保留小数后两位
abs(-1)
import math #引用math函数
math.ceil(5.0)
math.ceil(5.1)
math.ceil(5.01)
math.floor(6.0)
math.floor(6.11)
math.trunc(10.9)
math.pow(2,4)
math.pow(5,2)
#字符串连接
'A'+'B'
#字符串多次连接
'A'*3
#字符串简单切片
'python'[0]
'python'[5]
'python'[-1]
'python'[1:4]
'python'[2:]
#格式:string.strip([chars])
#用法:
#chars为空,默认去除头尾空白符包括\n \r \t '',即换行、回车制表符、空格
#chars不为空,函数将chars拆成一个个字符,去除头尾指定字符
'ABC123ABC'.strip()
'\nABC123ABC\r'.strip()
'ABC123ABC'.strip('AB') #匹配后面的'AB'当从左边开始匹配‘ABC123ABC'左边AB与匹配项相匹配后去除C与字符串不匹配所以跳转到右侧
#右侧第一字符与ab不符所以跳出
#格式:string.lstrip([chars]) 剔除字符串左边的字符
#格式:string.rstrip([chars]) 剔除字符串右边的字符
'apple'.startswith('a') #判断字符串'apple'是否a开头正确返回t错误返回f
'apple'.endswith('e')#判断字符串'apple'是否e结尾正确返回t错误返回f
#S.find('x')找到这个字符返回下标,多个返回第一个;不存在的字符返回-1
#S.index('x')找到这个字符返回下标,多个时返回第一个;不存在时报错
('apple').find('e')
('apple').index('m')
#s.replace(oldstr,newstr)字符串替换
'lemon'.replace('m','mmmmm')
'lemon'.replace('lemon','apple')
#字符串其他操作(一)
#len(s)返回字符串长度
#S.count('x')查找某个字符在字符串中出现次数
#S.upper()将字符串中小写字母转换成为大写字符
#S.lower()将字符串中的大写字母转换成为小写字符
#S.center(n'-')把字符串放中间,两边用-补齐,n为字符串长度,若n小于等于字符串长度,则返回原值
len('apple')
'lemmmmo'.count('m')
'apple'.upper()
'APPLE'.lower()
'lemmmmo'.center(15,'-')
# %s 使用str()函数进行字符串转换
# %d 转为十进制整数
# %f 转为浮点数
#字符串格式化有两种:%、format
'hello %s' %('lemmo') #里面有多少%后面就要有多少参数
'hello %s,%s' %('lemo','python')
'333%d' %(4) #后面参数以(4)替换前面的%d
'i am is %d years old' %(16)
'%f' %(3.14) #默认保留小数点后六位
#如果要保留小数点后3位可以那么做
'%.3f' %(3.14)
'i am %(name)s ,i am %(age)s years old' %{'name':'ally','age':'16'} #参考字典的用法使用格式化字符串不建议使用建议使用format
#'hello{}'.format('word')
#'{1},{0},{1}'.format('hello','world')
#'{first},{second},{first}'.format(first = "hello",second ='world')
'hello {}'.format('word')
'hello {} {}'.format('lemo','python') #如果前面{}中不定义的话前面一个为0后面一个为1如果想要先python 后lemo我们可以这样
'hello {1} {0}'.format('lemo','python') #如果定义项太多用数字定义太复杂我们可以使用引用
'my name {name},i am {age} years old'.format(name =('ally'),age =(16))
#Python 数字类型:
#整型 (int)
#浮点型 (float)
#复数 (complex)
#布尔型 (bool)
#包括 正整数、负整数和0
#不特别说明的的话是指常用得到十进制数,如0 1 2 3 4 5 6
#如果你要转为其他进制数可以参考:
#bin 是向二进制转化 0b1010
#oct 是向八进制转化 0o12
#int 是向十进制转换 10
#hex 是向十六进制化 0xa
#十进制转换各类举例
bin(10)
oct(10)
int(10)
#常见带小数的数,如3.14、1.2、1.5等;
#其他书写方式:
#10. #小数部分为零,可以不写
#.12 #整数部分为零,可以不写
#2e-5 科学计数法2*(10**-5)
10.
.12
#如何判断数字类型
type(10)
type(3.14)
print(3+1)
print(type(3+1))
print(3+1.)
print(type(3+1.))
print(3*1)
print(type(3*1))
print(3*1.)
print(type(3*1.))
print(3/1)
print(type(3/1))
print(3/1.)
print(type(3/1.))
#在两数相除不管什么型默认返回浮点型
print(3//1)
print(type(3//1))
print(3//1.)
print(type(3//1.))
#整除时将能整除的部分先整除不能整除的算浮点
#complex([real[,imag]]) 适用于科学计数 real是被填参数 imag是可选参数 real是实部 imag是虚部
complex(3,4)
complex(3,4).real
complex(3,4).imag
complex('3+4j')
int(True)
int(False)
isinstance(1,int)
isinstance(True,int)
bool(1)
bool(0)
bool(-1) #在数字类型下布尔类型为0的情况下返回都为Fslse
#布尔类型总结
#当布尔类型为0 None 空值的时候,布尔返回类型False
bool(0)
bool(None)
bool('')
bool(' ') #字符串中间如果有空格返回就是True
bool([])
bool({})
bool(())