码迷,mamicode.com
首页 > 编程语言 > 详细

三、python运算符

时间:2017-09-17 19:03:27      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:pre   一个   ons   count   用法   字符串   enter   运算   bre   

1 /和//的区别:/结果保留小数,例如5/3=1.666   而5//3=1

2 *和**:算术运算中,第一个代表乘,第二个代表幂。再字符串操作中,第一个代表拼接多少个相同字符串,第二个类似,见如下代码:

var = 2*3
print(var)             #输出6
var =2**3
print(var)             #输出8
var = hello
print(var*2)           #输出hellohello
print(str(var)**3)     #不可以这样使用

3 python中的三种控制流语句,if for while, 用此控制流,后边都以: 结尾,比如如下测试代码:

#测试for
for i in range(1,10):
    print(i,end=" ")      #输出1-10
#测试if                   #输出yes
var = biao
if var == biao:
    print(yes)
else:
    print(no)
#测试while
count = 5
while count>=1:
    count -= 1
    print(count,end=" ") #输出1-10

4 python中不存在switch case的用法,if else if else 的测试代码如下:  注意elif代表else if:

 

number = 23
while 1:
    guess = int(input(Enter an integer : ))#input函数将以字符串的形式返回我们输入的内容。然后我们通过int将这个字符串转换为一个整数。
    if guess == number:
    # 新块从这里开始
        print(Congratulations, you guessed it.)
        print((but you do not win any prizes!))
        break
    # 新块在这里结束
    elif guess < number:
        # 另一代码块
        print(No, it is a little higher than that)
        # 你可以在此做任何你希望在该代码块内进行的事情
    else:
        print(No, it is a little lower than that)
        # 你必须通过猜测一个大于( >) 设置数的数字来到达这里。
print(Done)

5 python中的for循环代码:(range用法)

for i in range(1, 5):     #range(1,5) 代表左闭右开区间
    print(i,end="")
else:
    print(\nThe for loop is over)

for i in range(1, 5 ,2):  #在[1,5)之间,以2为步进输出
    print(i,end="")
else:
    print(\nThe for loop is over)

#输出结果如下:
1234
The for loop is over
13
The for loop is over

6 python中while的用法,以及break、continue的用法

while True:
    s = input(Enter something : )
    if s == quit:              
        break                    #代表退出while循环,打印最后一句话 Input is of sufficient length
    if len(s) < 3:
        print(Too small)
        continue                 #退出本次循环,还在while True中。
print(Input is of sufficient length)

#进行如下测试
Enter something : ha
Too small
Enter something : hahaha
Enter something : quit
Input is of sufficient length

注:参考书籍《byte-of-python-chinese-edition》

三、python运算符

标签:pre   一个   ons   count   用法   字符串   enter   运算   bre   

原文地址:http://www.cnblogs.com/buptzlb/p/7536410.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!