python从入门到实践课后习题 2-1到2-11
直接贴出代码
2-1
message = 'I wanna learn Python'
print(message)
2-2
message = 'I wanna learn Python'
print(message)
message = 'I wanna write Python'
print(message)
2-3
words = 'pp, would you like to learn Python today?'
print(words)
2-4
name = "zhou qi heng"
print(name.title())
print(name.upper())
print(name.lower())
2-5
words = 'somebody says that, "To be both a speaker of words and a doer of deeds." '
print(words)
2-6
message = 'says that, "To be both a speaker of words and a doer of deeds."'
print(name, message)
2-7
name = " zhou qi heng\n"
print(name)
print(name.lstrip())
print(name.rstrip())
print(name.strip())
2-8
print(2 * 4)
print(2 ** 3)
print(int(16 / 2))
print((1 + 3) * 2)
2-9
num = 233
word = 'haha'
print(str(num) + word)
2-10
#leetcode上找了一个简单的题,二分查找
#The isBadVersion API is already defined for you.
#@param version, an integer
#@return a bool
#def isBadVersion(version):
class Solution(object):
def firstBadVersion(self, n):
start = 1
end = n
while(start < end):
mid = (start + end ) // 2
if isBadVersion(mid) == False:
start = mid + 1
else:
end = mid
return start
"""
:type n: int
:rtype: int
"""
2-11
import this