【22】Python基礎學習筆記1

來源:互聯網
上載者:User

標籤:als   比較   資訊   教科書   個人資訊   seve   你知道   學習筆記   keyword   

練習題:
1.簡述編譯型與解釋型語言的區別,且分別列出你知道的哪些語言屬於編譯型,哪些屬於解釋型
編譯型語言:就好比一本教科書,不管用到哪一個知識點,編譯型語言首先做的就是先將整本書所涉及的知識點都解釋一遍。 比如輸入一串命令,先標準輸入,然後根據編譯環境去找到翻譯內容,傳給電腦,最後cpu處理
解釋型語言:你需要哪一段,我給你解釋哪一段,不用整本書翻譯一遍,比較靈活,但是效率慢。標準輸入後,需要先pyc緩衝一個,然後進入虛擬機器,在轉換成機器識別語言,最後cpu執行
2.執行 Python 指令碼的兩種方式是什麼
python hello.py
#!/usr/bin/env python :不確定解譯器具體路徑
#!/usr/bin/python :在知道解譯器安裝的路徑時可以使用該方式。
3.Pyhton 單行注釋和多行注釋分別用什麼?
單行注釋:井號
多行注釋:多引號
4.布爾值分別有什麼?
True&False
5.聲明變數注意事項有那些?
keyword.kwlist
變數名不能有空格,可以用底線串連
最好首字母大寫
常量就變數名都大寫
不可以數字開頭

6.如何查看變數在記憶體中的地址?(百度找到的,是懂非懂,自己搞個變數看該變數記憶體位址,結果我就不貼了)

name="Alex"print(id(name).__doc__)

7.寫代碼i.實現使用者輸入使用者名稱和密碼,當使用者名稱為 seven 且 密碼為 123 時,顯示登陸成功,否則登陸失敗!

u=["zhang",123]username=input("Input name:")password=int(input("Input password:"))if username == u[0] and password == u[1]:    print("login success!")else:    print("false")
u=["zhang","123"]def login(x,y):    if x == u[0] and y==u[1]:        print("success")    else:        print("false")login(x=input("user:"),y=input("pass"))

ii.實現使用者輸入使用者名稱和密碼,當使用者名稱為 seven 且 密碼為 123 時,顯示登陸成功,否則登陸失敗,失敗時允許重複輸入三次

u=["zh","123"]count=0while count <3:    user_name=input("input username>>> ")    pass_word=input("input password>>> ")    if user_name==u[0] and pass_word ==u[1]:        print("success!")        break    else:        print("false")    count +=1

iii.實現使用者輸入使用者名稱和密碼,當使用者名稱為 seven 或 alex 且 密碼為 123 時,顯示登陸成功,否則登陸失敗,失敗時允許重複輸入三次

count=0user_info="zh:123#ch:123"user_list=user_info.split("#")user_dict={}for i in user_list:    item=i.split(":")    user_dict[item[0]]=item[-1]username=input("input user>>> ")if username in user_dict:    while count < 3:        password = input("input pass>>> ")        if password == user_dict[username]:            print("sueccess")            exit()        else:            print("密碼錯誤")        count += 1else:    print("user error")

8.寫代碼
a. 使用while迴圈實現輸出2-3+4-5+6...+100 的和
我的想法是:用偶數相加和減去奇數相加和

count=2l=[]a=[]while count <=100:    if count%2 ==0:        a.append(count)    else:        l.append(count)    count +=1print(sum(l))print(sum(a))print(sum(a)-sum(l))

b. 使用 while 迴圈實現輸出 1,2,3,4,5, 7,8,9, 11,12 d.

count =1l=[]while count<=12:    if count !=6 and count !=10:        l.append(count)    count +=1print(l)

d.使用 while 迴圈實現輸出 1-100 內的所有奇數

count=0l=[]while count <100:    if count%2 !=0:        l.append(count)    count +=1print(l)

e. 使用 while 迴圈實現輸出 1-100 內的所有偶數

count=0l=[]while count<=100:    if count%2==0:        l.append(count)        print(sum(l))    count +=1print(l)

9.現有如下兩個變數,請簡述 n1 和 n2 是什麼關係?
n1 = 123456
n2 = n1

答:n2是n1的因變數,隨著n1改變而改變

10.製作趣味模板程式(編程題)
需求:等待使用者輸入名字、地點、愛好,根據使用者的名字和愛好進行任意顯示
如:敬愛可愛的xxx,最喜歡在xxx地方幹xxx

def mode():    name=input("what‘s your name>>>")    address=input("home add>>>")    job=input("input job>>>")    print(‘‘‘    ------------%s的個人資訊-----------    name    :%s    address :%s    job     :%s    ‘‘‘%(name,name,address,job))if __name__==‘__main__‘:    mode()

11.輸入一年份,判斷該年份是否是閏年並輸出結果。(編程題)
註:凡符合下面兩個條件之一的年份是閏年。 (1) 能被4整除但不能被100整除。 (2) 能被400整除。

year=int(input("input year>>> "))if year%4 ==0:    print("%s年是閏年"%year)else:    print("不是閏年")def year(n):    if n%4==0:        print("閏年")    else:        print("No")if __name__ == "__main__":    year(n=int(input("input year>>>")))

12.假設一年期定期利率為3.25%,計算一下需要過多少年,一萬元的一年定期存款連本帶息能翻番?(編程題)
假定一年計算一次,結果是22年連本帶利翻一倍,如果固定一萬元不變,需要三十年翻倍。

count=0n=0.0325l=[]while True:    if count >1:        print(count)        print(len(l))        break    else:        count += n*(count+1)        l.append(count)

【22】Python基礎學習筆記1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.