元组( 元组是不可变的)
hello = (1,2,3,4,5)
type(hello)
myinfo = ['simida', 20, 'dalao', ['史蒂夫', 2000]]
print(myinfo[0])
print(myinfo[3][0])
type(myinfo)
状态:成立,不成立,用来做逻辑运算--》 判断
a = 1
b = 2
print(a > b)
可变类型:在id不变的情况下,value可以变,则称为可变类型,如列表,字典
a = [1,2,3,4,5]
print(id(a))
a[2]=88
print(a)
print(id(a))
不可变类型: value一旦改变,id也改变,则称为不可变类型(id变,意味着创造新的内存空间)
x = 123
pirnt(id(x))
x = 321
print(id(x))
1、
name = 'simida'
age = 10
print('my name is %s my age is %s ' %(name,age))
2、
name = 'simida'
age = 10
sex = 'man'
job = 'dalao'
msg = '''
------------- info of %s --------
Name : %s
Age : %s
Sex : %s
Job : %s
-------------- end -----------
'''
print(msg%(name,name,age,sex,job))
1 = [1,2,3,4,5]
a,b,c,d,e = 1 #把列表1里的值赋给a,b,c,d.e
print(a,b,c,d,e)
import random
a = [1,2,3,4,5]
result = random.choice(a) #从列表a里随机出数
print(result)
result = random.randint(0, 10) #在0-10里面随机输出
a = (1,2,3,4,5,6,7,8,9)
print(a[1:5]) #只显示第二位到第五位的数
print(a[:5]) #只显示前五位的数
print(a[5:]) #只显示第五位之后的数
result = ' linsdjf '
print(result.lstrip())
result = '-----fsdf------'
print(result.strip(-))
result = '192.168.88.123'
print(result.split('.',2)) #以 . 为分隔符分割
result = '192 168 88 123'
print(result.split()) #split()默认按空格分割,从左往右分割
print(result.rsplit(' ', 1)) #rsplit(' ',1) 以空格为分隔符从右往左分割1位
content = 'sdfs'
result = content.upper() #把content里的内容改成大写
print (result)
name = 'hello world'
print(name.title()) #把name里每个单词首位改成大写
#在{}处插入simada和23
result = 'my name is {} my age is {}' .format('simida',23)
print(result)
i = asdf\n
print(i.replace('\n','')) #结果去掉\n
extend
#添加一个字符串
a = [2,3,4,5,6]
a.extend('qwe')
a = [2,3,4,5,6]
a.extemd([123,1233,435,22,1,1])
print(a) #输出添加的列表在原列表后
append
#添加一个列表
a = [2,3,4,5,6]
a.append([123,1233,435,22,1,1])
print(a) #输出添加的列表带有括号'[]'
a = [2,3,4,5,6]
a.insert(3,'kk') #在第四位插入kk
pop
a = ['a','b','c','d','e']
a.pop() # 不加参数默认从后往左取出
a.pop(1) #添加索参数,按索引取出
result = a.pop(1)
remove
a = ['a','b','c','d','e',1,2,3]
a.remove('b')
print(a)
name = a.remove('b') #remove真是把元素从列表里删除而不是取出
print(name)
print(a.count('d')) #统计次数
a.reverse() #翻转
a.sort() #列表里的字符串和数字没法比
print(a)
a.clear() #清空列表
print(a)
a = (2,31,23,21)
result = ''.join(a) #把列表合并成一个字符串
print(a)
a = ('a','b','c','d','e','f')
print(len(a))
print(a._len_())
print(a._len_())
i = 0
result = len (a)
wihle i < result:
print(a[i])
i += 1
Powered By WordPress
手机扫一扫
移动阅读更方便
你可能感兴趣的文章