Little bits and pieces about python

Although I use python every day, I don’t know much about python if I think about it carefully... The pytorch code is copied from the east and west on github and then modified by yourself. When writing other python codes, you can always Baidu Google... if this is the case It's embarrassing that I can't answer some details of python when asked during the interview! This blog is used to record everything about the language of python, which can be added at any time.

1. The difference between Python and other languages

  • Python is an interpreted language. This means that unlike C and other languages, Python does not need to be compiled before it runs.
  • Python is dynamically typed, which means you don't need to specify the type when declaring a variable. You can define x=111 first, then x=”I'm a string”.
  • Python is an object-oriented language, all allow the definition of classes and can be inherited and combined. Python does not have access identifiers such as public and private in C++, so I trust the quality of programmers very much. I believe that every programmer is an "adult"~
    Class definition in python:
class Employee:
   empCount = 0
 
   def __init__(self, name, salary):
      self.name = name
      self.salary = salary
      Employee.empCount += 1
   
   def displayCount(self):
     print "Total Employee %d" % Employee.empCount
 
   def displayEmployee(self):
      print "Name : ", self.name,  ", Salary: ", self.salary
  • In Python, functions are first-class citizens. This means that they can be assigned, return values ​​from other functions, and pass function objects. Classes are not first-class citizens. The following explains in detail the function objects in Python.

A function is also an object and has attributes. As an object, it can also be assigned to other object names, or passed as a parameter.

  • Lambda expression:
    func = lambda x,y: x + y
    print func(3,4)
  • Functions are passed as parameters:
    def test(f, a, b):
    print 'test'
    print f(a, b)
    test(func, 3, 5)
  • map() function:
    map() is a built-in function of Python. Its first parameter is a function object.
    re = map((lambda x: x+3),[1,3,5,6])
  • filter() function:
    def func(a):
    if a > 100:
    return True
    else:
    return False
    print filter(func,[10,56,101,500])
  • Python runs slower than compiled languages. Fortunately, Python allows writing programs using C extensions, so bottlenecks can be dealt with. The Numpy library is a good example, because a lot of code is not written directly in Python, so it runs very fast.

Python generates random numbers
  • Random integer
  • random.randint(a,b): returns a random integer x, a<=x<=b
  • random.randrange(start,stop,[,step]): returns a random integer in the range (start,stop,step), excluding the end value.
  • Random real number
  • random.random( ): returns a floating point number between 0 and 1
  • np.random.randn(10000): Generate 10,000 random numbers that meet the standard normal distribution; np.random.randn((2,4)): Generate a standard normal distribution random number with a matrix size of (2,4) .

Guess you like

Origin blog.csdn.net/weixin_41332009/article/details/113839541