Python First impressions, Dafa good!

Source: Internet
Author: User

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!

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.