Python data learning notes, python learning notes

Source: Internet
Author: User

Python data learning notes, python learning notes

Data Type

I. Integer and floating point number

In Python, the definitions and operations of integers and floating-point numbers are the same as those of C ++. I don't need to talk about them here. I will explain one thing: Compared with C/C ++, Python, define integers that are not distinguished by int and long. assign values directly. This reflects the concise functions of Python.

Ii. boolean and null values

First, let's talk about the Boolean values True and False in Python. In Python, you can directly use True and False (case sensitive), or use an expression to calculate True or False.

In addition, there are three common boolean operators: and, or, and not, and the three-object operator and... or ....

There is also a special value in Python: NULL, expressed as None.

Note the following two points: first, None is case-sensitive (cannot be written as none); second, None is not equal to 0, because 0 is meaningful and None is null.

Iii. List

A list, as its name implies, is to list a table to indicate certain information. For example, if we need to store the names of two people, Zhang weisan and Li Si, we can store them using a list (represented by square brackets.

Isn't it strange? Yes, it seems that this is no different from a one-dimensional array in C ++. When we are learning, we can associate them to enhance our memory. At the same time, we should pay attention to the differences. The elements in the list can be different data types, such as Zhang San, 24 years old, we can also use the list to store the two attribute values (string and integer) of Michael Jacob's name and age ).

Common sequence operations:

1. Index

As I mentioned earlier, the list is to display information by listing a table to represent the sequence. So the first thing we need to think about is how to traverse the table? Next, we will learn about indexes.

All the elements in the list are numbered-increasing from 0 (no difference with C ++), so we can access the list through numbers.

Note: Numbers increase from 0 to indicate that the elements in the list are from left to right, And the last element is the last element in the list, therefore, Python provides such a mechanism to access the list by numbered negative. If the number is-2, it indicates that the last and last elements in the list are accessed.

2. Parts

Now, what if we want to print a continuous interval in the list? Print each element in the interval through loop traversal? This works, but Python provides us with a better solution, that is, sharding.

Partitions are implemented by two indexes separated by colons. The first index is the number of the first element to be extracted, the second index is the number of the next element of the last element in the continuous interval to be extracted in the list. In short, it is the idea of left-closed and right-open.

3. Multiplication

The two lists can be merged by '+'. A list can also be multiplied by an integer to indicate how many times the list is output.

List function: converts a parameter to a list representation. For example, you can modify it by converting a string that cannot be modified to a list.

Basic list operations

Element assignment: it is easy to change the list. We use index tags to assign values to a specific element with a clear position.

Partition assignment: The program can assign values to multiple elements at a time, which is very convenient.

Delete element: It is also easy to delete elements from the list, which can be easily done using the del statement. Likewise, we can also delete elements in parts.

Basic List Method

Append: Used to add new elements at the end of the list.

Count: count the times when an element appears in the list.

Extend: You can add multiple values of another list at one time at the end of the list, which looks like a join operation. But there is a difference between the two: the connection operation does not change the original two lists, while the extend operation extends a list, which has an impact on the original list.

Index: used to find the index location where an element appears for the first time in the list.

Insert: used to insert an element to a position in the list.

Pop: removes an element from the list. The default value is the last one and the value of this element is returned.

Remove: removes the first match of an element in the list. The difference between remove and pop is that remove has no return value.

Reverse: Flip the list elements.

Sort: sort the elements in the list.

Advanced sorting: Add parameters to sort for custom sorting.

Iv. tuples

The same as the list, tuples are a sequence. The difference is that elements of tuples cannot be modified (strings are the same if you just mention one sentence. Believe it? You can try it ). Elements are enclosed by parentheses and separated by commas. Empty tuples are represented by a pair of parentheses.

Note: When Using tuples to store an element, we need to add a comma after the unique element. Because 34 and (34) are the same in Python, (34) cannot be expressed as a tuples, and (34,) is a tuples.

Tuple function: converts a parameter to a tuples. Contact the list function.

The above is all the content in this article. I have introduced the Python data type in detail and hope to help you learn it.

Articles you may be interested in:
  • Introduction to basic built-in data types in basic python tutorials
  • Summary of common python data type conversion functions
  • Python Basic Data Types
  • Getting started with Python from scratch (2) Basic Data Types
  • Basic Data Types of Python3
  • Introduction to basic data types and variable declarations in the basic python tutorial
  • Parse data types and variables in Python in detail

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.