1. math模塊
math模塊在很多語言中都有,python也不例外,它主要提供數學計算的許多公式,包括三角函數、指數對數、角度轉換等,其中還定義了兩個常量圓周率pi和自然底數e。可以用import math或from math import * 來導入math模塊,區別是前一種導入要在調用函數前加上math. ;後一種直接使用函數即可。
math中的許多函數都比較常見,要用的時候查閱幫助文檔就可以。記下幾個今天學到的函數:
degree(x):將弧度x轉換爲角度;
radians(x):將角度x轉換爲弧度;
modf(x):將x轉換爲(f,i)表示,其中f表示x的小數部分,i表示x的整數部分,f和i都是浮點型的。
2. string
python中的string可以直接輸出,且有兩種表示方法,單引號表示 ('str')、雙引號表示("str")。通常情況下,如果要輸出的string中包含單引號,則string用雙引號表示;要輸出的string中包含雙引號,則string用雙引號表示。如果要輸出的string 中既包含單引號,又包含雙引號,則需要用轉義字符('\')。比較常見的如果string爲多個字符,用雙引號;單個字符用單引號。
#representation
print (' "Welcome to python", he said ')
print (" 'Welcome to python', he said ")
#blackslash \
print ('"welcome to python", he said. \'yes\', I\'m glad ')
1)創建strings
可以用str()函數創建,如s1=str("welcome"),也可以直接創建,如s2="welcome",兩者等同。另外,還可以用str()函數來將數字轉換爲string,如a = str(5);則a的結果爲'5'。
#str() function
s = str(5.0) # s : '5.0'
print (s) # 5.0
2)len,max,min
len()函數返回字符串中包含的字符個數;max()返回字符串中ASCII碼值最大的字符;min()返回字符串中ASCII碼值最小的字符。如:
s = "welcome"
print (len(s)) # 7
print (max(s)) # w
print (min(s)) # c
3)兩個字符串的聯接
a. 若兩個string靠在一起時,沒有其他操作符號,則默認聯接這兩個字符串;
b. 顯式的 “+”操作符,用來聯接兩字符串;
#concatenation
s = "abc" "cde" "feg"
print (s) # abccdefeg
s = "abc" + "cde" + "feg"
print (s) # abccdefeg
4)字符串Repetition操作符*
可以用*來重複表示某字符串多次,如s1="welcome",s1*3結果爲"welcomewelcomewelcome"。
5)python中的string類型有下標:從0到n-1。我們可以利用下標來讀取相應的內容
#read by sub
s1 = "hello World!"
print (s1[1]) # e
print (s1[0:2]) # he
print (s1[:8]) # hello Wo
print (s1[2:]) # llo World!
print (s1[-100:2]) # he
print (s1[-100:100]) # hello World!
print (s1[-8:-2]) # o Worl
print (s1[-1]) # !
注意的是,python中的string有負數下標,範圍爲-1到-n(實際上可以看成是[-1+len(s)]),-1是最後一個字符,-n是第一個。另外,[x:y]表示的是從第x位到第y-1位之間的內容。如果x小於最小下標-n,則它默認就從第一個字符開始,如果y大於字符串最大下標n-1,則它默認到最後一個字符。
6)python中的string一旦賦值,不能更改其內容;
#string is immutable
str = "I'm immutable."
str[5] = 'f'
print (str)
如果執行以上操作,python會報出'str' object does not support item assignment 的錯誤
7)in,not in
可以用in和not in來判斷子字符串是否在某字符串中,返回值是bool型變量True和False;如
s1 = "what a nice day!"
print ("hat" in s1) # True
print ("bat" in s1) # False
8)字符串比較
可以通過比較操作符(==,!=,>,>=,<,<=)來比較兩個字符串的大小(字典序),如:
print ("black"=="black") # True
print ("black"!="white") # True
print ("black">"white") # False
print ("black"<="white") # True
9)字符串的遍歷
有兩種字符串遍歷方式:
不帶下標的遍歷方式: for ch in s: print(ch)
帶下標的遍歷方式:for i in range(0,len(s),1): print(s[i])
10)isalnum()等內置函數
isalnum():如果字符串是由字母和數字組成的,則返回True,否則返回False;
isalpha():如果字符串是有字母組成的,則返回True,否則返回False;
isdigit():如果字符串全由數字組成的,則返回True,否則返回False;
isidentifier():如果字符串是python的關鍵字,則返回True,否則返回False;
islower():如果字符串全是小寫,返回True,否則返回False;
isupper():如果字符串全是大寫,則返回True,否則返回False;
isspace():如果字符串全由whitespace字符組成,則返回True,否則返回False;
舉個例子:
print ("welcome123".isalnum()) # True
print ("welcome&".isalpha()) # False
print ("wel to py".isalnum()) # False
print ("wel to py".isalpha()) # False
print ("20140722".isdigit()) # True
print ("print".isidentifier()) # True
print ("WELCOME".islower()) # False
print ("WELCOME".isupper()) # True
print ("\t\n".isspace()) # True
11)查找子串函數
endswith(s1):如果字符串以s1結尾,則返回True,否則返回False;
startwith(s1):如果字符串以s1開始,則返回True,否則返回False;
find(s1):如果字符串中包含s1,則返回字符串中最先出現的s1的起始下標,否則不包含,返回-1;
rfind(s1):如果字符串中包含s1,則返回字符串中最後出現的s1的起始下標,否則不包含,返回-1;
count(substring):返回字符串中包含substring的個數;
舉個例子:
s = "welcome to python"
print (s.endswith("hon")) # True
print (s.startswith("wel")) # True
print (s.find("you")) # -1
print (s.rfind("o")) # 15
print (s.count("o")) # 3
12)字符串轉換內置函數
capitalize():返回一個字符串,只有首字符大寫;
lower():返回一個字符串,所有字符小寫;
upper():返回一個字符串,所有字符大寫;
title():返回一個字符串,每個單詞的首字符大寫;
swapcase():返回一個字符串,大寫變小寫,小寫變大寫;
replace(old,new):返回一個字符串,所有old全部轉換爲new表示;
舉個例子:
s = "Welcome to Python, you Will enJoy IT!"
print (s.capitalize()) # Welcome to python, you will enjoy it!
print (s.lower()) # welcome to python, you will enjoy it!
print (s.upper()) # WELCOME TO PYTHON, YOU WILL ENJOY IT!
print (s.title()) # Welcome To Python, You Will Enjoy It!
print (s.swapcase()) # wELCOME TO pYTHON, YOU wILL ENjOY it!
print (s.replace("o","x")) # Welcxme tx Pythxn, yxu Will enJxy IT!
值得注意的是,因爲字符串是不可改變的,所以這些函數操作後的結果都是生成一個新的字符串。
13)strip內置函數
lstrip():返回一個字符串,刪除字符串首部的whitespace字符(包括' ', \t, \f, \r, \n);
rstrip():返回一個字符串,刪除字符串尾部的whitespace字符;
strip():返回一個字符串,刪除字符串首尾的whitespace字符;
14)格式化字符串
center(width):居中對齊,寬度是width,返回一個新字符串;
ljust(width):左對齊,寬度是width,返回一個新字符串;
rjust(width):右對齊,寬度是width,返回一個新字符串;
舉個例子:
s = "welcome"
print (s.center(13)) # ' welcome '
print (s.ljust(13)) # 'welcome '
print (s.rjust(13)) # ' welcome'
3. ASCII Code 和 Unicode code
ASCII Code 全稱是 American Standard Code for Information Interchange,它共表示128個字符(不加擴展ASCII碼),python默認的編碼方式就是ASCII碼,所以程序中有中文時,系統會將其轉換成ASCII碼錶示,很可能會產生亂碼或報錯。
Unicode code 是建立在全球各種語言基礎上的,通常表示爲\uxxxx,涵蓋了中文表示。python也支持unicode編碼,比如可以在程序中顯式的用unicode字符。通常會在文件頭上加#coding=utf-8 (注:utf-8是unicode的變種)。
python中有兩個函數可以轉換ascii字符和數字。
ord(ch) :將ascii字符ch轉換成對應的數字;
chr(num):將數字轉換成對應的ascii字符;