How to Pierce python Programming Paper

When a friend taught himself python, he found that he could understand the book and the toy code in it. But why can't they do exercises, write codes to solve problems, and write their own codes?

The reason is that beginners have not learned computational thinking, problem-solving methods and programming ideas.

The development of programming ideas needs a process, thinking in the coding process and typing more codes.

Sometimes, if someone breaks something you don't understand, the paper will break easily.

Please look at the following example.

Programming ideas 1:

People often ask how many lines of text should be extracted from a text file.

I believe everyone can understand and write counting cycles. The following code solves this problem with counting loop.

Code one:

A text file of several hundred meters needs to be written into a new file every 1000 lines.

Don't underestimate the counting period. This problem can be solved by counting cycles and judging statements.

# Encoding: utf-8

"""

Diam python remote video training

Consultation: qq: 1465376564

"""

Use open('dist_ 1.txt',' r') as f 1, and open('dist_new.txt',' w') as f2:

i = 0

For lines in f 1:

i += 1

If i% 1000 == 0:

F2. Write (line)

Code two:

Sorry, a log text file has 2000 lines. I want to extract 100 to 200 lines. How?

You can try the following methods.

Don't underestimate the while counting loop, in fact, it can be used to do many things.

# Encoding: utf-8

i = 0

file 1 = open("test.txt "," r ")

file2 = open("out.txt "," w ")

Although correct:

line = file 1.readline()

i += 1

if 100 & lt; Me and me & lt=200:

File2.write (line)

If I & gt200:

break

If not:

break

file 1.close()

file2.close()

Programming idea 2:

# Encoding: utf-8

"""

This code was written by Mr. Huang from Deaim Company. You can read the code yourself.

Arrested suspects A, B, C and D4. One of them is a thief. During the test:

A says I'm not a thief.

B said C was a thief.

C said the thief must be D.

D says c nonsense!

Three of them are telling the truth, one is telling a lie, and programming infers who is the thief.

(through exhaustive methods and logical expressions)

"""

For thieves in ['a',' b',' c',' d']:

Sum = (thief! = 'a')+(thief == 'c')+(thief == 'd')+(thief! ='d ')

If sum == 3:

Print "The thief is:% s% thief.

Summary: To learn python programming, first learn procedural programming, and then transition to object-oriented programming paradigm. Learn to abstract functions and classes. Develop computational thinking step by step, learn to solve problems and program ideas. This needs a process, don't rush it. Look at these videos, it may help you.