python語法入門

2021-10-24 04:47:39 字數 3350 閱讀 2305

# 輸出

print

("你好啊"

,end="")

print

("中國"

)你好啊中國

# 變數 格式化列印

cars =

100print

(f"我的車子有輛"

)我的車子有100輛

# 獲取鍵盤輸入

data =

input

("請輸入乙個數字"

)print

(data)

輸入乙個數字11

11

from sys import ar**

script,one = ar**

print

(script,one)

# 讀檔案

f =open

("test.txt"

,encoding=

"utf-8"

)data = f.read(

)print

(data)

f.close(

)ps f:\ooooooooooo\py> python .\t.py

我只是這世界

我只是生生流轉的

浩瀚海洋上的一束波光

僅僅在這一世

我是這女人的丈夫

孩子的父親

也曾長久地生活在雙親身邊

像遭到串腸河遺棄的漩渦

乙個寒冷的漩渦,消失

# 寫檔案

f =open

("a.txt"

,"w"

,encoding=

"utf-8"

)data = f.write(

""" 我只是這世界

我只是生生流轉的

浩瀚海洋上的一束波光

僅僅在這一世

我是這女人的丈夫

孩子的父親

也曾長久地生活在雙親身邊

像遭到串腸河遺棄的漩渦

乙個寒冷的漩渦,消失

""")f.close(

)

# 檢查檔案路徑

from os.path import exists

b = exists(

"a.txt"

)print

(b)true

# 函式

defadd

(one, two)

:return one + two

print

(add(12,

15))27

# 條件語句

dogs =

20cats =

10if dogs>cats:

print

("dogs is more"

)else

:print

("cats is more"

)ps f:\ooooooooooo\py> python .\t.py

dogs is more

# 列表

list=[

1,2,

3,4,

5,6,

7,8]

# 訪問元素

print

(list[0

])print

("-"*20

)# 新增元素

list9)

print

(list

)print

("-"*20

)# 刪除並返回第乙個元素

print

(list

.pop())

print

(list

)print

("-"*20

)ps f:\ooooooooooo\py> python .\t.py1-

----

----

----

----

---[

1,2,

3,4,

5,6,

7,8,

9]--

----

----

----

----

--9[

1,2,

3,4,

5,6,

7,8]

----

----

----

----

----

# 遍歷列表

list=[

"張三"

,"李四"

,"王五"

]for item in

list

:print

(item,end=

" ")

ps f:\ooooooooooo\py> python .\t.py

張三 李四 王五

# range函式

for item in

range(0

,3):

print

(item)

ps f:\ooooooooooo\py> python .\t.py01

2

# 字典

stuff =

# 遍歷

defprintstuff()

:for key,value in stuff.items():

print

(f" == "

)print

("-"*20

)printstuff(

)# 刪除

del stuff[

"age"

]printstuff(

)# 新增

stuff[

"gender"]=

"女"printstuff(

)ps f:\ooooooooooo\py> python .\t.py

name == 張三

age ==

18hobby == playbasketball--

----

----

----

----

--name == 張三

hobby == playbasketball--

----

----

----

----

--name == 張三

hobby == playbasketball

gender == 女

Python入門語法

python入門語法 動態變數 a 3 整數 a abc a abc 字串 a 3.0 小數 a true a false 布林型 a 3 a a a a 結果 3 3 3 3 靜態變數同語法 l 1,2,3 l 0 為1 l 1 為3 l 123 456 789 l 123 456 789 set...

python入門 基本語法

標準資料型別 number 數字 string 字串 list 列表 tuple 元組 sets 集合 dictionary 字典 number只支援int 表示長整型 float,bool,complex 複數 python中有type 函式檢測變數型別 del 要刪除物件 可以刪除物件 1 py...

python 語法入門 2

python的資料型別有 列表,元組,set,字典 還有列表內建函式,filter func,x,y func 返回true or false,將列表中的值,分別放到func中根據返回的true 來將該值取出 map func,x,y 將列表中的值 放到func中,將返回值放到返回的列表中。map ...