Python notes basic article-day3

Source: Internet
Author: User

I. Collection (unordered, de-duplication, relationship)
1. Create a Collection

Test = {"abc", "Def"}
Test = set () #创建空集合
Test = set ([AA,BB,CC,DD]) #创建集合
2. Set (set) method


# Add method
s = Set ()
S.add (ABC)
S.add (ABC)
S.add (ABC)
Print (s)
#==> {ABC}
# set does not save duplicate items, when add exists, do not add

# comparison method
# difference Method
S1 = {AA,BB,CC}
S2 = {BB,CC,DD}
diff = s1.difference (s2)
Print (diff)
#==> {AA}
# S1 vs S2, output S1 exists and S2 does not exist


# symmetric_difference
S1 = {AA,BB,CC}
S2 = {BB,CC,DD}
s = s1.symmetric_difference (s2)
Print (s)
#==> {AA, DD}
# S1 vs. S2, output S1 and S2 do not exist with each other value


# difference_update
S1 = {AA,BB,CC}
S2 = {BB,CC,DD}
s = s1.difference_update (s2)
Print (S,S1,S2)
#==>none {AA} {cc, DD, BB}
# This method does not return a value, so the S value is none, and the value of the nonexistent S2 in the S1 is re-assigned to S1


# symmetric_difference_update
S1 = {AA,BB,CC}
S2 = {BB,CC,DD}
s = s1.symmetric_difference_update (s2)
Print (S,S1,S2)
#==> None {AA, DD} {cc, DD, BB}
# The same Update method does not return a value, re-assigns the value S1 and S2 to each other that does not exist S1

# Remove method
# Discard
S1 = {AA,BB,CC}
S1.discard (AA)
S1.discard (DD)
Print (S1)
# ==> {cc, BB}
#移除元素方法, no error is removed when no element exists


# Remove
Same as discard, remove non-existent element, error
So it is recommended to use discard


# Pop
S1 = {AA,BB,CC}
s = S1.pop ()
Print (s)
Print (S1)
#==> cc {AA,BB}
# randomly removes an element, and gets the element


# intersection
# intersection intersection, find the intersection of two set
S1 = {AA,BB,CC}
S2 = {BB,CC,DD}
s = s1.intersection (s2)
Print (S,S1,S2)
#==> {cc, bb} {cc, AA, BB} {cc, DD, BB}


# and set
# Union and set
S1 = {AA,BB,CC}
S2 = {BB,CC,DD}
s = s1.union (s2)
Print (S,S1,S2)
#==> {cc, BB, AA, DD} {cc, AA, BB} {cc, DD, BB}


# Set Set can iterate update
# Update method
S1 = {AA,BB,CC}
S1.update ([dd,ff,66])
Print (S1)
# ==> {cc,%, AA, DD, BB, FF}
# The Update method can pass in an object that can be iterated

Two. function (reuse, return value)

def s1 ():
Print (' Hello World ')
S1 ()
#显示: Hello World


#函数的参数传入
def s1 (name):
Print (' My name is%s '%name)

S1 (' Alax ')
#显示: My name is Alax

#函数的多参数调用 (actual participation parameter corresponds)
def s1 (name,age,what):
Print (' My name is%s,age-%s, is%s '% (name,age,what))
S1 (' Alax ', ' Good Man ')
#显示: My name is Alax,age is at Good man

#函数的不同位置实参的传入 (You need to specify the corresponding parameter for the argument)
def s1 (name,age,what):
Print (' My name is%s,age-%s, is%s '% (name,age,what))

S1 (age=18,what= ' Good Man ', Name= ' Alax ')
#显示: My name is Alax,age, was good man


#函数形参的默认值 (formal parameters with default values must be placed at the end)
def s1 (name,age,what= ' Good Man '):
Print (' My name is%s,age-%s, is%s '% (name,age,what))

S1 (age=18,name= ' Alax ')
#显示: My name is Alax,age, was good man

S1 (age=18,what= ' Bad Man ', Name= ' Alax ')
#显示: S1 (age=18,what= ' Bad Man ', Name= ' Alax ')


#函数的动态参数, the arguments are placed in a tuple
def test (*args):
Print (Args,type (args))

Test (' abc ')
#显示 (' abc ',) <class ' tuple ' >

#函数的动态参数, the passed-in argument is a set of elements, and the entire Li list is placed in a tuple
def test (*args):
Print (Args,type (args))

Li=[aa,bb,cc,de]
Test (LI)
#显示 ([AA, BB, cc, de],) <class ' tuple ' >

A list of #传入带一个 *, placing each element in the Li list in a tuple
def test (*args):
Print (Args,type (args))

Li=[aa,bb,cc,de]
Test (*li)
#显示: (AA, BB, cc, DE) <class ' tuple ' >
Test (* ' Alex ')
#显示:(' a ', ' l ', ' e ', ' x ') <class ' tuple ' >
#总结: Adding a ' * ' before the argument is equivalent to adding a for loop through each element of the argument and putting it in a tuple


#动态参数形参含两个 *, key-value pairs are passed in and saved as a dictionary
def f2 (**kwargs):
Print (Kwargs)

F2 (n1= ' Alex ', n2=18)
#显示: {' N1 ': ' Alex ', ' N2 ': 18}

dic={' K1 ': ' v1 ', ' K2 ': ' V2 '}
F2 (kk=dic) #{' kk ': {' K1 ': ' v1 ', ' K2 ': ' V2 '}}
#显示 {' kk ': {' K1 ': ' v1 ', ' K2 ': ' V2 '}}

#万能参数
def f1 (*args,**kwargs):
Print (args)
Print (Kwargs)
F1 (aa,bb,cc,dd,ff,k1= ' AA ', k2= ' BB ')
#运行结果 (AA, BB, CC, DD, 123)
{' K1 ': ' AA ', ' K2 ': ' BB '}


#formate传参方式
#方式1
S1= ' I am {0},age{1} '. Format (' Alex ', 18)
Print (S1)
#显示: I am alex,age18

#方式2
S2= ' I am {0},age{1} '. Format (*[' Alex ',]) #传列表
Print (s2)
#显示i am Alex,age18

#方式3
S1= ' I am {name}, age{age} '. Format (name= ' Alex ', age=18) #字符串中name与age后面一定要加上name = ', age= for designation
Print (S1)
#显示: I am Alex, age18

#方式4
dic={' name ': ' Alex ', ' age ': 18}
S2= ' I am {name},age{age} '. Format (**dic) #传字典要加 * *
Print (s2)
#显示: I am alex,age18


#传过来的形参可使用方法不能重新赋值, it is recommended that the global variable name be capitalized
#传的是引用 is not a copy
def f5 (A1):
A1.append (' abc ')
LI=[AA,BB,CC,DD]
F5 (LI)
#显示: [AA, BB, CC, DD, ' ABC ']


1 #全局变量, all scopes, readable, "re-assigned" to global variables, need to add global, Special: list, dictionary, modifiable, not re-assigned
2 def f1 ():
3 Global name# indicates that name is a globals variable
4 name= ' abc '
5 Print (name)
6
7 name= ' De6 '
8 F1 ()
9 #显示: ABC

Python notes basic article-day3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.