print函数的使用方法

In [1]:
#使用场景为打印字符串
In [2]:
print("hallo word")
hallo word

print(value,...,sep=' ',end='\n'

In [3]:
#1.这里的value,如果输入一个或者多个就打印出一个或者多个
In [4]:
print('i love you')
i love you
In [5]:
print('i','love',"you")
i love you
In [6]:
#2.sep代表value之间的符合默认为空格
In [7]:
print("1","love","you",sep="-")
1-love-you
In [8]:
#3.end作为结尾一般默认为\n
In [9]:
print("hello word",end=".")
hello word.

换行与注释

In [10]:
#注释是为了代码的方便理解

单行注释

In [11]:
#使用场景一般以单行代码或者代码片段做注释
In [13]:
print('hello word')#这段代码讲述的是向屏幕输出hello word
hello word

多行注释

In [14]:
#使用场景一般以多行代码注释为主要描述,或者描述整段代码
In [ ]:
"""

"""
In [ ]:
'''

'''
In [15]:
'''
以下翻译为:
这是《十六世纪改革史》的第十卷,
也是第二辑的第五卷。
第一个系列描述了这个伟大时期的历史,
从开始到奥格斯堡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).')
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).
In [17]:
#当代码过长需要换行编写可以使用\+回车,主要为了方便阅读代码
In [18]:
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).')
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 greatepoch from its commencement down to the Confession of Augsburg (1530).

python单引号双引号以及转义字符的用法

In [26]:
#单引号或者双引号适合处理结构检查的字符串
In [19]:
print('hello')
hello
In [20]:
print("hello")
hello
In [21]:
#如果字符中包含’或者”
In [23]:
print('i'm')   #这样就会报错
  File "<ipython-input-23-11cc7cc4c01a>", line 1
    print('i'm')   #这样就会报错
             ^
SyntaxError: invalid syntax
In [24]:
#如果字符串内容比较复杂需要使用'''    '''或者"""   """ 注意中间需要空开三个空格再空格中编写字符
In [25]:
print('''  i'm 飞行D小胖 ''')
  i'm 飞行D小胖 

转义字符

In [27]:
#使用反斜杠(\)转义字符 \n为换行
In [28]:
print('\n')

In [29]:
#如果我们需要输出\n我们就需要在前面再加入一个\
In [30]:
print('\\n')
\n
In [31]:
#如果我们需要\\n那我们在前面就需要再加两个反斜杠以此类推,那如果我们需要100个反斜杠呢?那我们就需要使用原始字符串

原始字符串

In [32]:
#我们再字符串前加入R或者r就可以现在字符串内的本意不会有改便
In [33]:
print(r"\\\\\\\\\\\\\\\n")
\\\\\\\\\\\\\\\n

Python常用的数学运算与数学函数

+加号

In [34]:
1+1
Out[34]:
2

-减

In [35]:
1-1
Out[35]:
0

*乘号

In [36]:
1*4
Out[36]:
4
In [37]:
'''
注意在python中*是不能省略的列如
4*(6-2)不可以写成4(6-2)
'''
4*(6-2)
Out[37]:
16

/除法

In [38]:
6/2
Out[38]:
3.0

**乘方

In [31]:
4**4
Out[31]:
256

%求余

In [40]:
20%6
Out[40]:
2

//取整

In [41]:
20//6
Out[41]:
3

round四舍五入

In [1]:
round(8.9)        #默认四舍五入保留到整数位
Out[1]:
9
In [8]:
round(8.99,1)      #四舍五入保留到小数点后一位
Out[8]:
9.0
In [5]:
round(8.99,2)      #四舍五入保留小数后两位
Out[5]:
8.99

ABS绝对值取整

In [1]:
abs(-1)
Out[1]:
1

math数学函数

ceil向上取整

In [2]:
import math #引用math函数
In [3]:
math.ceil(5.0)
Out[3]:
5
In [5]:
math.ceil(5.1)
Out[5]:
6
In [6]:
math.ceil(5.01)
Out[6]:
6

floor向下取整

In [7]:
math.floor(6.0)
Out[7]:
6
In [8]:
math.floor(6.11)
Out[8]:
6

trunc 截取整数位

In [9]:
math.trunc(10.9)
Out[9]:
10

pow幂运算

In [12]:
math.pow(2,4)
Out[12]:
16.0
In [13]:
math.pow(5,2)
Out[13]:
25.0

字符串简单操作

In [14]:
#字符串连接
In [15]:
'A'+'B'
Out[15]:
'AB'
In [16]:
#字符串多次连接
In [17]:
'A'*3
Out[17]:
'AAA'
In [18]:
#字符串简单切片
In [19]:
'python'[0]
Out[19]:
'p'
In [20]:
'python'[5]
Out[20]:
'n'
In [21]:
'python'[-1]
Out[21]:
'n'
In [22]:
'python'[1:4]
Out[22]:
'yth'
In [24]:
'python'[2:]
Out[24]:
'thon'

stip()

In [26]:
#格式:string.strip([chars])
In [27]:
#用法:
#chars为空,默认去除头尾空白符包括\n \r \t '',即换行、回车制表符、空格
#chars不为空,函数将chars拆成一个个字符,去除头尾指定字符
In [32]:
'ABC123ABC'.strip()
Out[32]:
'ABC123ABC'
In [33]:
'\nABC123ABC\r'.strip()
Out[33]:
'ABC123ABC'
In [36]:
'ABC123ABC'.strip('AB') #匹配后面的'AB'当从左边开始匹配‘ABC123ABC'左边AB与匹配项相匹配后去除C与字符串不匹配所以跳转到右侧
#右侧第一字符与ab不符所以跳出
Out[36]:
'C123ABC'
In [37]:
#格式:string.lstrip([chars]) 剔除字符串左边的字符
In [38]:
#格式:string.rstrip([chars]) 剔除字符串右边的字符

判断字符串开头结尾字符

In [2]:
'apple'.startswith('a') #判断字符串'apple'是否a开头正确返回t错误返回f
Out[2]:
True
In [4]:
'apple'.endswith('e')#判断字符串'apple'是否e结尾正确返回t错误返回f
Out[4]:
True

返回字符串中字符的位置

In [12]:
#S.find('x')找到这个字符返回下标,多个返回第一个;不存在的字符返回-1
#S.index('x')找到这个字符返回下标,多个时返回第一个;不存在时报错
In [15]:
('apple').find('e')
Out[15]:
4
In [17]:
('apple').index('m')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-17-0bb9214b253e> in <module>
----> 1 ('apple').index('m')

ValueError: substring not found

字符串替换

In [18]:
#s.replace(oldstr,newstr)字符串替换
In [20]:
'lemon'.replace('m','mmmmm')
Out[20]:
'lemmmmmon'
In [21]:
'lemon'.replace('lemon','apple')
Out[21]:
'apple'
In [22]:
#字符串其他操作(一)
In [25]:
#len(s)返回字符串长度
#S.count('x')查找某个字符在字符串中出现次数
#S.upper()将字符串中小写字母转换成为大写字符
#S.lower()将字符串中的大写字母转换成为小写字符
#S.center(n'-')把字符串放中间,两边用-补齐,n为字符串长度,若n小于等于字符串长度,则返回原值
In [26]:
len('apple')
Out[26]:
5
In [27]:
'lemmmmo'.count('m')
Out[27]:
4
In [28]:
'apple'.upper()
Out[28]:
'APPLE'
In [29]:
'APPLE'.lower()
Out[29]:
'apple'
In [33]:
'lemmmmo'.center(15,'-')
Out[33]:
'----lemmmmo----'

字符串格式化

In [34]:
#   %s 使用str()函数进行字符串转换
In [35]:
#   %d 转为十进制整数
In [36]:
#   %f 转为浮点数
In [37]:
#字符串格式化有两种:%、format

%格式化字符串

In [38]:
'hello %s' %('lemmo')   #里面有多少%后面就要有多少参数
Out[38]:
'hello lemmo'
In [41]:
'hello %s,%s' %('lemo','python')
Out[41]:
'hello lemo,python'
In [46]:
'333%d' %(4)      #后面参数以(4)替换前面的%d
Out[46]:
'3334'
In [47]:
'i am is %d years old' %(16)
Out[47]:
'i am is 16 years old'
In [50]:
'%f' %(3.14)  #默认保留小数点后六位
Out[50]:
'3.140000'
In [51]:
#如果要保留小数点后3位可以那么做
'%.3f' %(3.14)
Out[51]:
'3.140'
In [63]:
 'i am %(name)s ,i am %(age)s years old' %{'name':'ally','age':'16'} #参考字典的用法使用格式化字符串不建议使用建议使用format
Out[63]:
'i am ally ,i am 16 years old'

format格式化字符串

In [52]:
#'hello{}'.format('word')
In [53]:
#'{1},{0},{1}'.format('hello','world')
In [54]:
#'{first},{second},{first}'.format(first = "hello",second ='world')
In [55]:
'hello {}'.format('word')
Out[55]:
'hello word'
In [57]:
'hello {} {}'.format('lemo','python') #如果前面{}中不定义的话前面一个为0后面一个为1如果想要先python 后lemo我们可以这样
Out[57]:
'hello lemo python'
In [59]:
'hello {1} {0}'.format('lemo','python') #如果定义项太多用数字定义太复杂我们可以使用引用
Out[59]:
'hello python lemo'
In [60]:
'my name {name},i am {age} years old'.format(name =('ally'),age =(16))
Out[60]:
'my name ally,i am 16 years old'

Number(数字):整型、浮点型与复数

In [2]:
#Python 数字类型:
#整型  (int)
#浮点型 (float)
#复数   (complex)
#布尔型  (bool)

整型(INTEGER)

In [4]:
#包括 正整数、负整数和0
#不特别说明的的话是指常用得到十进制数,如0 1 2 3 4 5 6 
#如果你要转为其他进制数可以参考:
#bin 是向二进制转化 0b1010
#oct 是向八进制转化 0o12
#int 是向十进制转换 10
#hex 是向十六进制化 0xa
In [5]:
#十进制转换各类举例
In [6]:
bin(10)
Out[6]:
'0b1010'
In [7]:
oct(10)
Out[7]:
'0o12'
In [8]:
int(10)
Out[8]:
10

浮点型(FLOAT)

In [10]:
#常见带小数的数,如3.14、1.2、1.5等;
#其他书写方式:
#10.  #小数部分为零,可以不写
#.12   #整数部分为零,可以不写
#2e-5   科学计数法2*(10**-5)
In [11]:
10.
Out[11]:
10.0
In [12]:
.12
Out[12]:
0.12
In [15]:
#如何判断数字类型
In [18]:
type(10)
Out[18]:
int
In [19]:
type(3.14)
Out[19]:
float
In [24]:
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.))
#在两数相除不管什么型默认返回浮点型
4
<class 'int'>
4.0
<class 'float'>
3
<class 'int'>
3.0
<class 'float'>
3.0
<class 'float'>
3.0
<class 'float'>
In [26]:
print(3//1)
print(type(3//1))

print(3//1.)
print(type(3//1.))
#整除时将能整除的部分先整除不能整除的算浮点
3
<class 'int'>
3.0
<class 'float'>

复数(COMPLEX)

In [28]:
#complex([real[,imag]])  适用于科学计数 real是被填参数 imag是可选参数 real是实部 imag是虚部
In [31]:
complex(3,4)
Out[31]:
(3+4j)
In [34]:
complex(3,4).real
Out[34]:
3.0
In [35]:
complex(3,4).imag
Out[35]:
4.0
In [38]:
complex('3+4j')
Out[38]:
(3+4j)
In [ ]:
 

Number(数字):布尔类型

用来判断条件是否成立,只有True和False两种返回值

In [39]:
int(True)
Out[39]:
1
In [40]:
int(False)
Out[40]:
0
In [41]:
isinstance(1,int)
Out[41]:
True
In [42]:
isinstance(True,int)
Out[42]:
True
In [43]:
bool(1)
Out[43]:
True
In [44]:
bool(0)
Out[44]:
False
In [45]:
bool(-1)   #在数字类型下布尔类型为0的情况下返回都为Fslse
Out[45]:
True
In [46]:
#布尔类型总结
#当布尔类型为0 None 空值的时候,布尔返回类型False
In [47]:
bool(0)
Out[47]:
False
In [48]:
bool(None)
Out[48]:
False
In [49]:
bool('')
Out[49]:
False
In [50]:
bool(' ') #字符串中间如果有空格返回就是True
Out[50]:
True
In [51]:
bool([])
Out[51]:
False
In [52]:
bool({})
Out[52]:
False
In [53]:
bool(())
Out[53]:
False
In [ ]:
 
© 2024 Zhao Runsen
TrustAsia 安全签章