python: number執行:shift+f10
注釋:ctrl+/
python: stringvar =
10print
(var)
#check value
python: liststr
='thisisastring'
(s[0])
#print the first char in string s;
(s[1:3
])#print string from index 1 to index 3(not include index 3)
python: tuplealist =
['runoob'
,786
,2.23
,'john'
,70.2
]#note: for non-number str, '' is a must
(alist[1]
)#print the second element in alist;
#add element at the end of list
del alsit[0]
#delete the first element
#list.insert(index, obj)
alist.insert(0,
'hali'
)#insert 'hali' at the first position
difference from list, the value of tuple can』t be changed.
python: dictionaryatuple =
('runoob'
,786
,2.23
,'john'
,70.2);
(atuple[1]
)
python: 資料型別轉換#d =
#值可以取任何資料型別,但鍵必須是不可變的,如字串,數字或元組。
#字典值可以沒有限制地取任何python物件,既可以是標準的物件,也可以是使用者定義的,但鍵不行。
tinydict =
(tinydict.values())
for key in tinydict.keys():
(key)
(tinydict[key]
)
控制語句a =
1b =
2if a ==
1and b ==2:
('case1'
)elif a ==
2or b ==3:
('case2'
)else
('case3'
)
while loopbreak
#終止所在的loop
continue
#跳出本次迴圈,繼續執行下次迴圈
for loopcount =
0while count <9:
(count)
count = count +
1else
('end'
)#while結束時執行else
元素遍歷迭代
for iterating_var in sequence:
statements(s)
#basic grammar, interating_var將遍歷sequence中所有元素
通過序列索引迭代for letter in
'python'
:# 第乙個例項
(letter)
fruits =
['banana',,
'mango'
]for fruit in fruits:
# 第二個例項
(fruit)
fruits =
['banana',,
'mango'
(len
(fruits)
(range
(len
(fruits)))
for index in
range
(len
(fruits)):
#range(3) equals to range(0,3)
('fruit'
+str
(index)
, fruits[index]
)#equal expression
for index in
range(0
,3):
('fruit'
, index, fruits[index]
)
def functionname( parameters ): #standard form
function_suite
return [expression]
def
printme
(astring)
(astring)
return
printme(
'call function printme'
)
there are different type of parameter type.
讀取鍵盤1個python module就是乙個py檔案;
模組能定義函式,類和變數,模組裡也能包含可執行的**。
模組的匯入:import module1 or from module1 import *
呼叫模組中的函式:模組名.函式名
檔案讀寫str1 =
input
("請輸入:"
("你輸入的內容是: "
, str1)
fo =
open
('test_data.txt'
,'w'
(fo.name)
fo.write(
'the_first_element'
)fo.close(
)fo =
open
('test_data_1.txt'
,'r+'
)str1 = fo.read(10)
str2 = fo.readline(
)fo.close(
(str1)
(str2)
class
employee
: empcount =
0def
__init__
(self, name, salary)
: self.name = name
self.salary = salary
employee.empcount +=
1def
display_count
(self)
('total employee number:'
, employee.empcount)
defdisplay_employee
(self)
('total employee number:'
('name:'
, self.name,
'salary:'
, self.salary)
emp1 = employee(
'ali',10
)emp2 = employee(
'cat',20
)emp1.display_count(
)emp1.display_employee(
)
python教學筆記 python學習筆記(一)
1.eval 函式 eval是單詞evaluate的縮寫,就是 求.的值的意思。eval 函式的作用是把str轉換成list,dict,tuple.li 1 1,2,3 print eval li 1 di 1 print eval di 1 tu 1 2,4,6 print eval tu 1 執...
python學習筆記
coding utf 8 coding utf 8 應該像八股文一樣在每個指令碼的頭部宣告,這是個忠告 為了解決中文相容問題,同時你應該選擇支援 unicode 編碼的編輯器環境,保證在執行指令碼中的每個漢字都是使用 utf 8 編碼過的。cdays 5 exercise 3.py 求0 100之間...
Python 學習筆記
python 學習筆記 def run print running.def execute method method execute run result running.condition false test yes,is true if condition else no,is false ...