《利用Python进行数据分析》第二章:Python基础 知识点总结

语言语义

  • 缩进
    一个冒号代表一个缩进代码块的开始。

    for x in array:
        if x < pivot:
            less.append(x)
        else:
            greater.append(x)
    
  • 语句不以分号结尾,分号用来分隔一行内多个语句。

  • 一切皆为对象:语言灵活

  • #注释

  • 对一个变量赋值时,我创建了一个指向等号右边对象的引用。

    a = [1, 2, 3]
    b=a
    a.append(4)
    b
    #[1, 2, 3, 4]
    #a b指向同一个对象
    
    def append_element(some_list, element):
        some_list.append(element)
        
    In [27]: data = [1, 2, 3]
    In [28]: append_element(data, 4)
    In [29]: data
    Out[29]: [1, 2, 3, 4]
    
  • 对象引用并不涉及类型。变量对于对象只是特定命名空间的名称;类型信息存储在对象中的。

    • 查看两个引用是否指向一个对象

      a = [1, 2, 3]
      b = a
      c = list(a)
      a is b
      #true
      a is not c
      #true
      
  • 二元操作符:见P41

  • 可变对象与不可变对象

    • 可变对象:列表、字典、NumPy数组
    • 不可变对象:字符串、元组

标量类型

  • None

  • str

    a = 'one way of writing a string' 
    b = "another way"
    c = """
    This is a longer string that
    spans multiple lines
    """
    
    #将其他对象转换成字符串
    s = str(a)
    
    #字符串可以看做除列表和元组外的另一种序列,可以使用切片
    s = 'python'
    list(s)
    #['p', 'y', 't', 'h', 'o', 'n']
    s[:3]
    #'pyt'
    
    #特殊字符处理:转义字符\
    s = '12\\34'#'12\34'
    print(s)
    #特殊字符处理:r原生字符串
    s = r'this\has\no\special\characters'
    
    #字符串的合并
    a+b
    
    #字符串格式化
    template = '{0:.2f} {1:s} are worth US${2:d}'
    template.format(4.5560, 'Argentine Pesos', 1)
    
  • bytes

  • float

  • bool

  • int

控制流

if语句
if x < 0:
    print('It's negative')
elif x == 0 or a < b:
    print('Equal to zero')
elif 0 < x < 5:
    print('Positive but smaller than 5')
else:
    print('Positive and larger than or equal to 5')
for语句
for i in range(4):
    for j in range(4):
        if j > i:
            break
        print((i, j))
        
sequence = [1, 2, None, 4, None, 5]
total = 0
for value in sequence:
    if value is None:
        continue
    total += value
pass语句

用于表示不执行任何操作。

range方法

返回一个迭代器,该迭代器生成一个等差整数序列。

包含起始不包含末尾

list(range(10))#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list(range(0, 20, 2))
list(range(5, 0, -1))#[5, 4, 3, 2, 1]
三元表达式
x = 5
y='Non-negative' if x >= 0 else 'Negative'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值