Python字符串
Python中不支持char单字符类型,单字符在Python中也是一个字符串
Python字符串更新
更新Python字符串方法
1 2 3 4
var1 = 'Hello World!' print "Updated String :- " ,var1[:6 ] + 'Python'
实际执行效果为
Updated String :- Hello Python
Python转义字符
Python字符串运算符
Python字符串格式化
Python三引号(triple quotes)
python中三引号可以将复杂的字符串进行复制:
python三引号允许一个字符串跨多行,字符串中可以包含换行符、制表符以及其他特殊字符。
Python字符串函数
python字符串内建
string.capitalize() 首字母大写
string.count(str,beg=0,end=len(string)) 返回beg与end之间的str在string中出现的次数
string.decode(encoding=’UTF-8’,errors=’strict’) 以encoding指定的编码格式解码string,如果出错默认报一个ValueError的异常,除非errors指定的是’ignore’或者’replace’
string.encode(encoding=’UTF-8’,errors=’strict’) 以encoding指定的编码格式编码string
string.endswith(obj,end=len(string))
string.find(str,end=len(string)) 没有返回-1
string.isalnum() 如果string至少有一个字符并且所有字符都是字母或数字则返回True,否则返回False
……
Python逻辑语句
Python条件语句
注意Python语句中的括号及语句块的使用,另外Python中没有switch语句只能使用elif替代:
1 2 3 4 5 6 7 8 9 10 11
num = 5 if num == 3 : print 'boss' elif num == 2 : print 'user' elif num == 1 : print 'worker' elif num < 0 : print 'error' else : print 'roadman'
Python循环语句
Python中支持while循环和for循环(不支持do while)。
while循环
1 2 3 4 5 6 7 8 9 10 11 12 13
i = 1 while i < 10 : i += 1 if i%2 > 0 : continue print i **该条件永远为true,循环将无限执行下去** var = 1 while var == 1 : num = raw_input("Enter a number :" ) print "You entered: " ,num
如果循环体只有一条语句可以与while写在同一行
For循环
1 2 3 4 5 6 7 8 9 10
for letter in 'Python' : print 'Current Letter :' ,letter fruits = ['banana' ,'apple' ,'mango' ] for fruit in fruits: print 'Current fruit :' ,fruit fruits = ['banana' ,'mango' ] for index in range(len(fruits)): print 'Current fruit :' ,fruits[index]
Python break语句 break语句打破当前循环不继续执行,如果循环嵌套,打破代码所在层的循环并执行外层循环。Python continue continue语句跳出本次循环,继续执行下一次循环Python pass pass语句是空语句,保证程序结构完整性,不做任何处理,占位
Python集合
python集合包括List、Tuple和Dictionary
Python中的List
详见python文件
更新list元素
1 2
list1[1 ] = 'math' print('list1[1]:' ,list1[1 ])
删除list元素
1 2
del list3[len(list3) - 1 ]print('list3:' ,list3)
Python列表脚本操作符
1 2 3 4 5 6 7 8 9 10 11
print('--------Python列表脚本操作符--------' ) print([1 ,2 ,3 ] + [4 ,5 ,6 ]) print(len([1 ,3 ])) print(['Hi!' ] * 4 ) print(3 in [1 ,3 ]) for x in [1 ,3 ]: print(x) L = ['spam' ,'Spam' ,'SPAM!' ] print(L[2 ]) print(L[-2 ]) print(L[1 :])
Python列表函数&方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
test1 = [1 ,3 ,4 ,6 ] aTuple = (123 ,'xyz' ,'zara' ,'abc' ) print(len(test1)) print(max(test1)) print(list(aTuple)) test1.append(7 ) test1.count(1 ) test1.extend(list(aTuple)) test1.index(4 ) test1.insert(4 ,4 ) test1.pop() test1.remove(1 ) print(test1) test1.reverse()
Tuple特性与List相似但不能更新
Python字典(Dictionary)
字典是另一种可变容器模型,且可存储任意类型对象,如其他容器模型。 字典由键和对应值成对组成。字典也被称作关联数组或哈希表。基本语法如下:
访问字典里的值
1 2 3
mydict = {'Name' : 'Zara' ,'Age' : 7 ,'Class' : 'First' }; print("dict['Name']: " ,mydict['Name' ]) print("dict['Age']: " ,mydict['Age' ])
修改字典
1 2 3
mydict['Age' ] = 8 mydict['School' ] = "DPS School" print('mydic:' ,mydict)
删除字典元素 字典值可以没有限制地取任何python对象,既可以是标准的对象,也可以是用户定义的,但键不行
1 2
del mydict['Name' ] mydict.clear()
字典内置函数&方法 详情戳这
1 2 3 4 5 6 7 8 9
dict1 = {'Name' : 'Zara' ,'Age' : 7 } dict2 = {'Name' : 'Mahnaz' ,'Age' : 27 } print(len(dict1)) print(str(dict2)) print(type(dict1)) dict3 = dict1.copy() dict1.clear() print(dict3.get('Name' ,'defalut = None' )) print(dict3.keys())
原文:大专栏 ?Python基础语法—字符串&语句&集合
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!