python(day001 基礎知識)

2021-09-26 06:10:19 字數 4652 閱讀 4960

1. pip:給python安裝各種程式包。在cmd下輸入pip獲取相關資訊。如果沒有則檢視是否配置環境變數。

2.isinstance的用法

>>> print (a)

1>>> a="hello"

>>> type(a)

>>> a=1

>>> b="hello"

>>> a+b

traceback (most recent call last):

file "", line 1, in typeerror: unsupported operand type(s) for +: 'int' and 'str'

>>> b=input(u'輸入:')

輸入:2

>>> type(b)

>>> print (a+int(b))

3>>> a=1.1

>>> isinstance(a,float)

true

>>> d=1+3j

>>> type(d)

>>> e=[1,2]

>>> isinstance(e,list)

true

>>> type(isinstance(e,list))

>>> a=true

traceback (most recent call last):

file "", line 1, in nameerror: name 'true' is not defined

>>> a=1<3

>>> type(a)

>>> print(a)

true

3.地板除

>>> 5/3

1.6666666666666667

>>> 5//3

1>>> int(5/3)

1>>> round(0.9,0)

1.0>>> round(1/3,5)

0.33333

>>> round(0.5,0) #並非四捨五入

0.0>>> round(2.5,0) #如果左右相同,取離自己近的偶數值

2.0>>> round(1.5,0)

2.0>>> round(3.5,0)

4.0>>> round(4.5,0)

4.0>>> round(3.51,0)

4.0>>> round(4.51,0)

5.0

>>> help(pow)

help on built-in function pow in module builtins:

pow(x, y, z=none, /)

equivalent to x**y (with two arguments) or x**y % z (with three arguments)

some types, such as ints, are able to use a more efficient algorithm when

invoked using the three argument form.

#x的y次方,結果除以z後,取餘數

>>> pow(5,2,5)

0>>> pow(5,2,10)

5

4.math包

pow和math.pow區別:pow三個引數,math.pow兩個引數,只有乘方功能。

>>> import math

>>> math.pow(3,2)

9.0>>> math.sqrt(8)

2.8284271247461903

>>> math.sqrt(4)

2.0>>> math.pi

3.141592653589793

>>> help(math.pow)

help on built-in function pow in module math:

pow(x, y, /)

return x**y (x to the power of y).

5.ord()和chr()

>>> ord('e')

101>>> ord('a')

97>>> ord('a')

65>>> chr(65)

'a'

6.取餘:

>>> 5%3

2>>> 5%2

1>>> divmod(5,2)

(2, 1)

>>> 5/2

2.5

7.ascii碼:

>>> 'a'>'a'

true

#比較時按照ascii碼比較

>>> ord('3')

51>>> ord(3)

traceback (most recent call last):

file "", line 1, in typeerror: ord() expected string of length 1, but int found

8.print ()

>>> print ('hello world',end='')

hello world>>> hh

traceback (most recent call last):

file "", line 1, in nameerror: name 'hh' is not defined

>>> print ('hello world',end='***')

hello world***>>> print ('hello world',end=' ***')

hello world ***>>>

9.input():輸入,內容為字串

>>> age=input('年齡')

年齡5>>> type(age)

>>> age=int(age)

>>> type(age)

10.if

#encoding=utf-8

resule=input('輸入成績:')

if int(resule)>=60:

print('及格啦!')

else:

print ("不及格!")

>>> if false:print(2)

... else:print(1)

...1

>>> type(true)

traceback (most recent call last):

file "", line 1, in nameerror: name 'true' is not defined

>>> type(true)

# encoding=utf-8

#輸入乙個字串,大於3列印大於3;小於3,列印小於3;if a>0;等於3,列印等於3

a=input('輸入字串:')

if len(a)>3:

print ('大於3')

elif len(a)==3:

print('等於3')

else:

print('小於3')

11.bool() 判斷真假

>>> bool()

false

>>> bool('')

false

>>> bool(())

false

>>> bool({})

false

>>> bool(0)

false

#空,都為假

>>> bool(-1)

true

12.and 和 or,取反 not

注:都是順序執行,and有短路效應

>>> a=3

>>> a>6 and a<11

false

>>> a>6 or a<11

true

>>> 1 and 1

1>>> 1 and 0

0>>> 0 and 0

0>>> 0 and 1

0>>> not

true

>>> not ()

true

>>> not 1

false

#複雜內容加()決定優先順序

#encoding=utf-8

#判斷是否能被2和5整除

result=input('輸入數:')

if int(result)%2==0 and int(result)%5==0:

print('可以被2和5整除')

else:

print ("不可以被2和5整除!")

#encoding=utf-8

#如果能被3或5整除,列印ok

result=input('輸入數:')

if int(result)%3==0 or int(result)%5==0:

print('ok')

else:

print ("不能被3或5整除!")

菜鳥學Linux 第001天筆記 基礎理論知識

主要學習的知識 rhce rh033 rh133 rh253 rhca rh401 rh423 ldap 學習不多 rh442 rh436 集群 rhs333 mysql 應用行業 運維工程師 系統工程師 系統架構師 dba 所有後期實驗都是在vmware裡操作,必須學會使用此虛擬機器。計算機體系結...

python day1 基礎語法

資料採集 資料分析 web開發 django 自動化運維 運維開發工程師 自動化測試 測試開發工程師 人工智慧 數學 演算法 解釋性語言 可移植性 例如 從windows系統轉移到linux系統 跨平台使用 缺點 執行緒 假的 需程序彌補 執行緒被包含於程序 第三方庫較多 anaconda包含所有庫...

Summary java程式設計基礎001

package test 100個燈泡按1 100編號,1號同學將編號為1的倍數的燈泡全部摁了一遍 亮 2號同學將編號為2的倍數的燈泡全部摁了一遍 關 同學將編號為3的 100號同學將編號為100的倍數摁了一遍,最後有 個燈泡是亮著的 分析 最後燈的編號的約數個數為奇數時,燈還會亮著 public ...