zoukankan      html  css  js  c++  java
  • Beginning Python From Novice to Professional (5) - 条件与循环

    条件与循环

    条件运行:

    name = raw_input('What is your name? ')
    if name.endswith('Gumby'):
    	print 'Hello, Mr.Gumby'
    What is your name? Gumby
    Hello, Mr.Gumby
    name = raw_input('What is your name? ')
    if name.endswith('Gumby'):
    	print 'Hello, Mr.Gumby'
    else:
    	print 'Hello, stranger'
    多个条件:

    num = input('Enter a number: ')
    if num > 0:
    	print 'The number is positive'
    elif num < 0:
    	print 'The number is negative'
    else:
    	print 'The number is zero'
    Enter a number: 5
    The number is positive
    Enter a number: -1
    The number is negative
    Enter a number: 0
    The number is zero
    while循环:

    x = 1
    while x<=100:
    	print x
    	x+=1
    for循环:

    numbers = [0,1,2,3,4,5,6,7,8,9]
    for number in numbers:
    	print number
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    循环中的else:

    for n in range(99,81,-1):
    	root = sqrt(n)
    	if root == int(root):
    		print n
    		break
    else:                                #仅仅在没有调用break时运行
    	print "Didn't find it!"

  • 相关阅读:
    怎么在myeclipse中怎么集成Tomcat。
    JSP .基础概念
    继承
    封装
    什么是面向对象
    数据排序
    开发的套路
    Javabean规范
    转发和重定向
    md5加密
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4315503.html
Copyright © 2011-2022 走看看