Python notes a basic concept and basic understanding

Source: Internet
Author: User
Tags bitwise

Brief introduction

The founder of Python is Van Rossum. In the October tiobe leaderboard, Python was the five, with a share of 3.775%.

Python is a strongly-typed definition language with dynamic interpretation .

Execution. Py is performed by the Python interpreter, progressive compilation → explanation , running.

Pycodeobject is the result of the compilation, after the run is complete, save to PYC so that the next time you run the PYc file directly

A dynamic type is the language in which data type checking is done during run time.

A strongly typed definition language is always the type once a variable is assigned a data type and is not cast.

Advantages: Elegant, clear and simple. High development efficiency. High-level language. Portability, extensibility, and embedding.

Cons: Slow, code cannot be encrypted, threads cannot justify multiple CPU problems.

Python interpreter: Cpyhon is the most widely used interpreter. Windows can use Pycharm to write Python programs


The rest of the content is based on Python3 .

Basic understanding


Print ("Hello world!")

Hello world!

Python hello.py # #指定解释器

#!/usr/bin/env Python # #在头部指定解释器

#_ *_coding:utf-8_*_


Variable

name = ' He quan '

Variable names can only be any combination of letters, numbers, or underscores

The first character of a variable name cannot be a number

The following keywords cannot be life-variable names

>>> Import keyword

>>> keyword.kwlist

[' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' finally ', ' for ', ' F ' Rom ', ' Global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' try ', ' while ', ' WI Th ', ' yield ']

Constants: Use uppercase and underline for global variables that do not change.

User_constant

Private variables: Use lowercase and a leading underscore only when used inside private.

_private_value

Built-in variables: lowercase, two leading underscores and post underscores.

__class__

Comments

When line comment: #内容

Multiline Comment: "" "

Content

"""


Input

Name = input ("What is your name?")


Module Initial Knowledge

Import Os,sys

Os.system (". Join (sys.argv[1:])") #把用户的输入参数当做命令交给os. System execution.

SYS.ARGV # #用来获取命令行参数

'. Join # #是字符操作函数


#!/usr/bin/env python

# Python startup file

Import Sys

Import ReadLine

Import Rlcompleter

Import atexit

Import OS

# tab Completion

Readline.parse_and_bind (' Tab:complete ')

# History File

Histfile = Os.path.join (os.environ[' HOME '), '. Pythonhistory ')

Try

Readline.read_history_file (Histfile)

Except IOError:

Pass

Atexit.register (Readline.write_history_file, Histfile)

Del os, Histfile, ReadLine, Rlcompleter

Type

There are six standard data types:

Number (numeric)

String (String)

List (lists)

Tuple (Ganso)

Sets (collection)

Dictionary (dictionary)

Number (numeric)

Python3 supports int, float, complex (plural).

In Python 3, there is only one integer type int, represented as a long integer, and no long in Python2.

BOOL is divided into: True, False

String (String)

The strings in Python are enclosed in single quotation marks (') or double quotation marks ("), and the special characters are escaped with a rice slash (\).

Notepad Unicode encoding

↓↑

UTF-8 Unicode

↓↑

File Abc.tx

Formatted string:%d digit%f floating-point%s character%x

List (lists)

A list can accomplish the data structure implementation of most collection classes. The type of elements in a list can be different, it supports numbers, and strings can even contain lists (nested).

A list is a comma-delimited list of elements written between square brackets [].

list = [' He ', 213]

Variable [head subscript: Tail subscript]

The index value starts with 0, and 1 is the starting position from the end. + is a list link operator, * is a repeating operation.

List.append Append

List.insert (1,) insert

List.pop (1) Removal

list[1]= Change

Tuple (Ganso)

Ganso (tuple) is similar to a list, except that the elements of the ancestor cannot be modified. Ganso is written in parentheses (), and the elements are separated by commas.

Element class lines in an element can also be different.

tuple = (' he ', 12345)

Tup1 = () # Empty tuple

Tup2 = (20,) # An element that needs a sub-element to add a comma

Set (SET)

A collection (set) is a sequence of elements that do not need to be duplicated.

The basic function is to test the membership and remove duplicate elements.

You can create a collection using the curly braces {} or the set () function.

S.add ()

S.remove ()

Dictionary (dictionary)

The Dictionary (dictionary) is another very useful built-in data type in Python.

A list is an ordered combination of objects, and a dictionary is an unordered combination of objects. The difference between the two is that the elements in the dictionary are accessed by keys, not by offsets.

A dictionary is a type of mapping that is identified by the {}, which is an unordered key (key): The value pair collection.

The key (key) must use the immutable type.

In the same dictionary summary, the key must be unique.

Dictionary = {' name ': ' Hequan ', ' age ': 1, ' site ': ' Www.hequan.lol '}


Both the dict and the set require key to be immutable.

The tuples are not variable.

(1,[2,3]) the value in [2,3] is variable.


Supplemental: operator

    1. Arithmetic: +-*/% * *//

    2. Compare: = = = > < >= <=

    3. Assignment: = = = = *=/+%= **=//= # # C+=a C=c+a

    1. Bit: is to calculate the number as a binary.

    • & Bitwise AND Operator: Two values that participate in the operation, if two corresponding bits are 1, the result of that bit is 1, otherwise 0

    • | Bitwise OR operator: As long as the corresponding two bits have one for 1 o'clock, the result is 1.

    • ^ Bitwise XOR Operator: When two corresponding binary differences, the result bit is 1.

    • ~ Bitwise inverse Operator: Reverse each bits of the data, which turns 1 to 0 and 0 to 1

    • << left move operator: The binary of the operations arithmetic all left a number of bits, the number of "<<" to the right to specify the number of bits moved, high drop, low 0.

    • >> Right Move operator: All the binary of the left operand of ">>" is shifted to the right of several bits, and the number to the right of ">>" specifies the number of bits to move.

Logic: And or not with or non-member: includes a string, a list, or a meta-ancestor.
    • In if in the specified sequence, the found value returns True, otherwise false is returned.

    • Not if the value returned true is not found in the specified sequence, otherwise false is returned.

Identity: A storage unit used to compare two objects.
    • is to determine whether two identifiers are referenced from an object

    • Is isn't is to determine whether two identifiers are referenced automatically by different objects

Priority level:
    • * * ~+-*/%//+->><< & ^| <=< >== <> = = ~= =%=/=/==-= + *= **= is in not OR and

Basic operations

    • Strip removing whitespace

    • Split split Str.split (str= "", Num=string.count (str))

    • Len (obj) length

    • OBJ[1] Index

    • OBJ[1:],OBJ[1:10] Slices


LL = [1,2,3,4,5]

Index:append Del/remove/pop len For/while:break;continue;pass;return;exit if in:

To end a cycle, single time, position, return, exit

Ganso elements can not be modified, elements of the meta-ancestor elements may be modified

T1 = (1,2,{' h1 ': 123})

t1[2][' H1 ']= ' H2 '

Print (T1)


D.keys (); D.values (); D.items () # Dictionary keys, values, and key-value pairs


Open R r+ W a b








This article is from the "what-all" blog, please be sure to keep this source http://hequan.blog.51cto.com/5701886/1886268

Python notes a basic concept and basic understanding

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.