Python Study Notes 1-basics and python Study Notes Basics

Source: Internet
Author: User

Python Study Notes 1-basics and python Study Notes Basics

1) annotations

A single line comment starts with #. For example: # The first comment

Multi-line comments can contain multiple # numbers, including ''' and "", for example:

# Comment on the first line

# Comment on the second line

'''

Comment on the third line

Comment on the fourth line

'''

"""

Comment on the fifth line

Line 6 comment

"""

2) lines and indentation

The most distinctive feature of python is that it uses indentation to represent code blocks without braces {}.

The number of indented spaces is variable,The statement of the same code block must contain the same number of indented spaces.. Example:

If True:

Print ("answer ")

Print ("true ")

Else:

Print ("answer ")

Print ("false ")

3) Standard Data Type

There are six standard data types in python3:

Number (Number)/String (String)/List (List)/Tuple (tuples)/Sets (SET)/Dictionary (Dictionary)

  Number

There are four types of numbers in python: integer, Boolean, floating point, and plural.

    • Int(Integer), such as 1, only one Integer type int, expressed as a Long integer, no Long in python2.
    • Bool(Boolean), such as true.
    • Float(Floating point number), such as 1.23, 3E-2
    • Complex(Plural), for example, 1 + 2j, 1.1 + 2.2j

Python can assign values to multiple variables at the same time, a = B = c = 1

You can also specify multiple variables for multiple objects. Example: a, B, c = 1, 2, "hello"

This instance assigns integer objects 1 and 2 to variables a and B, and string objects "hello" to variable c.

    

You can use the built-in type () function to query the object type referred to by the variable. For example:

A, B, c, d = 20, 5.5, true, 4 + 3j

Print (type (a), type (B), type (c), type (d ));

Result: <class 'int'> <class 'float'> <class 'bool '> <class 'compute'>

You can also use isinstance to determine whether:

>>> A = 111

>>> Isinstance (a, int)

 

  String

    • In python, single quotes and double quotes are identical.
    • You can use three quotation marks (''' or ") to specify a multi-line string.
    • Escape Character '\'
    • The backslash can be used for escape. The r can be used to prevent the backslash from being escaped .. For example, if r "this is a line with \ n", \ n is displayed, not a line break.
    • Cascade strings literally. For example, "this" "is" "string" is automatically converted to this is string.
    • Strings can be connected together using the + operator, which is repeated using the * operator.
    • There are two indexing methods for strings in Python: 0 from left to right and-1 from right to left.
    • The strings in Python cannot be changed.
    • Python does not have a separate character type. A character is a string with a length of 1.
    • The syntax format of string truncation is as follows: Variable [header Subscript: tail subscript]

Example:

Result:

  List)

The list can implement the data structure of most collection classes. The types of elements in the list can be different. It supports numbers, strings, and even lists (so-called nesting ).

A list is a list of elements written between square brackets ([]) and separated by commas.

Like a string, the list can also be indexed and intercepted. After the list is intercepted, a new list containing the required elements is returned.

List truncation format: Variable [header Subscript: tail subscript]. The index value is 0 as the starting value, and-1 is the starting position from the end.

The plus sign (+) is a list join operator, and the asterisk (*) is a repeated operation.

Example:

Result:

Conclusion: 1. List is written between square brackets, and elements are separated by commas.

2. Like strings, list can be indexed and sliced.

3. List can be spliced using the + operator.

4. The elements in the List can be changed.

  Tuple (tuples)The elements with the difference from the list as tuples cannot be modified, and the tuples are written in parentheses (). elements are separated by commas (,), and the element types can also be different. Example:

Result:

Tuples are similar to strings. They can be indexed and the subscript index starts from 0.-1 is the position starting from the end. It can also be intercepted.

Special tuples:

Tup1 = () # Empty tuples

Tup2 = (20,) # an element, which must be followed by a comma

String/list/tuple all belong to sequence (sequence ).

  Set)Is a sequence of unordered, non-repeating elements. The basic function is to test the member relationship and delete duplicate elements.

You can use braces {} or the set () function to create a set. Note: To create an empty set, you must use set () instead of {}, because {} is used to create an empty dictionary.

  

Result:

Dictionary)

A dictionary is another useful built-in data type in Python.

A list is a combination of ordered objects, and a dictionary is a collection of unordered objects. The difference between the two is that the elements in the dictionary are accessed by keys, rather than by offset.

A dictionary is a ing type. It is identified by "{}" and is unordered.Key: value)Set.

The key must be of an unchangeable type.

In the same dictionary, the key must be unique.

Example:

Result:

In addition, the dictionary type also has some built-in functions, such as clear (), keys (), and values.

Note: 1/the dictionary is a ing type, and its elements are key-value pairs.

2/The dictionary keywords must be of an unchangeable type and cannot be repeated.

3. Create an empty dictionary{}

4) escape characters and Indexes

Python uses the backslash (\) Escape Character. If you do not want the backslash to be escaped, you can add an r in front of the string to indicate the original string:

There are two indexing methods for python strings: Starting from left to right with 0 and starting from right to left with-1.

The python string cannot be changed.

 

 

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.