Python self-study Start Basics Tutorial - Data Types

In the procedure, the operation can be divided into a constant (Literals) and variables (the Variables)

1x = 10*2-5
2y = x+1
  1. Part of the above variables are Python data types.

  2. In the above code, 10,2,5 are constants; x, y is a variable representing the value of a variable. * The first row - operator belongs, belongs to the assignment symbol =

Python data types can be divided into Numbers (digital), String (String), List (list), Tuple (Ganso), Set (collection), Dictionary (dictionary)

Python Digital Numbers: numeric data type for storing a value, Python supports four different types of digital

1>  int (integer) 
2>  a float (floating point)
. 3>  BOOL (Boolean)
4 - Boolean True, False keyword, or the value 1 and 0, and can be a digital phase
. 5>  Complex (complex)
6 - a plurality of real part and an imaginary part, or may be used a + bj complex (a, b), where a and b are floating point.

  

Note:

  • Before the arithmetic operations are performed different types of values ​​need to be Type Casting (conversion type)
  • Python is simultaneously a plurality of variable assignments, such as a, b = 1, 2.
  • A variable can point to different types of objects through the assignment.
  • Numerical division comprises two operators: / returns a floating point number, // returns an integer.
  • Upon mixing calculations, Python integer will be converted into floating-point numbers.

Python字符串String:Python对字符串的表达方法相当灵活,可以通过单引号 '',双引号 "",三引号 '''''' 多种方法表达

 

1> 单引号表达,需要注意转义字符,例如\t,\r等
2> 双引号表达,用于语句中含有单引号时,可以避免歧义
3> 三引号表达,用于语句中含有单双引号时,或用于跨行书写,或用于注释

  

1str = 'I can print'        # 单引号
 2str = "it's my life"       # 双引号
 3str = '''It's my "love"''' # 三引号
 4# 三引号的换行输出
 5print("""1111
 6    2222
 7    3333
 8    """)
 9# 三引号添加注释
10'''我是注释'''
11# 字符串前加r 表示不转义
12print(r'\n')
13# python2下中文字符串前加 u
14print(u'我是中文')

  

注:

  • 反斜杠可以用来转义,使用r可以让反斜杠不发生转义。
  • 字符串可以用+运算符连接在一起,用*运算符重复。
  • Python中的字符串有两种索引方式,从左往右以0开始,从右往左以-1开始。
  • Python中的字符串不能改变。

Python列表List:列表用 [ ] 标识,支持字符、数字以及列表(即列表嵌套)

1# 数字列表
2list  = [0, 1, 2, 3, 4]
3# 字符列表
4list = ['a', 'b', 'c', 'd']
5# 混合列表
6list = [0, 1, 2, 'a', 'b']
7# 嵌套列表
8list = [0, [1, 2, 3], 4]

  

注:

  • List写在方括号之间,元素用逗号隔开。
  • 和字符串一样,list可以被索引和切片。
  • List可以使用+操作符进行拼接。
  • List中的元素是可以改变的。

列表list增删改查:

  • 增:apeend/insert
    注:append增加在列表尾部,insert可通过参数选择插入的下标
  • 删:pop 删除尾部元素
  • 改:重新赋值
  • 查:下标
 1# 尾部插入数字10
 2list = list.append(10)
 3# 第二位插入数字10
 4list = list.insert(1, 10)
 5# 删除尾部元素
 6list = list.pop()
 7# 修改第二位的值为'a'
 8list[1] = 'a'
 9# 查找第二位的值
10list[1]

  

Python元组tuple:元组用 () 标识,支持字符、数字以及列表

1# 定义元组
2tuple = ('a', 1, 2, [0, 1, 'b'])

  注:

  • 与字符串一样,元组的元素不能修改。
  • 元组也可以被索引和切片,方法一样。
  • 注意构造包含 0 或 1 个元素的元组的特殊语法规则。
  • 元组也可以使用+操作符进行拼接。

元组tuple增删改查:

一初始化就不能修改,所以没有增删改功能

  • 查:下标

1# 查找第二位的值
2tuple[1]

  

Python集合set:集合用 {} 标识,支持字符、数字以及元组

1# 定义集合
2set = {'a', 1, 2, (0, 1, 'b')}

  

注:

  • 集合中数据必须是唯一的,每种数据元素只会保留一份
  • 集合set是无序的,每次输出的元素排序可能会不同

集合set增删改查

  • 增:通过add 方法增加

  • 删:通过remove 方法删除
  • 改:无法修改
  • 查:通过in 方法查找

1# 增加数据c
2set.add('c')
3# 删除数据c
4set.remove('c')
5# 查找数据c,结果为布尔类型,True表示存在
6'c' in set

  

Python字典:字典用 {} 标识,字典由索引key和对应的值value组成

1# 定义字典
2dict = {'a':1, 2:[0, 1, 'b']}

  注:

  • 字典是一种映射类型,它的元素是键值对。
  • 字典的关键字必须为不可变类型,且不能重复,重复会覆盖原有value值。
  • 创建空字典使用 { }。

字典输出所有键、值

1# 输出所有的键
2dict.keys()
3# 输出所有的值
4dict.values()

  

字典dict增删改查

  • 增:通过键值增加
  • 删:通过键删除
  • 改:同增加方法
  • 查:通过get(键)查找
1# 增加一条记录
2dict['zhaoyun'] = 98
3# 删除一条记录
4dict.pop('zhaoyun')
5# 查找一条记录
6dict.get('zhaoyun')

  

Python数据类型转换:数据类型的转换,只需要将数据类型作为函数名即可

1# 将浮点类型转换为int
2int(12.22)
3# 将整数转换为字符串
4str(12)
5...

  

 

 

 

Guess you like

Origin www.cnblogs.com/daniumiqi/p/12095899.html