python基礎教程學習筆記 第一章 基礎知識

內建函數:abs 絕對值,pow冪函數,round 四捨五入爲最接近的整數
int long float 類型對象

math模塊下的floor 函數向下取整 ceil 將給定的數值轉換爲大於或者等於它的最小整數

導入模塊:
import math
from math import sqrt 可以每次調用不寫上模塊名字(保證不導入多個同名函數的前提下)
也可以使用foo=math.sqrt進行賦值,--》foo(4) ---2

複數:
sqrt(-1)--->math domian error 
不能求負數的平方根---》負數的平方根是虛數
sqrt 只能處理浮點數,而虛數(以及複數,即實數和虛數之和)
cmath(complex math 複數)


import cmath
cmath.sqrt(-1)--》1j

(1+3j)*(9+4j)--》-3+31j


python語言本身就提供了對複數的支持

交互式解釋器是python的強項之一:
通過idle --file--new file--輸入腳本--ctrl+F5 執行


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


通過命令行提示符運行python腳本:
切換到要執行的python文件目錄
python he.py (設置環境變量的前提下)

如果沒有設置環境變量:c:\python27\python he.py

字符串拼接:

x="hello,"
y="world"
x+y
'hello,world'


字符串表示:

print 1000L
1000
>>> print 'hello,world'
hello,world

print值被轉成字符串

str函數 --》將值轉換爲合理形式的字符串,以便用戶可以理解。
repr函數--》會創建一個字符串,他以合法的python表達式的形式來表示值

>>> print repr("hello, world")
'hello, world'
>>> print repr(1000L)
1000L
>>> print str('hello ,world')
hello ,world
>>> print str(1000L)
1000

repr(x)的功能也可以用`x`實現

>>> print 'the temperature is '+`temp`
the temperature is 42



注:str和int,一樣是類型對象,而repr僅僅是函數。

input 和raw_input比較:

name=input('what is your name')
print 'hello,'+name+"!"


執行--

======================= RESTART: E:/pythonlearn/he.py =======================
what is your namefa

Traceback (most recent call last):
  File "E:/pythonlearn/he.py", line 6, in <module>
  name=input('what is your name')
  File "<string>", line 1, in <module>
NameError: name 'fa' is not defined

input 會假設用戶輸入的是合法的python表達式
以字符串作爲輸入的名字,程序就沒有問題

======================= RESTART: E:/pythonlearn/he.py =======================
what is your name"aa"
hello,aa!

raw_input 函數會把所有的輸入當成原始數據(raw data)

>>> input('enter a number:')
enter a number:3
3
>>> raw_input('enter a number:')
enter a number:3
'3'

除非對inputyou特別的需要,否則應該儘可能使用raw_input函數

長字符串,原始字符串和Unicode

1.長字符串 三個引號代替普通的引號,可以在字符串之中同時使用單引號和雙引號,不需要使用反斜線進行轉義

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

普通字符串也可以跨行,如果一行之中最後一個字符是反斜槓,那麼換行符本身就轉義了,也就是被忽略了

>>> print 'hello\
world'
helloworld

2.原始字符串
普通字符串中,反斜線具有轉義的作用 \n換行

>>> print 'helo,\nworld'
helo,
world
原始字符串不會把反斜線當做特殊字符

>>> print r'c:\nowher'
c:\nowher
原始字符串最後一個字符不能是反斜線,除非你對反斜線進行轉義

>>> print r'this is illegal\'
SyntaxError: EOL while scanning string literal

>>> print r'this is legal\\'
this is legal\\

>>> print r'c:\program files\foo\bar' '\\'
c:\program files\foo\bar\

3.unicode字符串
字符串常量的最後一種類型就是Unicode字符串或者稱爲Unicode對象
python普通字符串在內部是以8爲的ascii碼形成存儲的,而Unicode字符串則是存儲爲16位Unicode字符。

>>> u'hello world'
u'hello world'

python 3.中,所有字符串都是Unicode字符串

算法是描述如何完成一項任務的方法。

表達式是計算機程序的組成部分,他用於表示值

標量是一個名字,他表示某個值

語句是告訴計算機做某些事情的指令。

python中的函數就像數學中的函數:可以帶參數,並且返回值

模塊是擴展,他可以導入到python中,從而擴展python的功能

程序 字符串

新函數
abs(number) 返回數字的絕對值
cmath.sqrt(number) 返回平方根,也可以應用於負數
float(object) 將字符串和數據轉換爲浮點數
help() 提供交互式幫助
input(prompt) 獲取用戶輸入
int(object) 將字符串和數組轉換成整數
long(object) 將字符串和數據轉換爲長整型數
math.ceil(number) 返回數的上入整數,返回值的類型爲浮點數 大於等於
math.floor(number) 返回數的下入整數,返回值的類型爲浮點數 小於
math.sqrt(number) 返回平方根,不適用與負數
pow(x,y[,z]) 返回x的y次冪(所得結果去z取模)

>>> pow(2,3,5) 2**3%5
3

raw_input(prompt) 獲取用戶輸入,返回的類型爲字符串
repr(object) 返回值的字符串表示形式
round(number[,ndigits]) 根據給定的精度對數字進行四捨五入
str(object) 將值轉換爲字符串





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章