Python基础--字符串str/序列/全局标志位/全局变量/内嵌函数和闭包
Posted 吴英强
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python基础--字符串str/序列/全局标志位/全局变量/内嵌函数和闭包相关的知识,希望对你有一定的参考价值。
字符串str
>>> str1 = 'i love you, wuyq'
>>> str1[:5]
'i lov'
>>> str1
'i love you, wuyq'
>>> dir(str)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>>
>>> "0 love 1".format("I", "wuyq")
'I love wuyq'
>>> "a love b".format(a = "I", b = "python")
'I love python'
>>>
>>> '%c' % 97
'a'
>>> '%c %d %x' % (97, 98, 10)
'a 98 a'
>>> '%c %d %X' % (97, 98, 10)
'a 98 A'
>>>
序列
>>>Help(list)
>>> numbers = [1, 3, 4, 6, 9, 12, -32]
>>> numbers
[1, 3, 4, 6, 9, 12, -32]
>>> sum(numbers)
3
>>> sum(numbers, 5)
8
>>> chars = '1234567'
>>> sum(chars)
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
sum(chars)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> sorted(numbers)
[-32, 1, 3, 4, 6, 9, 12]
>>> reversed(numbers)
<list_reverseiterator object at 0x000000000350B518>
>>> list(reversed(numbers))
[-32, 12, 9, 6, 4, 3, 1]
>>> enumerate(numbers)
<enumerate object at 0x0000000003575D38>
>>> list(enumerate(numbers))
[(0, 1), (1, 3), (2, 4), (3, 6), (4, 9), (5, 12), (6, -32)]
>>> a = [1, 2, 3, 4, 5, 6, 7, 8]
>>> b = [a, b, c, d]
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
b = [a, b, c, d]
NameError: name 'b' is not defined
>>> b = [4, 5, 6, 7, 8]
>>> zip(a, b)
<zip object at 0x000000000356B4C8>
>>> list(zip(a, b))
[(1, 4), (2, 5), (3, 6), (4, 7), (5, 8)]
>>>
全局标志位
>>> CON = 0
>>> def myfun():
global CON
print(CON)
CON += 1
if (CON % 2):
print('111')
else:
print('222')
return
>>> myfun()
0
111
>>> myfun()
1
222
>>>
全局变量
def discounts(price, rate):
final_price = price * rate
#old_price = 88 #这里试图修改全局变量
global old_price
old_price = 88 #这里试图修改全局变量
print('11修改后old_price的值是:', old_price)
return final_price
old_price = float(input('请输入原价:'))
rate = float(input("请输入折扣率:"))
new_price = discounts(old_price, rate)
print('22修改后old_price的值是:', old_price)
print('打折后价格是:', new_price)
内嵌函数和闭包
>>> def fun1():
print("fun1()....")
def fun2():
print("fun2()----")
fun2()
>>> fun1()
fun1()....
fun2()----
>>>
>>> def Fun1():
x = [5]
def Fun2():
x[0] *= x[0]
return x[0]
return Fun2()
>>> Fun1()
25
>>> def Fun1():
x = 5
def Fun2():
x *= x
return x
return Fun2()
>>> Fun1()
Traceback (most recent call last):
File "<pyshell#105>", line 1, in <module>
Fun1()
File "<pyshell#104>", line 6, in Fun1
return Fun2()
File "<pyshell#104>", line 4, in Fun2
x *= x
UnboundLocalError: local variable 'x' referenced before assignment
>>> def Fun1():
x = 5
def Fun2():
nonlocal x
x *= x
return x
return Fun2()
>>> Fun1()
25
>>>
以上是关于Python基础--字符串str/序列/全局标志位/全局变量/内嵌函数和闭包的主要内容,如果未能解决你的问题,请参考以下文章
Python 文本序列类型(字符串类型 str)[学习 Python 必备基础知识][看此一篇就够了][长字符串][原始字符串 r][字符串常用方法]
Python 文本序列类型(字符串类型 str)[学习 Python 必备基础知识][看此一篇就够了][长字符串][原始字符串 r][字符串常用方法]