1. 程式人生 > >python零基礎學習-基礎知識2-代碼初見

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

name clas nco and tin put 輸出 strong break

註釋及引號的使用

#我是一行註釋
‘‘‘
那麽巧, 我也是一行註釋
‘‘‘
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代表string print(‘‘‘---info of %s--- name:%s job:%s‘‘‘%(name, name, job)) #字符串拼接方法3 print(‘‘‘---info of {_name}--- name:{_name} job:{_job}‘‘‘.format(_name=name,_job=job)) #字符串拼接方法4 print(‘‘‘---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=0
num=15
while 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
        continue
else:
    print("Guess incorrectly for 3 times!")

無限循環

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

for循環

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

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