Python基础小知识

作者: Tester_Jingel | 来源:发表于2017-03-24 10:13 被阅读86次

    注:一个区分大小写的语言

    数据类型:

    • 整数 #a=2
    • 浮点数 #a=3.23和b=23.8E
    • 复数 #(-5+4j)

    字符串

    • 单引号 #'this is a demo1'
    • 双引号 #"this is a demo2"
    • 三引号 #'''this is a demo3,"what's your name?"''',可以在三引号中自由的使用单引号和双引号

    转义字符

    \n #换行

    \\ #转义字符

    format方法

    `name="ZhangSan"`
    `age=18`
    `print('{0} is {1} years old'.format(name,age))`
    

    变量

    Python声明一个变量不需要规定其变量的数据类型,会自动识别
    Python变量由字母、下划线或者数字组成 #数字不能该开头

    变量和常量的区别:见demo
    i = 5 i = i+1 s = "hello world"

    运算符和表达式

    • 运算符的优先级 #可以使用括号利于阅读且查看优先级
    • 相同优先级的运算符一般是按照从左到右的顺序计算的 #2+3+4

    控制流

    • if语句
    # -*- coding:utf-8 -*-
    #循环的输入:
    Number = 23
    running = True
    while running:
        Input_Number = int(input('Please enter a number:'))
        if Input_Number == Number:
            print('You got it ,good job!!!')
            running = False
        elif Input_Number >Number:
            print('this number is higher')
        else:
            print("this number is lower")
            break;
            print("done")    
    else:
        print("the while loop is over.")
    print("Done")
    
    • for循环
    for i in range(1,6):
        print(i)
    
    • while循环
    • break语句
    • continue语句

    函数

    • 基础格式
    def sayhello():
          print("hello!!!")
    sayhello()
    
    • 函数的参数
    # -*- coding:utf-8 -*-
    #定义获取最大值函数
    def getMaxSize(a,b):
        if a >b:
            print("the large size is ",a)
        elif a == b:
            print(a,"is equal to",b)
        else:
            print("the large size is ",b)
    getMaxSize(10,5)
    
    • 局部变量
    # -*- coding:utf-8 -*-
    x = 20
    def func(x):
        print("x is ",x)
        x=2  #此处的X是局部变量只在整个函数生效,在此函数之外则就无效了
        print("x is ",x)
    func(x)
    print("x is still",x)
    
    • 全局语句
    # -*- coding:utf-8 -*-
    gcount = 0
    def global_test():
        print (gcount) 
    def global_counter():
        global gcount
        gcount +=1
        return gcount    
    def global_counter_test():
        print(global_counter())
        print(global_counter())
        print(global_counter())
    global_counter_test()
    输出结果:1 2 3
    
    • 非全局语句
    # -*- coding:utf-8 -*-
    def make_counter():
        count = 0
        def counter():
            nonlocal count
            count += 1
            return count
        return counter
    def make_counter_test():
        mc = make_counter()
        print(mc())
        print(mc())
        print(mc())
    make_counter_test()
    
    • 默认参数值
    # -*- coding:utf-8 -*-
    def say(message,time=1):
        print(message*time)
    say("hello\n",5)
    
    • 关键参数
    # -*- coding:utf-8 -*-
    def func(a,b=5,c=6):
        print("a is",a,"\n", "b is",b,"\n", "c is",c,"\n")
    func(5)
    func(10,5,30)
    func(5,c=2)
    
    • VarArgs参数
      “*****”参数名:代表这是一个列表
      “****”参数名:代表这是一个字典
    # -*- coding:utf-8 -*-
    def total(inttial=5, *numbers,**keywords):
        count = inttial
        for number in numbers:
            count +=number
            print("number:",number)
            print("count:",count)
        for key in keywords:
            count +=keywords[key]
            print("key:",key)
            print("count:",count)
        return count
    print(total(10,1,2,3,vegt=50,frunits=100))
    输出结果:
    number: 1
    count: 11
    number: 2
    count: 13
    number: 3
    count: 16
    key: vegt
    count: 66
    key: frunit
    count: 166
    
    • keyword-only参数
      在带*号的参数后面声明参数,在输出函数时赋值或者给定一个默认参数值
    # -*- coding:utf-8 -*-
    def total(inttial=5, *numbers,vegetables):
        count = inttial
        for number in numbers:
            count +=number
        count += vegetables
        return "the count is :",count
    print(total(10,1,2,3,vegetables=50))
    print(total(10,1,2,3,))
    输出结果:
    ('the count is :', 66)
    Traceback (most recent call last):
      File "C:\Users\yuanyuan_cheng\Desktop\python-demo.py", line 10, in <module>
        print(total(10,1,2,3,))
    TypeError: total() missing 1 required keyword-only argument: 'vegetables'
    

    要是给vegetables=5,就不会出现上面的TypeError
    正确输出:('the count is :', 66) ('the count is :', 21)

    • return语句
    def maxsize(x,y):
        if x>y:
            return "the large value is ",x
            print(x)
        else:
            return "the large value is ",y
            print(y)
    print(maxsize(50,2))
    
    • DocString
    # -*- coding:utf-8 -*-
    def printMax(x,y):
        '''
        the docmentstring 
        '''
        x = int(x)
        y = int(y)
        if x>y:
            print(x,'is maximum')
        else:
            print(y,'is maximum')
    printMax(3,5)
    print(printMax.__doc__) #打印出信息
    help(printMax) #打印出函数下的信息
    输出结果:
    5 is maximum
        the docmentstring 
    Help on function printMax in module __main__:
    printMax(x, y)
        the docmentstring
    

    并不是早起有多困难,而是早起后干什么,这件事你没考虑好。

    相关文章

      网友评论

        本文标题:Python基础小知识

        本文链接:https://www.haomeiwen.com/subject/jnbgnttx.html