字符串,列表,元组,字典

#-*- coding:utf-8 -*-
a = b = c =1
print a,b,c

a,b,c = 1,2,"john"
print a,b,c

#字符串
str = "hello world"
print str
print str[2]
print str[2:7]
print str[2:]
print str * 2
print str + "Text"

#列表
list = ['tiantian',16,14.4,'zheli']
tinylist = [123,"john"]
print list
print tinylist
print list + tinylist
print list[1:3]

#元组
tuple = ('runoob',1243,3.33,'john',70.2)
tinytuple = (23,'john')
print tuple
print tinytuple
print tuple[2:5]
print tuple + tinytuple

#字典
dic = {}
dic[2] = 'This is two'
tinydict = {'name':'john','code':342,'dept':"sales"}
print dic
print dic[2]
print tinydict
print tinydict.keys()
print tinydict.values()

#python数据类型转换
#使用type查看数据类型
#首先声明一个固定的字典
person = {'name':'beida','dress':'beijing','age':'90'}
#取出所有的键
print person.keys()
#取出所有的值
print person.values()
#往字典里添加内容
person['price'] = 150
print person
#修改
person['name'] = 'qinghua'
print person
#删除
person.pop('name')
print person

#数据类型的转换
age1 = '18'

# 将 str 类型 转为 int 类型
age2 = int(age1)

print type(age1),type(age2)

# 将 int 类型 转为 str类型

age3 = str(age2)

print type(age3)

你可能感兴趣的:(字符串,列表,元组,字典)