def firstCharUpper(s):
return s[:1].upper()+s[1:]
print firstCharUpper('hello')
print firstCharUpper('sunday')
print firstCharUpper('september')
return s[:1].upper()+s[1:]
print firstCharUpper('hello')
print firstCharUpper('sunday')
print firstCharUpper('september')
2019-07-05
age = 8
if age >= 6:
print 'teenager'
elif age >= 18:
print 'adult'
else: age>=3
print 'kid'
if age >= 6:
print 'teenager'
elif age >= 18:
print 'adult'
else: age>=3
print 'kid'
2019-07-04
替换
L = ['Adam', 'Lisa', 'Bart']
L[-1]='Adam'
L[-3]='Bart'
print L
L = ['Adam', 'Lisa', 'Bart']
L[-1]='Adam'
L[-3]='Bart'
print L
2019-07-04
如果Paul同学排在最后一个,我们可以用list的pop()方法删除:
>>> L = ['Adam', 'Lisa', 'Bart', 'Paul']
>>> L.pop()
'Paul'
>>> print L
['Adam', 'Lisa', 'Bart']
pop()方法总是删掉list的最后一个元素,并且它还返回这个元素,所以我们执行 L.pop() 后,会打印出 'Paul'。
>>> L = ['Adam', 'Lisa', 'Bart', 'Paul']
>>> L.pop()
'Paul'
>>> print L
['Adam', 'Lisa', 'Bart']
pop()方法总是删掉list的最后一个元素,并且它还返回这个元素,所以我们执行 L.pop() 后,会打印出 'Paul'。
2019-07-04
import math
def quadratic_equation(a, b, c):
x1=(-b+math.sqrt(b**2-4*a*c))/(2*a)
x2=(-b-math.sqrt(b**2-4*a*c))/(2*a)
return x1,x2
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
def quadratic_equation(a, b, c):
x1=(-b+math.sqrt(b**2-4*a*c))/(2*a)
x2=(-b-math.sqrt(b**2-4*a*c))/(2*a)
return x1,x2
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
2019-07-04
def square_of_sum(L):
L1=[x**2 for x in L]
return sum(L1)
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
L1=[x**2 for x in L]
return sum(L1)
print square_of_sum([1, 2, 3, 4, 5])
print square_of_sum([-5, 0, 5, 15, 25])
2019-07-04
s = set(['Adam', 'Lisa', 'Paul'])
L = ['Adam', 'Lisa', 'Bart', 'Paul']
s=set([ ])
s.add('Bart')
print s
L = ['Adam', 'Lisa', 'Bart', 'Paul']
s=set([ ])
s.add('Bart')
print s
2019-07-04
for x in [0,1,2,3,4,5,6,7,8,9]:
for y in [0,1,2,3,4,5,6,7,8,9]:
if x<y:
print x*10 + y
for y in [0,1,2,3,4,5,6,7,8,9]:
if x<y:
print x*10 + y
2019-07-04
这一系列由上到下判断,上面判断为Tuer,后面则不在执行。
修改成:
>>> age = 8
>>> if age >= 18:
... print('peenager')
... elif age >= 6:
... print('adult')
...
adult
>>>
修改成:
>>> age = 8
>>> if age >= 18:
... print('peenager')
... elif age >= 6:
... print('adult')
...
adult
>>>
2019-07-03