1. 简介
变量存储在内存中的值,这就意味着在创建变量时会在内存中开辟一个空间。
基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中。
因此,变量可以指定不同的数据类型,这些变量可以存储整数,小数或字符。
2. 变量使用
2.1 数字
如上章节所说,数字分为int、long、float、complex,在实际的开发中,使用的最多的int,其次是float,由于pyton是弱类型变量,可以相互进行加减乘除运算。
示例 :
var_int : 10
var_float : 10.0
var_long : 51924361
var_complex : 3.14j
数字的四则运算:
print('1 + 2 = {}'.format(1 + 2)) # 1 + 2 = 3
print('3-1 = {}'.format(3 - 1)) # 3 - 1 = 2
print('2*4 = {}'.format(2 * 4)) # 2 * 4 = 8
print('6/3= {}'.format(6 / 3)) # 6 / 3 = 2
由于数字的四则运算和数学中的几乎一样,这里就不做多的介绍,只贴出一个示例参考了。
2.2 字符串
字符串,即由一串中英文字符组成的列表,叫做字符串, 字符串可以用’’或者””或者’’’’’’引起来。
字符串示例 :
单个字符 : ‘a’
字符串 : “this is string”
中文字符串 : ‘’’中文字符串‘’’
字符串可以进行拼接、删除、截取、查找、替换等一系列操作,例如:
- 拼接:
字符串相加 : ‘this’+’ is good’ = ‘this is good’
字符串格式化拼接 : '{}{}'.format(‘this’+’ is good’) = ‘this is good’
每个{} 代表format中的一个变量
代码示例:
one_str = 'this'
two_str = ' is good'
end_str = one_str + two_str
end1_str = '{}{}'.format(one_str, two_str)
结果为 : this is good
- 截取:
one_str = 'this is string'
left_str = one_str[:4] #:前不填为0, 取0~4个字符串(this)
right_str = one_str[5:] #:后不填为到最后,第5个字符串开始到结束(is string)
middle_str = one_str[1:8] #:取第2到第9个(his is )
- 查找:
ne_str = 'this is string'
one_index = one_str.find('string') #查找one_str字符串中string子串的开始位置
- 替换:
one_str = 'this is string'
dest_str = one_str.replace('string', 'man') #替换字符串中string为man,替换的结果为(this is man)
2.3 列表
序列是Python中最基本的数据结构。序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推, 示例如下:
one_list = []
two_list = [1, 2, 3]
three_list = ['a', 'b', 'this']
four_list = [1, 2, 10.0, 'a', 'this']
取值操作:
one_list = [1, 2, 3]
one_var = one_list[0]
[1, 2, 3]第一个变量为1
one_var = one_list.pop() #取索引为0的值,并删除掉
[1, 2, 3] pop第一个变量值3, 当前列表为[1, 2]
one_list = [1, 2, 3, 4, 5, 6, 7, 8]
left_list = one_list[:3]
([1, 2, 3, 4, 5, 6, 7, 8])列表取前3个变量作为子列表:([1, 2, 3])
right_list = one_list[5:]
([1, 2, 3, 4, 5, 6, 7, 8])列表取从第6个变量到最后作为字列表:([6, 7, 8])
middle_list = one_list[1:5]
([1, 2, 3, 4, 5, 6, 7, 8])列表取第2到第6个变量作为子列表:([2, 3, 4, 5])
增加一个值:
one_list = [1, 2, 3]
one_list.append('this')
one_list.append(10)
给one_list增加一个字符串this和整数1,结果为:
[1, 2, 3, 'this', 10]
列表相加:
one_list = [1, 2, 3]
two_list = ['a', 'b', 'this']
end_list = one_list + two_list
end_list为one_list和two_list拼接,结果为:
[1, 2, 3, 'a', 'b', 'this']
删除一个值:
one_list = ['a', 'b', 'c']
one_list.pop()
(['a', 'b', 'c'])pop删除第一个元素后结果为:(['a', 'b'])
one_list.remove('b')
(['a', 'b', 'c'])pop删除值为b元素后结果为:(['a', 'c'])
2.4 字典
字典是另一种可变容器模型,且可存储任意类型对象。
字典的每个键值 key=>value 对用冒号 : 分割,每个键值对之间用逗号 , 分割,整个字典包括在花括号 {} 中 , key一般为字符串变量,如下所示定义:
one_dict = {}
two_dict = {'dkey': 'value'}
three_dict = {'1key': 'value', '2key': 11, '2key': [1, 2, 3], '3key': {'11': 22}}
取值操作:
test_dict = {'1key': '1value', '2key': '2value'}
value = test_dict.get('1key')
value1 = test_dict['1key']
通过get方法或者 [key] 都能够直接去的key对应的value。
增加一个键值:
test_dict['3key'] = '3value'
给test_dict新增一个键值对 '3key':'3value',j结果为:
{'1key': '1value', '2key': '2value', '3key': '3value'}
删除一个键:
test_dict.pop('1key')
del test_dict['1key']
通过del或者pop都能删除1key这个键值对,删除后的结果为:
{'1key': '1value'}
获取dict的所有key或所有values:
test_dict.keys() -> ['1key', '2key']
test_dict.values() -> ['1value', '2value']
3. 参考代码
https://github.com/minlixia/python (base_1/02_var.py)