Python Learning Summary II-data types

Source: Internet
Author: User

Take you into the data type

one: integers, floating-point numbers

The definition and operation of integers and floating-point numbers in Python are the same as in C + +, and I don't need to say much here: Python defines integers without the distinction of int and long long, as opposed to C + +, and can be directly assigned. This demonstrates the simplicity of Python functionality.

Two: Boolean value, null value

First, let's talk about the Boolean value True and False in Python. You can use True and false directly in Python (note case), or you can calculate true or false from an expression.

In addition, there are three common Boolean operators and, or and not, and the three-mesh operator and...or ....

There is also a special value in Python: null, denoted by none.

note here two places : first, none note the case (cannot be written as none), and second, none is equal to 0, because 0 is meaningful, none is a null value.

Three: List

A list, as the name implies, lists a table to represent some kind of information. For example, we now need to store two people names Zhang San and John Doe, so we can store it with a list (denoted by square brackets).

is not feeling very strange, pro? Yes, the surface looks like this is no different from a one-dimensional array in C + +. When we learn, we can relate to each other to strengthen memory, but also to note the differences, the list of elements can be different data types, such as Zhang San 24 years old, we can also use a list to store Zhang San name and age of the two attribute values (string and integer bar).

operations for commonly used sequences:

Index

Just now that the list is displaying the information by listing a table to represent the sequence, then the first thing we want to think about is how to traverse the table? Next, we'll learn the index.

All the elements in the list are numbered-incrementing from 0 (not the same as C + +), we can access the list by number.

Note that the number increment from 0 means that the element in the list is from left to right, the last element is the penultimate element in the list, so Python provides a mechanism to access the list by a negative number, if the number is-2, It means that the second-to-last element in the list is accessed.

Sharding

Now consider such a question, what if we want to print a continuous interval in the list? By looping through, and then printing each element in the interval? This will do the same, but Python offers us a better way to do that is to Shard.

Shards are implemented by two indexes separated by colons, the first index is the number of the first element that needs to be extracted, and the second index is the number of the next element in the list that needs to extract the last element of the contiguous interval. In short, it is the thought that left closed right open.

Add multiply

Two lists can be combined by ' + ', and a list can 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, we can implement the modification by converting a string that cannot be modified to a list.

Basic list Operations

element Assignment : It is easy to change the list, and we use index tags to assign values to a specific, well-positioned element.

Shard Assignment : A program can assign values to multiple elements at once, which makes it easy to spice up.

deleting elements : Removing elements from the list is also easy, using the DEL statement to easily get it done. Similarly, we can also fragment delete elements.

Basic List Method

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

count: Counts the occurrences of an element in the list.

Extend: You can add multiple values of another list at the end of the list at once, which looks much like a join operation. But there is a difference between the two: the connection operation does not change the original two list, and the extend action is to extend a list, affecting the original list.

Index: The index position used to find the first occurrence of an element from the list.

Insert: For inserting an element into a position on a list.

pop: Removes an element from the list, defaults to the last one, and returns the value of the element.

Remove: Removes the first occurrence of an element in the list, and the difference between the pop is that remove has no return value.

Reverse: Flips the elements of the list.

Sort: Of course the elements in the list are sorted.

Advanced Sort : Implements a custom sort for the sort join parameter.

Four: meta-group

Tuples, like lists, are a sequence, but the elements of a tuple cannot be modified (just to mention the same thing.) Don't believe me? You can try it). Tuples enclose elements in parentheses, separating them with commas. An empty tuple is a pair of parentheses to represent.

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

tuple function : Convert the parameter to the form of a tuple, and you can contact the list function.

Python Learning Summary II-data types

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.