day02-Python基础

>>> if a > b:
... c = a+b
... else:
... c = a-b
...
>>> c
-1

三元运算:

>>> c = a+b if a>b else a-b
>>> c
-1

1:列表、元组

列表

>>> names = ['apple','banana','orange','watermelon']
>>> names[0]
'apple'
>>> names[1]
'banana'
>>> names[-1]
'watermelon'

切片:取多个元素

>>> names = ['apple','orange','banana','watermelon','persimmon']
>>> names[1:4]
['orange', 'banana', 'watermelon']
>>> names[1:-1]
['orange', 'banana', 'watermelon']
>>> names[:3]
['apple', 'orange', 'banana']
>>> names[3:]
['watermelon', 'persimmon']
>>> names[3:-1]
['watermelon']
>>> names[0::2]  #2代表,每隔一个元素,就取一个
['apple', 'banana', 'persimmon']
>>> names[::2]
['apple', 'banana', 'persimmon']
>>>
View Code
追加:
>>> names
['apple', 'orange', 'banana', 'watermelon', 'persimmon']
>>> names.append('litchi')
>>> names
['apple', 'orange', 'banana', 'watermelon', 'persimmon', 'litchi']
View Code
 插入:
>>> names
['apple', 'orange', 'banana', 'watermelon', 'persimmon', 'litchi']
>>> names.insert(2,"从banana前插入")
>>> names
['apple', 'orange', '从banana前插入', 'banana', 'watermelon', 'persimmon', 'litchi']
>>> names.insert(4,'从banana后添加')
>>> names
['apple', 'orange', '从banana前插入', 'banana', '从banana后添加', 'watermelon', 'persimmon', 'litchi']
View Code

 修改:

>>> names
['apple', 'orange', '从banana前插入', 'banana', '从banana后添加', 'watermelon', 'persimmon', 'litchi']
>>> names[1]="orange2"
>>> names
['apple', 'orange2', '从banana前插入', 'banana', '从banana后添加', 'watermelon', 'persimmon', 'litchi']
View Code

删除:

>>> names
['apple', 'orange2', '从banana前插入', 'banana', '从banana后添加', 'watermelon', 'persimmon', 'litchi']
>>> del names[2]
>>> names
['apple', 'orange2', 'banana', '从banana后添加', 'watermelon', 'persimmon', 'litchi']
>>> del names[3]
>>> names
['apple', 'orange2', 'banana', 'watermelon', 'persimmon', 'litchi']
>>> names.remove("orange2") #删除指定元素
>>> names
['apple', 'banana', 'watermelon', 'persimmon', 'litchi']
>>> names.pop()    #删除列表最后一个值
'litchi'
>>> names
['apple', 'banana', 'watermelon', 'persimmon']
View Code

扩展:

>>> names
['apple', 'banana', 'watermelon', 'persimmon']
>>> b = [1,2,3]
>>> names.extend(b)
>>> names
['apple', 'banana', 'watermelon', 'persimmon', 1, 2, 3]
View Code

拷贝:

>>> names
['apple', 'banana', 'watermelon', 'persimmon', 1, 2, 3]
>>> names_copy = names.copy()
>>> names_copy
['apple', 'banana', 'watermelon', 'persimmon', 1, 2, 3]
View Code

统计:

>>> names
['apple', 'banana', 'watermelon', 'persimmon', 1, 2, 3]
>>> names.count("apple")
1
View Code

排序&翻转:

>>> names
['apple', 'banana', 'persimmon', 'watermelon', 1, 2, 3]
>>> names.sort()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'int' and 'str'
>>> names[-3] = '1'
>>> names[-2] = '2'
>>> names[-1] = '3'
>>> names
['apple', 'banana', 'persimmon', 'watermelon', '1', '2', '3']
>>> names.sort()
>>> names
['1', '2', '3', 'apple', 'banana', 'persimmon', 'watermelon']
>>> names.reverse()
>>> names
['watermelon', 'persimmon', 'banana', 'apple', '3', '2', '1']
View Code

获取下标:

>>> names
['watermelon', 'persimmon', 'banana', 'apple', '3', '2', '1']
>>> names.index("banana")
2
View Code

元组

元组:只有两个方法:count、index

names = ("oracle","mysql","db2")
>>> names = ("oracle","mysql","db2")
>>> names.index("mysql")
1
>>> names.count("db2")
1
View Code

元组

特性:不可修改 

>>> name = "Guido" 
>>> name.capitalize()  #首字母大写
'Guido'
>>> name.casefold()  #大写全部转换成小写
'guido'
>>> name.center(50,"-") 
'----------------------Guido-----------------------'
>>> name.count('do') #统计do出现的次数
1
>>> name.encode() #将字符串编码成bytes格式
b'Guido'
>>> name.endswith("ui")  #判断字符串是否以ui结尾
>>> "Gui\tdo".expandtabs(10)  #将\t转换成多长的空格
'Gui       do'
False
>>> name.find('G') 查找A,找到返回其索引,找不到返回-1
0
View Code

 2:字符串操作

 特性-不可修改

>>> name = "Guido"
>>> name.capitalize() #首字母大写
'Guido'
>>> name.casefold() #大写全部转换成小写
'guido'
>>> name.center(50,"-")
'----------------------Guido-----------------------'
>>> name.count('do') #统计do出现的次数
1
>>> name.encode() #将字符串编码成bytes格式
b'Guido'
>>> name.endswith("ui") #判断字符串是否以ui结尾
>>> "Gui\tdo".expandtabs(10) #将\t转换成多长的空格
'Gui do'
False
>>> name.find('G') 查找A,找到返回其索引,找不到返回-1
0

format :
>>> msg = "my name is {},and age is {}"
>>> msg.format("James",58)
'my name is James,and age is 58'
>>> msg = "my name is {1},and age is {0}"
>>> msg.format("James",58)
'my name is 58,and age is James'
>>> msg = "my name is {name},and age is {age}"
>>> msg.format(age=22,name="james")
'my name is james,and age is 22'
>>>
format_map:
>>> msg.format_map({'name':'james','age':58})
'my name is james,and age is 58'
>>> msg.index('i')
8
>>> '10abc'.isalnum()
True
>>> '10'.isdigit()
True
>>> name = "Scott"
>>> name.isnumeric()
False
>>> name.isprintable()
True
>>> name.isspace()
False
>>> name.istitle()
True
>>> name.isupper()
False
>>> "|".join(['Guido','Python'])
'Guido|Python'
maketrans:
>>> intab = "linus" #This is the string having actual characters.
>>> outtab = "12345" #This is the string having corresponding mapping character
>>> trantab = str.maketrans(intab,outtab)
>>> str = "this is string example....wow!"
>>> str.translate(trantab)
'th25 25 5tr23g examp1e....wow!'


>>> msg
'my name is {name},and age is {age}'
>>> msg.partition('is')
('my name ', 'is', ' {name},and age is {age}')

>>> "beijing is beautiful city!".replace("beijing","Beijing",1)
'Beijing is beautiful city!'
>>> msg.swapcase()
'MY NAME IS {NAME},AND AGE IS {AGE}'
>>> msg.zfill(40)
'000000my name is {name},and age is {age}'
>>> name ="Hello,world!"
>>> name.ljust(40,"-")
'Hello,world!----------------------------'
>>> name.rjust(40,"-")
'----------------------------Hello,world!'

>>> b="guido_james_天才"
>>> b.isidentifier() #检测一段字符串可否被当作标志符,即是否符合变量命名规则
True

 

 

 

posted on 2017-02-04 14:42  HelonTian  阅读(158)  评论(0编辑  收藏  举报