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 | 'MyabcnameabcisabcSimon'.split('abc') |
.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
print():
end:
1 | print('Hello ',end='') |
sep:
1 | print('cats','dogs','mice',sep=",") |
print() will return ‘none’ value:
1 | spam = print('Hello!') |
def
1 | def hello(name): |
return
It will print a random answer.
1 | import random |
partial and overall
- Variables in one part can’t be used in overall situation.
1 | def spam(): |
1 | NameError Traceback (most recent call last) |
This is because the ‘eggs’ is only in the spam but not the overall situation.
- Variables in one part can’t be used in another part.
1 | def spam(): |
- Variables in overall situation can be used in one part.
1 | def spam(): |
- One part can be used in another part, and their name can be the same.
1 | def spam(): |
try
try, if the condition can’t be satisfied, move on to the except part:
1 | def spam(divideBy): |
out:
21.0
3.5
Error :Invalid argument.
None
42.0
guess the number:
1 | #This is a guess the numer game |
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!