Python Learning notes (2)

Source: Internet
Author: User

First, the initial knowledge of the module

1. SYS module

Import Sys

Print (Sys.path) #打印环境变量

Print (SYS.ARGV) #打印相对变量

2. OS Module

Import OS

Cmd_res = Os.system ("dir") #执行命令, do not save results

Cmd_res = Os.popen ("dir")

Print ("--", cmd_res)

3. What is PYC?

When you enter Python hello.py on the command line, it actually activates the Python interpreter, which is compiled and explained.

When the Python program runs, the result of the compilation is saved in the Pycodeobject in memory, and when the Python program finishes running, the Python interpreter writes Pycodeobject back to the PYc file.

When the Python program runs for the second time, the program will first look for the PYc file on the hard disk, and if it is found, load it directly or repeat the process.

Ii. python data types

1. Digital

Int,long (Long-integer) #Python3没有长整型

Float (float type)

2. Boolean values

True or FALSE, 1 or 0

A = 1

If a:

Print ("a")

3. Ternary operation

result = value 1 if condition else value 2

4.16 Notation of the binary

h suffix, or, 0x prefix

5. Encoding and decoding

Text is always Unicode, represented by the STR type

Binary data is represented by the bytes type

Coding Encode:str >>> bytes

Decoding decode:bytes >>> str

1 " I love Beijing Tian ' an gate " 2 Print (msg) 3 Print " Utf-8 " ) )4print(Msg.encode (encoding="utf-8"). Decode (encoding="utf-8"))

Third, the use of the list

names = ["Yuji", "Yuji", "Xiangyu", "Nvwa", "Yangjian"]
Print (names)
Print (names[0],names[2])  #打印第1个和第3个

Slice:
Print (Names[1:3])  #顾头不顾尾
Print (names[-1])  #取最后一个
Print (names[-2:])  #取最后两个
Print (Names[:3])  #取前面3个
Print (Names[0:-1:2])  #跳着切片
Print (Names[::2])  #0和-1 can be omitted

Various uses:
Names.append ("Luban")  #追加到后面
Names.insert (1, "Zhangfei")  #1为插入位置的下标
Names[0] = "Chengyaojin"  #更改
Names.remove ("Luban")  #删除
Del Names[0]
Names.pop ()  #默认删除最后一个
Names.pop (1)

Print (Names.index (' Yuji '))  #查看元素的位置
Print (Names.count ("Yuji"))  #计数
Names.clear ()  #清空
Names.reverse ()  #反转顺序
Names.sort ()  #排序, sorted by special symbols > Numbers > Letters, which is the order of ASCII code  


Names2 = [1,2,3,4]
Names.extend (names2) #扩展


Shallow copy: Used to create a federated account
1 ImportCopy2person = ['name',['Salary', 100]]3P1 =person[:]4P2 =person[:]5P1[0] ='Fangshao'6P2[0] ='Kuang'7P1[1][1]=508 Print(P2,P1)

Deep copy

1 ImportCopy2names = ["Yuji","Yuji","Xiangyu",["Libai","Hanxin"],"Nvwa","Yangjian"]3name2 = copy.deepcopy (names)

Meta-group

Tuples are read-only lists and cannot be modified, so only the count and index two usages.

Four, String common operation (string cannot change)




















Python Learning notes (2)

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.