python第二章學習筆記

2021-09-25 06:20:47 字數 3747 閱讀 4785

from math import *

print(pi)

print(e)

3.141592653589793

2.718281828459045

print(id(11))

print(id('python'))

print (type(12))

print(type(1.2))

140733182862288

2498184443808

140733182865520

140733182865520

140733182865520

python變數的特色:

1 不需要宣告

2 可以隨時賦不同的值

3可以使用乙個賦值符號給多個變數賦值

x,t,z=1,2,3

print(x)

print (t)

print (z)

123

a=5

print (a.bit_length())

b=8print(b.bit_length())

3

4

from decimal import decimal#高精度數

a=decimal(1)/decimal(3)

print(a)

0.3333333333333333333333333333

from fractions import fraction#分數

print (fraction(1,2))

print (fraction(1.25))

print (fraction(12,15))

a=fraction(2,3)

b=fraction(1,3)

print(a+b)

1/2

5/44/5

1序列型別

不可變序列:

資料不能再改變。字串,元組,位元組序列

可變序列:列表,位元組陣列

字串: 『abc』 「123」 『『『qwer』』』

元組型別:(1,「2.3」,「c」)//可以有多種資料型別

位元組序列:

資料是一系列的位元組。以』b『開頭的字串

str="abcd位元組序列"

print(str)

a=str.encode("utf-8")

print(a)

a=str.encode("gb2312")

print(a)

列表:[1,2] .[「feb」,[1,2]]

l=[1,2,3]

print(l)

l[0]='zxc'

print(l)

[1, 2, 3]

[『zxc』, 2, 3]

集合陣列 無序 不重 資料可變

字典 鍵和值 值是可變的

c=

print(c)

c['name']="lyf"

print(c)

c['wugong']="ssp"

print(c)

運算子:

lambda

f=lambda x,y:x+y

print(f(1,2))

if…else

x=1

y=0z=3

m=x if y else z

print(m)

m=3

x=1

y=3z=3

m=x if y else z

print(m)

m=1

x=0

y=3z=3

m=x if y else z

print(m)

m=0

x=1

y=3z=0

m=x if y else z

print(m)

m=1

邏輯運算子 or and not

or 有1為1

and 都是1才是1

not 取反,not 非零常數 false

not 0 true

x=0

y=2print(x or y)

print(x and y)

print(not x)

print(not y)

2

0true

false

關係符號運算子

< <= >= != ==

返回值是bool型別的

成員運算子

in, not in, is,is not

x=0

y=print(x in y)

true

位運算子

| :按位或

x=1

y=2print(x|y)

3

^:按位異或

x=2

y=3print(x^y)

1

&:按位與

x=2

y=3print(x&y)

:左移

x=8

print(x>>1)

4

<<: 右移

x=3

print(x<<2)

12

~:取反

0按位取反是-1

x=3

print(~x)

-4

/:除號

x=1

y=3print(x/y)

0.3333333333333333

//:整除

x=1

y=3print(x//y)

:冪

print(23)

8小偷問題

for x in range(1,5):

if(((x!=1)+(x==3)+(x==4)+(x!=4))==3):

print(x)

內建函式:

abs():取絕對值

bin():轉化為二進位制

bool():非零常數為true 0 為false

complex(a,b) a+bj

divmod(a,b) 返回 a除以b的商和餘數

eval(s) 返回s所表示的值

s="1+2"

print(eval(s))#3

pow(x,y,z) 返回x的y次方除z的餘數

pow(x,y) 返回x的y次方

print(pow(2,3))#8

print(pow(2,3,5))#5

sorted() 返回有序列表

s=[4,5,1]

print(sorted(s))#[1, 4, 5]

第二章學習筆記

在c 中,陣列下標從0開始,而不是1.c 不支援陣列的抽象,也不支援對整個陣列的操作。在c 中,物件可以靜態分配 即編譯器在處理程式源 時分配,也可以動態分配 即程式執行時,用執行時刻庫函式來分配。靜態與動態記憶體分配的兩個主要區別是 1 靜態物件是有名字的變數,可以直接對你進行操作。而動態物件是沒...

第二章學習筆記

ansi c 有翻譯和執行兩種環境,且不必在一台機器上,例如交叉編譯器 cross compiler 作業系統也是如此 freestanding environment 翻譯 將源 轉換為可執行機器指令 執行 實際執行 翻譯經過以下階段 形成的目標檔案字尾可能在不同系統下不同,如 o obj cc ...

python深度學習第二章筆記

2.1mnist例項 coding utf 8 project python深度學習 file name 2.1mnist author win10 time 2020 4 11 11 22 product name pycharm from keras.datasets import mnist ...