Basic technical framework for 3.Python programming languages

Source: Internet
Author: User

3.1 Viewing data item data types

Type (name)

3.2 View data item data ID

ID (name)

3.3 Object reference

Notes Python saves all data as a Memory object

in Python, a variable is actually a reference to a Memory object

  Dynamic type: At any point in time, an object reference can re-reference a different object (which can be a different data type) whenever needed

"=" is used to bind a variable name to an object in memory: Bind directly if the object exists beforehand, otherwisethereferenced object is created by "="

Variable command rules

  Can contain only letters, numbers, and underscores, and cannot start with a number

  Distinguish letter Case

Prohibit use of reserved words

  Command conventions

  the variable name (_x) is not imported by the From module import * statement by a single underscore

The variable name (__x__), preceded by an underscore, is a system-defined variable name that has a special meaning for the Python interpreter

  variable names (__x) that start with two underscores but end without underscoresare local variables of the class

In interactive mode, the variable name "_" is used to save the result of the last expression

Note: The variable name has no type and the object has

3.4 Combined data types

A collection of data elements that are organized in a way, such as numbering an element.

Python 's commonly used combination data types

  Sequence type

list: Using [] create, such as [' call ', ' Me ', ' tshmeal ', '. '] (cannot be changed)

tuples: Using () created, such as (' one ', ' one ', ' one ')(can be changed)

string also belongs to sequence type

do slice:name = "string"

Name[0:2]:St

Name[:2]:St

Name[2:]:ring

Name[2:3]:R

Name[0:4:2]:SR-- Specify step as 2

Len (name):6

A list is a mutable sequence, a tuple is an immutable sequence

In fact, lists and tuples do not actually store data, but instead hold object references

a Python object can have a specific "method (function)" that it can be called

data types, such as tuples, lists, and strings, are "sized", that is, their lengths can be measured using the built-in function len ()

  Collection type

Collection

in Python, a combined data type is also an object, so it can be nested

[' Hello ', ' world ', [[+]]

Mapping type

Dictionary

3.5 logical Operators

  Logical operation is the basic function of any programming language

Python provides 4 sets of logical budgets

  Identity operator

Is: To determine whether the left object reference is the same as the less right operand-end object reference, or with None ;

  Comparison operators

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

Member operators

In or not in: Test member Relationship

  logical operators

And, or, not

3.6 input/Output

  Input

Input (),raw_input ()

The Python interpreter provides 3 standard file objects, standard input, standard output, and standard error, respectively, in the sys module with Sys.stdin ,sys.stdout , and sys.stderr forms available

Python Print statement implementation Prints a programmer-friendly standard output stream interface

  from a technical point of view,print is a stream that converts one or more objects into its text representation and then sends it to standard output or another similar file

in the in Python, printing is closely linked to the concept of files and streams

The file write method is to write the string to any file

print defaults to printing objects to the stdout stream, and adds some automatic formatting

  Essentially,the print statement is just a concrete implementation of the human nature of Python, which provides a simple interface for sys.stdout.write () . Plus some default formatting

Print accepts a comma-delimited list of objects, does not add a newline character automatically at the end of the line, and adds a comma after the last element if it is not required

  Output formatting

Print "String%format1%format2 ..."% (Variable1, variable2, ...)

num = 7.9

Print "The num is%f"% num

7.900000

Dir (__builtins__) view built-in functions

Help (str) to see how the str function is used

Character output format

D,i decimal integer or Long integer

U unsigned integer or long integer

o octal integer or Long integer

x hexadecimal integer or Long integer

X hexadecimal integer (capital letter)

F Floating-point numbers , such as [-]m.dddddd

E floating-point numbers, such as [-]m.dddddde_+xx

E Floating-point numbers, such as [-]m.dddddde_+xx

Use %e or %ewhen the G,g index is less than -4 or higher, otherwise use %f

s string or arbitrary object. Format code uses str () to produce strings

R same as repr () generated string

C Single character

% literal %

the modifier that can be used after%, (if any, only in the following order)

%[(name)][flags][width][.prcecision]typecode

A key name in parentheses that belongs to the following dictionary, which is used to select a specific item

One or more of the following flags

-: Indicates left alignment, default to right alignment

+: Indicates a number symbol, positive numbers will also take "+"

0: Represents a 0 fill

A number that specifies the minimum width

A decimal that is used to segment the width of a field by precision

A number that specifies the maximum number of characters to be printed in the string, the number of digits after the decimal point in the floating point, or the integer the minimum number of digits;

Example:

d={' x ': +, ' y ': 27.490325, ' Z ': 65}

Print "% (x) -10d% (y) 0.3g"% d

3.7 Dictionary

A11 = {' A ': 1, ' B ': 2}

A11[' a ']

the creation and invocation of the 3.8 function

  Functions are the basic components for modular programming

Python defines functions using def statements

  Functions can be parameterized by passing different parameters to invoke the

  each The Python function has a return value that defaults to None, and you can use "return value" to explicitly define the return value

The DEF statement creates a function object and simultaneously creates an object reference to the function

  Functions are also objects, can be stored in a combined data type, or can be passed as arguments to other functions

Callable () can be used to test whether a function can be called

Example:

def testFunc (arg1):

Print Arg1

Callable (TestFunc)

True

TestFunc (' Hello world! ')

Hello world!

  Built-in functions

Dir (),ID (),type (),str (), help(), len (),callable ()

  Import Template

Import Random

D=random.choice ([' A ', ' B ', ' C '])

Print D

B

Basic technical framework for 3.Python programming languages

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.