Python Learning Note one

Source: Internet
Author: User

http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000

1. Input and OUTPUT statements

Print statement statements can also follow multiple strings, separated by commas ",", each string is printed sequentially, and a comma "," will output a space. Try to compare the following two words:

Print ' 100+200= '100+200  '<stdin>', line 1    print'  100+200='100+200                      ^syntaxerror:invalid syntaxprint'  100+200=', 100+200100+200= 300
python Print statement

If there are a lot of characters in the string that need to be escaped, you need to add a lot of, \ in order to simplify, Python also allows to use r‘‘ ‘‘ A string that represents the internal default does not escape

If there is a lot of line wrapping inside the string, it is not good to read in a single line, and in \n order to simplify, Python allows ‘‘‘...‘‘‘ the format to represent multiple lines of content

The basic input in Python can be used raw_input.

>>> name=raw_input (")

Logical operations in the 2,python

The logical operations in Python are implemented directly with and, or, not; &,| is used for set operations

3.python Coding Issues

Http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ 001386819196283586a37629844456ca7e5a7faa9b94ee8000

In many programming languages, there are many kinds of interception functions available for strings, in fact, the purpose is to slice the strings. Python does not have an intercept function for a string, it can be done simply by slicing one operation.

Functions in 4.python

The Python function can use required parameters, default parameters, variable parameters, and keyword parameters, which can all be used together, or only some of the 4 parameters, but note that the order of the parameter definitions must be: required, default, variable, and keyword parameters.

For mutable parameters: Python allows you to precede a list or tuple with a * number to change the elements of a list or tuple into mutable parameters.

For keyword arguments: variable parameters allow you to pass in 0 or any of the parameters that are automatically assembled as a tuple when the function is called. The keyword parameter allows you to pass in 0 or any parameter with parameter names that are automatically assembled inside the function as a dict.

For any function, it can be called in a similar func(*args, **kw) manner, regardless of how its arguments are defined.

The default parameter must be used immutable object, if it is a mutable object, run there will be a logic error!

Be aware of the syntax for defining mutable parameters and keyword parameters:

*argsis a variable parameter, and args receives a tuple;

**kwis a keyword parameter, kw receives a dict.

And how to pass in the syntax for variable and keyword arguments when calling a function:

Variable parameters can be directly passed func(1, 2, 3) in:, you can first assemble a list or a tuple, and then pass in *args : func(*(1, 2, 3)) ;

Keyword parameters can be directly passed in: func(a=1, b=2) , you can first assemble dict, and then pass in **kw : func(**{‘a‘: 1, ‘b‘: 2}) .

It *args is customary to use and **kw be python, but it is also possible to use other parameter names, but it's best to get used to idioms.

Iterations in 5.python

By default, the Dict iteration is key. If you want to iterate over value, you can use for value in d.itervalues() it if you want to iterate over both key and value, using thefor k, v in d.iteritems();如果同时迭代list的下标和元素,那么使用

 for  in Enumerate (['A''B''C' ]):...      print I, value
Determines whether an element can be iterated, using Isinstance(‘abc‘, Iterable)

Python Learning Note one

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.