Python learning 3: List, tuples, and python learning list1. List: 1. How to define a list:
list1 = [1,2,3,4,"hello","world"]
As shown above, list1 is a list. The content of the list is enclosed in brackets.
print(list1[2])
The value is calculated using the following table. The following table starts from 0, returns the subscript numbered 2, and the obtained value
running the Func () functionreturnFunc (*args, * *kw)returnWrapper@log#equivalent to execute now = log (now)defNow ():#calling Now () will execute the wrapper () function returned in the log () functionPrint '2013-12-25* * Case 2 (decorator with parameters, three levels of nesting):ImportFunctoolsdefLog (text):#First Level nestingdefDecorator (func):#Second Level nesting@functools. Wraps (func)defWrapper (*args, **kw):#Third Layer NestingPrint '%s%s ():'% (text, func.)__name__)#print a row of l
() is the return list in P2, which copies a copy of the memory, P3 in the generator, does not replicate within the P2 can be used D.iteritems ()6. Dictionary RestrictionsThe key of the dictionary cannot be duplicated, and can be hash, unordered7. Default DictionaryFrom collections Import DefaultdictD1 = defaultdict (list) #default初始化时, need to pass in a function, this function is also called the factory function, when we access the key by subscript d
the value of key is the set typec['a'].add (1) c['a'].add (2) c['a'].add (2) c['b'].add (4)Print('the value of key is a dictionary of the list type:', D)Print('the value of key is a dictionary of the set type:'C>>> ================================ RESTART ================================>>>the value of key is a dictionary of list type: Defaultdict (class 'List', {'b': [4],'a': [1, 2, 2]}) The value of key is a dictionary of the set type: Defaultdict (class 'Set', {'b': {4},'a': {1, 2}})>>>One t
A python conditional statement determines which block of code is executed by judging the execution result of one or more conditional statements.The IF statement in Python is used for control program execution in the basic form:If judging condition:Execute statement ...ElseExecute statement ...#coding =utf8A = 2If a > 0:print ' A greater than 0 'Elseprint ' A is less than 0 'The judgment condition of the IF
Put together the code, and gradually add:
Import OS
#1.os.getcwd() #获取当前目录
print os.getcwd() #示例
print os.getcwd()+‘\\resources\\baidu.txt‘ #获取当前目录下的resources文件夹下的baidu.txt文件
print ‘----当前目录.形式:‘,os.curdir #返回当前目录(‘.‘)
#2.os.listdir(dirname) #列出dirname下的目录和文件
print ‘----当前目录下所有文件:‘,os.listdir(os.getcwd())#相当于在os.getcwd()目录下执行dir命令,返回为list
Description1. Import three modules, Urllib.request.urlopen is used to open the URL link, urllib.parse the UrlEncode method to convert the data object in the browser network to the second parameter of Urlopen data,json.loads () method to convert the string returned by Response.read to JSON format.2. The data object is all the attributes in the bottom formdata of the headers in the translate from the review element's network.3. The data in Urlopen (Url,
factors other than the data set.2) orthogonal between the main components, can eliminate the interaction between the original data components of the factors.3) Calculation method is simple, the main operation is eigenvalue decomposition, easy to achieve.The main drawbacks of PCA algorithms are:1) The meaning of each characteristic dimension of principal component has certain fuzziness, which is not better than the interpretation of original sample ch
numbers, over 100 of the time, to stop the program. The range of the random number 1-20 is generated, requiring the recording of the resulting random number, as well as the last and the number of random numbers.Mode 1:import randomresult =0random_num_list =[]while 1: random_num = random.randint(1,20) random_num_list.append(random_num ) result+=random_num if result >100: breakprint("一共产生了 %s 个随机数:" %len(random_num_list))print("产生随机数如下:",random_num_list)print("最后的随机数之和:",result
1. Q: Briefly describe the relationship between Unicode and utf-8 in Python? What are the commonly used functions? A: Unicode is a set of character encodings that define a unique encoding for each character; Utf-8 is an implementation of Unicode that uses variable-length encoding to store characters; commonly used functions:1) Decode: implementation of conversion from other encodings to Unicode encoding2) Encode: Enables conversion from Unicode to oth
, True, color_red)TextRectObj = textSurfaceObj. get_rect ()Pygame. display. update ()
If _ name _ = '_ main __':Main () 3. Key knowledge points:
Sound processing uses the pygame. mixer. music module, which provides a wide range of methods, as follows:
[Plain]Pygame. mixer. music. loadNotes: loading music filesPrototype: pygame. mixer. music. load (filename): return NonePygame. mixer. music. playViews: plays music.Prototype: pygame. mixer. music. play
are matchedPrint (Re.findall ("A.C", "123131asc,ssf\nafaa-c", re. Dotall))? Match 0 or one, there must match, if not also canPrint (Re.findall (' ab? ', "AA Ssafsfaabbssa"))* Match 0 or infinity + match one or infinity {n,m}print (Re.findall (' \d+. \d+ '). * Greedy match, select the most valuePrint (Re.findall ("A.*c", ' assfafasfcssssfafsfsf111223c '))Match to assfafasfcssssfafsfsf111223c.A non-greedy match that matches the value of a compound that is less earlyPrint (Re.findall ("A.C ", ' as
Ashamed, this topic is to refer to other people's blog made out, the main difficulty in1. How to get a zip file2.comments is a thing, the original author also in the blog "What the hell is the comments?"From this problem, I find that1. The information is not necessarily only on this page, you can modify the URL to obtain additional information.2. For information that you cannot understand in the tip, such as "comments" in the subject, you can try searching in the
I. Standard Functions
Cmp (): Compare the size
Str (): Convert to string
Type (): type
Cmp (...) cmp (x, y)-> integer Return negative (negative) if x
As follows:
>>> cmp(5,3.2)1>>> cmp(3.5,8)-1
Ii. Conversion factory Functions
Loss of Precision
>>> int(1.847)1>>> long(42)42L>>> float(42)42.0>>> complex(42)(42+0j)>>> complex(2.4,-8)(2.3999999999999999-8j)
Iii. Functions
Used for numerical calculation: asb (), coerce (), divmod (), pow (), round ()
Asb (): absolute; complete; authoritarian; n: ab
Familiar with Python (3) and familiar with python
String is a topic center.
ID of a string
In many cases, we need to perform operations on each character in the string (see the following content for details). To perform operations accurately, we must perform the task of numbering the characters. For example, if there are 50 students in a class and all of these st
to understand with the following code)
#!/usr/bin/python #-*-Coding:utf-8-*-#1 a = {"name": ' Ljs ', "age":, ' weight ': "Print"% (name) s% (age) d (weigh t) D; "
%a #ljs 22 75; #将已经定义的变量扩展到字符串中 name = ' Ljs ' age = Weight = + R = '% (name) s% (age) d (weight) D; "
%vars () print R #ljs 22 75; The print VARs () #vars () function contains a dictionary of all variables defined at this time #2 and 3 a =
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.