Python initial learning necessary to master knowledge points (5-minute speed learning)

Source: Internet
Author: User
Tags access properties class definition

1. String
python中单引号和双引号使用完全相同。使用三引号(‘‘‘或""")可以指定一个多行字符串。转义符 ‘\‘反斜杠可以用来转义,使用r可以让反斜杠不发生转义。。 如 r"this is a line with \n" 则\n会显示,并不是换行。按字面意义级联字符串,如"this " "is " "string"会被自动转换为this is string。字符串可以用 + 运算符连接在一起,用 * 运算符重复。Python 中的字符串有两种索引方式,从左往右以 0 开始,从右往左以 -1 开始。Python中的字符串不能改变。Python 没有单独的字符类型,一个字符就是长度为 1 的字符串。字符串的截取的语法格式如下:变量[头下标:尾下标]
#!/usr/bin/python3str=‘Runoob‘print(str)                 # 输出字符串print(str[0:-1])           # 输出第一个到倒数第二个的所有字符print(str[0])              # 输出字符串第一个字符print(str[2:5])            # 输出从第三个开始到第五个的字符print(str[2:])             # 输出从第三个开始的后的所有字符print(str * 2)             # 输出字符串两次print(str + ‘你好‘)        # 连接字符串print(‘------------------------------‘)print(‘hello\nrunoob‘)      # 使用反斜杠(\)+n转义特殊字符print(r‘hello\nrunoob‘)     # 在字符串前面添加一个 r,表示原始字符串,不会发生转义#打印结果如下RunoobRunooRnoonoobRunoobRunoobRunoob你好------------------------------hellorunoobhello\nrunoob

I share a group of my: 725479218, the group has to provide learning address, there is want to learn to study together, but also provide technical exchange

2, line change/No line break
print 默认输出是换行的,如果要实现不换行需要在变量末尾加上 end="":
s = "lamxqx"print(s[1:3],end="")print(s*2)结果:amlamxqxlamxqx------------------------------------------s = "lamxqx"print(s[1:3])print(s*2)结果:amlamxqxlamxqx
3. Type of Judgment
lass A:    passclass B(A):    passisinstance(A(), A)  # returns Truetype(A()) == A      # returns Trueisinstance(B(), A)    # returns Truetype(B()) == A        # returns False--------------------------------------------区别:type()不会认为子类是一种父类类型。isinstance()会认为子类是一种父类类型
Object oriented
Example--------------------------------------------------------------#!/usr/bin/python#-*-coding:utf-8-*-class      Employee: ' base class for all employees ' 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 displayemploy  EE (self): print "Name:", Self.name, ", Salary:", self.salary "create first object of Employee class" EMP1 = Employee ("Zara", 2000) "Create The second object of the employee class "EMP2 = Employee (" Manni "," Emp1.displayemployee ") Emp2.displayemployee () print" Total Employee%d "% Employee.empcount-------------------------------------------------------Knowledge Points: 1, empcount variable is a class variable, Its value is shared across all instances of this class. You can use Employee.empcount access in internal classes or outside classes. 2, the first method __init__ () method is a special method, called the class constructor or initialization method, when the instance of the class is created, it calls the method 3, the self represents the instance of the class, and self is necessary to define the methods of the class, although it is not necessary to pass in the corresponding parameters when calling. Execute the above code output as follows: Name:zara, Salary:2000name:manni, Salary:5000total Employee 2 You can add, delete, modify the properties of the class as follows: Emp1.age = 7 # Tim Add an ' age ' attribute Emp1.age = 8 # Modify ' Age ' property del emp1.age # delete ' age ' property you can also access properties using the following functions: GetAttr (obj, name[, default]): Accesses the properties of the object. Hasattr (obj,name): Checks if a property exists. SetAttr (Obj,name,value): Sets a property. If the property does not exist, a new property is created. Delattr (obj, name): Deletes the attribute. Hasattr (EMP1, ' age ') # returns True if the ' age ' property exists. GetAttr (EMP1, ' age ') # returns the value of ' age ' property setattr (EMP1, ' age ', 8) # Add attribute ' age ' value to 8delattr (EMP1, ' age ') # Delete attribute ' age '------ ----------------------------------------------------python built-in class properties __dict__: A class's properties (containing a dictionary, consisting of the data properties of the Class) __doc__: The document string for the class _ _NAME__: Class name __module__: The module where the class definition resides (the full name of the class is ' __main__.classname ' if the class is in an import module mymod, then classname.__module__ equals Mymod) __ Bases__: All of the parent classes of the class comprise an element (containing a tuple of all the parent classes) the Python built-in class property invocation instance is as follows: instance #!/usr/bin/python#-*-coding:utf-8-*-class Employee: ' Base class for all employees ' 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.salaryprint" employee.__doc__: ", Employee.__doc__print" employee.__name__: ", Employee.__name__print" employee.__module__: ", Employee.__module__print" employee.__bases__: ", Employee.__bases__print" Employee.__dict__: ", employee.__dict__ executes the above code output as follows: employee.__doc__: base class for all employees employee.__name__: employeeemployee.__module__: __main__ Employee.__bases__: () employee.__dict__: {' __module__ ': ' __main__ ', ' displaycount ': <function displayCount at 0x10a939c80>, ' Empcount ': 0, ' displayemployee ': <function displayemployee at 0x10a93caa0>, ' __doc__ ': ' \xe6\ X89\x80\xe6\x9c\x89\xe5\x91\x98\xe5\xb7\xa5\xe7\x9a\x84\xe5\x9f\xba\xe7\xb1\xbb ', ' __init__ ': <function __init __ at 0x10a939578>}

Python initial learning necessary to master knowledge points (5 minutes fast learning)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.