Python3菜鸟教程(1):基本数据类型

教程链接

零零散散的学了一段时间python,准备系统的跟着菜鸟教程复习一遍,也算是查漏补缺,温故知新。太简单的常识问题不做记录,简单写写平时没有注意的地方或者重点

# -*- coding:UTF-8 -*-

is_output = 1

if __name__ == '__main__':
    # 转义字符的处理
    if is_output == 0:
        print('hello pytho\n')
        print(r'hellp python')

    # 元组到字典的强制类型转换
    if is_output == 0:
        tup = [('xie', 23), ('du', 22), ('zhang', 12)]
        dic = dict(tup)
        print(dic['xie'])

    # 整数的除法
    if is_output == 0:
        a = 1
        b = 2
        print(a / b)   # 正儿八经的除法  结果为浮点数
        print(a // b)  # 类似c/c++里的整数除法
        print((1.0 * a) // b)  # 简单的说就是除后结果向下取整

    # str list 的下标
    if is_output == 0:
        # 下标两种表示 [0, str(obj) - 1], [-len(obj), -1]
        content_str = "hello python!"
        content_list = ['hello', 'python', '!']
        print(content_str[-1], content_str[len(content_str) - 1])
        print(content_list[1], content_list[-2])

    # str list切片
    if is_output == 1:
        content_str = 'hello python'     # content_str[0] = 'H' TypeError, str中的元素不能修改
        content_list = ['hello', 'python', '!']
        new_str = content_str[0:5]
        print(new_str)
        new_list = content_list[0:1]     # 截取list中的一部分,返回值仍然是一个list
        print(new_list)
        list_element = content_list[0]   # 去list中的一个元素
        print(list_element)

        content_list[0] = 'Hello'  #字符串的切片可以理解为深拷贝
        print(new_list)


 

你可能感兴趣的:(Python学习)