Python Learning Notes

Source: Internet
Author: User

1. Code Conversion

General hard disk storage is utf-8, read into in-memory Unicode, and how they are converted

A = ' hello ' \xe4\xbd\xa0\xe5\xa5\xbd ' <type ' str ' >

b = u ' Hello ' u ' \u4f60\u597d ' <type ' Unicode ' >

A.decode (' utf-8 ') u ' \u4f60\u597d ' (utf-8 format decoded to Unicode)

B.encode (' utf-8 ') ' \XE4\XBD\XA0\XE5\XA5\XBD ' (Unicode format encrypted to Utf-8)

Note: In the python2.7 version you need to convert as above, in the script to display Chinese,

Just add # _*_ coding:utf-8 _*_ or #coding =utf-8 at the beginning of the file.

After the python3.4 version, no conversion is required


2. Call the system command and deposit the variable:

1.import OS

A = Os.system (' df-th ')

b = Os.popen (' df-th ', ' R ') returns a file object


2.import commands

c = commands.getoutput (' df-th ') returns a string


3. SYS call

Import Sys

Sys.exit

Print Sys.arg

Sys.path


4. Import Template Method:

1.import sys [as newname]

2.from sys import argv or (*)

It is recommended that the first, the second imported object or variable will conflict with the current variable.


5. User interaction:

In the python2.7 version

Raw_input: interactive input string;

Input: Enter numbers interactively;

In the python3.4 version only raw_input, want to get the number, need to make an int transformation.

Example:

#_ *_ Coding:utf-8 _*_

info = ' This var is printed out ... '
Name = raw_input (' Please input your name: ')
age = Int (raw_input (' Age: '))
#age = input (' Age: ')
Job = Raw_input (' Job: ')
Salary = input (' Salary: ')
Print type (age)

Print ""
Personal Information of%s:
Name:%s
Age:%d
Job:%s
Salary:%d
--------------------------
"% (Name,name, age,job,salary)


This article is from the "Autumn Fairy tale" blog, please be sure to keep this source http://wushank.blog.51cto.com/3489095/1651859

Python Learning 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.