Home  >  Article  >  Backend Development  >  python3 quick start

python3 quick start

巴扎黑
巴扎黑Original
2017-06-23 15:46:052030browse

During the process of self-study python, some friends found that they can understand books and toy codes in books, but why can't they do exercises, write code to solve problems, or write code by themselves?

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

The development of programming ideas requires a process. Think during the coding process and use your hands to type code.

Sometimes, if you don’t understand something, if someone touches it a little bit, the layer of paper will be easily torn.

Please see the example below.

Programming Idea One:

经常有人问,一个文本文件,要抽取多少行以后的文本。
相信记数循环,大家都看得懂,也会写。下面的代码就是利用记数循环来解决这个问题。

代码一:
一个几百M的文本文件,需要每隔1000行写到新的文件中。
不要小看了计数循环,用计数循环和判断语句就可以解决这个问题。# coding:utf-8"""零基础入门学习Python3  """with open('dist_1.txt','r') as f1 ,open('dist_new.txt','w') as f2:
    i = 0for line in f1:
        i += 1if i % 1000 == 0:
            f2.write(line)

代码二:
请问一个日志文本文件有2000行,我要提取其中的100行到200行,怎么做?
你可以试试下面的方法。
别小看while计数循环,其实它可以用来干很多事。#coding:utf-8i = 0
file1 = open("test.txt","r")
file2 = open("out.txt","w")while True:
    line = file1.readline()
    i += 1if 100<=i and i<=200:
        file2.write(line)if i >200 :breakif not line:breakfile1.close()
file2.close()

Programming Idea Two:

#coding:utf-8"""抓了a,b,c,d4名犯罪嫌疑人.其中有一名是小偷,审讯中:
        a说我不是小偷
        b说c是小偷
        c说小偷肯定是d
       d说c胡说!
其中有3个人说的是实话,一个人说的是假话,编程推断谁是小偷。
(用穷举法和逻辑表达式)"""for thief in ['a','b','c','d']:

      sum = (thief != 'a') + (thief == 'c') + (thief == 'd') + (thief !='d')      if sum == 3:          print "小偷是:%s " % thief

Summary: Learn python To program, first learn procedural programming, and then transition to the object-oriented programming paradigm. Learn function abstraction and class abstraction. Step by step, develop computational thinking, learn problem-solving methods, and programming ideas. This requires a process, so don’t rush it. Take a look at these videos, they may help you.

The above is the detailed content of python3 quick start. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn