This is the 20th day of my participation in the Genwen Challenge

Examples of 41

The title

Mimic the use of static variables.

Analysis of the

A static variable is a variable whose scope is the entire program.

code

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @version : 1.0
# @Time : 2021/4/10 13:24
# @Author : cunyu
# @Email : [email protected]
# @Site : https://cunyu1943.site
# official account: Village Yuyao
# @File : 41.py
# @Software: PyCharm
# @desc: Exercise example 41

class Demo:
    static_value = 10

    def addStatic(self) :
        self.static_value += 1
        print(self.static_value)


if __name__ == '__main__':
    demo = Demo()
    for i in range(5):
        demo.addStatic()
Copy the code

The results of

Examples of 42

The title

Learn how to use auto to define variables.

Analysis of the

The auto keyword does not exist in Python, so use variable scope as an example.

code

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @version : 1.0
# @Time : 2021/4/10 13:28
# @Author : cunyu
# @Email : [email protected]
# @Site : https://cunyu1943.site
# official account: Village Yuyao
# @File : 42.py
# @Software: PyCharm
# @desc: Exercise example 42


def autofunc() :
    num = 1
    print('Method inside %d' % num)
    num += 1


if __name__ == '__main__':
    num = 10
    for i in range(3) :print('num = %d' % num)
        num += 1
        autofunc()

Copy the code

The results of

Examples of 43

The title

Another example of mimicking static variables.

Analysis of the

Similar to example 41.

code

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @version : 1.0
# @Time : 2021/4/10 13:34
# @Author : cunyu
# @Email : [email protected]
# @Site : https://cunyu1943.site
# official account: Village Yuyao
# @File : 43.py
# @Software: PyCharm
# @desc: Exercise example 43

class Num:
    nNum = 1

    def inc(self) :
        self.nNum += 1
        print('nNum = %d' % self.nNum)


if __name__ == '__main__':
    nNum = 2
    inst = Num()
    for i in range(3):
        nNum += 1
        print('The num = %d' % nNum)
        inst.inc()

Copy the code

The results of

Examples of 44

The title

Add two matrices with 3 rows and 3 columns, and return a new matrix:

,7,3 X = [[12], [4, 5, 6], [7, 8, 9]] Y = [,8,1 [5], [6,7,3], [4, 9]]Copy the code

Analysis of the

All you need to do is add the elements in the corresponding positions of the matrix and store them in the new matrix.

code

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @version : 1.0
# @Time : 2021/4/10 20:35
# @Author : cunyu
# @Email : [email protected]
# @Site : https://cunyu1943.site
# official account: Village Yuyao
# @File : 44.py
# @Software: PyCharm
# @desc: Exercise example 44

if __name__ == '__main__':
    X = [[12.7.3],
         [4.5.6],
         [7.8.9]]

    Y = [[5.8.1],
         [6.7.3],
         [4.5.9]]

    result = [[0.0.0],
              [0.0.0],
              [0.0.0]]

    for i in range(len(X)):
        for j in range(len(X[0])):
            result[i][j] = X[i][j] + Y[i][j]

    print(result)

Copy the code

The results of

Examples of 45

The title

Count the sum from 1 to 100.

Analysis of the

Is a cumulative process, using the cycle can be achieved.

code

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @version : 1.0
# @Time : 2021/4/10 20:39
# @Author : cunyu
# @Email : [email protected]
# @Site : https://cunyu1943.site
# official account: Village Yuyao
# @File : 45.py
# @Software: PyCharm
# @desc: Exercise example 45

if __name__ == '__main__':
    sum = 0
    for i in range(1.101) :sum += i
    print("Total: %d" % sum)

Copy the code

The results of