Python Basic--data type

Source: Internet
Author: User
Tags arithmetic operators bitwise operators logical operators

I. Introduction of Ptyhon

Ptyhon language refers to the language style of Python, and its own grammatical structure, etc.

The Python interpreter is a software to interpret and execute,

      • Cpython (most commonly used version)
        The official version of Python, implemented using the C language, is the most widely used, and the CPython implementation converts the source file (py file) into a bytecode file (PYc file) and then runs on the Python virtual machine.
      • Jyhton
        Python Java implementation, Jython will dynamically compile Python code into Java bytecode, and then run on the JVM.
      • IronPython
        In Python's C # implementation, IronPython compiles Python code into C # bytecode and then runs on the CLR. (similar to Jython)
      • PyPy (Special)
        Python implements Python, which compiles Python bytecode bytecode into machine code.
      • Rubypython, Brython ...

Second, the installation of the Python interpreter

Installation under window:

1 Download install from www.python.org, download install python latest version, preferably download python2 version and Python3 version, note Download python2 and Python3 installation path,

If you want to find Python2 and 3 in any location, set it in the environment variable, locate the Python.exe file,

Method: Computer--attribute--Advanced system setting--environment variable--system Setup

If you find the script file, add the path to the script, you can call the PIP command, and Pip can coexist in multiple versions,

In Linux Installation

1. Python can be loaded in any location,

Path temporary change

Permanent change, Vim--etc-prefile

Or you can log in again and it will take effect.

Execution of Python internal files

2. File start

Make an absolute path

#! Usr/bin/env python, absolute path, change platform, can also use

. Execute script Incoming parameters

Python has a large number of modules, you can use the class library in 3,

    • Python-supplied modules
    • Industry-Open Source modules
    • Modules developed by programmers themselves

Import is the importing module

3. About PYC Files

When you execute Python code, if you import a different. py file, a. pyc file with the same name is automatically generated during execution, which is the bytecode generated after the Python interpreter was compiled.

PS: Code is compiled to generate bytecode, and bytecode can be obtained by decompile

Third, variable

Program execution when a series of state changes, in order to save this change, change, reflect the program changes, volume, reflecting a state of the program,

1. Declaration and reference of variables

Name= ' Egon ', by name, refers to the value of the variable.

Declaration Rules for variables:

Can only start with a letter, followed by a number, or underline,

Keyword cannot do variable name:

[' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ',

' Def ', ' del ', ' elif ', ' Else ', ' except ', ' exec ', ' finally ',

' For ', ' from ', ' global ', ' if ', ' import ', ' in ', ' is ', ' lambda ',

' Not ', ' or ', ' pass ', ' print ', ' raise ', ' return ', ' Try ', ' when ', ' with ', ' yield '

2. Changing the value of a variable

The value of the variable can be changed, the address of the memory is not moved, the value is saved in memory, and Python specifies that the discarded value is recycled,

A binding relationship is made between the variable name and the value, or you can unbind it.

X=1

Y=x lifted the relationship with X and 1.

Or you can use Del X to remove the relationship between X and 1.

Name=1

name=2

Name=1

name=2

3. Input and output

Python3, unified with input input, using raw_input and input inputs in Python2,

Name=input (' Please enter name: ')

Print (' name ')

Attention:

If you enter a password, you can import the Getpass module in order not to be visible

Execute (Cannot execute in Pycharm, need to execute in terminal)

Import getpasspwd=getpass.getpass (' Please enter password:')print(PWD)

4. Operators

1. Arithmetic operators

2. Comparison operators

3. Logical operators

Priority ranking, from high to Low, (), Not,and,or,

and annotations:

    1. In Python, and and or to perform Boolean logic calculations, as you would expect, but they do not return Boolean values, but instead return one of the values they actually compare.
    2. The value of a left-to-right calculus expression in a Boolean context, and returns the last value if all the values in the Boolean context are true.
    3. If a value in the Boolean context is False, and returns the first false value

or annotations:

    1. When using or , the left-to-right calculus is evaluated in a Boolean context, just like and. If a value is true,or returns the value immediately
    2. If all values are false,or returns the last False value
    3. Note that or in a Boolean context, the expression calculus continues until the first true value is found, and then the remaining comparison values are ignored

And-or used in combination:

    1. Combining the two preceding grammars, reasoning can be.
    2. To enhance the readability of the program, it is best to be associated with parentheses, for example:
      (1 and ' X ') or ' Y '

4. Assignment operators

5. Bitwise operators

6. Member Operators

Name ='uhh'if'h' in name:     Print ('ok') Else :     Print ('no')

7. Identity operator

A=1b=1print is b)  return ture

8. Precedence of Operators

9.bytes Type

Python3 built-in type bytes and str usages and various encoding conversions between byte and string
The most important new feature of Python 3 is probably a clearer distinction between text and binary data. Text is always Unicode, represented by the STR type, and binary data is represented by the bytes type. Python 3 does not mix str and bytes in any implicit way, which makes the distinction between them particularly clear. You cannot stitch strings and byte packets, search for strings in a byte packet (or vice versa), or pass a string into a function with a byte packet (or vice versa).

' I'm ' Print (msg) Print (Msg.encode (encoding="utf-8"))  has a string, converted to bytecode print(msg.encode (encoding="utf-8"). Decode (encoding="utf-8"))  Convert bytecode to string
I b ' \xe6\x88\x91 ' I'm

Python Basic--data type

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.