a = [0, 1, 2, 3, 4, 5]
print("[0, 1, 2, 3, 4, 5] 测试结果为:")
print("a[-1]-> %s" %a[-1]) ###取最后一个元素
print("a[:-1]-> %s" %a[:-1]) ### 除了最后一个取全部
print("a[::-1]-> %s" %a[::-1]) ### 取从后向前(相反)的元素
print("a[2::-1]-> %s" %a[2::-1]) ### 取从下标为2的元素翻转读取
print("a[3::-1]-> %s" %a[3::-1]) ### 取从下标为3的元素翻转读取
输出结果================================
[0, 1, 2, 3, 4, 5] 测试结果为:
a[-1]-> 5
a[:-1]-> [0, 1, 2, 3, 4]
a[::-1]-> [5, 4, 3, 2, 1, 0]
a[2::-1]-> [2, 1, 0]
a[3::-1]-> [3, 2, 1, 0]
re = math.floor(32.9)
print("floor(32.9) is ")
print("floor(32.9) is %s" %re)
print("floor(32.9) is %d" %re)
输出结果=====================================
floor(32.9) is
floor(32.9) is 32
floor(32.9) is 32
print("hello")
print('"hello",he said')
print("\"Hello, world!\" she said")
print("hello "+ "world")
#"""这种情况无需转义字符"""
print(""""hello!",he said to her,and 'nice to meet you!',she replyed!""")
#原始字符串,转移字符串在这里面失效
print(r'C:\Program Files\python')
输出结果==========================================
hello
"hello",he said
"Hello, world!" she said
hello world
"hello!",he said to her,and 'nice to meet you!',she replyed!
C:\Program Files\python
greeting = 'hello'
print("0位置是字母\"%s\" -1位置是字母\"%s\"" %(greeting[0], greeting[-1]))
#切片----------------------------------------------------------------------------
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print("#numbers[0:1] is ")
print(numbers[0:1])
print("#numbers[0:2] is ")
print(numbers[0:2])
print("#numbers[-3:-1] is ")
print(numbers[-3:-1])
输出结果=============================================
0位置是字母"h" -1位置是字母"o"
#numbers[0:1] is
[1]
#numbers[0:2] is
[1, 2]
#numbers[-3:-1] is
[8, 9]
#序列相加---------------------------------------------------------------
print("#[1, 2, 3] + [4, 5, 6] is ")
print( [1, 2, 3] + [4, 5, 6])
#in 运算符
permissions = 'we are'
print("""#'w' in permissions""")
print('w' in permissions)
print('we' in permissions)
输出结果======================================
#[1, 2, 3] + [4, 5, 6] is
[1, 2, 3, 4, 5, 6]
#'w' in permissions
True
True
#切片插入----------------------------------------------------------------
numbers = [1,5]
numbers[1:1] = [2,3,4]
print("""#numbers 插入后是""")
print(numbers)
输出结果==============================
#numbers 插入后是
[1, 2, 3, 4, 5]
#参数--------------------------------------------------------------------------
format = "Hello, %s ,%s end"
values = ("ni",'hao')
print(format % values)
#模板
tmpl= Template("Hello, $chineseyou ,$chinesegood end")
print(tmpl.substitute(chineseyou = "ni",chinesegood = "hao"))
输出结果===========================================
Hello, ni ,hao end
Hello, ni ,hao end
#简单替换方式-----------------------------------------------------------------------
print( "{3} {0} {2} {1} {3} {0}".format("be", "not", "or", "to"))
print( "{name} is approximately {value:.2f}.".format(value=math.pi, name="π"))
输出结果==============================================
to be or not to be
π is approximately 3.14.
#字段替换顺序和名称 3 2 4 1----------------------------------------------------------
print( "{foo} {1} {bar} {0}".format(1, 2, bar=4, foo=3))
输出结果===============================================
3 2 4 1
#字符串替换,使用中的fullname----------------------------------------------------------
fullname = ["nash", "sundy"]
print("Mr {name[0]}".format(name=fullname))
print('One googol is {:,}'.format(10**100))
print( "Pi is {pi:.4f}".format(pi=math.pi))
输出结果===================================================
Mr nash
One googol is 10,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000
Pi is 3.1416
#符号填充-----------------------------------------------------------------------------------------
#所有字符一共12位 000000003.14
print( '{:012.2f}'.format(math.pi))
#多余空位的对齐方式:左\居中\右
print('{0:<10.2f}\n{0:^10.2f}\n{0:>10.2f}'.format(math.pi))
输出结果===================================================
000000003.14
3.14
3.14
3.14