Python Learning Notes

Source: Internet
Author: User
Tags variable scope

1. There is no switch statement and can only be implemented with if and elif.

2.while ... else statement

3. Examples of other languages that cannot be written like this:

#-*-Coding:utf-8-*-
Fruits = [' banana ', ' apple ', ' Mango ']
For index in range (len (fruits)):
print ' Current fruit: ', fruits[index]
Print "Good bye!"

Range returns the number of a sequence.

4.for ... else statement

5.pass does not do anything, generally used as a placeholder statement. (I don't know what the birds are for?) )

The 6.dir () function returns a list of the types of variables, methods, and definitions in the current scope without arguments, and returns the properties, method list, and parameters of the parameter. (Very useful debugging function)

7.python also has math, but it needs to be first introduced before use
Import Math

8. Common random functions: random (), Shuffle (LST)

9. Three quotation marks allow a string to span multiple lines. PHP is <<<eof EOF;
hi = "HI"
There ""

10. Commonly used String functions
String.isnumeric ()
String.Replace (str1, str2, Num=string.count (STR1))
String.find (str, beg=0, End=len (String))-PHP is Strpos
String.strip ([obj])-PHP is Trim
String.Lower ()-PHP is Strtolower
String.upper ()-PHP is Strtoupper

11. List, equivalent to array, tuple equivalent to read-only array

12. A dictionary, equivalent to a JSON object.

13. Date and Time
Import time
Time.time ()-Timestamp
Time.localtime (Time.time ())-Detailed time
Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ())-formatted time

14. Calendar, need to learn more about

15. Can change (mutable) and non-change (immutable) objects

In Python, strings, tuples, and numbers are objects that cannot be changed, and List,dict are objects that can be modified.

Immutable type: Variable assignment a=5 and then assign value a=10, here is actually reborn into an int value object 10, then a point to it, and 5 is discarded, not change the value of a, the equivalent of a new student into a.

Variable type: variable assignment la=[1,2,3,4] After assigning a value la[2]=5 is to change the value of the third element of List LA, itself LA is not moving, but its internal part of the value has been modified.

Parameter passing of the Python function:

Immutable types: Values such as C + + are passed, such as integers, strings, and tuples. such as fun (a), passing only a value, does not affect the A object itself. For example, in Fun (a) to modify the value of a, just modify another copy of the object, does not affect the a itself.

mutable types: References such as C + + are passed, such as lists, dictionaries. such as Fun (LA), is the real transfer of LA, the modified fun outside of LA will also be affected

16. The default parameter is no different from other languages.

17. Indefinite length parameter. PHP is a later version of this.
def functionname ([Formal_args,] *var_args_tuple)

18. Anonymous function, very simple, only one line. Lambda [arg1 [, Arg2,..... argn]]:expression

19. Variable scope: Same as Javascript.

20.import into the lead module.
From import: Take the mineral water out of the car and give it to me.
Import: Give me the car.

21. The package is a folder, but the folder must exist under the __init__.py file, the contents of the file can be empty. __int__.py is used to identify the current folder as a package.

22. File operation
fo = open ("Foo.txt", "W")
Print "File name:", Fo.name
Print "is closed:", fo.closed
Print "Access mode:", Fo.mode
Print "whether to force a space at the end:", Fo.softspace

23. Object-Oriented

Constructor for class: __init__

Self represents an instance of a class, and self is necessary when defining a method of a class, although you do not have to pass in the appropriate parameters when calling.

The instantiation class is typically used in other programming languages with the keyword new, but in Python there is no such keyword, and the instantiation of the class is similar to how the function is called.

destructor __del__, __del__ is called when the object is destroyed, and the __del__ method runs when the object is no longer being used

__private_attrs: Two underscores begin with, declaring that the property is private and cannot be used outside of the class or accessed directly.

_foo: A variable of type protected that begins with a single underscore, that is, the protection type can only allow access to itself and subclasses, not to the From module import *

24. Regular Expressions
Re.match (Pattern, string, flags=0)
Re.search (Pattern, string, flags=0)

25.global is the same as PHP.

26. Functions can return multiple values, which is much more convenient than PHP. For example the following:
Import Math

def move (x, y, step, angle=0):
NX = x + step * Math.Cos (angle)
NY = y-step * Math.sin (angle)
Return NX, NY

X, y = Move (+, +, MATH.PI/6)

27. Variable Parameters
Def calc (*numbers):
sum = 0
For N in numbers:
sum = SUM + N * n
return sum

28. Under window, generate EXE with PYINSTALLER-F tkint.py, then set this EXE to run in Group Policy.
Under Linux, generate PYC with python-m py_compile file.py, so long as you protect your code.


Python Learning Notes

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.