【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
>>> 










  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值