当前位置:网站首页>Logic is a good thing

Logic is a good thing

2022-07-06 20:23:00 Crossin's programming classroom

Zero basis python Introductory tutorial :python666.cn

Hello everyone , Welcome to Crossin Programming classroom of !

Crossin I often receive such questions at the backstage of official account :

“ English is not good, can you learn programming ?”

“ Is programming demanding on Mathematics ?”

Translation is : mathematics / Is English a prerequisite for learning programming ?

I think this problem is a bit like , If you are not tall, you can't play basketball . Although for professional basketball players , Height is a key factor , But for ordinary basketball fans , Even for a basketball related practitioner , This is not an inevitable condition . Put it on programming , Mathematics and English are both important , but :

  1. At the entry stage , You don't have to care about this at all , Follow a tutorial and imitate it well Just go

  2. For most daily development , Middle school level of mathematics and English have been able to cope

  3. Different from the congenital condition of height , English and mathematics can be improved through learning , Where there is a shortage, there is a remedy . A popular sentence on the Internet :“ At the low level of effort of most people , There's no talent at all .” You can recite words for a long time , reading , It's enough to improve yourself . Always put XX I'm afraid it's more about making excuses for myself .

however , Have an ability , It really needs enough attention in the early stage of learning programming . But this , Many tutorials do not specifically emphasize , Related books often assume that you have mastered , So many learners are not aware of their weakness in this aspect , Leave hidden dangers for future study . All I have to say is :

Logic

In a way , Logic can be partially counted as mathematics , There are relevant contents in high school mathematics textbooks . This part also happens to be the foundation we need to know most when learning programming .

One 、 Boolean algebra

from really (1)、 false (0) Two kinds of state , as well as And (and)、 or (or)、 Not (not) Three basic operations constitute . Boolean algebra seems simple , But it corresponds to the two states of on and off in the digital circuit , yes The logical basis of computer . You may have heard of , Everything on the computer is internally controlled by 0 and 1 Indicated by , In other words , All the behaviors of the computer , The essence is realized by Boolean logic .

In programming languages , Boolean type (bool) It is also a very important existence . Without it , We can't make conditional judgments , There is no way to control the execution of the program .Python Of if、while Statements must be judged by the type of rybool .

therefore , Even if you don't want to study logic deeply , The basic rules of Boolean algebra must be understood anyway . Simply speaking , This is the next one “ Truth table ”:

p

q

not p

p and q

p or q

1

1

0

1

1

0

1

1

0

1

1

0

0

0

1

0

0

1

0

0

These basic rules will combine into more complex logic , such as : 

not (p or q) Equivalent to (not p) and (not q)

( Example :“ Not administrators or members ” amount to “ Not an administrator and not a member ”)

The logic judgment faced in actual development is complex and diverse , But in the final analysis, it will be transformed into the most basic rules .

Thinking questions 1: How to express “ through New users who have registered through activities or recharge more than 100 Old users of Yuan , Except for internal personnel

Two 、 Propositional logic

Statements with unique truth values are called propositions , A proposition that cannot be decomposed into simpler propositions is called an atomic proposition . such as ,“1+1=2” It's an atomic proposition ,“ All cats are white ” It's an atomic proposition ( False proposition ),“ There are aliens ” It is also an atomic proposition ( Although we can't judge the truth , But the result must be unique ). The combination of propositions and logical operations , It will produce more complex logic .

such as Sufficient and necessary conditions .

If the proposition p It is inevitable to deduce a proposition q, that p Namely q Sufficient conditions of ,q Namely p Necessary conditions , Write it down as p→q.

For example, the poetry of Han Yuefu “ Mountain without edges , Harmony , But I dare to fight with you ”, How to express in logical language ?

( Mountain without edges and Harmony ) → And you Jue ?

Think about whether something is wrong ? According to semantics ,“ Mountain without edges ”、“ Harmony ” yes “ And you Jue ” Of Necessary condition , If “ And you Jue ” It's true , There must be “ Mountain without edges ” and “ Harmony ”, On the contrary, it may not . So the correct logic is :

And you Jue → ( Mountain without edges and Harmony )

Another example syllogism .

① You can't learn programming well without mastering basic logic knowledge

②Python Is a programming language

therefore , Learn from good examples Python You need to master basic logical knowledge

A big premise plus a small premise , A conclusion can be derived . This is the most commonly used form of argument , It seems simple , But there are always people who are confused about this . Take up a Examples of mistakes

① Rich people use iPhone XS Max

② I use iPhone XS Max

therefore , I am rich

These logical relationships 、 The derivation process is closely related to the logical structure in the program . If you can't keep your mind clear about this , The code written is likely to differ from expectations , Or there are loopholes in some special cases .

Thinking questions 2: A small function of signing in and receiving awards every day , Every day 11~13 Point and 18~20 Open up , Ordinary users can receive it once a day , Member users can receive it once in each period . How to realize the logical structure of this program ?( I often take this as an interview question , Many people can't give the right answer in a short time )

3、 ... and 、 inductive

occasionally , We cannot deduce an inevitable conclusion through logic , But it can still Through a series of experience and existing conclusions , Find out the basic rules .

such as :

①X The team has always equipped all members Dell Notebook as work computer

②C The teacher recently joined X The team

Through these two points , We can derive that ,C Teachers may also use Dell The notebook .

But you need to know The conclusion of induction is not necessarily true , If you encounter the inevitable counterexample of logical reasoning , Even if it is no longer in line with common sense , The conclusion of induction will also be overturned .

For example, in addition to the above two points , We also know that :

③C The teacher signed an apple endorsement

④ Apple spokesmen are not allowed to use other brands of mobile phones and notebooks

that , Above “C Teachers use Dell The notebook ” The conclusion is not tenable .

although Induction is not inevitable , But it is still important to solve the problem . Especially when you encounter errors in development, you need debug( debugging ) When , Not all errors can be directly seen from the error information ( such as Python Garbled code often occurs during development ), At this time, if you have rich experience and strong induction ability , The efficiency will be greatly improved . This is also an important manifestation of the gap between senior programmers and junior programmers .

Now the online programming tutorials are overwhelming , But what? , Everyone likes to write about how to make up a picture of a reptile catching 、 Grasp data and so on , But few articles are willing to talk about the thinking process behind . Readers don't want to read it after writing , Because I can't see any effect , Where can we adjust a few functions to get a result that is straightforward . That you find , You can write the same code according to the example , But once an error is reported or there is no expected result , I'm completely blind , The parameters can only be changed from east to West , Run it over and over again , Expect a miracle . A new question in the future , I still don't know where to start .

This is because the logic behind the program is not understood . I often say in the Q & a group : Don't guess when you encounter a program error ! Make assumptions , Then verify the hypothesis through the output , Finally, locate the problem .

The text is just an introduction , It's impossible for you to master logic . But the logical basis mentioned above , Any book 《 Discrete Mathematics 》 perhaps 《 logic 》 There are all books on , And just a small part at the beginning is very useful . Find a book to read . I really want to learn programming well , Don't limit yourself to reading online tutorials , These are all chewed and fed into your mouth . If you can only accept such secondary processed knowledge , Without the process of chewing , Then your baby teeth on study will never fall out .

Understanding that logic and thinking are logical is not completely equivalent . Some people have never learned logic , It's logical to speak and do , While some people have learned logic , It's just reciting mathematical formulas , All day “ Logical thinking ” Talking doesn't mean doing things logically .

On the network , You can often see some logical fallacies , Give a few common examples :

  • overgeneralization : You say with X The product encountered Y problem . Someone retorted , I also use X product , People around me also use X product , No problem , So you must have deliberately hacked .

  • Relevance is cause and effect : People in a certain area like to drink raw milk , At the same time, the average life expectancy of people in this area is higher than that in surrounding areas , So drinking raw milk can prolong life .

  • Black and white : The Internet is attacking a product for eavesdropping on user chat , And I have a criminal record of spam advertising before . You say eavesdropping is technically unrealistic , There is also a lack of clear evidence . Then someone thinks you don't spray together , You are the Navy 、 cleaning 、 Flatterer .

  • ……

Similar illogical flooding the network .

There are many prejudices around us 、 The concept of discrimination , There are many people who like to argue “ Bar fine ”, in the final analysis , You will find that these people have a common characteristic : Logic confusion . such as , All of them will be sexist 、 Regional discrimination , It is because some special cases make inferences about the whole . And steal the concept 、 Reverse cause and effect 、 Motive speculation , It's the logical fallacy that punks like to abuse .

Not just in programming , If we pay more attention to logic , There will be fewer contradictions in this world . Even without special study and training , Just keep humble , Learn to listen , Treat different voices with an inclusive attitude , Think more about each other's views , Looking at things from different angles , Your logic will be more complete .

Conversely , When you find that the Internet is full of illogical remarks , I don't want to express any views anymore .

thank forward and give the thumbs-up The fellow ~


_ Previous articles are recommended _

Core principles of video games


If you need to know Paid premium courses And Teaching Q & a service

Please be there. Crossin Programming classroom of Internal reply : 666

fd427a3d49cea064cd6f75fe8882f8e6.png

原网站

版权声明
本文为[Crossin's programming classroom]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061223232244.html