本文共 3651 字,大约阅读时间需要 12 分钟。
1.运算符
(1)算术运算符:
(2)关系运算符:
(3)赋值运算符:
(4)逻辑运算符:
优先级:() > not > and > or
2.数据类型:
raw_input(): 接收字符串类型
input() : 接收数字类型
整型:
a =-100
abs(a) 求a的绝对值
a.__abs__() 求a的绝对值
符点型:
round()方法 默认保留1位小数,采用四舍五入方法进行计算,最后一位为偶数.
1 2 3 | a = 3.0 round (a) round ( 2 ) |
先进行四舍五入运算,如果小数点精度的最后一位是偶数符合条件,如果小数点精度最后一位四舍五入以后为奇数,则舍弃小说点精度以后所有数字,以保证小数点精度,最后一位为偶数
1 2 3 4 5 6 | c = 2.555 d = 1.545 print ( round (c, 2 )) print ( round (d, 2 )) 2.56 1.54 |
布尔类型:
1 2 | True False |
3.字符串
dir()查看有哪些内置方法
type()查看是什么类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | str1 = 'aaaaabasdfxqs353235asdf' print (str1.find( 'fxq' )) 9 str1 = 'aaaaabasdfxqs353235asdf' print (str1.replace( 'fxq' , 'Fengxiaoqing' )) aaaaabasdFengxiaoqings353235asdf str1 = 'aaaaabasdfxqs353235asdf' print (str1.split( 's' )) [ 'aaaaaba' , 'dfxq' , '353235a' , 'df' ] str1 = 'aaaaabasdfxqs353235asdf' print ( 'SSS' .join(str1.split( 's' ))) aaaaabaSSSdfxqSSS353235aSSSdf a = ' aaaaaba sdfx qs353 235asdf ' print (a.strip()) print (a) print (a.lstrip()) #去掉左边空格 print (a.rstrip()) #去掉右边空格 aaaaaba sdfx qs353 235asdf aaaaaba sdfx qs353 235asdf aaaaaba sdfx qs353 235asdf aaaaaba sdfx qs353 235asdf format ()方法: name = 'fengxiaoqing' age = 30 home = 'chengde' print ( 'hello' + name) print ( 'hello {0}' ). format (name) print ( 'hello %s' ) % name print ( 'hello %d' ) % age print ( '我的年龄是:{0} 我的家:{1}' ). format (age,home) print ( '{name}:{age}' . format (name = 'fxq' ,age = 20 )) hellofengxiaoqing hello fengxiaoqing hello fengxiaoqing hello 30 我的年龄是: 30 我的家:chengde fxq: 20 |
4.list操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | 'append' , 'count' , 'extend' , 'index' , 'insert' , 'pop' , 'remove' , 'reverse' , 'sort' str1 = '12poqiwrtgopwert' str2 = list (str1) print ( type (str1)) print ( type (str2)) print ( list (str1)) print ( dir (str2)) print ( '####' * 20 ) a = [ 123 , 'bbb' , 'ace' ] print (a[ 1 ]) print (a.index( 'bbb' )) a.insert( 1 , 'aaa' ) print (a) a.sort() print (a) a.reverse() print (a) a.append( 'ooo' ) print (a) a.pop() print (a) a.remove( 123 ) print (a) a.pop( 1 ) print (a) < type 'str' > < type 'list' > [ '1' , '2' , 'p' , 'o' , 'q' , 'i' , 'w' , 'r' , 't' , 'g' , 'o' , 'p' , 'w' , 'e' , 'r' , 't' ] [ '__add__' , '__class__' , '__contains__' , '__delattr__' , '__delitem__' , '__delslice__' , '__doc__' , '__eq__' , '__format__' , '__ge__' , '__getattribute__' , '__getitem__' , '__getslice__' , '__gt__' , '__hash__' , '__iadd__' , '__imul__' , '__init__' , '__iter__' , '__le__' , '__len__' , '__lt__' , '__mul__' , '__ne__' , '__new__' , '__reduce__' , '__reduce_ex__' , '__repr__' , '__reversed__' , '__rmul__' , '__setattr__' , '__setitem__' , '__setslice__' , '__sizeof__' , '__str__' , '__subclasshook__' , 'append' , 'count' , 'extend' , 'index' , 'insert' , 'pop' , 'remove' , 'reverse' , 'sort' ] ################################################################################ bbb 1 [ 123 , 'aaa' , 'bbb' , 'ace' ] [ 123 , 'aaa' , 'ace' , 'bbb' ] [ 'bbb' , 'ace' , 'aaa' , 123 ] [ 123 , 'aaa' , 'ace' , 'bbb' , 'ooo' ] [ 123 , 'aaa' , 'ace' , 'bbb' ] [ 'aaa' , 'ace' , 'bbb' ] [ 'aaa' , 'bbb' ] |
列表切片:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | a = [ 11 , '222' , '33' , 444 , 555 , 666 ] print (a[ 3 :]) print (a[ 1 : 5 ]) print (a[ 1 : 6 : 2 ]) print (a[: 4 ]) print (a[ - 1 ]) print (a[ - 2 :]) print (a[ - 4 : - 2 ]) [ 444 , 555 , 666 ] [ '222' , '33' , 444 , 555 ] [ '222' , 444 , 666 ] [ 11 , '222' , '33' , 444 ] 666 [ 555 , 666 ] [ '33' , 444 ] |
本文转自 枫叶云 51CTO博客,原文链接:http://blog.51cto.com/fengyunshan911/2063040
转载地址:http://btxqa.baihongyu.com/