Python practice example (15)

Posted by Northern Flame on Sun, 01 Dec 2019 14:37:16 +0100

85. Input an odd number, and then judge that the result of at least 9 divided by the number is an integer.

Program analysis: 999999 / 13 = 76923.

#!/usr/bin/python
#coding=utf-8

if __name__ == '__main__':
    zi = int(input('Enter a number:\n'))
    n1 = 1
    c9 = 1
    m9 = 9
    sum = 9
    while n1 != 0:
        if sum % zi == 0:
            n1 = 0
        else:
            m9 *= 10
            sum += m9
            c9 += 1
    print('%d Nine can be%d To be divisible by:%d' % (c9, zi, sum))
    r = sum / zi
    print('%d / %d = %d' % (sum, zi, r))

 

86, two string connector.

#!/usr/bin/python
#coding=utf-8

if __name__ == '__main__':
    a = 'Py'
    b = 'thon'
    c = a + b
    print(c)

 

87. Answer results (structure variable transfer).

#!/usr/bin/python
#coding=utf-8

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)

 

88. Read 7 integer values (1-50). For each value read, the program prints * of the number of values.

#!/usr/bin/python
#coding=utf-8

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

 

89. A company uses the public telephone to transmit data. The data is a four digit integer, which is encrypted during the transmission process. The encryption rules are as follows: add 5 to each number, then replace the number with the remainder of sum divided by 10, and then exchange the first and fourth bits, and the second and third bits.

 

#!/usr/bin/python
#coding=utf-8

from sys import stdout
if __name__ == '__main__':
    a = int(input('Enter four numbers:\n'))
    aa = []
    aa.append(int(a % 10))
    aa.append(int(a % 100 / 10))
    aa.append(int(a % 1000 / 100))
    aa.append(int(a / 1000))

    for i in range(4):
        aa[i] += 5
        aa[i] %= 10
    for i in range(2):
        aa[i], aa[3 - i] = aa[3 - i], aa[i]
    for i in range(3, -1, -1):
        stdout.write(str(aa[i]))

 

90. List use instance.

#!/usr/bin/python
#coding=utf-8

testList = [10086, 'China Mobile', [1, 2, 4, 5]]
#List length
print(len(testList))
#To end of list
print(testList[1:])
#Add elements to list
testList.append('I\'m new here!')
#Last element of pop-up list
print(testList.pop(1))

matrix = [[1, 2, 3],  
[4, 5, 6],  
[7, 8, 9]]
col2 = [row[1] for row in matrix]
print(col2)
col2even = [row[1] for row in matrix if row[1] % 2 == 0]
print(col2even)

 

 

 

 

reference material:

Python 100 cases

Topics: Python Mobile