开发者社区> 问答> 正文

python if 多条件语句怎么写

python if 多条件语句怎么写

展开
收起
云计算小粉 2018-05-10 20:11:04 2604 0
2 条回答
写回答
取消 提交回答
  • image.png

    2019-11-22 15:46:24
    赞同 展开评论 打赏
  • 1.print和import
    1.1 print 略
    1.2 import
    (1)impore somemodule
    (2)from somemodule import somefunction
    (3)from somemodule import somefunction,anotherfunction
    (4)from somemodule import*
    (5)import somemodule as somename #为整个模块提供别名
    (6)from somemodule import somefunction as somename #为某个模块的某个函数提供别名

    2.赋值
    2.1 序列解包:将多个值的序列解开,然后放到变量的序列中。
    2.2 链式赋值:将同一个值赋给多个变量。
    2.3 增量赋值:eg:x+=1

    3.语句块:条件为真时执行或执行多次的一组语句,由代码前放置空格缩进语句创建。

    4.条件语句
    4.1 布尔变量 python中的假:False None 0 “” () [] {}
    4.2 if语句
    4.3 else语句
    4.4 elif语句
    4.5 嵌套代码块
    4.6 条件
    (1)比较运算符

    (2)相等运算符 ==

    (3)同一性运算符 is

    (4)成员资格运算符 in

    (5)字符串和序列比较

    (6)布尔运算符

    4.7 断言: assert 放入检查点,确保某个条件一定为真才能让程序正常工作

    1. 循环

    5.1 while循环

    5.2 for循环

    5.3 循环遍历字典元素

    5.4 迭代工具

    (1) 并行迭代:同时迭代两个序列

    (2)按索引迭代

    (3)翻转和排序迭代

    5.5 跳出循环

    (1) break

    (2) continue

    (3) while True/break

    5.6 循环中的else子句

    6.列表推导式-轻量级循环

    7.pass,del,exec

    本章相关代码:

    -- coding: utf-8 --

    1.print 与import

    1.1 print 使用逗号输出

    print 'Age:',42 #Age: 42
    name='xiaming'
    age='42'
    print name,age #xiaming 42

    2.赋值语句

    2.1.序列解包:将多个值的序列解开,然后放到变量的序列中

    x,y,z=1,2,3
    print x,y,z #1 2 3
    x,y=y,x #2 1 3
    print x,y,z
    v=1,2,3
    x,y,z=v
    print x #1

    2.2.链式赋值 将同一个值赋给多个变量的捷径

    x=y=4
    print x,y

    2.3.增量赋值

    x=2
    x+=1
    x*=2
    a='bo'
    a+='x'
    a*=2
    print x,a #6,boxbox

    3:语句块:缩排

    4.条件与条件语句

    4.1.布尔变量的作用

    4.2 if elif else语句

    name=raw_input('what is your name?')
    if name.endswith('ming'):

    print 'hello,ming'

    elif name.endswith('hong'):

    print 'hello,hong'

    else:

    print 'hello'
    

    4.3 嵌套语句

    num=raw_input('input a number')
    if num>10:

    if num<20:
        print '10<num<20'
    else:
        print 'num>=20'

    else:

    print 'num<=10'
    

    #5.循环

    5.1.while循环

    x=1
    while x<=10:
    print x
    x+=1

    name=''
    while not name:

    name=raw_input('enter your name:')

    print 'Hello,%s!'%name #Hello,ming!

    5.2.for 循环

    names=['ming','hong','qiang','qing']
    for n in names:

    print n
    

    print range(10) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] rang(0,10)
    for num in range(1,10):

    print num
    

    5.3.循环遍历字典元素

    d={'x':1,'y':2,'z':3}
    for key in d:

    print key,d[key]    #y 2 x 1 z 3
    

    5.4.一些迭代工具

    5.4.1 并行迭代

    names=['ming','hong','qiang','qing']
    ages=[10,11,12,13]
    for i in range(len(names)):

    print names[i],'is',ages[i],'years old'  #ming is 10 years old...
    

    print zip(names,ages) #[('ming', 10), ('hong', 11), ('qiang', 12), ('qing', 13)] 将两个序列压缩,返回一个元组的列表
    for n,a in zip(names,ages):

    print n, 'is',a, 'years old'   #ming is 10 years old...
    

    5.4.2按索引迭代

    5.4.3翻转和排序迭代

    5.5.跳出循环

    5.5.1break

    from math import sqrt
    for n in range(99,0,-1):

    print n
    root=sqrt(n)
    if root==int(root):
        print "the biggst is %s"%n
        break
    

    5.5.2 continue

    5.5.3 while True/break

    while True:

    word=raw_input('Please enter a word:')
    if not word:break
    print 'The word was '+word
    

    5.6 循环体中的else子句

    from math import sqrt
    for n in range(99,80,-1):

    print n
    root=sqrt(n)
    if root==int(root):
        print "the biggst is %s"%n
        break
    else:
        print "didn't find it"
    

    6 列表推导式-轻量级循环:利用其它列表创建新列表

    print [x*x for x in range(10)] #[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
    print [x*x for x in range(10) if x%3==0] #[0, 9, 36, 81]
    print [(x,y,z) for x in range(2) for y in range(2) for z in range(2)]

    [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]

    7 三个语句:pass,del,exec

    7.1 pass :

    7.2 del:删除不再使用的对象

    7.3 exec:执行一系列Py语句 exel:计算Py表达式,并且返回结果值

    2019-07-17 22:25:18
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载