Python Basic concepts-key elements

Source: Internet
Author: User
Tags arithmetic operators logical operators

1. Feature 1: Data type

Python provides several built-in data types, and now we're only looking at two of them. Python uses the int type to represent integers (positive or negative integers), and the STR type to represent strings (sequences of Unicode characters).

If you need to convert one data item from one type to another, you can use the syntax datatype (item), for example:

the int () conversion can allow spaces at the beginning and tail, so the int (' 45 ') is also correct. The SRT () conversion plan can be applied to all data items.

2. Feature 2: Object reference

After defining the data type, the next thing to do is to define a variable that stores a type of data, but Python has a variable like this, and instead uses an object reference. There is no perceptible difference between a variable and an object reference for fixed objects, such as INTs and STRs. For mutable objects, there are differences, but there is little impact in the actual work. Such as

x = ' Blue '

y = ' green '

z = x

When the first statement above is executed, Python creates a str object whose text content is "blue", and also creates an object reference named X, which x refers to as the Str object of the ratione materiae. The popular saying variable x has been assigned the blue string.

In other languages, the operator "=" is inconsistent with the variable assignment operator. In Python, the function of ' = ' is to bind an object to an object that is referenced in memory.

3. Feature 3: Combining data types

Python provides several combinations of data types, including associative arrays and collections, and here we discuss only two of them: tuples and lists

Python tuples and lists can be used to store any number of data items of any type, the tuple is fixed, the emergency cannot be changed after creation, the list is mutable, and the data item can be inserted or removed when needed.

In essence, lists and tuples do not actually store data items, but instead hold object references. When you create a list and a tuple (and when you insert a data item in a list), you are actually using a copy of its given object reference. In the case of literal items (such as integers or strings), an object of the appropriate data type is created in memory, and the object reference is placed in a list or tuple.

Data types such as tuples, lists, and strings are "size", meaning that measurements such as length or size are meaningful for these data types, and it is meaningful to pass the data item parameters of those data types to the Len () function.

So Python data items are "objects" (also known as "instances") of a particular data type (also known as a "class"). The key difference between an object and a data item that some other language provides is that there can be a "method." A method is a function that a particular object can call. For example, the data type list has a append () method that allows you to add objects as follows:

In a procedural programming language, the same functionality can be accomplished using the Append () method of the list in the following manner (fully valid Python syntax):

The list type has many other methods, including the Insert () method, which inserts a data item at a given index location, and the Remove () method, which is used to remove the data item at a given index location.

Insert () Syntax:

List.insert (Index,obj)

    • Index--the indexed position at which the object obj needs to be inserted.
    • Obj--To insert an object from the list.

Remove () Syntax:

List.remove (obj)

Obj--the object to be removed from the list

4. Feature 4: Logical operators

4.1 Identity operator

Since all of the Python variables are actually object references, it is sometimes meaningful to ask whether two more object references are pointing to the same object. The IS operator is a two-tuple operator that returns true if the left side of the object reference is the same object as the right-side object reference.

It is important to note that, in general, it makes no sense to compare INTs, STRs, and many other data types.

One benefit of identity comparison is that it is very fast, because it does not have to be checked against the object itself that is being compared-the IS operator only needs to compare the memory address where the object resides-the same address stores the same object.

The most common use is to compare data items to the built-in null object None, which is typically used as a positional tag value indicating "Unknown" or "nonexistent", as follows

The above using is isn't a reverse test of the identity test

4.2 Comparison operators

<, >, <=, = =,! =, >=,

A particularly useful feature of the Python comparison operator is the ability to perform knot-chain comparisons, such as;

4.3 Member Operators

For a data type of a sequence or collection, such as a string, list, or tuple, we can use the operator in to test the membership and test the non-member relationship with not in.

For lists and tuples, the in operator uses a linear search, which may be slower for very large combinations of types (containing more than tens of thousands of items of data), whereas for dictionaries or collections, in operations can be very fast.

4.4 Logical operators

Python provides 3 logical operators: And, or, not.

5. Feature 5: Control flow statement

A little----refer to the blog Python basic concept-loop http://www.cnblogs.com/Annaying/p/7778630.html

6. Feature 6: Arithmetic operators

+ 、-、 *,/, + =, *=

7. Feature 7: Input/output

Output: Print ()

Enter: Input (), receive user input, as follows:

The last if statement does this: if the user does not enter any numeric values, then print will not be output and will be avoided by the addition of 0.

8. Feature 8: Creation and invocation of functions

function creation Syntax:

def functionname (arguments):

Suite

Here, argument is optional, and if you have multiple arguments, you must separate them with commas. Each Python function has a return value of none by default, unless we use the syntax return value to return from the function, at which point value is the actual return value. The return value can be either a value or a set of values. As follows:

Python provides a large number of built-in functions, its standard library of a large number of modules contain more functions, we can directly use, before using the import statement, the standard module is lowercase letters, the use of attention to distinguish.

Python Basic concepts-key elements

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.