Python学习笔记(二)

勘误:

缩进和换行的问题,因为我刚刚接触,理解得太肤浅,在上一篇笔记中写错了。

def结束要换行,if转elif和else不用换行,for循环结束要换行。

路曼曼其修远兮,吾将上下而求索。

Functions:

def printArea(width = 1, height = 2):

>>>printArea()

>>>printArea(4,2.5)

>>>printArea(height = 5, width = 3)

>>>printArea(width = 1.2)

>>>printArea(height = 6.2)

width.    height.

1.              2.

4.              2.5

3.              5

1.2.           2

1.              6.2


str

s1 = str() 等价于s1 = "",定义一个空字符串。

s[-1] == s[-1 + len(s)]

s1 = "Welcome"

s2 = s1 * 3 # s2 = "WelcomeWelcomeWelcome"


>>>"come" in s1

True

>>>"come" not in s1

False


s = "welcome to python"

>>>s.isalnum()

False

# isalnum() returns True if s is alphanumeric, 文数字,只含大写字母、小写字母或数字。

>>>"welcome".isalpha()

True

# isalpha() returns True if s is a alphabetic string

>>>"2012".isdigit()

True

>>>"first".isidentifier()

False

# isidentifier() returns True if s is a Python identifier.

>>>s.islower()

>>>s.isupper()

>>>s.isspace()


>>>s.endswith("thon")

True

>>>s.startswith("good")

False

>>>s.find("become")

-1

>>>s.rfind("o")

17 # returns the highest index where "o" starts in the string s, or -1 if "o" is not found in s.

>>>s.count("o")

3


>>>s1=s.capitalize()

>>>s1

Welcome to python

>>>s2=s.upper()

>>>s2

WELCOME TO PYTHON

>>>s3=s.lower()

>>>s3

welcome to python

>>>s4=s.title()

>>>s4

Welcome To Python

>>>s5=s.swapcase()

>>>s5

wELCOME tO pYTHON

>>>s6=s.replace("pYTHON","HEAVEN")

>>>s6

wELCOME tO HEAVEN

>>>s

welcome to python


>>>s="   welcome to python\t"

>>>s1=s.lstrip()

'welcome to python\t'

>>>s2=s.rstrip()

'   welcome to python'

>>>s3=s.strip()

'welcome to python'


>>>s="welcome"

>>>s1=s.center(11)

'  welcome  '

#center(11) places s in the center of a string with 11 characters

>>>s2=s.ljust(11)

'welcome    '

>>>s3=s.rjust(11)

'    welcome'


>>>r1=Rational.Rational(4,2)

>>>r1

2

>>>r2=Rational.Rational(3,2)

>>>r2

3/2


你可能感兴趣的:(Python学习笔记(二))