misc
global
修饰一个变量指示该变量为外部作用域的变量。如在一个函数内使用global
修饰一个变量,则该变量是外部作用域的变量而不是内部的。
x = 10
def func1():
global x
x = 100
print(x)
- 可变参数
def total(a=5, *numbers, **phonebook):
print(type(numbers))
print(type(phonebook))
print('a', a)
for single_item in numbers:
print(single_item)
for first_part, second_part in phonebook.items():
print(first_part,second_part)
total(10,1,2,3,Jack=1123,John=2231,Inge=1560)
- 可变参数中,当==直接传递变量时,就是给’ * ‘传递,为tuple;当使用’变量名 = foo’的方式来传递时,就是给’ * * ‘的,为dict==
dir()
得到模块定义的所有名称的列表- ==别忘了除了list和tuple外,还有字符串也可以进行索引==
in
、 not in
- ==集合==:
set([foo, bar])
:无序的不可重复的list - ==函数返回多个值:使用元组进行返回即可==
def funcTuple():
return 1, 2, 'tuple'
ret1, ret2, ret3 = funcTuple()
print(ret1, ret2, ret3)
"""
Spyder Editor
This is a temporary script file.
"""
import sys
print('hello, world')
print('{argName1} ..... {0}'.format('bar', argName1='foo',))
print(r'\n', '\n')
g_foo = 10
def func1():
'''
如果注释掉函数中的global语句,将会导致变量为局部变量而不是外部的全局变量
'''
global g_foo
g_foo = 100
func1()
print('g_foo is ', g_foo, '\n')
def total(a=5, *numbers, **phonebook):
print(type(numbers))
print(type(phonebook))
print('a', a)
for single_item in numbers:
print(single_item)
for first_part, second_part in phonebook.items():
print(first_part,second_part)
total(10,1,2,3,Jack=1123,John=2231,Inge=1560)
print()
print(dir(sys))
print()
str_tmp = 'tmp'
print(str_tmp[1])
set_tmp = set([1, 2, 3, 4, 5])
for iter in set_tmp:
print(iter)
print()
set_tmp.add(1)
for iter in set_tmp:
print(iter)
print()
def funcTuple():
return 1, 2, 'tuple'
ret1, ret2, ret3 = funcTuple()
print(ret1, ret2, ret3)
print([i**i for i in range(-10, 10, 3) if i % 2 == 0])