1. 程式人生 > >一.Python安裝與初識

一.Python安裝與初識

 Python安裝與初識

  Python初識

    python的優勢開發效率高

                    劣勢執行效率低

    python的種類:JPython ,CPython ,pypy

 

 

 

    python之禪:(自行谷歌翻譯)

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

                          

python安裝

    Windows安裝:

   1.python官網地址

   2.在本地安裝

   3.設定環境變數

        右鍵-> 我的電腦 -> 屬性 -> 高階系統設定 -> 環境變數 -> 系統變數 -> Path -> 新建 -> python軟體下bin資料夾

   4.cmd測試

 python編譯python檔案

#a.py為python程式碼檔案

python a.py

 

 

   Linux安裝: 

          yum 安裝     yum install python

          apt安裝        apt-get install python

          ......

 python基礎

           變數   

//一:字串

   //四種形式字串
   //1
   name='Hello World!'
   //2
   name="Hello World!"  
   //3
   name="""Hello World!"""
   //4
   name='''Hello World!'''

//二:數字

   num=123
   

           輸出                       

//控制檯輸出
  
  print("Hello World!")
  


//控制檯變數輸出
  name="Hello World!"
  print(name)

 

              輸入

//控制檯輸入
  
  num=input("請輸入你的年齡?")

//輸出年齡
   
  print("我的年齡是%d"%(int(num)));

 

 邏輯判斷

       if

//if作用是判斷

age=input("請輸入你的年齡?")

if age < 12 :

    print("兒童")

elif age < 18 :
    
    print("少年")

else :
    
    print("青年")

 

 迴圈

         while

//while迴圈


#coding=gbk

num=1

while num < 10:

	print("迴圈第%d次!"%(num))

	num = num + 1

 

         for

#coding=gbk

//for range()迴圈數字

for i in range(10):

	print(i)

//for迴圈字串

for i in "Hello World!":
	
	print(i)

Test

從1迴圈到100

#coding=gbk

'''從1迴圈到10'''

for i in range(100):

    print(i)

 從1加到100

#coding=gbk

''' 1+2+3+....+100 '''

num=0

for i in range(100):

    num+=i


print(num)

 1-2+3-4+5-6+....-100

#coding=gbk

''' 1-2+3-4+...-100 '''

num = 0

for i in range(100):

    if i % 2 == 0 :
        
        num=num-i
    
    else:
        
        num=num+i

print(num)

使用者登入,三次機會重試 

#coding=gbk

'''使用者登入,三次機會重試 '''

num = 0

while num < 3:

    user_name = input("請輸入使用者名稱:")

    if user_name == "tom" :
    
        print("登陸成功!")
        
        num=3
    
    else :
        
        print("登陸失敗!")
        
                
        num = num + 1 

        print("還有%d次機會!"%(3-num))