Basic contact with each language, need to do: 1.print a "Hello world!" 2. Understand the primary data type 3. Learn the control statement.
When we learn control statements, it is generally inseparable from if,for, While,switch (case). This article makes a simple introduction to Python's basic control statements. We use if while to make a classic "Guess the number Game", if for to do a "output perfect number."
Before that, for some of the students who have not used python and familiar with C + + and other uses {} to do blocks to pay attention to, Python is the use of indentation in the block, a little trouble. However, the habit is acceptable for some time, and the advantage is that the code is basically the same as in the intuitive form. So-called simple easy to read.
To say the problem of indentation, belongs to the people we have just begun to learn the trouble.
A=3b=5if a>b: print Aelse: print b print A+b
The above code is to output A, a, the largest, the last output a+b, note that the last line of print a+b no shelf, so the error, indentation problem.
After that, let's take a look at the advantages of indentation. Let's say you wrote a C + + class.
Will you ever make this mistake
#include <iostream> #include <cstdio> #include <cstring>using namespace Std;int main () { int a=160 , b=170; 1 case, when a is larger than B, suppose A is odd then output a, otherwise output error, so there is no problem if (a>b) if (a&1) cout<<a; else cout<< "Error"; else cout<<b; 2 case, when a is larger than B, suppose A is odd then output a, when B is no less than a small output a, so called the hang else //This program The second case is no output, not 170. In Python. This problem will not come up! if (a>b) if (a&1) cout<<a; else cout<<b;
Well, the main focus of this article is to tell the basic control statements
First of all, we have written the output, a and the largest, with pseudo-code appears to be
If a>b print aelse print bendif
And here is the most important if condition statement, in PythonifHow to use
If expression: #注意到 ":", leakage can not, in writing scripts always Tsunenori small error If_suit
Example
Age=19if age>18: print ' You're adult! '
Result
You are adult!
Of course. Have if nature has else
If expression: if_suitelse: else_suit
Example
Iq=250if iq>250: print "Your IQ is very high!" else: print "Normal guy!"
Result
Normal guy!
There are not fewer else if, but Python's ElseIf is written in the same way as the shell Elfi
Example
Iq=250if iq>250: print "Your IQ is very high!" Elif iq==250: print "You are 250!" else: print "Normal guy!"
Result
You are 250!
(The above sample content is only for entertainment, do not seriously, the foundation is always boring, many ridicule their own fun!) )
Next is the while, and the basic way to use it is
While expression: while_suit
Exmple
Cnt=0while cnt<5: print cnt cnt+=1
Result
01234
In addition, there is while, we must also mention break. Break is breaking the loop, and basically the same as C + +. Give me a sample.
Cnt=1while cnt<10: print cnt if cnt%2==0: Break ; Cnt+=1
The result is
12
Guess numbersWe all know that is to set a random number as the answer (also can be manually set), then enter the number, system discrimination. Big or small, assuming equality jumps out of the loop
Code
#!/usr/bin/env python# Coding=utf-8import Randoma=1b=1000ans=random.randint (A, B) cnt=0while True: num=int (raw_ Input (' Guess the Number: ')) cnt+=1 if Num==ans: print "great!%d is the answer!" %num print "guessed%d times"%cnt break elif Num>ans: print "%d is greater than the ANS"%num else: print "%d is less than the ANS"%num
To use random, the approximate method is to generate a random number from a A, B. As the example says
Result
Guess the number:00 is less than the ansguess the number:10001000 are greater than the ansguess the number:500500 is greate R than the ansguess the number:250250 is greater than the ansguess the number:125125 the the the than the number: 185185 is greater than the ansguess the number:155155 was less than the ansguess the number:170170 was less than the ansgues s The number:177177 is greater than the ansguess the number:173173 are greater than the ansguess the number:172172 is great Er than the ansguess the number:171great!171 is the answer! You guessed
This is a good two-point ^_^, understand the two-point program Ape is very valuable. Memories of the May Guangdong provincial race. We are dead on a two-point problem, too much penalty!
Sorry for the Iron card.
For
As a matter of fact. Python's for is different from the C + + for sense, and he is more like for each iteration.
The basic use method is:
For Iterating_var in sequence: statements (s)
For example, let's take a sample.
prolan=[' C ', ' CPP ', ' JAVA ', ' PYTHON ']for item in Prolan: print Item
The result of this output is:
Ccppjavapython
Suppose you don't want to wrap. Then add "," after item
That
prolan=[' C ', ' CPP ', ' JAVA ', ' PYTHON ']for item in Prolan: print Item,
The result is
C CPP JAVA PYTHON
And what we want to do
for (int i=0;i<100;i++) printf ("%d", I);
Such a thing in Python to write like this
For I in Range (0,100): print I,
So far, we can do things. Output 0~10000Perfect Number
First of all, the perfect number of science first, the perfect number is all non-intrinsic factors added up for itself. For example, 6 is a perfect number, 6 of the non-intrinsic factors are 1,2,3,1+2+3=6.
We can use for and if to implement
A very rough approach, completely speaking, not an algorithm.
#!/usr/bin/env python# coding=utf-8for i in Range (1,10000): fact=0 for J in Range (1,i): if i%j==0: fact +=j if fact==i: print I
A slightly improved nlogn, which we all know. No more nonsense.
#!/usr/bin/env python# coding=utf-8import mathfor i in Range (1,10000): fact=1 end=int (math.sqrt (i)) +1 For j in Range (2,end): if i%j==0: fact+=j if j*j!=i: fact+=i/j; If fact==i: print I
Result
16284968128
In addition, it is worth mentioning that. And alsoFor : else, While ... else
For those of us who are accustomed to writing C + +, it is "novelty"! But I think it's better not to write more, to avoid confusion. Kiss!
Give me a sample.
For I in Range (1,10): if i&1: breakelse: print "For-else"
This will not go to the other, that is, the normal end will not go else. Actually, I understand
For I in range (1,10)
Do somting
If i==10
Do Else
That is assumed to be
For I in Range (1,10): if i>250: breakelse: print "For-else"
This will output
For-else
While.. The same is true of else.
So far. Basically the control statement is finished.
In Python 2.X There is no case, this does not know is good or bad, we can only use
If.. Elif. else to replace it!
Python base Two-basic control statements