zoukankan      html  css  js  c++  java
  • 第五章 条件 、循环和其它语句(笔记主要用于自己记忆)

    1.1 print 、import

    print使用变量逗号隔开,打印多个值;

    from  模块名 import  函数名或者*【这个符号代表所有】

    from  模块名 import  函数名或者*【这个符号代表所有】 as  自定义的名字  【这个是取别名】

    1.2 序列解包【赋值】

    解释:将多个值的序列解开,然后放到变量中;

      values = 1,2,3
      x,y,z = values
      x
      1
     
    

     popitem():随机返回并删除字典中的一对健值对(一般删除末尾组)

    备注:这个是python3的方法

    site= {'name': '菜鸟教程', 'alexa': 10000, 'url': 'www.runoob.com'}
    pop_obj=site.popitem()
    print(pop_obj)   
    print(site)
    输出结果
    ('url', 'www.runoob.com')
    {'name': '菜鸟教程', 'alexa': 10000}
    

     赋值需要注意的地方:元素的数量和赋值的数量必须是相等的,否则会报错

    1.3 链式赋值

    x=y=1

    1.4 增值赋值

    ford + ="foo"
    ford * =2
    

     1.5 布尔值

    标准值False和None、所有类型的数字0(包括浮点型、长整型和其他类型)、空序列(比如空字符串、元组和列表)以及空的字典都为假,其他一切都为真;

    bool():将值转换成bool型

    注意:bool([])=bool("")=false  但是他们的本身并不相等

    1.6 if  else 语句

    name = raw_input('what is your name?')
    if name.endswith('Gumby'):
        print 'Hello,Mr.Gumby'
    else:
        print 'Hello,stranger'
    

     1.7 elif 语句  :else if 的缩写

    num = raw_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'
    

     1.8 比较运算符

    需要特别注意的:
    x is  y                 x和y是同一个对象
    x is not y             x和y不是同一个对象
    x in y                  x是y容器的成员
    x not in y            x不是y容器的成员

    1.9 相等运算符

    'a'=='a'

    2.0  is:同一性运算符

    理解:来自同源

    x = y = [1,2,3]
    z = [1,2,3]
    x==y  ==>True
    x==z  ==>True
    x is y ==>True
    x is z  ==>False
    

    2.1 in 成员资格运算符

    2.2 断言

    作用:程序发生错误条件,奔溃程序

    2.3 循环

    name = ''
    while not name:
        name = raw_input('please ')
    print 'Hello,%s!' % name
    
    words = ['this','is','an','ex','parrot']
    for word in words:
        print word
    
    numbers = [0,1,2,3,4,5,6,7,8,9]
    for number in numbers:
        print number
    
    等价于
    
    for number in  range(0,10):
        print numbe
    

     Range()包含下限,但不包含上限;如果希望下限为0,可以只提供上限;

    Xrange()函数的行为类似于Range(),区别在于Range()函数一次创建整个序列,而Xrange()一次只创建一个数,当需要迭代一个更大的序列时,Xrange()更有效;

    并行迭代

    names = ['anne','beth','george','damon']
    ages = [12,45,32,102]
    for i in range(len(names)):
        print names[i],'is',ages[i],'years old'
    
  • 相关阅读:
    Eclipse中的快捷键
    Eclipse配置
    Java语言概述
    视口及媒体查询
    弹性盒flex
    less——css预处理语言
    过渡/动画/变形
    列表/表格/表单
    渐变
    背景background
  • 原文地址:https://www.cnblogs.com/JuanZi-Sunny/p/9929516.html
Copyright © 2011-2022 走看看