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_suit
    
example

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

You are  adult!

当然。有if自然有else

if expression:  
    if_suit
else:
    else_suit
example

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_suit
exmple

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

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-else
while..else也如此。

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

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

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

原文地址:https://www.cnblogs.com/yutingliuyl/p/7215777.html