Python基础二--基本控制语句,,基本接触每一种语言,


基本接触每一种语言,都需要做的:1.print 一个"Hello world!" 2.了解基本的数据类型 3.学习控制语句。

当我们学习控制语句,一般都离不开if,for ,while,switch(case)。本文就做一个简单的介绍python的基本控制语句,其中我们用if while来做一个经典的“猜数字游戏”,if for来做一个“输出完美数”。
在此之前,对于一些没用过python的同学而熟悉c/c++等用{}来做块的要注意了,python的是利用缩进来分块的,小小烦恼,但是习惯一段时间还是可以接受的,这样的好处就是从直观上看来大家的代码是基本一样的格式,所谓的简单易读。

来说一下缩进的烦恼,属于我们初学者的烦恼。

a=3b=5if a>b:   print aelse:   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 aelse   print bendif
而这里就是用到了最基本的if条件语句,python中的if的用法

if expression:  #注意到  ":",漏了可不行,在写脚本时时常范的小错误    if_suit    
example

age=19if age>18:     print 'You are  adult!'
result

You are  adult!

当然,有if自然有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!

还有不能少的else if,但是python的elseif写的是和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!

(以上例子内容仅供娱乐,不要认真,基础总是枯燥的,多多调侃自己找乐子!)

接下来是while了,基本用法就是

while expression:        while_suit
exmple

cnt=0while cnt<5:    print cnt    cnt+=1
result

01234


另外,有while,我们还必须提到break,break就是打破循环嘛,和c++基本一样,举个例子

cnt=1while cnt<10:    print cnt    if cnt%2==0:         break;    cnt+=1
这时候结果就是

12
猜数字大家都清楚,就是在设定一个随机数作为答案(也可以手动设置),之后输入数字,系统判别,大了或者小了,如果相等就跳出循环

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 "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:00 is less than the ansGuess the number:10001000 is greater than the ansGuess the number:500500 is greater than the ansGuess the number:250250 is greater than the ansGuess the number:125125 is less than the ansGuess the number:185185 is greater than the ansGuess the number:155155 is less than the ansGuess the number:170170 is less than the ansGuess the number:177177 is greater than the ansGuess the number:173173 is greater than the ansGuess the number:172172 is greater than the ansGuess the number:171Great!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
这样输出的结果是:

CCPPJAVAPYTHON
如果不想换行,那么就在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-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

一个稍微的改良nlogn,这个大家都懂,就不废话了。

#!/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

另外,还值得一提的是,还有for..else,While ...else

对于我们这种习惯写C/C++的人来说,甚是“新鲜”!不过我觉得还是不要用好~多写点,避免混淆啊!亲!

举个例子

for i in range(1,10):   if i&1:       breakelse:   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:       breakelse:   print "for-else"
这样就会输出

for-else
while..else也如此。

至此,基本上的控制语句已经讲完了。

在python 2.X中是没有case的,这不知道是好是坏,我们只能用

if..elif..else来代替了!

Python基础二--基本控制语句,布布扣,bubuko.com

Python基础二--基本控制语句

评论关闭