感觉答案太粗暴了,所以我‘化简为繁’写了下
t = ('a', 'b', ['A', 'B'])
t = list(t) #t = ['a', 'b', ['A', 'B']]
l = t[2] #l = ['A', 'B']
t.pop(2) #t = ['a', 'b']
l = tuple(l) #l = ('A', 'B')
t.append(l) #t = ['a', 'b',('A', 'B')]
t = tuple(t) #t = ('a', 'b',('A', 'B'))
print t
t = ('a', 'b', ['A', 'B'])
t = list(t) #t = ['a', 'b', ['A', 'B']]
l = t[2] #l = ['A', 'B']
t.pop(2) #t = ['a', 'b']
l = tuple(l) #l = ('A', 'B')
t.append(l) #t = ['a', 'b',('A', 'B')]
t = tuple(t) #t = ('a', 'b',('A', 'B'))
print t
2017-02-03
最新回答 / 清风慢摇
首先,这不是C语言,不能写成if (x%2) == 1这样,应该是if x%2 == 1:这样,还有,你的循环应该是while x<=100,照你那样做,只能循环到99,加不到100的位置。最后,再提一个小小的意见,以后写代码,最好sum = 0.0,写成这种(建议),应为这样方便后面对sum的操作!
2017-02-03
s = set(['Adam', 'Lisa', 'Bart', 'Paul'])
print 'adam' in [x.lower() for x in s]
print 'bart' in [x.lower() for x in s]
print 'adam' in [x.lower() for x in s]
print 'bart' in [x.lower() for x in s]
2017-02-03
print r'''"To be, or not to be": that is the question.
Whether it's nobler in the mind to suffer.'''
Whether it's nobler in the mind to suffer.'''
2017-02-03
s = 'Python was started in 1989 by \"Guido\".\n Python is free and easy to learn.'
print s
print s
2017-02-03
x1 = 1
d = 3
n = 100
x100 = x1+(n-1)*d
s = (x1+x100)*n/2
print s
d = 3
n = 100
x100 = x1+(n-1)*d
s = (x1+x100)*n/2
print s
2017-02-03
print "hello, python"
print "hello", "pythom"
print "hello", "pythom"
2017-02-03
严蔚敏的数据结构里有这题的详解
# -*- coding: utf-8 -*-
#
# File Desc : recursive function in python
# Author : wallace_lai
# At : 2017-02-03 10:08
def move(n, a, b, c):
if n == 1:
print a + '-->' + c
else:
move(n - 1, a, c, b)
print a + '-->' + c
move(n - 1, b, a, c)
move(4, 'A', 'B', 'C')
# -*- coding: utf-8 -*-
#
# File Desc : recursive function in python
# Author : wallace_lai
# At : 2017-02-03 10:08
def move(n, a, b, c):
if n == 1:
print a + '-->' + c
else:
move(n - 1, a, c, b)
print a + '-->' + c
move(n - 1, b, a, c)
move(4, 'A', 'B', 'C')
2017-02-03
有一种情况是dat < 0时方程无解
import math
def quadratic_equation(a, b, c):
dat = b * b - 4 * a * c
if dat < 0:
return None
else:
x1 = ((-b) + math.sqrt(dat)) / (2 * a)
x2 = ((-b) - math.sqrt(dat)) / (2 * a)
return x1, x2
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
import math
def quadratic_equation(a, b, c):
dat = b * b - 4 * a * c
if dat < 0:
return None
else:
x1 = ((-b) + math.sqrt(dat)) / (2 * a)
x2 = ((-b) - math.sqrt(dat)) / (2 * a)
return x1, x2
print quadratic_equation(2, 3, 0)
print quadratic_equation(1, -6, 5)
2017-02-03
L = range(1, 101)
print L[-10:]
print L[-46::5]
答案有错吧,应该从 【-47】开始。 -46 开始只有9个啊...
print L[-10:]
print L[-46::5]
答案有错吧,应该从 【-47】开始。 -46 开始只有9个啊...
def firstCharUpper(s):
return s[0].upper() + s[1:]
print firstCharUpper('hello')
print firstCharUpper('sunday')
print firstCharUpper('september')
return s[0].upper() + s[1:]
print firstCharUpper('hello')
print firstCharUpper('sunday')
print firstCharUpper('september')
2017-02-02