python3 认真学习的第三天,从零开始

spacenum=6
i=1
listspacenum=spacenum
triangle=[]
while listspacenum>=0:
    leftspacelist=['6'] * listspacenum
    starlist=['*'] * (2*i-1)
    rightspacelist=['6'] * listspacenum
    linelist= leftspacelist + starlist + rightspacelist
    triangle.append(linelist)
    listspacenum-=1
    i+=1
for line in triangle:
    print(line)


acount=[
    ['song','123456'],
    ['zhang','123456'],
    ['wang','123456']
]
username=input('输入姓名:')
password=input('输入密码:')
if [username,password] in acount:
    print('登录成功!')
else:
    print('登录失败!')

#序列相加相乘
>>> print([1,2,3]+[4,5,6])
[1, 2, 3, 4, 5, 6]
>>> pring(['hello']+['world'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'pring' is not defined
>>> pring('hello'+'world')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'pring' is not defined
>>> pring('hello'+'world')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'pring' is not defined
>>> print('hello'+'world')
helloworld
>>> print(['hello']+['world'])
['hello', 'world']
>>> print([1,2,3,4]+['hello'])
[1, 2, 3, 4, 'hello']
>>> print([1,2,3,4]+'hello')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "str") to list
>>> print([1,2,3,4]+['h','e','l','l','o'])
[1, 2, 3, 4, 'h', 'e', 'l', 'l', 'o']
>>> print('hello'*5)
hellohellohellohellohello
>>> print([50]*5)
[50, 50, 50, 50, 50]
>>> print([nice]*5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'nice' is not defined
>>> print(['nice']*5)
['nice', 'nice', 'nice', 'nice', 'nice']
>>> print([none]*5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'none' is not defined
>>> print([None]*5)
[None, None, None, None, None]
>>> str=['hello','a','babi']
#检查某个值是否属于某个序列
>>> print('hello' in str)
True
>>> print('helo' in str)
False
>>> name=['song','cheng','zhou']
>>> print('song' in name)
True
>>> print('cheng' in name)
True
>>> print('zhang' in name)
False
>>> print('cheng ' in name)
False
#序列的长度,最大值,最小值
>>> values=[11,56,78,2,36,-1,100]
>>> print(len(values))
7
>>> print(min(values))
-1
>>> print(max(values))
100
>>> print(max(1,2,3,4,5))
5
>>> print(min(1,2,3,4,5))
1
>>> print(min('a',12,36))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'int' and 'str'
>>> list=['abc',122,89898]
>>> print(max(list))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'int' and 'str'

#xiu修改列表
>>> s=['song','zhang','wang']
>>> s[0]='20'
>>> s[1]=['哈哈哈哈']
>>> s
['20', ['哈哈哈哈'], 'wang']
>>> s=['song','zhang','wang']
>>> s[-1]='100'
>>> s
['song', 'zhang', '100']
>>> s[-3]='灭霸'
>>> s
['灭霸', 'zhang', '100']
>>> s[3]='445'#超出列表的长度
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> s[-4]='大黄蜂'#超出列表范围
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
#删除列表
>>> a=[0,1,2,3,4,5,6,7]
>>> del a[3]
>>> a
[0, 1, 2, 4, 5, 6, 7]
>>> a=[0,1,2,3,4,5,6,7]
>>> del a[3]
>>> a
[0, 1, 2, 4, 5, 6, 7]

name=list('nice')
name[1:]=list('ike')
print(name)

#列表方法
#append\clear\copy\count\extend\index\insert\pop\remove\reverse\sort\
>>> number=[1,2,3,4]
>>> number.append(5)#append 末尾添加值
>>> number
[1, 2, 3, 4, 5]
>>> number.append([6,7])
>>> number
[1, 2, 3, 4, 5, [6, 7]]
#clear清除列表
>>> name=['song','wang','li']
>>> name
['song', 'wang', 'li']
>>> name.clear()
>>> name
[]
#copy 复制列表
>>> aa=[1,2,3]
>>> bb=aa.copy()
>>> bb,aa
([1, 2, 3], [1, 2, 3])
>>> b[1]=30
>>> bb[1]=30
>>> b
[1, 30, 3]
#count 统计
>>> x=[1,1,1,1,1,1,1,1,'a,','a','a','a','a','a','a']
>>> x.count(1)
8
>>> x.count('a')
6
#extend 在列表后插入新的列表
>>> a=[1,2,3]
>>> b=[4,5,6]
>>> a.extend(b),a
(None, [1, 2, 3, 4, 5, 6])
#index 查询某个值第一次出现的位置
>>> s=['i','like','python','python']
>>> s.index('python')
2
#insert将值插入指定的位置
>>> number=[1,2,3,4,5]
>>> number.insert(1,0)
>>> number
[1, 0, 2, 3, 4, 5]
>>> number.insert(0,0)
>>> number
[0, 1, 0, 2, 3, 4, 5]
#pop移除列表中的元素,默认是最后一个
>>> number=[1,2,3,4,5]
>>> number.insert(1,0)
>>> number
[1, 0, 2, 3, 4, 5]
>>> number.insert(0,0)
>>> number
[0, 1, 0, 2, 3, 4, 5]
#remove 删除列表中某个值第一次匹配项
>>> name=['song','song','zu','ying']
>>> name.remove('song')
>>> name
['song', 'zu', 'ying']
>>> name.remove('hehe')#不存在,抛出异常
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
#reverse 序列倒序排放
>>> number=[1,2,3,4,5,6]
>>> number.reverse()
>>> number
[6, 5, 4, 3, 2, 1]
>>> name=['song','zu','ying']
>>> name.reverse()
>>> name
['ying', 'zu', 'song']
#sort 列表中的元素按顺序排列,默认
>>> number
[6, 5, 4, 3, 2, 1]
>>> number.sort()
>>> number
[1, 2, 3, 4, 5, 6]
>>> names=[1,2,3,'song','zu','ying']
>>> names
[1, 2, 3, 'song', 'zu', 'ying']
>>> names.reverse()
>>> names
['ying', 'zu', 'song', 3, 2, 1]#倒序可以序列里面的值不一样
>>> names.sort()
Traceback (most recent call last):#顺序字符型和数值型不能直接比较,抛出异常
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'int' and 'str'
#使用副本可以不改变原有序列的顺序
>>> x=[22,32,2,14,66,78]
>>> y=x[:]
>>> x,y
([22, 32, 2, 14, 66, 78], [22, 32, 2, 14, 66, 78])
>>> y.sort()
>>> x,y
([22, 32, 2, 14, 66, 78], [2, 14, 22, 32, 66, 78])
>>> y=sorted(x)#sorted 可以直接创建副本
>>> y
[2, 14, 22, 32, 66, 78]
>>> x=['e','d','c','b','g']
>>> x
['e', 'd', 'c', 'b', 'g']
>>> sorted(x)#
['b', 'c', 'd', 'e', 'g']
#利用sorted进行倒序排列
>>> number=[-1,0,1,2,3,4,5,6,7]
>>> number.sort(reverse=True)
>>> number
[7, 6, 5, 4, 3, 2, 1, 0, -1]
#元组不可修改,只读
>>> number=1,2,3,4,5,6
>>> number
(1, 2, 3, 4, 5, 6)
>>> number=()
>>> number
()
>>> numbers=[1,2,3]#序列转换成元组
>>> value=tuple(numbers)
>>> value
(1, 2, 3)

>>> 5*(12+1,)#带逗号是元组
(13, 13, 13, 13, 13)
>>> 5*(12+1)#数值
65
#输入若干参数保存在序列,end退出,结果按降序排列
null=[]
while True:
    x=input('输入一个整数,end退出')
    y=x
    if x=='end':
        break
    else:
        null.append(y)
null.sort(reverse=True)
print(null)
#切片
number1=[15,14,13,12,11]
number2=[6,7,8,9,10]
number2.extend(number1[1:3])
number2.sort()
print(number2)
#往一个空序列添加数值,求最大值
null=[]
n=0
while True:
    n+=1
    x=int(input('输入一个整数:'))
    if n > 8:
        break
    else:
        null.append(x)
print(null,'最大值是:',max(null))
#输入一个整数n,产生一个二维列表为n*n
number=input('请输入一个大于一的整数')
n=int(number)
i=1
m=n*n
numbers=[]
values=[]
while i<=m:
    values.append(i)
    if i % n==0:
        numbers.append(values.copy())
        values.clear()
    i+=1
for num in numbers:
    print(num)


i=0
j=1
while i<n:
    while j<n:
        numbers[i][j],numbers[j][i]=\
        numbers[j][i],numbers[i][j]
        j+=1
    i+=1
for number in numbers:
    print(number)


#字符串
>>> s='hello world'
>>> s[0]
'h'
>>> s[1]
'e'
>>> s[6:8]
'wo'
>>> s[6:]
'world'
>>> s[::2]
'hlowrd'
>>> s1='cba'
>>> 5*s1
'cbacbacbacbacba'
>>> s1*10
'cbacbacbacbacbacbacbacbacbacba'
#字符串格式化
>>> formatStr='hello %s.today is %s,are any there today'
>>> values=('mike','wednedsday')
>>> print(formatStr % values)
hello mike.today is wednedsday,are any there today
hello mike.today is wednedsday,are any there today
>>> formatStr1='pi是圆周率,它的值是%.4f(保留小数点后%d位)'
>>> from math import pi
>>> values1=(pi,4)
>>> print(formatStr1 % values1)
pi是圆周率,它的值是3.1416(保留小数点后4位)
>>> formatStr2='这件事的成功率为%d%%,如果有%s的参加,成功率会提升至%d%%'
>>> values2=(50,'宋祖英',70)
>>> print(formatStr2 % values2)
这件事的成功率为50%,如果有宋祖英的参加,成功率会提升至70%

from string import Template
templatel = Template('$s是我最喜欢的语言,$s非常强大且容易学习')
print(templatel.substitute(s='python'))

from string import Template
template2=Template('${s}stitute')
print(template2.substitute(s='sub'))

from string import Template
temalate3=Template('$x相当于多少$y')
print(temalate3.substitute(x=20,y='英镑'))

temalate4=Template('$a$$相当于多少$b')
date={}
date['a']=100
date['b']='英镑'
print(temalate4.substitute(date))
'''
#字符串format格式化方法
>>> print('{} {} {} '.format(1,2,3))
1 2 3
>>> print('{a}{b}{c}'.format(a=1,c=2,b=3))
132
>>> s1='today is {},是这个月的第{}天'
>>> print(s1.format('星期五',24))
today is 星期五,是这个月的第24天
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值