[Python] Python Classic Examples

Source: Internet
Author: User

Python Classic Example 1

Title: If a number is exactly equal to the sum of its factors, this number is called the "end number". For example, 6=1+2+3. Programming to find all the finished numbers within 1000.

#!/usr/bin/python#-*-Coding:utf-8-*- fromSysImportStdOut forJinchRange2,1001): K = [] n =-1s = j forIinchRange1, j):ifJ% i = =0: n + =1S-= i k.append (i)ifs = =0:PrintJ forIinchRange (N): Stdout.write (str (k[i))) Stdout.write ("')PrintK[n]
2

Monkey Eat Peach Problem: The first day the monkey picked up a number of peaches, ate half, not addicted, and ate a second morning and the rest of the peach eaten half, and eat one more. Every morning after eating the rest of the day half of the zero one. When I want to eat again in the morning of the 10th day, I see only one peach left. For the first day to pick a total number.

#!/usr/bin/python# -*- coding: UTF-8 -*-1forin range(9,0,-1):    12    x2 = x1print x1
range(self, begin, stop, step)

A simple math problem.

3

Title: a 5-digit number, judging whether it is a palindrome number. That is, 12321 is a palindrome number, single-digit and million-bit the same, 10 bits and thousands of the same.

#!/usr/bin/python# -*- coding: UTF-8 -*-a = int(raw_input("请输入一个数字:\n"Trueforin range(len(x)/2):    if1]:        False        breakif flag:    print"%d 是一个回文数!" % aelse:    print"%d 不是一个回文数!" % a

You can use a negative number to represent the backward forward count.

4

Title: Output The value of the list in reverse order.

a = [‘one‘‘two‘‘three‘]forin a[::-1]:    print (i)
a[::-1] #表示从-1开始遍历
5

Title: Separate the list by commas.

L = [1,2,3,4,5‘,‘forin L)print (s1)
join #表示以什么为间隔点,产生新的字符串。
6

Displays the font color.

#!/usr/bin/python# -*- coding: UTF-8 -*-class bcolors:    ‘\033[95m‘    ‘\033[94m‘    ‘\033[92m‘    ‘\033[93m‘    ‘\033[91m‘    ‘\033[0m‘    ‘\033[1m‘    ‘\033[4m‘print"警告的颜色字体?" + bcolors.ENDC
Format: \033[display mode; foreground color; M Description: Foreground background color--------------------------------------- -                 +Black to                 ARed +                 theGreen -                 +Yellow color the                 -Blue *                 $Purplish red $                 $Cyan BluePanax Notoginseng                 -The meaning of white display means-------------------------0Terminal default settings1Highlight Display4Use underline5Flashing7Anti-white display8Example not visible: \033[1; to; +M <!--1-Highlight to-Front color red +-Black-->\ background color033[0M <!--uses the terminal default setting, which cancels the color setting--
7

Title: Output an array in reverse order.

Program analysis: Swap with the first and last one.

if‘__main__‘:    a = [9,6,5,4,1]    N = len(a)     print a    forin2):        11],a[i]    print a

[Python] Python Classic Examples

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.