example_081.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
' 求未知数 '
__author__ = 'Kevin Gong'
a = 809
for i in range(10, 100):
b = i * a
if b >= 1000 and b <= 10000 and 8 * i < 100 and 9 * i >= 100:
print(b, '= 800 * ', i, ' + 9 * ',i)
for i in range(10, 100):
if 8 * i > 99 or 9 * i < 100:
continue
if 809 * i == 800 * i + 9 * i:
print(i)
break
example_082.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
' 八进制转换为十进制 '
__author__ = 'Kevin Gong'
n = eval('0o' + str(int(input('输入一个八进制的数:'))))
print(n)
example_083.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
' 求 0—7 所能组成的奇数个数 '
__author__ = 'Kevin Gong'
if __name__ == '__main__':
sum = 4
s = 4
for i in range(2, 9):
print(sum)
if i <= 2:
s *= 7
else:
s *= 8
sum += s
print('sum = %d' % sum)
example_084.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
' 连接字符串 '
__author__ = 'Kevin Gong'
delimiter = ','
mylist = ['Brazil', 'Russia', 'India', 'China']
print(delimiter.join(mylist))
example_085.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
' 输入一个奇数,然后判断最少几个 9 除于该数的结果为整数。 '
__author__ = 'Kevin Gong'
if __name__ == '__main__':
n = int(input('请输入一个奇数:'))
i = 1
c9 = 1
m9 = 9
sum = 9
while i != 0:
if sum % n == 0:
i = 0
else:
m9 *= 10
sum += m9
c9 += 1
print('%d 个 9 可以被 %d 整除:%d' % (c9, n, sum))
r = sum / n
print('%d / %d = %d' % (sum, n, r))
example_086.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
' 两个字符串连接程序 '
__author__ = 'Kevin Gong'
a = 'guangtou'
b = 'feipang'
print(b + a)
example_087.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
' 结构体变量传递 '
__author__ = 'Kevin Gong'
if __name__ == '__main__':
class student:
x = 0
c = 0
def f(stu):
stu.x = 20
stu.c = 'c'
a = student()
a.x = 3
a.c = 'a'
f(a)
print(a.x, a.c)
example_088.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
' 读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。 '
__author__ = 'Kevin Gong'
if __name__ == '__main__':
n = 1
while n <= 7:
a = int(input('input a number:\n'))
while a < 1 or a > 50:
a = int(input('input a number:\n'))
print(a * '*')
n += 1
example_089.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
' 采用公用电话传递数据 '
__author__ = 'Kevin Gong'
from sys import stdout
if __name__ == '__main__':
a = int(input('输入四个数字:\n'))
raw = []
raw.append(a % 10)
raw.append(a % 100 / 10)
raw.append(a % 1000 / 100)
raw.append(a / 1000)
for i in range(4):
raw[i] += 5
raw[i] %= 10
for i in range(2):
raw[i],raw[3 - i] = raw[3 -i],raw[i]
for i in range(3, -1, -1):
stdout.write(str(raw[i]))
example_090.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
' 列表的使用 '
__author__ = 'Kevin Gong'
testList = [10086, '中国移动', [1, 2, 4, 5]]
# 访问列表长度
print(len(testList))
# 到列表结尾
print(testList[1:])
# 向列表添加元素
testList.append('i\'m new here!')
print (len(testList))
print (testList[-1])
# 弹出列表的最后一个元素
print (testList.pop(1))
print (len(testList))
print (testList)
matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
print (matrix)
print (matrix[1])
col2 = [row[1] for row in matrix] # get a column from a matrix
print (col2)
col2even = [row[1] for row in matrix if row[1] % 2 == 0] # filter odd item
print (col2even)
网友评论