Python study note 1 (first knowledge of Python)

Source: Internet
Author: User

Soon after I learned python, my job was mainly to use. net, so many things of thinking will be related to C. Especially in terms of understanding, the general idea is to compare with C # To see what is the difference

Array and tuple
Tuple is defined as an immutable list in Python. In My intuitive understanding, it is an array (but this is very strange. A standard module in python is array, which has never been used yet, for more details, see ).
The initialization method for tuple is very strange.
Userid = 1, 2, 4, 5
Or
Userid = (1, 2, 3, 4, 5)
The results are the same. Think of this userid = 1, 2, 4, 5 C # is certainly not acceptable, but this approach is like the C # int A, B, C, D, however, this indicates defining multiple int data types.

No "{}" at the beginning and end
This symbol will appear in C # functions, class definitions... and so on. Python is replaced by indentation and colons.CodeBlock. If you write Python too much, you will find that this is indeed a perfect thing. This is because the code specification of C # emphasizes indentation. Now I have removed all {} and missed many keyboards.

Default function parameters
This is also a tough task. Because there are many differences with C #.
1: Python functions can define default values. This is like the stored procedure of MSSQL.
2: keyword parameters can also be used for python functions.
Def get (name, ID, age)
Pass
Get (name = '000000', id = 1, age = 333)

Reference the statement in concise Python
"There are two advantages to doing this-first, because we don't have to worry about the order of parameters, it is easier to use functions. 2. If other parameters have default values, we can only assign values to those parameters we want .", This is indeed much more convenient than C.

Variable parameters of a function

Python:
Def fprintf (* ARGs ):
For I in ARGs:
Print I
Fprintf (1, 2, 3, 4, "ssss ")

This is defined in C #.
Public static void usevariableparameters (Params int [] list) {for (INT I = 0; I <list. length; I ++) {console. writeline (list [I]);} console. writeline ();}

However, Python also supports list, tuple, and dictionary as parameters. The parameters of list and tuple are passed in with * Arg. Dictinary is passed in as ** Arg.

>>> ARGs = [3, 6]
>>> Range (* ARGs)

>>> Def parrot (voltage, state = 'a stiff ', Action = 'voom '):
>>> D = {"voltage": "four million", "State": "bleedin 'demised", "action": "voom "}
>>> Parrot (** D)

The Python syntax is too flexible.

Loop in the for in Form
This is also very interesting. I found that in Python, The for (int I; I <cout; I ++) format is no longer available. Using the For I in range (10) method is indeed an improvement. In addition, C # also simulates this implementation through the LINQ method.

Filter category syntax
Cool

[Mapping-expression for element in source-list if filter-expression]

>>> Li = ["A", "mpilgrim", "foo", "B", "C", "B", "D", "D"]
>>> [ELEM for ELEM in Li if Len (ELEM)> 1]

C #'s LINQ can also implement this function now, but python is cool and simple.

Special Properties of and or
In python, "and" and "or" return results instead of "bool.
And if it is false, return the value on the left and if it is true, return the value on the right.
Or returns the first true value. If both are false, the last value is returned.

But there is a good correlation that C # does not have, which is equivalent to a binary operator.
In C #, the ternary operators and
String name;
Name = string. isnullorempry (name )? String. Empty: "username is:" + name

If C # supports the concept of Python, you can simply write it out.
Name = Name and "username is:" + name
C # is expected to support similar syntaxes.

Slice and copy of List objects
More specifically, I just think it is better than the list method of C #. In particular, I hope that. NET will support this syntax in the future.

Shoplist [: 5] returns the value in the return range of a sequence.
Shoplist [:] returns the copy of the entire sequence

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.