First impression of Python, great !, Python first impression method
To use flask to develop web applications, I started to read a little Python over the past two days. I haven't seen the part of my website background written in Python, Which is attracted by its powerful data processing capabilities and syntax flexibility. It must be a little strange to me, but when I see the flexible use of these features of Python, I can't help but like it when I write some cool code. Let's write one or two examples to deepen your memory and praise Python. (The Code draws on the ideas of great gods)
1. Remove the number of input files from 1 to 1000 and print them.
To determine whether a number is a retrieval number, I must have written all the programming skills. When I was still thinking about getting each digit of an integer and comparing it with the original number in reverse order, I saw a line of code in the comment area .. Now I am writing Python, not Java !! Code:
Def is_palindrome (n): return str (n) = str (n) [:-1] output = filter (is_palindrome, range )) print (list (output ))
Convert integers into strings, and use Python slices, reverse-order strings, and comparison... Done!
2. format the name of a list in upper case and lower case.
Slice is also used flexibly. Code:
L1 = ["AdaM", "sMith", "JoBs", "BRYANT"] def formatname (name): name = name [0]. upper () + name [1:]. lower () return nameL2 = map (formatname, L1) print (list (L2 ))
Server! This uses Java to write down how many lines are needed... I am not black, I am big Java, but Java powder... However, Python is much more convenient.
* ** Note: filter and map are high-order functions provided by Python. They receive a function, and a list is used as a parameter. Each element in the list is computed using the received function.