Python学习笔记-数字,列表,元祖,切片,循环

数字

1,加减乘除:+,-,*,/

2,平方:**

3,立方:**3

4,字符串转换:str(数字)

5,浮点数:带小数点  0.2

Python编程建议

import this

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
>>> 

列表:内部数据可以修改

1,实例化:list=【1,2,3】

2,排序:

列表数据永久修改型: 字母顺序:sort(),字母反顺序:sort(reverse=True)

列表数据临时修改型:字母顺序:sorted(列表),字母反顺序:sorted(列表,reverse=True)

反转:列表.reverse()

3,末尾添加:append(元素)

4,插入:insert(索引,元素)

5,删除:remove(元素),del 列表[索引],删除且返回末尾元素 pop(),删除返回指定位置元素pop(索引)

6,长度:len(列表)

7,访问方法:list[0],最后一个元素list[-1],list[1]='bbb'

>>> names=[]
>>> 
>>> print(names)
[]
>>> 
>>> names.append('li xiaobin')
>>> print(names)
['li xiaobin']
>>> 
>>> names.insert(0,'liu jing')
>>> print(names)
['liu jing', 'li xiaobin']
>>> 
>>> names.insert(0,'wang jing')
>>> print(names)
['wang jing', 'liu jing', 'li xiaobin']
>>> 
>>> del names[1]
>>> print(names)
['wang jing', 'li xiaobin']
>>> 
>>> last= names.pop()
>>> print(names)
['wang jing']
>>> print(last)
li xiaobin
>>> 
>>> names.append('tian')
>>> print(last)
li xiaobin
>>> print(names)
['wang jing', 'tian']
>>> last=names.pop(0)
>>> print(names)
['tian']
>>> 
>>> print(last)
wang jing
>>> 
>>> names.remove('tian')
>>> print(names)
[]
>>> names.insert(0,'li')
>>> 
>>> names.append('wang')
>>> names.append('liu')
>>> names.append('zhang')
>>> print(names)
['li', 'wang', 'liu', 'zhang']
>>> names.sort()
>>> print(names)
['li', 'liu', 'wang', 'zhang']
>>> names.sort(reverse=true)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'true' is not defined
>>> names.sort(reserve=true)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'true' is not defined
>>> names.sort(reverse=True)
>>> print(names)
['zhang', 'wang', 'liu', 'li']
>>> names.insert(2,'bao')
>>> print(names)
['zhang', 'wang', 'bao', 'liu', 'li']
>>> print(sorted(names))
['bao', 'li', 'liu', 'wang', 'zhang']
>>> print(names)
['zhang', 'wang', 'bao', 'liu', 'li']
>>> print(sorted(names,reverse=True))
['zhang', 'wang', 'liu', 'li', 'bao']
>>> print(names)
['zhang', 'wang', 'bao', 'liu', 'li']
>>> reverse(names)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'reverse' is not defined
>>> names.reverse()
>>> print(names)
['li', 'liu', 'bao', 'wang', 'zhang']
>>> names.reverse()
>>> print(names)
['zhang', 'wang', 'bao', 'liu', 'li']
>>> len(names)
5
>>> 

 元祖:内部数据不可以修改,变量可以重新赋值

1,实例化:cnt=(1,200,300)

2,排序,长度与列表一致

切片:读取列表部分段

1,

猜你喜欢

转载自www.cnblogs.com/lixiaobin/p/pythonstudy2.html