Python 學習筆記

2021-10-16 17:42:25 字數 4463 閱讀 4581

執行:shift+f10

注釋:ctrl+/

python: number

var =

10print

(var)

#check value

python: string

str

='thisisastring'

;print

(s[0])

#print the first char in string s;

print

(s[1:3

])#print string from index 1 to index 3(not include index 3)

python: list

alist =

['runoob'

,786

,2.23

,'john'

,70.2

]#note: for non-number str, '' is a must

print

(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

python: tuple

difference from list, the value of tuple can』t be changed.

atuple =

('runoob'

,786

,2.23

,'john'

,70.2);

print

(atuple[1]

)

python: dictionary

#d = 

#值可以取任何資料型別,但鍵必須是不可變的,如字串,數字或元組。

#字典值可以沒有限制地取任何python物件,既可以是標準的物件,也可以是使用者定義的,但鍵不行。

tinydict =

print

(tinydict.values())

for key in tinydict.keys():

print

(key)

print

(tinydict[key]

)

python: 資料型別轉換

a =

1b =

2if a ==

1and b ==2:

print

('case1'

)elif a ==

2or b ==3:

print

('case2'

)else

:print

('case3'

)

控制語句

break

#終止所在的loop

continue

#跳出本次迴圈,繼續執行下次迴圈

while loop

count =

0while count <9:

print

(count)

count = count +

1else

:print

('end'

)#while結束時執行else

for loop

元素遍歷迭代

for iterating_var in sequence:

statements(s)

#basic grammar, interating_var將遍歷sequence中所有元素

for letter in

'python'

:# 第乙個例項

print

(letter)

fruits =

['banana',,

'mango'

]for fruit in fruits:

# 第二個例項

print

(fruit)

通過序列索引迭代

fruits =

['banana',,

'mango'

]print

(len

(fruits)

)print

(range

(len

(fruits)))

for index in

range

(len

(fruits)):

#range(3) equals to range(0,3)

print

('fruit'

+str

(index)

, fruits[index]

)#equal expression

for index in

range(0

,3):

print

('fruit'

, index, fruits[index]

)

def functionname( parameters ):	#standard form

function_suite

return [expression]

def

printme

(astring)

:print

(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

("請輸入:"

)print

("你輸入的內容是: "

, str1)

檔案讀寫

fo =

open

('test_data.txt'

,'w'

)print

(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(

)print

(str1)

print

(str2)

class

employee

: empcount =

0def

__init__

(self, name, salary)

: self.name = name

self.salary = salary

employee.empcount +=

1def

display_count

(self)

:print

('total employee number:'

, employee.empcount)

defdisplay_employee

(self)

:print

('total employee number:'

)print

('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 ...