Python学习笔记--变量

1.字符串

msg='hello world1'
print(msg)
print(msg[0])
print(msg[:3])
print(msg[2:5])
print(msg[:5])
print(msg*2)
print(3*msg)
print(msg+" GGBond")

  运行结果:

1 hello world1
2 h
3 hel
4 llo
5 hello
6 hello world1hello world1
7 hello world1hello world1hello world1
8 hello world1 GGBond

2.列表

  列表类似于Java的数组,是一个有序可变集合的容器,支持内置的基础数据结构甚至列表,列表可以嵌套,。不同的数据结构也可以放在同一个列表中,没有同一类型的限制。

list_a=["a","b",3,["hhh","hello",'world']]
list_b=["hello"]
1 print (list_a[0])
2 print (list_a[2])
3 print (list_a[3])
4 print (list_a[1:3])
5 print (list_a[1:])
6 print (list_a[:3])
7 print (list_a)
8 print (list_b*2)
9 print (list_b+list_a)

运行结果:

1 a
2 3
3 ['hhh', 'hello', 'world']
4 ['b', 3]
5 ['b', 3, ['hhh', 'hello', 'world']]
6 ['a', 'b', 3]
7 ['a', 'b', 3, ['hhh', 'hello', 'world']]
8 ['hello', 'hello']
9 ['hello', 'a', 'b', 3, ['hhh', 'hello', 'world']]

3.元组

   元组可以视为不可变的列表,在复制后就不可以再进行二次改变

  

tuple_a=("str",1,("a","a"))
tuple_b=("b",2,["b","b"])
1 print(tuple_a[0])
2 print(tuple_a[2])
3 print(tuple_b+tuple_a)
4 print(tuple_a*2)
1 str
2 ('a', 'a')
3 ('b', 2, ['b', 'b'], 'str', 1, ('a', 'a'))
4 ('str', 1, ('a', 'a'), 'str', 1, ('a', 'a'))

4.字典

  字典其实就是key-value键值对的集合,无序的容器

dict_a={
    "name":"GGbond",
    "age":21,
    1:"level_1"
}
1 print(dict_a["name"])
2 print(dict_a[1])
3 print(dict_a)
4 print ("name" in dict_a)
5 print ("xxx"in dict_a)
6 print(dict_a.keys())
7 print(dict_a.values())
8 print(dict_a.items())

运行结果:

GGbond
level_1
{'name': 'GGbond', 'age': 21, 1: 'level_1'}
True
False
dict_keys(['name', 'age', 1])
dict_values(['GGbond', 21, 'level_1'])
dict_items([('name', 'GGbond'), ('age', 21), (1, 'level_1')])
原文地址:https://www.cnblogs.com/jifeng0902/p/13881693.html