In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the practical quick grammar skills of python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the practical quick grammar skills of python"?
Preface
Do you know what aura 3 means?
Do you know how to print the module path?
Do you know how the reduce () function can be used to concatenate strings with lists?
But you probably know all about it.
But look down, there may be python tips you don't know to learn it to make you more than double your efficiency!
In judgment
It can be used directly to determine whether a variable is in the list.
We can validate multiple values in the following ways:
If 7 in: print ("fat science") answer: fat science does not have to use: if mechnology 1 or melee 3 or melee 5 or mumbo 7: four ways to flip strings / lists flip the list itself testList = [1m 3L5] testList.reverse () print (testList) #-> [5,3,1] flip and iterate out for element in reversed in a loop ([1ml3] ): print (element) # 1-> 5pm 2-> 3pm 3-> 1 line code flips the string "Test Python" [::-1] # output "nohtyP tseT" more examples: > > a = [1, 2, 5, 4, 5, 5, 6, 7) > > b = (1, 2, 5, 5, 5, 6, 7) > > c='Let me show you a little thing' > a [::-1] [7, 6, 5, 4, 3, 2, 1] > > b [:-1] (7, 6, 5, 4, 3, 3) 2, 1) > c [::-1] 'gniht elttil a uoy wohs em teL' > a [1, 2, 3, 4, 5, 6, 7] > b (1, 2, 3, 4, 5, 6, 7) > > c'Let me show you a little thing' > > a.reverse () > > a [7, 6, 5, 4, 3, 2, 1] relative to reverse The slicing method does not change the structure of the list So this is a useful technique in practical application, using slice flip list [1,3,5] [::-1] # output [5Power3j1] square a=5a**2=25a**3=125 binary conversion dec = input ('10 digits:) print ("convert to binary to:", bin (dec)) print ("convert to octal to:", oct (dec)) print ("convert to hexadecimal to:" Hex (dec) string1 = '101010'print (' convert a binary string to a decimal number as:' Int (string1,2)) string1 = '367'print (' octal string converted to decimal number:', int (string1,8)) string3 = 'FFF'print (' hexadecimal string converted to decimal number:', int (string1,16)) converts lowercase letters in the string to uppercase letters str = "this is string example from runoob.wowned characters!" The output of the above instance of print ("str.upper ():", str.upper ()) is as follows: str.upper (): THIS IS STRING EXAMPLE FROM Runo. You can initialize multiple variables at one time using the list List = [1meme 2mage3] xPoweryListprint (xmemymemz) #-> 1 2 3 print module path import socketprint (socket) # list to remove repetition list0 = [1mem2pyr3, 2, 2, 5, 6, 5] list0 = list (set (list0)) > > list0. 6] Dictionary and list derivation # list 1 = [[0 for i in range (4)] for i in range (4)] # generate 2D list print (l) # [[0,0,0,0], [0,0,0,0], [0,0,0,0], [0,0,0] 0]] # Dictionary derives testDict = {I * i for i in xrange (10)} testSet = {I * 2 for i in xrange (10)} print (testSet) print (testDict) # set ([0,2,4,6,8,10,12,14,16,18]) # {0: 0,1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64 9: 81} concatenation string you may have seen this concatenation method a = "I" b = "love" c = "you" print (a+b+c) look at this l = ['axiajiaoyuzhongbaozhongjingc'] print (' 'join (l)) # using the characters to the left of join as a split circular enumeration index, it is easy to find the subscript and the corresponding elements list = [10,20,30] for I, value in enumerate (list): print (I,':': Value) # 1-> 0: 10: 2-> 1: 20: 3-> 2: 30 enable file sharing
Python allows you to run a HTTP server to share files from the root path. The command opens a server at the default port of 8000, and you can pass a custom port number to the command above as the last parameter. The following is the command to turn on the server
Python3-m http.server skillfully uses else statement (important)
Python's else clause can be used not only in if statements, but also in for, while, and try statements. This language feature is no secret, but it has not been taken seriously.
L = [1, for i in l, 2, 3, 4, 5] for i in l: print: print: breakelse: print, split
Python split () slices the string by specifying a delimiter, and if the parameter num has a specified value, only num substrings are separated.
Syntax:
Str.split (str= "", num=string.count (str))
Simplify:
Str.split ("") reduce
Reduce acts a function on a sequence [x1, x2, x3,...] This function must take two arguments, and reduce accumulates the result with the next element of the sequence
Simple example:
> from functools import reduce > def fn (x, y): return x * 10 + y > reduce (fn, [1,3,5,7,9]) 13579 We combine the latest knowledge to do one problem.
Enter an int integer and return a new integer without repeating numbers in right-to-left reading order.
Result= "" for i in input () [::-1]: if i not in result: result+=iprint (result) sorted > sorted ([36, 5,-12, 9,-21]) [- 21,-12, 5, 9, 36] # you can receive a key function to achieve custom sorting, such as sorting by absolute size: > > sorted ([36, 5,-12, 9,-21], key=abs) [5, 9,-12,-21]
Let's look at another example of string sorting:
> sorted (['bob',' about', 'Zoo',' Credit']) ['Credit',' Zoo', 'about',' bob']
Pass the key function to sorted to achieve the sort that ignores case:
> sorted (['bob',' about', 'Zoo',' Credit'], key=str.lower) ['about',' bob', 'Credit',' Zoo']
To reverse sort, you don't need to change the key function, you can pass in the third parameter, reverse=True:
> sorted (['bob',' about', 'Zoo',' Credit'], key=str.lower, reverse=True) ['Zoo',' Credit', 'bob',' about'] executes a string expression and returns the value of the expression > x = 7 > eval ('3 * x') 21 > eval ('pow (2Magne2)') 4 > > eval ('2 + 2') 4 > nag81 > > eval ("n + 4") 85 to this point I believe that you have a deeper understanding of "what are the practical quick grammar skills of python?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.