python程序设计:基础1习题
2014-03-22 15:38
441 查看
1.输入平面上两个点,计算两点的距离
import math
x1,y1=input('please the start point x1,y1:')
x2,y2=input('please the start point x2,y2:')
distance=math.sqrt((x1-x2)**2+(y1-y2)**2)
print'distance=',distance
please the start point x1,y1:0,0
please the start point x2,y2:3,4
distance= 5.0
2.任意输入3个单词,将他们按字典顺序排列
string=raw_input('please input 3words with "," in them:')
x,y,z=string.split(',')
if x>y:
x,y=y,x
if x>z:
x,z=z,x
if y>z:
y,z=z,y
print x,y,z
please input 3words with "," in them:iker,peng,xiao
iker peng xiao
3. 解二元一次方程组。输入他们的系数,输出结果。(我们使用了numpy 的库)
import numpy as np
a=np.zeros((2,3))
a[0][0],a[0][1],a[0][2]=input('please input 3 numbers for the first function:')
a[1][0],a[1][1],a[1][2]=input('please input 3 numbers for the second function:')
if a[0][0]*a[1][1]==0:
print "are you kidding me?"
else:
a[1]=a[0]*(-a[1][0]/a[0][0])+a[1]
a[0]=a[1]*(-a[0][1]/a[1][1])+a[0]
print 'the answer is:x1=',a[0][2]/a[0][0],'x2=',a[1][2]/a[1][1]
please input 3 numbers for the first function:1,2,3
please input 3 numbers for the second function:4,9,7
the answer is:x1= 13.0 x2= -5.0
please input 3 numbers for the first function:0,1,2
please input 3 numbers for the second function:1,2,3
are you kidding me?
4,矩阵按其形状输出
a=input('please input a 3*3 array:')
for x in a:
s=''
for y in x:
s1='%6d'%y
s=s+s1
print s
please input a 3*3 array:[[1,2,1],[2,3,4],[4,5,0]]
1 2 1
2 3 4
4 5 0
import math
x1,y1=input('please the start point x1,y1:')
x2,y2=input('please the start point x2,y2:')
distance=math.sqrt((x1-x2)**2+(y1-y2)**2)
print'distance=',distance
please the start point x1,y1:0,0
please the start point x2,y2:3,4
distance= 5.0
2.任意输入3个单词,将他们按字典顺序排列
string=raw_input('please input 3words with "," in them:')
x,y,z=string.split(',')
if x>y:
x,y=y,x
if x>z:
x,z=z,x
if y>z:
y,z=z,y
print x,y,z
please input 3words with "," in them:iker,peng,xiao
iker peng xiao
3. 解二元一次方程组。输入他们的系数,输出结果。(我们使用了numpy 的库)
import numpy as np
a=np.zeros((2,3))
a[0][0],a[0][1],a[0][2]=input('please input 3 numbers for the first function:')
a[1][0],a[1][1],a[1][2]=input('please input 3 numbers for the second function:')
if a[0][0]*a[1][1]==0:
print "are you kidding me?"
else:
a[1]=a[0]*(-a[1][0]/a[0][0])+a[1]
a[0]=a[1]*(-a[0][1]/a[1][1])+a[0]
print 'the answer is:x1=',a[0][2]/a[0][0],'x2=',a[1][2]/a[1][1]
please input 3 numbers for the first function:1,2,3
please input 3 numbers for the second function:4,9,7
the answer is:x1= 13.0 x2= -5.0
please input 3 numbers for the first function:0,1,2
please input 3 numbers for the second function:1,2,3
are you kidding me?
4,矩阵按其形状输出
a=input('please input a 3*3 array:')
for x in a:
s=''
for y in x:
s1='%6d'%y
s=s+s1
print s
please input a 3*3 array:[[1,2,1],[2,3,4],[4,5,0]]
1 2 1
2 3 4
4 5 0
相关文章推荐
- Python程序设计基础 数据结构与算法习题
- Python程序设计基础 函数与模块习题
- python程序设计基础3:python选择和循环结构
- python基础 循环结构程序设计(下)
- JAVA语言程序设计基础课后习题第三章
- Python程序设计与算法基础教程·第四章上机实践
- python_基础习题(1)
- CDays-2 习题二 (编写类Class,并实现简单的栈)及相关内容解析。Python 基础教程 Class
- Python基础习题(一)
- 【鱼C·零基础入门学习Python】课后习题答案 第14课
- JAVA语言程序设计基础课后习题第四章
- Python基础习题(二)
- Python学习笔记:程序设计的基础知识
- python基础 循环结构程序设计
- python程序设计基础4:python函数设计和使用
- Python程序设计与算法基础教程·第三章上机实践
- python 笔记 基础知识回顾 《笨办法学Python》习题22 ——12.29
- Python基础(18)_面向对象程序设计2(反射、__str__、__del__、__item__系列)
- Python程序设计与算法基础教程·第八章上机实践
- 【鱼C·零基础入门学习python】第16课到第22课习题答案