python基礎(1)

來源:互聯網
上載者:User

標籤:64位   app   red   oca   而不是   python2   pass   last   字串   

基礎安裝配置1、安裝python     2.7預設安裝路徑:C:\python27          3.6 64位預設安裝路徑:C:\Users\Jam\AppData\Local\Programs\Python\Python35  Linux的yum依賴內建Python,為防止錯誤,此處更新其實就是再安裝一個Python! 查看預設Python版本python -V 1、安裝gcc,用於編譯Python源碼    yum install gcc2、下載源碼包,https://www.python.org/ftp/python/3、解壓並進入源碼檔案4、編譯安裝    ./configure    make all    make install5、查看版本    /usr/local/bin/python2.7 -V6、修改預設Python版本    mv /usr/bin/python /usr/bin/python3    ln -s /usr/local/bin/python3 /usr/bin/python7、防止yum執行異常,修改yum使用的Python版本    vi /usr/bin/yum    將頭部 #!/usr/bin/python 修改為 #!/usr/bin/python2.7 
   
2、配置環境變數【右鍵電腦】--》【屬性】--》【進階系統設定】--》【進階】--》【環境變數】--》【在第二個內容框中找到 變數名為Path 的一行,雙擊】 --> 【Python安裝目錄追加到變值值中,用 ; 分割】如:原來的值;C:\python27,切記前面有分號例如:     ;C:\Python27;C:\Users\Jam\AppData\Local\Programs\Python\Python35CMD視窗要同時運行3.6或者2.7版本,需要到安裝目錄把其中一個python.exe改名,例如改成python2.exe,在命令列視窗執行python2就能開啟python2.7

 

input和 raw_input

python 3.5裡面 input輸入不論輸入的是數字還是字串, 都被預設為是字串,如果要輸入數字需要將字串轉換成數字,使用int方法。

python 2.7  裡面用input輸入字串預設認為輸入的是變數,然後會去找這個變數,找不到然後會報錯,輸入數字python認為輸入的就是數字會存下來,所以不會報錯。如果輸入的是字串就需要用raw_input而不是input,僅在2.7裡面出現。  import getpass[[email protected] ~]# cat test_getpass.pyimport getpassusername = input("username:")password = getpass.getpass("password:")print(username,password)[[email protected] ~]# python3 test_getpass.pyusername:jam password:jam hello[[email protected] ~]#  C:\Users\Jam>python2Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:40:30) [MSC v.1500 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> import getpass>>> username = input("username:")username:jamTraceback (most recent call last):  File "<stdin>", line 1, in <module>  File "<string>", line 1, in <module>NameError: name ‘jam‘ is not defined>>> username = raw_input("username:")username:jam>>> password = getpass.getpass("password:")password:>>> print(username,password)(‘jam‘, ‘hello123‘)>>> C:\Users\Jam>pythonPython 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> username = input("username:")username:jam>>> password = getpass.getpass("password:")Traceback (most recent call last):  File "<stdin>", line 1, in <module>NameError: name ‘getpass‘ is not defined>>> import getpass>>> username = input("username:")username:jam>>> password = getpass.getpass("password:")password:>>> print(username,password)jam hello123>>> #!/usr/bin/env python
# -*- coding: utf-8 -*-

#pycharm裡面無法使用getpass方法,在linux和windows的python上面可以正常運行
import getpass
username = input("username:")
passwrod = getpass.getpass("password:")
print(username,passwrod) 

 

import os[[email protected] ~]# python3Python 3.5.1 (default, May 12 2016, 00:36:44)[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linuxType "help", "copyright", "credits" or "license" for more information.>>> import os>>> os.system("df  -TH")檔案系統       類型      容量  已用  可用 已用% 掛載點/dev/sda2      xfs        19G  1.9G   17G   11% /devtmpfs       devtmpfs  503M     0  503M    0% /devtmpfs          tmpfs     513M     0  513M    0% /dev/shmtmpfs          tmpfs     513M  7.0M  506M    2% /runtmpfs          tmpfs     513M     0  513M    0% /sys/fs/cgroup/dev/sda1      xfs       521M  124M  398M   24% /boottmpfs          tmpfs     103M     0  103M    0% /run/user/00條件判斷if else文法規則如果 if 語句判斷為 True,就會執行它下面的子代碼(下層縮排代碼 )。、否則的話,就執行 el se 下面的子代碼。當然後面的 el se 也不是必須要加的,如果不加 el se 的話,那 if 條件判斷為 Fa l se 的話,那它就什麼也不做。 and與or文法規則

 

看這裡寫的if user_input == valid_user and passwd == valid_passwd:&#0;的意思就是讓if 條件必須同時滿足2個條件了,當然你還可以

 

再後面再寫1個and ,2個and…… n個and,那if條件就必須要滿足n個條件才會執行它下面的文法了,但是正常人都不寫&#0;太多個and,因為可讀性太差。所以一般一個if判斷同時滿足1至3個條件就可以了;跟and對應的文法是or,“或”的意思,and是“與”的意思,就是滿足1個或另1個或另1個條件。。。 條件判斷elif文法規則

 

elif的意思就是如果不滿足第一個 if 條件,那程式就會繼續往下走,再判斷是否滿足 elif 條件。如果不滿足,就再繼續走(這裡你可以加多個elif判斷),只要遇到有滿足的 elif 就停下來執行它後面的代碼,然後結束。如果最終沒有碰到滿足的條件,就最終執行 else 文法。elif可以寫多行

 

python基礎(1)

聯繫我們

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