老男孩python第一天筆記


1、變量   把結果存下來,變量存在內存里,name1 = "hepeng"    name2=name1   name1 = "hp"    print(name1,name2)

  name2是name1在內存中的指向相當於軟連接,輸出結果,name1=hp  name2=hepeng

2、字符編碼都是向下兼容的,unicode萬國碼,utf8 存英文1byte 存中文3byte

3、用戶交互程序,username = input("請輸入")

  變量引用格式化輸出,%s字符串,%d只能接受數字,%f浮點小數

 name = hp

  info= "Name:%s"  %  (name)

  print(info)

  字符串轉 整形 int  整形轉字符串str

  用format格式化 輸出

  

name = "hp"
  info2 = "Name:{_name}".format(_name = name)
  print(info2) 

 

 

4、密碼密文使用getpass

  import getpass

  password = getpass.getpass("password:")    getpass  在pycharm里不好使,在命令行可以

5、if else循環,用戶名密碼匹配輸出ok,不匹配輸出wrong

if  username == user and  password = pwd :
    print("ok")
  else:
    print("wrong")

 

 

6、while循環,年齡猜三次,正確就退出,三次不對給提示

 count = 0 
 age = 23
 while  count <3:
  guess_age = int(input("guess age"))
  if guess_age == age:
    print("you got it")
    break
  elif guess_age > age:
    print("too bigger")
  else:
    print("too smaller")
  count +=1
else:
  print("you have try too many choice")

 

  

7、for 循環 

等同上邊while代碼,用for循環實現age猜三次

age = 23

for i in range(3):
  guess_age = int(input("guess age"))
  if guess_age == age:
    print("you got it")
    break
  elif guess_age > age:
    print("too bigger")
  else:
    print("too smaller")
else:
  print("you have try too many choice")

for i in range(0,10,3) 0-10隔三個

8、continue跳出本次循環進入下一次循環,break結束整個循環

for i in range(10):
    if  i  < 3:
        print("loop",i)
    else:
        continue
    print("hehe")

當i循環到3的時候就不執行print了,continue直接跳出本次循環進行下次循環

for i in range(10):
    if  i  < 3:
        print("loop",i)
    else:
        break
    print("hehe")

當i循環到3的時候直接跳出循環,不在執行,兩個的輸出結果是一樣的,但是原理不同

    

 

 

 

 

 

 

 

 

 

 

 

     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM