To develop web apps with flask, these two days began to look a little bit python. I haven't seen the part of writing a website backstage with Python, and it's attracted by its powerful data processing power and grammatical flexibility. It must have been my more strange, but I couldn't help but see the flexibility of using Python's features to write some artifice-like code. Here are one or two examples to deepen your memory and compliment python. (The code draws on the ideas of the Great gods.)
1. Remove the number from 1 to 1000 that is not a palindrome number, and then print.
Judge whether a number is a palindrome number, must have learned the programming has written. When I was thinking about taking every bit of an integer, and then adding up in reverse and comparing the original number to equal, I saw the comment area of the big God Line of code to fix, crazy. Now it's writing python, not java!!. Post code:
def is_palindrome (n): return str (n) ==str (n) [:: -1]output=filter (Is_palindrome,range (1,1000))print( List (output))
convert integers to strings, flexibly use python slices, reverse strings, compare ... Get!
2. Format the name in a list: capitalize the first letter and lower case .
It is also flexible to use slices. Code :
l1=["AdaM","sMith","JoBs", " BRYANT " ]def FormatName (name): name=name[0].upper () +name[1:].lower () return nameL2=map (formatname,l1)print(list (L2))
Clothing! This is written in Java how many lines ... Not black me big Java, I am Java powder ... But Python is much more convenient.
Note: Filter and map are high-order functions provided by Python, which receive a function, a list as a parameter, and a function for each element in the table.
Python First impressions, Dafa good!