您的位置:首页 > 编程语言 > Python开发

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

    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python