Python基礎二--基本控制語句
基本接觸每一種語言,都須要做的:1.print 一個"Hello world!" 2.了解主要的數據類型 3.學習控制語句。
當我們學習控制語句,一般都離不開if,for ,while,switch(case)。本文就做一個簡單的介紹python的基本控制語句。當中我們用if while來做一個經典的“猜數字遊戲”,if for來做一個“輸出完美數”。
在此之前,對於一些沒用過python的同學而熟悉c/c++等用{}來做塊的要註意了,python的是利用縮進來分塊的,小小煩惱。可是習慣一段時間還是能夠接受的,這種優點就是從直觀上看來大家的代碼是基本一樣的格式。所謂的簡單易讀。
來說一下縮進的煩惱,屬於我們剛開始學習的人的煩惱。
a=3 b=5 if a>b: print a else: print b print a+b上面的代碼所做的就是輸出a,b中最大,最後輸出a+b,註意到最後一行print a+b沒有頂格,所以就error了,縮進問題。
之後,我們來說一下縮進的優點。假設你寫過C/C++類似的
你會不會犯過這個錯誤
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int main(){ int a=160,b=170; //1案例,當a比b大時候,假設a為奇則輸出a,否則輸出Error,這樣是沒有問題的 if(a>b) if(a&1) cout<<a; else cout<<"Error"; else cout<<b; //2案例,當a比b大時候,假設a為奇則輸出a,當b不比a小輸出a,這樣就是所謂的懸掛else //這程序第二個案例就是沒有輸出,而不是 170。在python。這個問題就不會出現了! if(a>b) if(a&1) cout<<a; else cout<<b; return 0; }
好了,本文的主要是講述基本控制語句
首先,大家都寫過的輸出a,b最大者,用偽代碼看來就是
if a>b print a else print b endif而這裏就是用到了最主要的if條件語句,python中的if的使用方法
if expression: #註意到 ":",漏了可不行,在寫腳本時時常範的小錯誤 if_suitexample
age=19 if age>18: print 'You are adult!'result
You are adult!
當然。有if自然有else
if expression: if_suit else: else_suitexample
IQ=250 if IQ>250: print "Your IQ is very high!" else: print "Normal guy!"
result
Normal guy!
還有不能少的else if,可是python的elseif寫的是和shell一樣的elfi
example
IQ=250 if IQ>250: print "Your IQ is very high!" elif IQ==250: print "You are 250!" else: print "Normal guy!"result
You are 250!
(以上樣例內容僅供娛樂,不要認真,基礎總是枯燥的,多多調侃自己找樂子!)
接下來是while了,基本使用方法就是
while expression: while_suitexmple
cnt=0 while cnt<5: print cnt cnt+=1result
0 1 2 3 4
另外,有while,我們還必須提到break。break就是打破循環嘛,和c++基本一樣。舉個樣例
cnt=1 while cnt<10: print cnt if cnt%2==0: break; cnt+=1這時候結果就是
1 2猜數字大家都清楚,就是在設定一個隨機數作為答案(也能夠手動設置),之後輸入數字,系統判別。大了或者小了,假設相等就跳出循環
code
#!/usr/bin/env python # coding=utf-8 import random a=1 b=1000 ans=random.randint(a,b) cnt=0 while True : num=int(raw_input('Guess the number:')) cnt+=1 if num==ans: print "Great!%d is the answer!"%num print "You 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
當中要用到random,大概的使用方法就是從a,b之間產生一個隨機數。如樣例所說
result
Guess the number:0 0 is less than the ans Guess the number:1000 1000 is greater than the ans Guess the number:500 500 is greater than the ans Guess the number:250 250 is greater than the ans Guess the number:125 125 is less than the ans Guess the number:185 185 is greater than the ans Guess the number:155 155 is less than the ans Guess the number:170 170 is less than the ans Guess the number:177 177 is greater than the ans Guess the number:173 173 is greater than the ans Guess the number:172 172 is greater than the ans Guess the number:171 Great!171 is the answer! You guessed 12 times
這個二分還是不錯的^_^,懂二分的程序猿是非常值錢的。回憶五月中的廣東省賽。我們就是死在了一條二分題上,罰時太多了!
遺憾鐵牌。
for
實際上。python的for 跟c++的for感覺是不一樣的,他更像for each叠代。
基本使用方法就是:
for iterating_var in sequence: statements(s)
舉個樣例來看。
ProLan=['C','CPP','JAVA','PYTHON'] for item in ProLan: print item這樣輸出的結果是:
C CPP JAVA PYTHON假設不想換行。那麽就在item後增加" ,"
即
ProLan=['C','CPP','JAVA','PYTHON'] for item in ProLan: print item,結果就是
C CPP JAVA PYTHON還有我們想做
for(int i=0;i<100;i++) printf ("%d ",i);
這樣的事情,在python要這麽寫
for i in range(0,100): print i,到此為止,我們就能夠做事情了。輸出0~10000完美數
首先,先科普一下完美數,完美數就是全部非本身的因子加起來為本身。比方6就是一個完美數,6的非本身因子有1,2,3,1+2+3=6。
我們能夠用for 和 if 來實現
一個很粗糙的方法,全然講不算是算法
#!/usr/bin/env python # coding=utf-8 for i in range(1,10000): fact=0 for j in range(1,i): if i%j==0: fact+=j if fact==i: print i
一個略微的改良nlogn,這個大家都懂。就不廢話了。
#!/usr/bin/env python # coding=utf-8 import math for 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
1 6 28 496 8128
另外,還值得一提的是。還有for..else,While ...else
對於我們這樣的習慣寫C/C++的人來說,甚是“新奇”!只是我認為還是不要用好~多寫點,避免混淆啊。親!
舉個樣例
for i in range(1,10): if i&1: break else: print "for-else"這樣break掉就不會走else,也就是正常結束才會走else.實際上我理解為
for i in range(1,10)
//do somting
if i==10
//do else
即假設是
for i in range(1,10): if i>250: break else: print "for-else"這樣就會輸出
for-elsewhile..else也如此。
至此。基本上的控制語句已經講完了。
在python 2.X中是沒有case的,這不知道是好是壞,我們僅僅能用
if..elif..else來取代了!
Python基礎二--基本控制語句