Python Learning Notes

Source: Internet
Author: User
Tags python list

1. Dir () function

Returns a list of all properties and functions for all query objects.

2. Python built-in data types: integer (numeric), string, tuple, list, dictionary, and Boolean types

1) integer--int--number--immutable data type
Python has 5 types of numbers, the most common being the integral type Int,int Python method is very useful. Example: 1234,-1234

2) Symbolic = = For Boolean--bool----immutable data type
Boolean is a special type of Python number, it is only true and false two kinds of values, it is mainly used to compare and judge, the result is called a Boolean value. For example: 3==3 gives true,3==5 gives false

3) string--str--with "or" "--immutable data type
For example: ' www.iplaypython.com ' or ' hello '

4) List--list--with [] symbol-Variable data type
Example: [1,2,3,4]

5) tuple--tuple--with () notation-immutable data types
For example: (' d ', 300)

6) Dictionary--dict--with {} notation-variable data type
For example: {' name ': ' Coco ', ' country ': ' China '}

3. Type () function

Returns the data type of the query object

4. Python string str

1) Single or double quotation mark creation

2) string str () method

Use the Str method to create a new string, such as a=123; B=str (a)

STR () Turns data that is not a character type into a character type

3) string index in Python

Forward index: ABCDE (01234)

Reverse index: ABCDE ( -5,-4,-3,-2,-1), that is, from right to left, rightmost is-1

such as: X[0:-1] represents all numbers, so-called slicing is this

5. Python integral type

The operands of the integer data type variables currently supported by Python are: Add (+), subtract (-), multiply (*), divide (/), and power (* *)

Integer and Character conversions: STR (123),,,,,,,,, int (' 1235 ')

6. Python list

List lists have a strong role in Python in that the list can contain different types of data pairs, and it is an ordered set. All sequences can be used in the standard operation method, the list can also be used. such as slicing, indexing, and so on, the Python list is a mutable data type that supports additions, modifications, deletions, and so on.

Create a new list that supports all data types

Modify the list: directly with subscript to modify, such as a=[1,22,222],a[0]=2 =>a=[2,22,222]

List methods: Append, count, Extend, index, Python list insert, Python reverse, Del, remove, pop and sort sort

7. Python tuples

1) Tuple creation

Creating a tuple method is simple, the tuple is enclosed in parentheses (), and the elements in parentheses are separated by commas, so that the tuple is created, sometimes the assignment can be omitted, if there is only one element, followed by a comma, such as A=1,

Tuple () Creates a tuple: Creates a list as a tuple of tuples ([+ +]); Converts a string into a tuple of tuples (' Hello ')

2) read values from tuples

Tuples support index and slice values

3) Modifying tuples

Cannot be modified, can be converted to list modification, and then reversed

The meta-Group several summary:

1, tuples are an ordered set,
2, tuples and lists can use indexes, slices to take values.
3, after the creation of tuples can not be in situ modification and replacement operations.
4. Tuples support nesting, which can include lists, dictionaries, and different tuples.
5, tuples support the operation of general sequence, for example: +, *
>>> (+) + (3,4)
(1,2,3,4)
>>> (7,8)
(7,8,7,8,7,8,7,8)
Note the +, * operation, returns a new tuple

8. Python dictionary type

Dictionary variable name [key Name key] = key corresponding value

Use the Dict () function to create a dictionary variable directly

Dictionary Delete, three ways:

1, del method: delete the corresponding value of the key, Del space variable name [key name], if the only write variable name is to delete this dictionary
2, Clear method: Empty the dictionary content, variable name. Clear ()
3, Python pop method: Delete the value corresponding to the key, but it will be the output of the corresponding value and then delete

9. Collection types for Python

Create: Set (variable)

Added: A=set (' Boy '),

Add:a.add (' python '); a=>[' y ', ' python ', ' B ', ' O ']

Update:a.update (' python '); a=>[' B ', ' H ', ' o ', ' n ', ' P ', ' t ', ' Y ']

Delete:a = set (' Boy '),a.add (' python '), a.remove (' Python '); [' Y ', ' b ', ' O ']

Set Operator: Intersection (&), set (|), Difference set (-), not equal to (! =), equals (= =), is a member relationship (in), not a member relationship (not in)

Set () is unordered and cannot be evaluated with indexes and tiles

10. Help () built-in functions

To open the Help document for the module: Assist (' str ')

11. File read

Open ()---opening file

Read ()---reading a file

1), read (): reads all content

2), ReadLine (): Indicates progressive reading

Close ()---Close file

12. Comments

Single-line Comment: #

Multiline comment: "" or "" "" ""

Starting note: To prevent problems with Chinese comments, the file is added at the beginning: # Coding=utf-8

13. Special Notices

Continue,break,pass (act as placeholder), global (declares global variable), assert (Assert, assert expression [, parameter], catch exception in advance), lambda (anonymous function)

Description: This knowledge point is suitable for beginners to see, reference website: Http://www.iplaypython.com/jinjie

Python Learning Notes

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.