|
|
|
|
@ -0,0 +1,142 @@
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
Created on Mon Oct 25 22:08:21 2021
|
|
|
|
|
|
|
|
|
|
@author: Administrator
|
|
|
|
|
"""
|
|
|
|
|
import numpy as np
|
|
|
|
|
#科学计数法
|
|
|
|
|
print(4.3e-3)#表示4.3*10**-3次方
|
|
|
|
|
#不想采用科学计数法
|
|
|
|
|
np.set_printoptions(suppress=True)
|
|
|
|
|
#format()函数
|
|
|
|
|
name="xiaoming"#字符串的格式化,会产生新的字符串
|
|
|
|
|
age=18
|
|
|
|
|
favourite=666
|
|
|
|
|
print(f"name is:{name},aged{age}")
|
|
|
|
|
#^居中填充,<左对齐填充,>右对齐填充
|
|
|
|
|
print(f"name is:{name},I like{favourite:*<8}")#:后面表示填充的字符,不指定默认空格+对齐方式+用填充几位
|
|
|
|
|
#数字格式化
|
|
|
|
|
#{:.2f}保留2位小数
|
|
|
|
|
#{:.2e}以科学计数法形式输出
|
|
|
|
|
#{:.2%}以百分号形式输出
|
|
|
|
|
print("{:0<10d}".format(3))#{:0<10d}用0填充10位,左对齐
|
|
|
|
|
a,b=1.234567,0.00321
|
|
|
|
|
print("{0:.3f}".format(a))#这样可以同时输出多个,0,1,2....表示参数序号
|
|
|
|
|
print("a="+format(a,".3f")+",b="+format(b,".2e"))#format()为str类型
|
|
|
|
|
print(type(format(a,".3f")))#str类型
|
|
|
|
|
|
|
|
|
|
#变量名不可以数字起头
|
|
|
|
|
#只包含字母,数字和下划线
|
|
|
|
|
|
|
|
|
|
a,b=5,2
|
|
|
|
|
a,b=b,a#在这里是同时执行的,a=b=2,此时b=a仍应该将a视为原来的5,故b=2
|
|
|
|
|
print(a,b)#输出2,5
|
|
|
|
|
a,b=5,2
|
|
|
|
|
a=b
|
|
|
|
|
b=a
|
|
|
|
|
print(a,b)#输出2,2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print(0.53+0.3)#输出结果不是0.83哦,试试吧
|
|
|
|
|
print(format(0.53+0.3,".2f"))
|
|
|
|
|
from decimal import *
|
|
|
|
|
print(Decimal("0.53")+Decimal("0.3"))
|
|
|
|
|
|
|
|
|
|
msg="www.BaiDu.com"
|
|
|
|
|
print(msg.upper())#全大写
|
|
|
|
|
print(msg.lower())#全小写
|
|
|
|
|
print(msg.capitalize())#首字母大写,其他小写
|
|
|
|
|
print(msg.title())#每个单词首字母大写,其他小写
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
x=input().split(",")#python列表中字符串转数字
|
|
|
|
|
print(x)
|
|
|
|
|
lst1=list(map(eval,x))#返回map对象,在转换成列表
|
|
|
|
|
print(lst1)
|
|
|
|
|
|
|
|
|
|
#按指定符号分隔一个列表
|
|
|
|
|
lst1=[1,2,3,4,5,6]
|
|
|
|
|
str1="*".join(str(n) for n in lst1)#"sep(自己指定的分隔符)".join(字符串元素形成的列表,字典,元组等),转完之后是字符串
|
|
|
|
|
print(str1)
|
|
|
|
|
|
|
|
|
|
print(str1="sss")#不能这么用,要分开写
|
|
|
|
|
str1="sss"
|
|
|
|
|
print(str1)
|
|
|
|
|
|
|
|
|
|
#decimal库
|
|
|
|
|
n1=1.1
|
|
|
|
|
n2=2.2
|
|
|
|
|
from decimal import *#Decimal(“”)用于保持
|
|
|
|
|
print(Decimal(n1)+Decimal(n2))
|
|
|
|
|
print(Decimal("1.1")+Decimal("2.2"))
|
|
|
|
|
|
|
|
|
|
msg="www.BaiDu.com"
|
|
|
|
|
print(msg.upper())#全大写
|
|
|
|
|
print(msg.lower())#全小写
|
|
|
|
|
print(msg.capitalize())#首字母大写,其他小写
|
|
|
|
|
print(msg.title())#每个单词首字母大写,其他小写
|
|
|
|
|
|
|
|
|
|
#对字符串进行sorted方法
|
|
|
|
|
str1="hello world"
|
|
|
|
|
print(sorted(str1))#“ ”>字母表排序
|
|
|
|
|
print(type(sorted(str1)))#list类型
|
|
|
|
|
|
|
|
|
|
from random import *#调用随机数模块
|
|
|
|
|
computer=randint(1, 3)
|
|
|
|
|
player=eval(input("1==剪刀,2==石头,3==布"))
|
|
|
|
|
print(computer)
|
|
|
|
|
if player==computer:
|
|
|
|
|
print("平局")
|
|
|
|
|
elif (player==1 and computer==2) or (player==2 and computer==3) or (player==3 and computer==1):
|
|
|
|
|
print("玩家失败")
|
|
|
|
|
else:
|
|
|
|
|
print("玩家胜利")
|
|
|
|
|
|
|
|
|
|
def isprime(a):#判断是否为素数
|
|
|
|
|
global c#指明为全局变量
|
|
|
|
|
if a==2:
|
|
|
|
|
c=1
|
|
|
|
|
for b in range(2,a):
|
|
|
|
|
if a%b==0:#非素数
|
|
|
|
|
c=0
|
|
|
|
|
break
|
|
|
|
|
else:#素数
|
|
|
|
|
c=1
|
|
|
|
|
return c
|
|
|
|
|
|
|
|
|
|
def gcp(a,b):#辗转相除求最大公约数
|
|
|
|
|
while b%a!=0:
|
|
|
|
|
a,b=b%a,a
|
|
|
|
|
return a
|
|
|
|
|
a,b=eval(input())
|
|
|
|
|
c=gcp(a,b)
|
|
|
|
|
d=c*(a/c)*(b/c)
|
|
|
|
|
print("最大公约数:%d"% c)
|
|
|
|
|
print("最小公倍数:%d"% d)
|
|
|
|
|
|
|
|
|
|
x=4
|
|
|
|
|
if 2:#True
|
|
|
|
|
print(5)
|
|
|
|
|
else:
|
|
|
|
|
print(6)
|
|
|
|
|
|
|
|
|
|
#lst.pop([i])
|
|
|
|
|
#方法签名中i两边的方括号表示这个参数是可选的,而不是要你输入方括号。你会在 Python 参考库中经常看到这种表示方法
|
|
|
|
|
|
|
|
|
|
nameList=eval(input())#可以直接转换成列表
|
|
|
|
|
print(nameList)
|
|
|
|
|
print(type(nameList))
|
|
|
|
|
|
|
|
|
|
lst1=[1,2,3,4,3,5,7,8]
|
|
|
|
|
x=len(lst1)#x=48
|
|
|
|
|
print(x/2)#4.0浮点数
|
|
|
|
|
print(x//2)#4整数,int才可以作为列表的索引
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|