python第一天学习复习(3.6)

  • python3.x+ 与 2.x+主要区别是,支持的编码集,还有一些内库,方法名的修改
  • input()方法,接受用户输入,其中在python2.x中是raw_input(),在2.x中,input的输入,必须以引号括起来,要不认为是变量
    • 其中 print是方法,必须输入内容,必须使用()
  • python中以缩进标示一个模块
  • 条件判断语句
    •  age = 23
       inputage = int(input(<span style="color:#800000;">"</span><span style="color:#800000;">age:</span><span style="color:#800000;">"</span><span style="color:#000000;">))
      </span> <span style="color:#0000ff;">if</span> inputage ==<span style="color:#000000;"> age:
      </span>     <span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">your are right</span><span style="color:#800000;">"</span><span style="color:#000000;">)
      </span> <span style="color:#0000ff;">else</span><span style="color:#000000;">:
      </span>     <span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">you are wrong</span><span style="color:#800000;">"</span>)

      input()输入,得到的类型是 str ,可以使用 type()函数来判断数据类型

  • 循环
    • while
      • age = 23
        <span style="color:#0000ff;">while</span> 1<span style="color:#000000;">:
            inputage </span>= int(input(<span style="color:#800000;">"</span><span style="color:#800000;">age:</span><span style="color:#800000;">"</span><span style="color:#000000;">))
            </span><span style="color:#0000ff;">if</span> inputage ==<span style="color:#000000;"> age:
                </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">your are right</span><span style="color:#800000;">"</span><span style="color:#000000;">)
                </span><span style="color:#0000ff;">break</span>
            <span style="color:#0000ff;">else</span><span style="color:#000000;">:
                </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">you are wrong</span><span style="color:#800000;">"</span>)

        break跳出循环,其中循环语句后可以跟else,表示循环正常完成后执行的代码,如果是break跳出循环,此为中断循环

        • age = 23<span style="color:#000000;">
          count </span>=<span style="color:#0000ff;">while</span> count < 3<span style="color:#000000;">:
              inputage </span>= int(input(<span style="color:#800000;">"</span><span style="color:#800000;">age:</span><span style="color:#800000;">"</span><span style="color:#000000;">))
              </span><span style="color:#0000ff;">if</span> inputage ><span style="color:#000000;"> age:
                  </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">your input is bigger</span><span style="color:#800000;">"</span><span style="color:#000000;">)
              </span><span style="color:#0000ff;">elif</span> inputage <<span style="color:#000000;"> age:
                  </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">your input is smaller</span><span style="color:#800000;">"</span><span style="color:#000000;">)
              </span><span style="color:#0000ff;">else</span><span style="color:#000000;">:
                  </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">your age is ok</span><span style="color:#800000;">"</span><span style="color:#000000;">)
                  </span><span style="color:#0000ff;">break</span><span style="color:#000000;">
              count </span>+= 1
          <span style="color:#008000;">#</span><span style="color:#008000;">这里else表示,循环执行三次后,执行下面的这句话</span>
          <span style="color:#0000ff;">else</span><span style="color:#000000;">:
              </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">you input is too more</span><span style="color:#800000;">"</span>)
    • for循环

      • range()函数,range(10),表示从0-->10,左闭右开,range(0,10,2),表示从 0-->10左闭右开,其中步长为2
        • <span style="color:#0000ff;">for</span> i <span style="color:#0000ff;">in</span> range(10<span style="color:#000000;">):
              </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(i)
          </span><span style="color:#008000;">#</span><span style="color:#008000;">如果打印 2,到10 的偶数</span>
          <span style="color:#0000ff;">for</span> i <span style="color:#0000ff;">in</span> range(2,10,2<span style="color:#000000;">):
              </span><span style="color:#0000ff;">print</span><span style="color:#000000;">(i)
          </span><span style="color:#008000;">#</span><span style="color:#008000;">求100以内偶数的和</span>
          sum =<span style="color:#0000ff;">for</span> i <span style="color:#0000ff;">in</span> range(0,100,2<span style="color:#000000;">):
              sum </span>+=<span style="color:#000000;">i
          </span><span style="color:#0000ff;">print</span>(sum)
              • 上面求100以内的偶数和,如果不设定步长
        • <em><em>sum =<span style="color:#0000ff;">for</span> i <span style="color:#0000ff;">in</span> range(100<span style="color:#000000;">):
              </span><span style="color:#0000ff;">if</span> i % 2 ==<span style="color:#000000;"> 0:
                  sum </span>+=<span style="color:#000000;">i
              </span><span style="color:#0000ff;">else</span><span style="color:#000000;"> :
                  </span><span style="color:#0000ff;">continue</span>
          <span style="color:#0000ff;">print</span>(sum)</em></em>

          这里其实使用一个判断语句就可以,加上continue是抢到continue的作用,是跳出此次循环进入下次循环,for语句后也可以跟else,拿上面的while例子举例

          <em><em>age = 23<span style="color:#000000;">
          count </span>=<span style="color:#0000ff;">for</span> i <span style="color:#0000ff;">in</span> range(3<span style="color:#000000;">):
              inputage </span>= int(input(<span style="color:#800000;">"</span><span style="color:#800000;">age:</span><span style="color:#800000;">"</span><span style="color:#000000;">))
              </span><span style="color:#0000ff;">if</span> inputage ><span style="color:#000000;"> age:
                  </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">your input is bigger</span><span style="color:#800000;">"</span><span style="color:#000000;">)
              </span><span style="color:#0000ff;">elif</span> inputage <<span style="color:#000000;"> age:
                  </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">your input is smaller</span><span style="color:#800000;">"</span><span style="color:#000000;">)
              </span><span style="color:#0000ff;">else</span><span style="color:#000000;">:
                  </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">your age is ok</span><span style="color:#800000;">"</span><span style="color:#000000;">)
                  </span><span style="color:#0000ff;">break</span>
          <span style="color:#008000;">#</span><span style="color:#008000;">这里else表示,循环执行三次后,执行下面的这句话</span>
          <span style="color:#0000ff;">else</span><span style="color:#000000;">:
              </span><span style="color:#0000ff;">print</span>(<span style="color:#800000;">"</span><span style="color:#800000;">you input is too more</span><span style="color:#800000;">"</span>)</em></em>
  • 字符串的格式化

    • <em><em>name = input(<span style="color:#800000;">"</span><span style="color:#800000;">name</span><span style="color:#800000;">"</span><span style="color:#000000;">)
      age </span>= input(<span style="color:#800000;">"</span><span style="color:#800000;">age</span><span style="color:#800000;">"</span><span style="color:#000000;">)
      info </span>= <span style="color:#800000;">'''</span><span style="color:#800000;">
        your information %s
        
      name:%s
      age:%s  
      
      
      </span><span style="color:#800000;">'''</span>%<span style="color:#000000;">(name,name,age)
      </span><span style="color:#008000;">#</span><span style="color:#008000;">多行,使用''''''括起来</span>
      info1 = <span style="color:#800000;">'''</span><span style="color:#800000;">
        your information {_name}
        
      name: {_name}
      age: {_age}
      
      
      </span><span style="color:#800000;">'''</span>.format(_name=name,_age=age)</em></em>

例如用户输入的是密码想以密文形式展示,需要引入python的内库,getpass

<span style="color:#0000ff;">import</span><span style="color:#000000;"> getpass

passwd </span>= getpass.getpass(input(<span style="color:#800000;">"</span><span style="color:#800000;">password:</span><span style="color:#800000;">"</span>))

新闻动态联系方式广告合作招聘英才安科实验室帮助与反馈About Us

Copyright © 2013 - 2017 Ancii. All Rights Reserved京ICP备13035690号 京公网安备11010802014868号