python零基礎學習-基礎知識2-代碼初見

來源:互聯網
上載者:User

標籤:pyc   number   pass   welcome   break   har   知識   無限迴圈   continue   

注釋及引號的使用

#我是一行注釋‘‘‘那麼巧, 我也是一行注釋‘‘‘print(‘‘‘列印多行字串-第一行列印多行字串-第二行‘‘‘)print("我在嘗試引號嵌套‘我在嘗試引號嵌套")print(‘那麼巧, 我也在嘗試引號嵌套"我也在嘗試引號嵌套‘)

要求使用者輸入字元, 及字串拼接

注意: 使用者輸入的都為字串, 如需當做數字使用需要進行轉換: int(age)

#要求使用者輸入name=input("what‘s your name?")job=input("what‘s your job?")#列印輸出內容print(name,job)#字串拼接方法1, 盡量不要使用, 要佔多塊記憶體print(‘‘‘---info of ‘‘‘+name+‘‘‘---name:‘‘‘+name+‘‘‘job:‘‘‘+job)#字串拼接方法2, %s代表stringprint(‘‘‘---info of %s---name:%sjob:%s‘‘‘%(name, name, job))#字串拼接方法3print(‘‘‘---info of {_name}---name:{_name}job:{_job}‘‘‘.format(_name=name,_job=job))#字串拼接方法4print(‘‘‘---info of {0}---name:{0}job:{1}‘‘‘.format(name,job))

類比linux登入, 密碼密文展示

注意: 該寫法在pycharm中不不好用

import getpass #引入標準庫password=getpass.getpass("password:")

 if...else流程判斷

注意: 由於沒有{ }等結束符, 縮排必須正確

if _username==username and _password==password:    print("Welcome {name}".format(name=username))else:    print("Invalid username or password")

while迴圈

count=0num=15while count<3:    guess=int(input("please a number:"))    if guess==num:        print("correct!")        break    elif guess>num:        print("Bigger!")        count+=1        continue    else:        print("Smaller!")        count+=1        continueelse:    print("Guess incorrectly for 3 times!")

無限迴圈

while True:    name=input("name:")

for迴圈

for i in range(10):    name=input("name:")    if name=="bell":        breakelse:    print("Not correct")

 

python零基礎學習-基礎知識2-代碼初見

聯繫我們

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