Python-学习4

Python-functions

others

.upper()–capital the letters

.lower()–lower the letters

.islower()–check whether all the letters are lower

.isupper()–check whether all the letters are capital

.isalpha()–check wether all are string(includes letters and Chinese)

.isalnum()–check whether all are string or numbers

.startswith()–check whether the string starts with the context in the ()

.endswith()–check whether the string ends with the context in the ()

‘, ‘.join()–join the list with’,’

.split()–split to form the list with ‘ ‘

1
2
'MyabcnameabcisabcSimon'.split('abc')
#out:['My', 'name', 'is', 'Simon']

.rjust(10)–add 10 spaces to the left of the string

.ljust(10)–add 10 spaces to the right of the string

.rjust(10,’*’)–add 10 stars to the left of the string

.center(20,’*’)–put the string in the middle of 20 stars

.strip(‘Spam’)–skip the Spam in the front and in the end

end:

1
2
3
print('Hello ',end='')
print('World')
#out:Hello World

sep:

1
2
print('cats','dogs','mice',sep=",")
#out:cats,dogs,mice

print() will return ‘none’ value:

1
2
3
4
5
spam = print('Hello!')
#out:Hello!

None == spam
#out:True

def

1
2
3
4
5
6
def hello(name): 
print('Hello ' + name)
hello('Alice')
hello('Bob')
#out:Hello Alice
# Hello Bob

return

It will print a random answer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import random
def getAnswer(answerNumber):
if answerNumber == 1:
return 'It is certain'
elif answerNumber == 2:
return 'It is decidedly so'
elif answerNumber == 3:
return 'Yes'
elif answerNumber == 4:
return 'Reply hazy try again'
elif answerNumber == 5:
return 'Ask again later'
elif answerNumber == 6:
return 'Concentrate and ask again'
elif answerNumber == 7:
return 'My reply is no'
elif answerNumber == 8:
return 'Outlook not so good'
elif answerNumber == 9:
return 'Very doubtful'
r = random.randint(1, 9)
fortune = getAnswer(r)
print(fortune)

partial and overall

  1. Variables in one part can’t be used in overall situation.
1
2
3
4
def spam():
eggs = 31337
spam()
print(eggs)
1
2
3
4
5
6
7
NameError                                 Traceback (most recent call last)
<ipython-input-9-4fa03afe709c> in <module>
2 eggs=31337
3
----> 4 print(eggs)

NameError: name 'eggs' is not defined

This is because the ‘eggs’ is only in the spam but not the overall situation.

  1. Variables in one part can’t be used in another part.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
def spam():
eggs=99
bacon()
eggs=bacon()
print(eggs)
def bacon():
ham=101
eggs=0
print('1')
return eggs
spam()
#out:1
# 1
# 0
  1. Variables in overall situation can be used in one part.
1
2
3
4
5
6
7
def spam():
print(eggs)
eggs=42
spam()
print(eggs)
#out:42
# 42
  1. One part can be used in another part, and their name can be the same.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def spam():
eggs='spam local'
print(eggs)#prints 'spam local'
def bacon():
eggs='bacon local'
print(eggs)#print 'bacon local'
spam()
print(eggs)#print 'bacon local'
eggs='global'
bacon()
print(eggs)#prints 'global'
#out:bacon local
# spam local
# bacon local
# global

try

try, if the condition can’t be satisfied, move on to the except part:

1
2
3
4
5
6
7
8
9
10
def spam(divideBy):
try:
return 42 / divideBy
except ZeroDivisionError:
print('Error :Invalid argument.')

print(spam(2))
print(spam(12))
print(spam(0))
print(spam(1))

out:

21.0
3.5
Error :Invalid argument.
None
42.0

guess the number:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#This is a guess the numer game
import random
secretNumber=random.randint(1,20)
print('I am thinking of a number between 1 and 20.')
#Ask the player to guess 6 times
for guessesTaken in range(1,7):
print('Take a guess!')
guess=int(input())
if guess<secretNumber:
print('Your guess is too low.')
elif guess>secretNumber:
print('Your number is too high.')
else:
break
if guess==secretNumber:
print('Good job!You guessed my number in '+str(guessesTaken)+' guesses!')
else:
print('Nope. The number I was thinking of was '+str(secretNumber)+'')

out:

I am thinking of a number between 1 and 20.
Take a guess!
1
Your guess is too low.
Take a guess!
4
Your guess is too low.
Take a guess!
6
Your guess is too low.
Take a guess!
9
Your guess is too low.
Take a guess!
15
Good job!You guessed my number in 5 guesses!