国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

《python基礎(chǔ)教程》筆記之基礎(chǔ)知識(shí)

2019-11-14 17:22:17
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
  • 數(shù)字相關(guān)

在Python程序前加上 from __future__ import division 或者在解釋器里面直接執(zhí)行它,或者通過(guò)命令行運(yùn)行Python時(shí)使用命令開(kāi)關(guān)-Qnew,會(huì)使單斜線不再整除,如

>>> 1/2
0.5

而雙斜線實(shí)現(xiàn)整除,如

>>> 1//2
0

  •  輸入

>>> x = input('x:')

  • 模塊

用import導(dǎo)入模塊,然后按照“模塊.函數(shù)”的格式使用這個(gè)模塊的函數(shù),如

>>> import math
>>> math.floor(23.96)
23.0

 在使用了“form模塊import函數(shù)”這種形式的import命令之后,就可以直接使用函數(shù),而不需要模塊名作為前綴了,如

>>> from math import sqrt
>>> sqrt(9)
3.0

CMath模塊可以實(shí)現(xiàn)處理復(fù)數(shù),如

>>> cmath.sqrt(-9)
3j

  • 程序

在IDLE中,file->new file會(huì)彈出一個(gè)編輯窗口,在里面編輯如下

name = raw_input("what is your name")PRint 'hello,' + name + '!'

然后file-save 保存為hell.py,然后F5運(yùn)行,則在解釋器中出現(xiàn)結(jié)果,如

>>> ================================ RESTART ================================
>>>
what is your name55
hello,55!

或者雙擊hell.py,則命令行窗口一閃而逝。

 

在代碼中,#右邊的一切都會(huì)被忽略,那部分即為注釋。

  •  字符串

反引號(hào)、str和repr是將值轉(zhuǎn)換為字符串的三種方式,其中str會(huì)把值轉(zhuǎn)換為合理形式的字符串,以便用戶理解,而repr會(huì)創(chuàng)建一個(gè)字符串,以合法的平Python表達(dá)式的形式來(lái)表示值,如

tmp = 1000print 'hello ' +  `tmp`print 'hello ' +  str(tmp)print 'hello ' + repr(tmp)

結(jié)果如下
hello 1000
hello 1000
hello 1000

 

input會(huì)假設(shè)用戶輸入的是合法的Python表達(dá)式(或多或少有些與repr函數(shù)相反的意思),raw_input會(huì)把所有的輸入當(dāng)做原始數(shù)據(jù),然后將其放到字符串中,如

print 'hello ' + raw_input('your name:')print 'hello ' + input('your name:')

結(jié)果如下

your name:55
hello 55
your name:55

Traceback (most recent call last):
  File "E:/work/Code/python/hell.py", line 2, in <module>
    print 'hello ' + input('your name:')
TypeError: cannot concatenate 'str' and 'int' objects

 

如果要書(shū)寫(xiě)一個(gè)跨多行的長(zhǎng)字符串,可以使用三個(gè)引號(hào)代替普通引號(hào),如

print '''This is a long string.it continues here.and it's not over yet."hello world!".still here.'''

結(jié)果如下
This is a long string.
it continues here.
and it's not over yet.
"hello world!".

普通字符串也可以跨行,如果一行之中最后一個(gè)字符是反斜線,那么,換行本身就“轉(zhuǎn)義”了,也就是被忽略了(同時(shí)適用于表達(dá)式),如

print 'This is a long string./it stop here.'

結(jié)果

This is a long string.it stop here.

原始字符串不會(huì)吧反斜線當(dāng)做特殊字符,在元是字符串中輸入的每個(gè)字符都會(huì)與書(shū)寫(xiě)的方式保持一致,如

print r'E:/work/Code/python' '//'

結(jié)果如下

E:/work/Code/python/

 


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 蓝田县| 宁阳县| 吉隆县| 通化县| 普兰店市| 普安县| 阿拉善左旗| 株洲市| 龙游县| 都匀市| 怀化市| 河曲县| 乌鲁木齐市| 包头市| 伊宁县| 名山县| 涞水县| 洪雅县| 广西| 夏邑县| 金乡县| 佛冈县| 彭州市| 贡觉县| 孝昌县| 郎溪县| 武强县| 颍上县| 巴彦淖尔市| 交口县| 大田县| 共和县| 翁牛特旗| 芜湖市| 都江堰市| 丰原市| 沙田区| 湟中县| 自治县| 上蔡县| 肃北|