Python Basic Notes

Source: Internet
Author: User

Path and file name do not have Chinese

#! /usr/bin/env python

#-*-Coding:utf-8-*-2.7 Need to add this line. 3.5 No

Version differences:

python3.5 print (' Hello,world ') default Utf-8 input ("Please enter name") Utf-8 convert gbk:decode (' UTF8 ') first decode to UNIC Ode, then Encode ("GBK")

python2.7 print ' Hello,world ' default ASCII raw_input ("Please enter first Name:") utf-8 convert GBK:

Single-line comment: #; Multiline comment: "A lot of lines"

. pyc file: Python:python--> bytecode--machine-to-computer, this. PYC is a bytecode file, and if the PY is deleted, PYC is also available

The assignment in Python is the address in the assigned memory,a=6;b=a; if A=7,b is still 6, because there's a space for a 7 for a.

Continue: Jump out of the current loop and continue the next loop

Break: Jump out of all loops

See which methods are available for an object:

Method 1:print (Type temp), locate the class first, and then hold down CTRL and click on the class above the

Method 2:help (Type (temp)) or dir (temp)

python2.7 sometimes do not display text, but the reality of hexadecimal, this time need to encode, B=a.decode ("UTF8"); C=b.encode (' GBK ') or x=str (y,encoding= ' utf-8 ')

To modify itself and create a copy:

In general, the method of the string is mostly to create a copy, List,tuple,dic most of them are to modify their own

Although the elements of a tuple cannot be changed, the array in tu= (1,2,[' A ', ' B ') can be changed, but not the elements of the tuple can not be changed, only that the tuple has no related methods.

Ternary operation: name = value 1 if condition else value 2//If condition is true, name= value is 1, if condition is not true, Name= value 2

Depth copy

One, number and string

For numbers and strings, assignments, shallow copies, and deep copies are meaningless because they always point to the same memory address.

Import copy# ######### number, string ######## #n1 = 123# N1 = "I am Alex age" Print (ID (n1)) # # # # # # # #n2 = N1print (id (n2)) # # # # Shallow copy Shell # #n2 = copy.copy (n1) print (ID (n2))  # # # deep Copy # #n3 = copy.deepcopy (n1) print (ID (n3))

Second, other basic data types

1, assign a value , just create a variable that points to the original memory address, such as:

N1 = {"K1": "WU", "K2": 123, "K3": ["Alex", 456]}  n2 = N1

2. Shallow copy

Shallow copy , creating only the first layer of data in memory

Import Copy  n1 = {"K1": "WU", "K2": 123, "K3": ["Alex", 456]}  n3 = Copy.copy (N1)

3. Deep Copy

deep copy , recreate all data in memory (except for the last layer, i.e., Python's internal optimization of strings and numbers)

Import Copy  n1 = {"K1": "WU", "K2": 123, "K3": ["Alex", 456]}  N4 = copy.deepcopy (n1)

Python Basic Notes

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.