Python变量类型

Python有五个标准的数据类型:
1. Numbers(数字)
2. String(字符串)
3. List(列表)
4. Tuple(元组)
5. Dictionary(字典)

知识点包括
1. 内存
2. 赋值
3. 多个变量同时赋值
4. 数字大小(int, float,long)
5. 数据类型转换
6. 需要添加以下字符串,支持中文: #-- coding: UTF-8 --
7. …..

测试程序

# Variable Types.py
#-*- coding: UTF-8 -*- #支持中文
print('********* Variable Types *********')

nValueA = 1238
fValueB = 10.2903
strValueC = "Hello Python"
strValueD = 'Hello Python'
print(nValueA)
print(fValueB)
print(strValueC)
print(strValueD)

print('\n' + 'nValueE = nValueF = nValueG = 77')
nValueE = nValueF = nValueG = 77
print(nValueE, nValueF, nValueG)

print('\n' + 'nValueE, nValueF, nValueG = 1000, 7000, "This is a dog"')
nValueE, nValueF, strValueG = 1000, 7000, 'This is a dog'
print(nValueE, nValueF, strValueG)

print('\n' + 'nValueE + nValueF')
print(nValueE + nValueF)

del nValueA, fValueB
#print(nValueA, fValueB)    #Error

#ffValue(2, 3)
#print(ffValue);

print('\n' + '****************************** String **************')

strTestA = "I LOVE CHINESE"
print(strTestA)
strTestB = strTestA[2:9]
print("strTestA[2:9]: " + strTestB)
print("strTestA[3]: " + strTestA[3])
print("strTestA[2:]: " + strTestA[2:])
print("strTestA[:8]: " + strTestA[:8])
print("strTestA * 2: " + strTestA *2)

print('\n' + '****************************** List **************')
listTest1 = ['LOVE', 'YOU', '###', 520, 13.14, 'China', 168]
print('listTest1: ')
print(listTest1)
listTest1[2] = "!!!!!"
print('listTest1: ')
print(listTest1)
print('listTest1[2]: ' + listTest1[2])
print('listTest1[1:3]: ');
print(listTest1[1:3])   
print('listTest1[:5]: ');
print(listTest1[:5])
print('listTest1 * 2: ')
print(listTest1 * 2)

print()
listTest2 = ["Don't", 'Go', 88888]
print('listTest2: ')
print(listTest2)
print('listTest1 + listTest2: ')
print(listTest1 + listTest2)

print('\n' + '****************************** Tuple **************')
tupleTest1 = ('I', 'LOVE', 1314, 5.20, "YOU")
print(tupleTest1)
print("tupleTest1[2]: ")
print(tupleTest1[2])
#print(tupleTest1[6])       #Error
print("tupleTest1[1:5]: ")
print(tupleTest1[1:5])
#tupleTest1[2] = 888888    #Error

print()
tupleTest2 = ('China', 'Is', 'Good', 168)
print('tupleTest2: ')
print(tupleTest2)
print('tupleTest1 + tupleTest2: ')
print(tupleTest1 + tupleTest2)
print('tupleTest1[1:3] + tupleTest2[:2]: ')
print(tupleTest1[1:3] + tupleTest2[:2])

print('\n' + '****************************** Dictionary **************')
dicTest1 = {}
dicTest1['one'] = 168
dicTest1[2] = 'Work'
dicTest1['9.8'] = 'Hard'
print('dicTest1: ')
print(dicTest1)
print("dicTest1['one']")
print(dicTest1['one'])
print('dicTest1.keys(): ')
print(dicTest1.keys())
print('dicTest1.values(): ')
print(dicTest1.values())

print()
dicTest2 = {1:'I', 'two':'Love', 3.0:'China'}
print('dicTest2: ')
print(dicTest2)
print('dicTest2.keys(): ')
print(dicTest2.keys())
print('dicTest2.values(): ')
print(dicTest2.values())
#print('dicTest1 + dicTest2: ') #Error
#print(dicTest1 + dicTest2)     #Error

print('\n' + '****************************** Format Type**************')
strFormatTest1 = '59'
nFormatTest1 = int(strFormatTest1)
print(nFormatTest1)
fFormatTest1 = float(strFormatTest1)
print(fFormatTest1)
chFormat = chr(5)
print(chFormat)
strFormatTest2 = str(45.6)
print(strFormatTest2)

输出结果

********* Variable Types *********
1238
10.2903
Hello Python
Hello Python

nValueE = nValueF = nValueG = 77
77 77 77

nValueE, nValueF, nValueG = 1000, 7000, "This is a dog"
1000 7000 This is a dog

nValueE + nValueF
8000

****************************** String **************
I LOVE CHINESE
strTestA[2:9]: LOVE CH
strTestA[3]: O
strTestA[2:]: LOVE CHINESE
strTestA[:8]: I LOVE C
strTestA * 2: I LOVE CHINESEI LOVE CHINESE

****************************** List **************
listTest1: 
['LOVE', 'YOU', '###', 520, 13.14, 'China', 168]
listTest1: 
['LOVE', 'YOU', '!!!!!', 520, 13.14, 'China', 168]
listTest1[2]: !!!!!
listTest1[1:3]: 
['YOU', '!!!!!']
listTest1[:5]: 
['LOVE', 'YOU', '!!!!!', 520, 13.14]
listTest1 * 2: 
['LOVE', 'YOU', '!!!!!', 520, 13.14, 'China', 168, 'LOVE', 'YOU', '!!!!!', 520, 13.14, 'China', 168]

listTest2: 
["Don't", 'Go', 88888]
listTest1 + listTest2: 
['LOVE', 'YOU', '!!!!!', 520, 13.14, 'China', 168, "Don't", 'Go', 88888]

****************************** Tuple **************
('I', 'LOVE', 1314, 5.2, 'YOU')
tupleTest1[2]: 
1314
tupleTest1[1:5]: 
('LOVE', 1314, 5.2, 'YOU')

tupleTest2: 
('China', 'Is', 'Good', 168)
tupleTest1 + tupleTest2: 
('I', 'LOVE', 1314, 5.2, 'YOU', 'China', 'Is', 'Good', 168)
tupleTest1[1:3] + tupleTest2[:2]: 
('LOVE', 1314, 'China', 'Is')

****************************** Dictionary **************
dicTest1: 
{2: 'Work', '9.8': 'Hard', 'one': 168}
dicTest1['one']
168
dicTest1.keys(): 
dict_keys([2, '9.8', 'one'])
dicTest1.values(): 
dict_values(['Work', 'Hard', 168])

dicTest2: 
{1: 'I', 3.0: 'China', 'two': 'Love'}
dicTest2.keys(): 
dict_keys([1, 3.0, 'two'])
dicTest2.values(): 
dict_values(['I', 'China', 'Love'])

****************************** Format Type**************
59
59.0

45.6
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值