一、基础知识

  1. 算数运算

    序号表达式含义
    13/2除以,结果1.5
    23//2整除,结果1
    32**3pow(2, 3),结果8
  2. 获取用户输入

    >>>x = input("x:")
    x:>? 123
    >>>y = input("y:")
    y:>? 456
    >>>print(int(x)*int(y))
    56088
    
  3. print用法

    打印多个参数时,在参数中间插入了空格

    >>>print('Age:',42)
    Age: 42
    >>>name = 'Gumby'
    >>>salutation = 'Mr.'
    >>>greeting = 'Hello,'
    >>>print(greeting, salutation, name)
    Hello, Mr. Gumby
    

    print自定义分隔符sep =

    print自定义结束符end=,默认为换行符,即print变量之后会换行

    >>>print("I", "wish", "to", "register", "a", "complaint", sep="_")
    I_wish_to_register_a_complaint
    >>> print('Hello,', end=' ')
    ...print('world!')
    Hello, world!
    
  4. 导包

    # 导包4种方式
    import somemodule
    from somemodule import somefunction
    from somemodule import somefunction, anotherfunction, yetanotherfunction
    from somemodule import *
    
    
    
    # 当导入的两个模块都含有函数open时
    
    #以第一种方法导入
    import module1
    import module2
    #调用
    module1.open(...)
    module2.open(...)
     
    # 或者
    from module1 import open as open1
    from module2 import open as open2
        
    
        
    # as用法
    >>>import math as foobar
    >>>foobar.sqrt(4)
    2.0
    
    >>>from math import sqrt as foobar
    >>>foobar(4)
    2.0
    
  5. 序列解包

    >>>x, y, z = 1, 2, 3
    >>>print(x, y, z)
    1 2 3
     
    >>>x, y = y, x
    >>>print(x, y, z)
    2 1 3
     
    >>>values = 1, 2, 3
    >>>values
    (1, 2, 3)
    >>>x, y, z = values
    >>>x
    1
     
    >>>scoundrel = {'name':'Robin', 'girlfriend':'Marion'}
    >>>key, value = scoundrel.popitem()
    >>>key
    'girlfriend'
    >>>value
    'Marion'
     
    >>>x, y, z = 1, 2
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    ValueError: not enough values to unpack (expected 3, got 2)
    >>>x, y, z = 1, 2, 3, 4
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    ValueError: too many values to unpack (expected 3)
     
    >>>a, b, *rest = [1, 2, 3, 4]
    >>>rest
    [3, 4]
     
    >>>name = "Albus Percival Wulfric Brian Dumbledore"
    >>>first, *middle, last = name.split()
    >>>middle
    ['Percival', 'Wulfric', 'Brian']
     
    >>>a, *b, c = "abc"
    >>>a, b, c
    ('a', ['b'], 'c')
    
  6. 断言

    工作原理伪代码

    if not condition:
    crash program
    

    目的是让程序在错误条件出现时立即崩溃(这样胜过以后再崩溃)

    >>>age = 10
    >>>assert 0 < age < 100
    >>>age = -1
    >>>assert 0 < age < 100
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    AssertionError
    
    >>>age = -1
    >>>assert 0 < age < 100, 'The age must be realistic'
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    AssertionError: The age must be realistic
    
  7. 列表推导

    # ==运算符表示内容相同,is表示引用相同
    
    ta = [x * x for x in range(10)]
    tb = []
    for x in range(10):
    	tb.append(x * x)
    assert ta == tb
    
    ta = [x * x for x in range(10) if x % 3 == 0]
    tb = []
    for x in range(10):
    	if x % 3 == 0:
        	tb.append(x * x)
    assert ta == tb
    
    ta = [(x, y) for x in range(3) for y in range(3)]
    tb = []
    for x in range(3):
        for y in range(3):
            tb.append((x, y))
    assert ta == tb
    
    girls = ['alice', 'bernice', 'clarice']
    boys = ['chris', 'arnold', 'bob']
    ta = [b + '+' + g for b in boys for g in girls if b[0] == g[0]]
    tb = []
    for b in boys:
        for g in girls:
            if b[0] == g[0]:
                tb.append(b + '+' + g)
    assert ta == tb
    
  8. 占位符

    if 1 == 2:
    	pass
    
  9. Python不同版本中 类的区别

​ 截止到Python2.1时,只存在旧式类;Python2.2引进新式类;Python3中只有新式类;有些功能(如特性和函数super)不适用于旧式类。

​ 如果文件开头有__metaclass__ = type,则文件中的类都是新式类;如果在类的作用域内有__metaclass__ = type,则仅当前类为新式类;

​ 新式类隐式地继承object。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MallocLu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值