zoukankan      html  css  js  c++  java
  • Python基础知识之二

      在上一篇写了一些Python的基本语法,这篇主要是写Python里面的条件和循环和函数结构,不多说,直接进入正题。

      Python的条件和循环语句:

        1.if语句:

          if 条件语句:

            代码块

          elif 条件语句:

            代码块

          else:

            代码块    

        2.while循环:

          while 条件语句:

            代码块

        3.for循环:

          for item in items:

            代码块

        4.关键字

          1.break:在代码块中终止循环,并跳出整个循环

          2.continue:在代码块中终止当前循环,并进入下一个循环

          3.pass:在Python中为了保证函数的完整,不允许存在空函数时,添加pass

        5.函数:

          def 函数名(属性名1:类型=默认,属性名2:类型=默认):

            代码块   #类型可以忽略

        6.注意事项:

          1.在Python中函数是没有括号的,所以Python 有严格的字符缩进规则

          2.Python语句结尾不用带分号

          3.Python严格区分大小写

          4.暂时就想到这么多,看官若想补充,请留言:)

      Python条件和循环以及函数语句的例子:

    #if循环和print函数的三种带变量输出方式
    num1=12 if num1>12: print("num1>%d"%(num1)) elif num1==12: print("num1=%d"%num1) else : print("num1<%",num1)
    #输出结果为 num1=12
    #while和if嵌套使用并展示break和continue用法
    num2=0 while num2<6: num2 = num2 + 1 if num2==5: break elif num2==3: continue print(num2)
    #输出1,2,4
    #展示怎么偏历list和range函数
    list = ["a","b",21]
    for i in list:
        print(i)  #输出 a,b,21
    for i in range(3):
        print(i) #输出0,1,2
    def people(name:str="",age:int=18,sex:str="boy") -> int:   #函数返回一个int类型的值 可省略
        print("name:%s
    age:%d
    sex:%s
    "%(name,age,sex))
    people("rose",20,"girl")  #name:rose,age:20,sex:girl

    好了,Python的基本知识就到这里了,后面会写一些Python的进阶知识:)

      

  • 相关阅读:
    IIS的各种身份验证详细测试
    HTTP Error 401.3 Unauthorized Error While creating IIS 7.0 web site on Windows 7
    C/S and B/S
    WCF ContractFilter mismatch at the EndpointDispatcher exception
    Configure WCF
    Inheritance VS Composition
    Unhandled Error in Silverlight Application, code 2103 when changing the namespace
    Java RMI VS TCP Socket
    Principles Of Object Oriented Design
    Socket处理发送和接收数据包,一个小实例:
  • 原文地址:https://www.cnblogs.com/GaryNie/p/8494877.html
Copyright © 2011-2022 走看看