First impression of Python, great !, Python first impression method

Source: Internet
Author: User

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.

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.