python基础知识

启动方式:
    1 直接Python
    2 python -c 'command' [arg] ...
    3 python -m 模块 [arg] ...

定义文件编码: # -*- coding: utf-8 -*-

整型:
    1 **: 5 ** 2 5的2次方
    2 //: 17 // 3 = 5 取正

字符串(不可变对象):
    1 字符串前加r,原始字符: print(r'a\nb') 输出 a\nb
    2 """ ...""" 注释
    3 +操作符连接 * 重复次数
    4 word=abcd: word[0:2]为 ab 0:开始位置(包含) 2结束位置(不包含)

列表(可变对象):
    1 定义列表: squares = [1, 4, 9, 16, 25] or list(range(5)) | list([1,2])

条件控制:
    1   if (x > 100) or ok in ('a','b','ok'):
            .....
        elif (x < 100):
            ...
    2   for n in range(2, 10):
            for x in range(2, n):
                if n % x == 0:
                    print(n, 'equals', x, '*', n//x)
                    break
            else:(注意:循环的else子句在未出现break时运行 try语句的else子句在未出现异常时运行)
                # loop fell through without finding a factor
                print(n, 'is a prime number')
        执行结果:
            2 is a prime number
            3 is a prime number
            4 equals 2 * 2
            5 is a prime number
            6 equals 2 * 3
            7 is a prime number
            8 equals 2 * 4
            9 equals 3 * 3

函数:
    1 基本定义:
        def fib(n):
            ret = [];
            a, b = 0, 1;
            while a < n:
                ret.append(a);
                a, b = b, a+b;
            return ret;
        print(fib(30))
    2 函数参数定义:
        def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
        调用方式:
            parrot(1000)                                          # 1 positional argument
            parrot(voltage=1000)                                  # 1 keyword argument
            parrot(voltage=1000000, action='VOOOOOM')             # 2 keyword arguments
            parrot(action='VOOOOOM', voltage=1000000)             # 2 keyword arguments
            parrot('a million', 'bereft of life', 'jump')         # 3 positional arguments
            parrot('a thousand', state='pushing up the daisies')  # 1 positional, 1 keyword

        def cheeseshop(kind, *arguments, **keywords):
        调用方式:
            cheeseshop("Limburger", "It's very runny, sir.",
           "It's really very, VERY runny, sir.",
           shopkeeper="Michael Palin",
           client="John Cleese",
           sketch="Cheese Shop Sketch")
        参数解释:
            *name必须出现在**name之前
            *name形式的形式参数组合,这种形式接收一个元组 这个元组包含除形式参数之外的所有位置参数【It's very runny, sir. It's really very, VERY runny, sir.】
            **name形式的形式参数,它将接收一个字典【shopkeeper="Michael Palin" client="John Cleese" sketch="Cheese Shop Sketch】
        def write_multiple_items(file, separator, *args):
        参数解释: 
            *args 所有参数 排除位置参数和形参
    3 pring(my_function.__doc__) //打印函数的注释
    4 print(my_function.__annotations__) //函数 参数注解


类和函数的命名风格要一致;传统上使用 CamelCase (驼峰风格)命名类 而用 lower_case_with_underscores(小写字母加下划线)命名函数和方法

转载于:https://my.oschina.net/hackdebug/blog/3002930

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值