Python basics notes - rookie tutorial

python Chinese encoding

Python default encoding format to ASCII format, print characters will complain not modified before encoding.
Solution: In the beginning of the file to join # - - Coding: UTF-8 - - or # coding = utf-8

tips: Python3.X source file using the utf-8 encoding by default, so you can properly resolve the Chinese, without specifying the UTF-8 encoding. If you use the editor, and the need to format storage py file for UTF-8, otherwise it will error;

Python identifiers

  1. Letters, numbers, underscores
  2. You can not begin with a number
  3. Case sensitive
  4. It has a special significance to underscore identifiers beginning.
    It begins with a single underscore: class attribute represents not directly accessible, require access through the classes offered. Can not be used from ... import * import.
    It starts with a double underscore: private members of the class representatives.
    Double underscore at the beginning and end: Python in behalf of special methods to enhance identification. eg: __ Constructor the init () __ representative of the class.

Python reserved word

Reserved words can not do constant or variable or any identifier name.

and assert break class continue def
elif else except exec finally for
from global if import in is
lambda not or pass print raise
return try while with yield

Multi-line statement

Backslash \ multi-line display
Here Insert Picture Description

Python comment

  • Single-line comments: The beginning #
  • Multi-line comments: using three single quotation marks ( '' '), or three double quotation marks ( "" ")
    Here Insert Picture Description

Python blank line

The method of the partition blank lines between classes or between functions, classes and functions are also separated by an empty line inlet.
tips: do not empty line does not complain, just for easy maintenance

print output

The default line feed output, to achieve the output does not wrap to include a comma at the end of variables (,)

Here Insert Picture Description
operation result:
Here Insert Picture Description

Python standard data types

  • Number (Digital)
  • String (String)
  • List (list)
  • Tuple (tuple)
  • Diractionary (dictionary)

Python number (Number) Type

Type for storing the digital data value.
They are immutable data types, which means that changing the numeric data type will be assigned a new object.
Python supports four different types of numbers:

  • int (integer)
  • long (Long)
  • float (float)
  • complex (plural)

Python String (String)

Strings or string (String) is a string of characters consisting of numbers, letters, the underscore.
The value of the order:

  • From left to right, the default indexes starting from 0, the maximum length of the string in the range of less 1
  • From right to left, starting at -1 index default, the maximum range is the beginning of a string

Python List

List with [] identified python is the most common complex data type
cutting list values may be used in variable [head superscript: Tail index], the corresponding list will be taken. Default start index 0 from left to right, right to left at -1 default index, the subscript can represent an empty take the head or tail.
List secondary assignment.

Python tuples (Tuple)

Identified by tuple (). Internal element separated by commas.
Tuples can not quadratic assignment, equivalent to read-only list.

Python dictionary (Dictionary)

List is an ordered collection of objects, dictionaries are unordered collections of objects.
The difference between the two is that: among the elements of the dictionary is accessed by a key, rather than by shifting access.
Dictionaries with "{}" logo. A dictionary index (key) value and a value corresponding to its composition.
Each dictionary key key => value of the colon: segmentation, key-value pair with a comma between each split, including the entire dictionary in braces {} in

Python data type conversion

function description
int(x [,base]) The integer x is converted to a
long(x [,base] ) The converted to a long integer x
float(x) The transition to a floating point x
int(x [,base]) Creating a complex
complex(real [,imag]) The object is converted to a string x
str(x) The object is converted to a string x
repr(x) The string object is converted to an expression x
eval(str) Python expression for calculating effective in the string, and returns an object
tuple(s) Jiang sequence is converted into a sequence s
list(s) Converted to a list
set(s) Is converted to a set can be changed
dict(d) Creating a dictionary, d is a need to sequence (key, value) tuple
frozenset(s) Is converted to a set of immutable
chr(x) Convert a character certificate for
unichr(x) Convert integer unicode character
words (x) The integer value is converted to a character
hex(x) Convert integer to a hexadecimal string
oct(x) Convert integers to octal string

Operators Python

  • Arithmetic operators: +, -, *, /,%, ** (power), // (rounded down)
  • Comparison operators: ==, =, <> (not equal, Python3 obsolete),>, <,> =, <=!
  • Assignment operator: =, + =, - =, * =, / =, =%, ** = // =
  • Bitwise Operators: & (and), | (or), ^ (exclusive or) ~ (negation), << (shift left), >> (shift right)
  • Logical operators: and, or, not
  • Member operator: in, not in
  • Identity operator: it is, is not
Released four original articles · won praise 0 · Views 27

Guess you like

Origin blog.csdn.net/qq_41614773/article/details/105142496