python3學習筆記--001--python HelloWorld

來源:互聯網
上載者:User

標籤:

python預設使用UTF-8編碼

一個python3版本的HelloWorld代碼如下:


#!/usr/bin/env pythonprint (‘Hello World!‘)
如果此python指令檔名為:hello.py,則運行此指令檔的方法有兩種:


1、python hello.py


[[email protected] python]$ python hello.py Hello World![[email protected] python]$
2、修改hello.py的許可權,./hello.py



[[email protected] python]$ ./hello.py Hello World![[email protected] python]$
第一個行稱為shebang(shell執行)行,作用是指定了要使用哪個解譯器
shebang行通常有兩種等式:
#!/bin/bin/python

#!/usr/bin/env python
第一種形式使用指定的解譯器,第二種等式使用在shell環境中發現的第一個python解譯器

對於python2.x 和 python3.x同時安裝的情況而言,一個可靠且可行的方法是使用ln命令,在/usr/bin/目錄下建立不同名字的連結。比如我只建立了指向python3解譯器的python軟連結,如果有需要,還可以建立一個指向python2解譯器的python2軟連結



python的關鍵要素:

1.輸入輸出:

首先是輸出:print()

在windows上安裝python後,會在菜單中看到Python 3.4 Docs Server (pydoc - 64 bit),開啟之後,會在瀏覽器中看到如下頁面:

其中print是我輸入的文本,斷行符號之後會看到如下內容:

我感覺這種方式的協助文檔看起來更好一點。

可以看到其中很多參數都有了預設值,這個解釋還是很不錯的

輸入:input


input(...)input([prompt]) -> string Read a string from standard input.  The trailing newline is stripped.If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.On Unix, GNU readline is used if enabled.  The prompt string, if given,is printed without a trailing newline before reading.
值得注意的是input返回的是string類型


一個使用了input和print的例子:


#!/usr/bin/env pythonprint (‘Hello World!‘)name=input("input your name:")print("your name is : " + name)
可以看到在python中聲明一個變數是時不需要顯示的指明其類型,這和js有點類似


2. 內建類型中的int和str

python中,int類型要比C語言的友好的多,我們可以使用很大很大的int類型的數字而不必擔心溢出

對於string類型,可以使用[]來取得字串中某個字元

但是需要提出的是int和string 類型都是不可變的。不過我們可以使用int(str)可str(int)等方式來改變一個資料項目的類型

3.對象引用

在python中可以使用=運算子直接將一個變數指向另一個變數,一個實際的例子:


[[email protected] python]$ /bin/cat hello.py #!/usr/bin/env pythonprint (‘Hello World!‘)name=input("input your name:")var=nameprint("your name is : " + var)sex=input("input your sex:")var=sexprint("your name is : " + var)age=input("input your age:")var=int(age)print("your age is : ",sep=‘ ‘,end=‘‘)print(var)[[email protected] python]$ ./hello.py Hello World!input your name:xiao dai mayour name is : xiao dai mainput your sex:nanyour name is : naninput your age:24your age is : 24[[email protected] python]$
可以看到其中var變數引用了不同的變數,其指向的內容和值的類型也隨之改變



HelloWorld暫時到這裡


python3學習筆記--001--python HelloWorld

聯繫我們

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