Python programming basics and case collection, classic python programming introduction

Hello everyone, this article will focus on the basics of python programming and the collection of cases. Introduction to python programming and detailed explanations of cases are things that many people want to understand. To understand the examples of python introductory programs, you need to understand the following things first.

 

【Procedure 1】

Topic: Enter a line of characters, and count the number of English letters, spaces, numbers and other characters in it.

1#!/usr/bin/python

2#-*- coding:utf-8 -*-

3#there is no ++ operator in Python

4 import string

5defmain():

6s = raw_input('input a string:')

7letter =

8 spaces =

9digit =

10other =

11forcins:

12ifc.isalpha():

13 letters+=1

14elifc.isspace():

15space+=1

16elifc.isdigit():

17digit+=1

18else:

19other+=1

20print'There are %d letters,%d spaces,%d digits and %d other characters in your string.'%(letter,space,digit,other)

21

22if__name__=='__main__':

23main()

【Procedure 2】

Topic: Find the value of s=a+aa+aaa+aaaa+aa...a, where a is a pseudo-original digital locomotive collector . For example, 2+22+222+2222+22222 (a total of 5 numbers are added at this time), and the addition of several numbers is controlled by the keyboard.

【Procedure 3】

Topic: A number is called a "perfect number" if it is exactly equal to the sum of its factors. For example 6=1+2+3. Programming

Find all perfect numbers up to 1000.

'''

【Procedure 28】

Topic: There are 5 people sitting together, how old is the fifth person? He said he was 2 years older than the 4th person. Ask the 4th person how old he is, he said he is older than the 4th

3 people are 2 years older. Ask the third person, and say that he is two years older than the second person. Ask the second person and say that he is two years older than the first person. at last

Ask the first person, he said 10 years old. How old is the fifth person?

'''

def fun(i):

if i==1:

return 10

return fun(i-1)+2

print fun(5)

'''

【Procedure 29】

Topic: Given a positive integer with no more than 5 digits, requirements: 1. Find out how many digits it is, 2. Print out the digits in reverse order.

'''

def fun(i,cnt):

if i==0:

print 'There are %d digit in the number.'%cnt

return

print i%10,

i/=10

cnt+=1

fun(i,cnt)

i = int(raw_input('Input a number:'))

fun(i,0)

'''

【Procedure 30】

Topic: A 5-digit number, judge whether it is a palindromic number. That is, 12321 is a palindrome number, the ones and ten thousand digits are the same, and the tens and thousands digits are the same.

'''

ans=['Yes','No']

i = int(raw_input('Input a number(10000~99999):'))

if i99999:

print 'Input Error!'

else:

i = str(i)

flag = 0

for j in range(0,2):

if i[j]!=i[4-j]:

flag = 1

break

print ans[flag]

'''

【Procedure 36】【Screening Method】

Topic: Find the prime numbers within 100

'''

a = [0]*101

for i in range(2,11):

for j in range(i+i,101,i):

a[j]=-1;

for i in range(2,101):

if a[i]!=-1:

print ' ',i,

'''

【Procedure 37】

Question: Sort 10 numbers

'''

print 'input 10 numbers please:'

l = []

for i in range(10):

l.append(int(raw_input('Input a number:')))

#You can use the sort function directly: l.sort()

#You can also write your own sorting code (selection sorting)

for i in range(9):

for j in range(i+1,10):

if l[j]

temp = l[d]

l[j] = l[i]

l[i] = temp

print l

'''

【Procedure 38】

Topic: Find the sum of the diagonal elements of a 3*3 matrix

'''

l = []

for i in range(3):

for j in range(3):

l.append(int(raw_input('Input a number:')))

s = 0

for i in range(3):

s += l[3*i+i]

print s

'''

【Procedure 39】

Question: There is a sorted array. Now enter a number and ask to insert it into the array according to the original rules.

'''

l = [0,10,20,30,40,50]

print 'The sorted list is:',l

cnt = len(l)

n = int(raw_input('Input a number:'))

l.append(n)

for i in range(cnt):

if n

for j in range(cnt,i,-1):

l[j] = l[j-1]

l[i] = n

break

print 'The new sorted list is:',l

'''

【Procedure 40】

Question: Output an array in reverse order.

'''

a = [1,2,3,4,5,6,7,8,9]

l = only

print a

for i in range(l/2):

a[i],a[li-1] = a[li-1],a[i] #Note this clause

print a

'''

【Procedure 56】

Topic: Draw a circle [Tkinter module]

'''

if __name__ == '__main__':

from Tkinter import *

canvas = Canvas(width=800, height=600, bg='red')

canvas.pack(expand=YES, fill=BOTH)

k = 1

j = 1

for i in range(0,26):

canvas.create_oval(310 - k,250 - k,310 + k,250 + k, width=1)

k += j

j += 0.3

mainloop()

'''

【Procedure 57】

Question: Draw a straight line.

1. Program analysis:

2. Program source code:

'''

if __name__ == '__main__':

from Tkinter import *

canvas = Canvas(width=300, height=300, bg='green')

canvas.pack(expand=YES, fill=BOTH)

x0 = 263

y0 = 263

y1 = 275

x1 = 275

for i in range(19):

canvas.create_line(x0,y0,x0,y1, width=1, fill='red')

x0 = x0 - 5

y0 = y0 - 5

x1 = x1 + 5

y1 = y1 + 5

x0 = 263

y1 = 275

y0 = 263

for i in range(21):

canvas.create_line(x0,y0,x0,y1,fill = 'red')

x0 += 5

y0 += 5

y1 += 5

mainloop()

'''

【Procedure 58】

Question: Draw a square.

'''

if __name__ == '__main__':

from Tkinter import *

root = Tk()

root.title('Canvas')

canvas = Canvas(root,width = 400,height = 400,bg = 'yellow')

x0 = 263

y0 = 263

y1 = 275

x1 = 275

for i in range(19):

canvas.create_rectangle(x0,y0,x1,y1)

x0 -= 5

y0 -= 5

x1 += 5

y1 += 5

canvas.pack()

root.mainloop()

'''

Topic: Draw a picture and synthesize an example.

1. Program analysis:

2. Program source code:

'''

if __name__ == '__main__':

from Tkinter import *

canvas = Canvas(width = 300,height = 300,bg = 'green')

canvas.pack(expand = YES,fill = BOTH)

x0 = 150

y0 = 100

canvas.create_oval(x0 - 10,y0 - 10,x0 + 10,y0 + 10)

canvas.create_oval(x0 - 20,y0 - 20,x0 + 20,y0 + 20)

canvas.create_oval(x0 - 50,y0 - 50,x0 + 50,y0 + 50)

import math

B = 0.809

for i in range(16):

a = 2 * math.pi / 16 * i

x = math.ceil(x0 + 48 * math.cos(a))

y = math.ceil(y0 + 48 * math.sin(a) * B)

canvas.create_line(x0,y0,x,y,fill = 'red')

canvas.create_oval(x0 - 60,y0 - 60,x0 + 60,y0 + 60)

for k in range(501):

for i in range(17):

a = (2 * math.pi / 16) * i + (2 * math.pi / 180) * k

x = math.ceil(x0 + 48 * math.cos(a))

y = math.ceil(y0 + 48 + math.sin(a) * B)

canvas.create_line(x0,y0,x,y,fill = 'red')

for j in range(51):

a = (2 * math.pi / 16) * i + (2* math.pi / 180) * k - 1

x = math.ceil(x0 + 48 * math.cos(a))

y = math.ceil(y0 + 48 * math.sin(a) * B)

canvas.create_line(x0,y0,x,y,fill = 'red')

mainloop()

Author: ZH Cheese——Zhang He

Source: http://www.cnblogs.com/CheeseZH/

Disclaimer: The copyright of the article belongs to the author. If there is any infringement, please contact the editor to delete it

Guess you like

Origin blog.csdn.net/mynote/article/details/132261503