python基本知識總結1

2021-09-28 19:28:40 字數 4257 閱讀 8585

python輸入輸出

python邏輯運算子

python成員運算子

說到程式語言,不得不提的就是變數型別,它是程式語言的靈魂。

python基本的變數型別分為數字型非數字型。具體列舉如下:

數字型整型(int)、浮點型(float)、布林型(bool)、複數型(complex)

非數字型

字串(str)、列表(list)、元組(tuple)、字典(dict)

注意上述所以這些int、float、bool都不是python的關鍵字,但利用這些字元可以執行資料型別轉換。

// python關鍵字匯出

>>>

import keyword

>>>keyword.kwlist

['false'

,'none'

,'true'

,'and'

,'as'

,'assert'

,'break'

,'class'

,'continue'

,'def'

,'del'

,'elif'

,'else'

,'except'

,'finally'

,'for'

,'from'

,'global'

,'if'

,'import'

,'in'

,'is'

,'lambda'

,'nonlocal'

,'not'

,'or'

,'pass'

,'raise'

,'return'

,'try'

,'while'

,'with'

,'yield'

]

//python強制型別轉換

>>>

int(

3.2)

3>>>

float(4

)4.0

>>>

complex(3

)(3+

0j)>>>

bool(3

)true

這裡的int()等強制型別轉換可以看做函式呼叫,這在python的學習中極為常見——當你想把一種資料型別轉換為另一種資料型別。

//字串轉列表

>>>

list

('python')[

'p',

'y',

't',

'h',

'o',

'n']

//列表轉字串

>>>

str([1

,2,3

,4])

'[1, 2, 3, 4]'

//字串轉元組

>>>

tuple

('python')(

'p',

'y',

't',

'h',

'o',

'n')

//元組轉字串

>>>

str((1

,2,3

))'(1, 2, 3)'

注意

python不再區分字元和字串,也不區分單引號』 『和雙引號」 「,在c/c++中』e』表示char型別,「e"表示string型別。

python中的字串強制型別轉換是str,不是string

python定義變數的時候不需要指明變數型別,這也是為什麼int等不是關鍵字的原因,通過給變數賦初值的方式,python通過初值設定變數型別

>>>

type

('python'

)<

class

'str'

>

>>>

type(3

)<

class

'int'

>

>>>

type

(3.0

)<

class

'float'

>

>>>

import numpy as np

>>> a = np.

array([

1,2,

3])>>>

type

(a)<

class

'numpy.ndarray'

>

注意

type()函式對有些包的變數能使用,對有些包的變數不能使用

//整除

>>>

3//2

1>>>3/

21.5

//求餘

>>>3%

21//方次

>>>2**

38>>>

1.5**

22.25

>>>

1.5**

3.13.514656635974386

python的輸入是通過input()函式實現的,函式接受乙個字串作為輸入提示字串。注意python的輸入物件型別始終是str型別,需要強制型別轉換才能變成想要的型別。

>>> a =

input

("input:"

)input:

123>>>

type

(a)<

class

'str'

>

>>> a =

input

("input:"

)input:[1

,3,5

]>>>

type

(a)<

class

'str'

>

python的輸出是靠print函式實現的,可以使用類似c語言的輸出方式,也可以使用類似c++語言的輸出方式,還可以直接輸出變數

//直接輸出變數

>>>

print

('python'

)python

>>>

print

(123

)123

>>>

print([

1,2,

3])[

1,2,

3]//類似c語言的輸出

>>> a =

123>>>

print

("this is %d:"

%(a)

)this is 123

:>>> b =

'python'

>>>

print

("this is %s:"

%(b)

)this is python:

>>> c =

123.456

>>>

print

("this is %f:"

%(c)

)this is 123.456000

:

python的邏輯運算子和c/c++的邏輯運算子存在很大的差別,python的邏輯與或非分別通過關鍵字and、or、not實現。

>>>

bool

(0 and 1

)false

>>>

bool

(1 and 2

)true

>>>

bool

(0 or 2

)true

>>>

bool

(not 0

)true

python的成員運算子可以判斷乙個物件是否在乙個序列中,運算子關鍵字是in 、not in

>>> a =[1

,2,3

]>>>

1in a

true

>>>

4in a

false

>>>

4 not in a

true

>>> b =

'python'

>>>

'p'in b

true

Python基本知識1

字串整數 int 只有int型別 浮點數 float python語言沒有單精度雙精度之分,python的浮點數就是雙精度 注 1.type x 可以判斷資料型別,如type 1 2.兩個整型 相除得到的是float型,若想得到整型結果,用2 2 表示 二進位制表示 0bxx 如 0b10 八進位制...

python基本知識總結

print hello m input please input 匯入turtle庫並更名為t import turtle as t 變數賦值即定義,無需另外定義語句,刪繁就簡 變數有型別,int,float,str,list,dict不同型別不可比較和運算,a int m 將m轉為整型並賦值給a ...

python 基本知識總結2

設定指令碼檔案檔案編碼型別改為utf 8的型別 coding utf 8 字典的 update 更新操作dict a dict b dict a.update dict b print dict a 用於測試 或作為主程式執行if name main 當指令碼檔案作為模組被匯入其他指令碼時,這部分 ...