Python-Day-01 Python入門指導

  • Python介紹

    • Python優缺點

優勢:易學、可移植性、解釋型語言、面向對象、豐富的庫python

缺點:強制縮進、速度不如C、沒法加密、不能使用多線程linux

    • Python版本

python2.X python3.x編程

python 2.7兼容版本,中止更新庫windows

python3.x新版本,目前流行版本,持續更新維護庫python3.x

    • Python安裝

windows安裝:https://www.python.org/getit/下載安裝python,注意解決環境變量問題,繼續安裝Pycharm客戶端,進行python編譯調試多線程

linux:自帶python,可能需更新版本ide


 

  • Python編程入門

  • 字符集介紹

ASCII--->GB2312--->GBK1.0--->GBK18030--->Unicode--->UTF-8加密

  • 第一個python程序

1 #!/usr/bin/env python
2 print("hello world!")
  •  定義變量

1 #!/usr/bin/env python
2 # Author: Panda Yu
3 name = "panda"
4 print(name)

 

  • input輸入 

1 #!/usr/bin/env python
2 name = input("yourname:")
3 print("your name is:",name)

 

  • 代碼註釋

註釋掉單行使用#spa

註釋掉多行使用線程

'''
name = input("myname:")
print("my name is:",name)
'''
  •  多行輸入及數據帶入

'''   '''   多行輸入,文字拼接
%s 字符代入
{_name}.format(_name=name) 字符帶入
 1 #!/usr/bin/env python
 2 # Author: Panda Yu
 3 name = input("Name:")
 4 job = input("Job:")
 5 age = input("Age:")
 6 info = '''-----info of %s-------
 7 Name:%s
 8 Job:%s
 9 Age:%s
10 ______________________
11 '''%(name,name,job,age)
12 print(info)
input_%s
 1 #!/usr/bin/env python
 2 # Author: Panda Yu
 3 name = input("Name:")
 4 job = input("Job:")
 5 age = input("Age:")
 6 info = '''-----info of {_Name}-------
 7 Name:{_Name}
 8 Job:{_Job}
 9 Age:{_Age}
10 _______________________________
11 '''.format(_Name=name,_Job=job,_Age=age)
12 print(info)
input_format

 

  • if條件語句

簡易輸入用戶名密碼進行對比

 1 #!/usr/bin/env python
 2 # Author: Panda Yu
 3 _username = "panda"
 4 _passwd = "123456"
 5 username = input("Username:")
 6 password = input("Password:")
 7 if username == _username and password == _passwd:
 8     print("welcome to you!")
 9 else:
10     print("there are errror username or password!")
if_else

定義數值,屢次比較

int() 數據類型轉換爲int

myage = 23   對應    guessage = int(input("guess age:"))

myage = "23"  對應   guessage = input("guess age:")

 1 #!/usr/bin/env python
 2 # Author: Panda Yu
 3 myage = 23
 4 guessage = int(input("guess myage:"))
 5 if myage == guessage:
 6     print("you got it!")
 7 elif guessage > myage:
 8     print("think smaller!")
 9 else:
10     print("you should think bigger!")
if_elif_else

 

  • while條件語句

1. while true

猜想數值,猜對退出,N次錯誤退出

 1 #!/usr/bin/env python
 2 # Author: Panda Yu
 3 myage = 23
 4 count = 0
 5 while True:
 6     if count > 2:
 7         break
 8     guessage = int(input("guess myage:"))
 9     if myage == guessage:
10         print("you got it!")
11         break
12     elif guessage > myage:
13         print("think smaller!")
14     else:
15         print("you should think bigger!")
16         count +=1
while_true

2.while+條件

whiile count < 3:

         ........

else:

   print()

 1 #!/usr/bin/env python
 2 # Author: Panda Yu
 3 myage = 23
 4 count = 0
 5 while count < 3:
 6     guessage = int(input("guess myage:"))
 7     if myage == guessage:
 8         print("you got it!")
 9         break
10     elif guessage > myage:
11         print("think smaller!")
12     else:
13         print("you should think bigger!")
14         count +=1
15 else:
16     print("you have error too many times!")
while_條件_else

3.while+條件+if 屢次判斷

猜錯三次,詢問是否繼續,繼續count重置爲0,不繼續 break 提示good bye

 1 #!/usr/bin/env python
 2 # Author: Panda Yu
 3 myage = 23
 4 count = 0
 5 while count < 3:
 6     guessage = int(input("guess myage:"))
 7     if myage == guessage:
 8         print("you got it!")
 9         break
10     elif guessage > myage:
11         print("think smaller!")
12     else:
13         print("you should think bigger!")
14         count +=1
15         if count == 3:
16             continue_file = input("are you want to continue??")
17             if continue_file != "n":
18                 count = 0
19             else:
20                 print("good bye!")
21                 break
while_條件_if_else_if_else

 

  • for條件語句

1.for循環 打印I的值

for i in range(x,y,z)

x 起始值

y 結束值

z 間隔值

1 #!/usr/bin/env python
2 # Author: Panda Yu
3 for i in range(10):
4     print("look:",i)

 

1 #!/usr/bin/env python
2 # Author: Panda Yu
3 for i in range(1,10,2):
4     print("look:",i)

2.for循環三次,猜數字,三次後退出打印信息

 1 #!/usr/bin/env python
 2 # Author: Panda Yu
 3 myage = 23
 4 for i in range(3):
 5     guessage = int(input("guess age:"))
 6     if guessage == myage:
 7         print("you got it!")
 8         break
 9     elif guessage > myage:
10         print("think smaller!")
11     else:
12         print("think bigger!")
13 else:
14     print("you have error many times!")
for_if

  • break/continue區別對比

break 退出整個循環

continue 退出當前循環

1 #!/usr/bin/env python
2 # Author: Panda Yu
3 for i in range(10):
4     if i < 5:
5         print("look:",i)
6     else:
7         print("he he he")
8         #continue
9         break

相關文章
相關標籤/搜索