You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

numbers = []
numbers.append(x)
print("{:.2f}".format(dayup))
print('*',end='')
print('月份简写是',x,'.',sep='')
print('\n***********************\n')
factorial()求阶乘
求绝对值fabs(x)
sqrt(x)
from random import *
print(i,'\t''\t',a,sep='')
iswpunct() 函数用来检测一个宽字符是否是标点符号。
判断是否数字
print(str_1.isdigit())
判断是否字母
print(str_1.isalpha())
判断是否为数字和字母的组合
print(str_1.isalnum())
判断是否为标点符号
import string
punc = string.punctuation
if str_1 in punc:
print('is punctuation')
print('第{:>2}年折旧{:.2f}'.format(i,s)) #右对齐填充到宽度为2
print('') 换行没有间隔
print('\n') 换行有间隔
print('') #中间输出一个空行
Python提供了strip()方法,可以去除字符串两侧(不包含内部)全部的空格;
print(source_string.strip().title()) //kuohao
print(source_string.replace('day','time'))
print('。'*x,sep='',end='')
列表相关
Python 中可以通过组合一些值得到多种 复合 数据类型。其中最常用的 列表 ,可以通过方括号括起、逗号分隔的一组值得到。一个 列表 可以包含不同类型的元素,但通常使用时各个元素类型相同。
字符串(以及各种内置的 sequence 类型)一样,列表也支持索引和切片:
>>> squares = [1, 4, 9, 16, 25]
>>> squares[0] # indexing returns the item
1
>>> squares[-1]
25
>>> squares[-3:] # slicing returns a new list
[9, 16, 25]
>>> squares[:]
[1, 4, 9, 16, 25]
列表同样支持拼接操作:
>>> squares + [36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
你也可以在列表结尾,通过 append() 方法 添加新元素:
>>> squares.append(200)
>>> squares
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 200]
内置函数 len() 也可以作用到列表上:
>>> letters = ['a', 'b', 'c', 'd']
>>> len(letters)
4
也可以嵌套列表 (创建包含其他列表的列表), 比如说:
>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'
列表数据类型还有很多的方法。这里是列表对象方法的清单:
list.append(x)
在列表的末尾添加一个元素。相当于 a[len(a):] = [x] 。
list.extend(iterable)
使用可迭代对象中的所有元素来扩展列表。相当于 a[len(a):] = iterable 。
list.insert(i, x)
在给定的位置插入一个元素。第一个参数是要插入的元素的索引,所以 a.insert(0, x) 插入列表头部, a.insert(len(a), x) 等同于 a.append(x) 。
list.remove(x)
移除列表中第一个值为 x 的元素。如果没有这样的元素,则抛出 ValueError 异常。
list.pop([i])
删除列表中给定位置的元素并返回它。如果没有给定位置a.pop() 将会删除并返回列表中的最后一个元素。( 方法签名中 i 两边的方括号表示这个参数是可选的,而不是要你输入方括号。你会在 Python 参考库中经常看到这种表示方法)。
list.clear()
删除列表中所有的元素。相当于 del a[:] 。
list.index(x[, start[, end]])
返回列表中第一个值为 x 的元素的从零开始的索引。如果没有这样的元素将会抛出 ValueError 异常。
可选参数 start 和 end 是切片符号,用于将搜索限制为列表的特定子序列。返回的索引是相对于整个序列的开始计算的,而不是 start 参数。
list.count(x)
返回元素 x 在列表中出现的次数。
list.sort(key=None, reverse=False)
对列表中的元素进行排序(参数可用于自定义排序,解释请参见 sorted())。
list.reverse()
反转列表中的元素。