python程式設計:基礎1習題__python

來源:互聯網
上載者:User

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
    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.