python基础应用

重点保留三位小数

#圆柱体的表面积
#输入底面半径R和高H,输入圆柱体的表面积,保留3位小数
#3.5 9
#area = 274.889

r = 3.5
h = 9
pi = 3.14
x = pi * r * r 
y = 2 * pi * r * h
z = x * 2 + y
print("z = %0.1f" % z )
print("z = %0.2f" % z )
print("z = %0.3f" % z )

 

 

反转

#不使用if
#输入三个数,分离出它的百位,十位和个位,反转后输出
#123
#321
a = 123
b = a / 100 
s = a /10 % 10
g = a % 10
print(a)
print('%d' % g,"%d" % s, '%d' % b)

#切片函数
test = "123"
test1 = test[::-1]
print("输出为:",test1)

输出形如aabb的4位完全平方数(7744问题)

for x in range(1, 1000):
    n = int (x * x) 
    hi = int(n / 100)
    lo = int(n % 100)
    if hi / 10 ==hi % 10 and lo / 10 == lo % 10:
        print("%d" % n)

3n+1求次数

n =3
count = 0
while n > 1:
    if n % 2 == 1:
        n = n * 3 + 1
    else:
        n /= 2
    count += 1
print(count)

 

 

 

你可能感兴趣的:(python)