Python Basic knowledge points

Source: Internet
Author: User

Self-study record:

1. String

the single and double quotation marks in Python are used exactly the same. Use three quotation marks (" "or "") to specify a multiline string. Escape character'\ 'Backslashes can be used to escape, using R to allow backslashes to escape. such as R"This was a line with \ n"the \ n will be displayed, not newline. cascading strings by literal meaning, such as" This" " is" "string"will be automatically converted to this is string. String can be usedThe + operator joins together, using *The operator repeats. There are two ways to index a string in Python, from left to right0Start, from right to left with-1start. The string in Python cannot be changed. Python does not have a separate character type, and a character is a length of1the string. The syntax for intercepting a string is as follows: variable [head subscript: tail subscript]

#!/usr/bin/Python3 str='Runoob'print (str) # output string print (str[0:-1] # Outputs all characters from the first to the penultimate print (str[0] # output string The first character of print (str[2:5]) # outputs the character print from the third start to the fifth (str[2:]) # output All characters after the third start of print (str*2) # output String two times print (str+'Hello') # Connection string print ('------------------------------') Print ('Hello\nrunoob') # use backslash (\) +N Escape Special character print (R'Hello\nrunoob'# Add an R in front of the string to represent the original string, no escaping # printing results are as follows Runoobrunoornoonoobrunoobrunoobrunoob Hello------------------------------Hellorunoobhello\nrunoob

2, line change/No line break

The print default output is newline, and if you want to implement no newline, add end=""to the end of the variable:

" LAMXQX " print (s[1:3],end="") print (s*2) Result: Amlamxqxlamxqx ------------------------------------------"lamxqx"print (s[  1:3]) print (s*2) Result: Amlamxqxlamxqx

3. Type of Judgment

class A:    passclass  B (a):    passisinstance (A (), a)  # returns Truetype (A ()) = = a      # Returns Trueisinstance (B (), a)    # returns True= = a        # returns False----------------------- --------------------- difference:type () does not consider a subclass to be a parent class type. Isinstance () considers a subclass to be a parent class type 

--------------------------------------------------------------------------------------------------------------- ----------

Object oriented

Example--------------------------------------------------------------#!/usr/bin/python#-*-coding:utf-8-*-classEmployee:'base class for all employees'Empcount=0def __init__ (self, Name, salary): Self.name=name Self.salary=Salary Employee.empcount+=1def displaycount (self): print"Total Employee%d"%Employee.empcount def displayemployee (self): print"Name:", Self.name,", Salary:", Self.salary"to create the first object of an Employee class"EMP1= Employee ("Zara", -)"Create a second object of the Employee class"EMP2= Employee ("Manni", the) Emp1.displayemployee () Emp2.displayemployee () print"Total Employee%d"%Employee.empcount-------------------------------------------------------Knowledge Points:1, the Empcount variable is a class variable whose value is shared among all instances of the class. You can use Employee.empcount access in internal classes or outside classes. 2The first method, the __init__ () method, is a special method called the constructor or initialization method of a class that is called when an instance of the class is created.3, self represents an 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 arguments when called. Execute the above code to output the results as follows: Name:zara, Salary: -Name:manni, Salary: theTotal Employee2You can add, remove, and modify the properties of the class as follows: Emp1.age=7# Add a' Age'Property Emp1.age=8# Modify' Age'Property del emp1.age # Delete' Age'properties You can also access properties using the following functions: GetAttr (obj, name[,default]): Access 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') # If there is' Age'property returns True. GetAttr (EMP1,' Age') # Back' Age'the value of the SetAttr property (EMP1,' Age',8) # Add Property' Age'Value is8delattr (EMP1,' Age') # Delete Attribute' Age'----------------------------------------------------------python built-in class properties __dict__: A class's properties (consisting of a dictionary that consists of a class's data properties) __DOC__: The class's document string __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-*-classEmployee:'base class for all employees'Empcount=0def __init__ (self, Name, salary): Self.name=name Self.salary=Salary Employee.empcount+=1def displaycount (self): print"Total Employee%d"%Employee.empcount def displayemployee (self): print"Name:", Self.name,", Salary:", self.salary print"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 at0x10a939c80,'Empcount':0,'Displayemployee':
<function Displayemployee at0x10a93caa0,'__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__ at0x10a939578}

Python Basic knowledge points

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.