Python 初學筆記

2021-06-26 09:14:46 字數 1082 閱讀 5052

>>> def to_celsius(t):

…           return (t-32.0)*5.0/9.0

在函式名後用冒號,不是等號。函式實際定義在下一行,該行縮排4個空格,並以return 標記。

str(t),將t轉換為字串

>>>』12』+str(34)+』56』

『123456』

在字串兩端的單引號或者雙引號分別替代成3個,即可擴充套件為多行字串。

用print語句來實現列印輸出。

>>> line = 5

>>> print 'there is a error in line:', line,'!'

格式化列印輸出則為》 print 'there isa error in line: %d !' % line

>>> col = 8

>>> print 'there isa error in line: %d at column %d!' % (line,col)

there is a error in line: 5at column 8!

使用raw_input()來得到使用者輸入,還可以接收乙個字串引數以對使用者進行提示

>>> line =raw_input()

gogogo

>>> line

'gogogo'

>>> name =raw_input("what is your name\n")

python可以指定引入某個模組中所需的,如下是從math中引入所需的sqrt和pi

>>> from mathimport sqrt, pi

>>> sqrt(9)

python在引入模組的時候就會將其執行一次,但只會在第一次引入時進行載入。

條件語句塊

if condition:

block

elif condition:

block

else:

block

for variable in ssss:

block

while condition:

block

一些有用的函式range, enumerate,

Python初學筆記

工作需要,要接觸python的程式,學習一下 使用的3.2版本 話說比2.5變化還真多 print都變了 總體感覺,py比較perl來說,特點還是非常之強烈的 1 py可以自動字串複製 x 3 x 3 333 2 py是強型別變數,和perl不同,型別變數不能混用 3 字串連線 號和join等str...

Python初學筆記

whileformat.1 while 迴圈控制條件 迴圈體format.2 while 迴圈控制條件 迴圈體else 語句forformate.1 for 目標識別符號 in 序列 迴圈體注意 若 in 的序列是乙個列表,則目標識別符號會代替序列裡的變數 for xx in range n 迴圈體...

Python初學筆記

一 安裝 直接通過軟體管理程式,搜尋python,安裝 安裝過程中自定義路徑,有個選項類似 add python3.5 to path 勾選後便可以在cmd命令視窗,通過輸入python,啟動python編譯器 二 第乙個程式列印hello world 1 在編譯器下,後面輸入print hello...