首页 > 技术文章 > python基本的知识

wangshilin 2019-07-30 20:18 原文

今日内容整理如下:

1.当引号给予变量的时候,及变成字符串,不同的变量用逗号分开。

2.Sep=‘,’更改多个元素之间的连接符,默认是换行,End更改结尾,默认是换行。

3.格式化输出:print(‘{} 真帅’.format(‘张器呢’))

4.str():将指定的对象转换成字符串形式,可以指定编码。

 

chr():将整数转换成该编码对应的字符串(一个字符)。

 

 

ord():将字符串(一个字符)转换成对应的编码(整数)。

 

注意:

PEP 8要求:

  • 用小写字母拼写,多个单词用下划线连接。

  • 受保护的实例属性用单个下划线开头(后面会讲到)私有的实例属性用两个下划线开头(后面会讲到)

附练习与作业:

1.摄氏度转华氏度 :

c=int(input('Enter a degree in Celsius:'))
f=(9/5)*c+32
print('%d Celsius is %.1f Fahrenheit' % (c,f))
 运行结果:
Enter a degree in Celsius:43
43 Celsius is 109.4 Fahrenheit
 
 2.求圆柱的体积与底面积
import math
r,h= map(float, input('Enter the radius and length of a cylinder :').split(','))
area=r*r*math.pi
volume=area*h
print('The area is %.4f'% area)
print('The volume is %.1f' % volume)
 运行结果:
Enter the radius and length of a cylinder :5.5,12
The area is 95.0332
The volume is 1140.4
 3.将英尺转化为米数
y=float(input('Enter a value feet:'))
m=y*0.305
print('%.1f feet is %.4f metters'% (y,m))
 运行结果:
Enter a value feet:16.5
16.5 feet is 5.0325 metters
 
 
4.计算能量:
z=float(input('Enter the amount of water in kilograns :'))
t=float(input('Enter the initial temperature :'))
f=float(input('Enter the fianl temperature :'))
Q=z*(f-t)*4184
print('The energy needed is %.1f' % Q)
 运行结果:
Enter the amount of water in kilograns :55.5
Enter the initial temperature :3.5
Enter the fianl temperature :10.5
The energy needed is 1625484.0
5.计算利息
 
r,h= map(float, input('Enter blance and interest rate (e.g.,3 for 3%) :').split(','))
l=r*(h/1200)
print('The interest is %.5f' % l)
 运行结果:
Enter blance and interest rate (e.g.,3 for 3%) :1000,3.5
The interest is 2.91667
 
 6.加速度
V0,V1,t= map(float, input('Enter the radius and length of a cylinder :').split(','))
a=(V1-V0)/t
print('The average acceleration is %.4f' % a) 
 
 运行结果:
Enter the radius and length of a cylinder :5.5,50.9,4.5
The average acceleration is 10.0889
 
 7.复利值
m=float(input('Enter the monthly saving amount:'))
y=m*(1+0.00417)
e=(m+y)*(1+0.00417)
s=(m+e)*(1+0.00417)
d=(m+s)*(1+0.00417)
w=(m+d)*(1+0.00417)
l=(m+w)*(1+0.00417)
print('After the sixth month, the accout value is %.2f' % l)
 运算结果:
Enter the monthly saving amount:100
After the sixth month, the accout value is 608.82
 
 
 8.对一个整数的每个数位求和
a=int(input('Enter a number between 0 and 1000:'))
low = a % 10
mid = a // 10 % 10
high = a // 100
he = low  + mid + high 
print('The sum of the digits is %d ' % he)
运算结果:
Enter a number between 0 and 1000:999
The sum of the digits is 27 

 

推荐阅读