Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Programming style and object description of Python

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "Python programming style and object description". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1) #! / usr/bin/env python

2) # _ * _ coding:utf-8_*_ (it seems to be written as # coding=utf-8)

In fact, the above statement can be written as long as it conforms to coding [: =]\ s * ([-\ w.] +); it means that coding should be closely followed by a colon or equal sign, followed by as many spaces as you want, and then add any character before and after utf-8,coding=utf-8.

\ w matches the characters that make up the word.

\ s represents a space

3) on the third line, it is best to write a script document in double quotes to explain the script.

"."

4) Multiline comments, beginning with three double quotes and ending with three double quotes.

"

.

.

"

Note that the first line many people will write as #! / usr/bin/python, if the system default python to write this is correct, but if you upgrade a python, then this new version of python may be installed in the usr/local/bin directory, if you add env, you will directly use the new version of python, because the new python will be automatically written in env.

Programming style

1. Syntax requirements, indentation unification: belong to the same code block, that is, at the same level, language indentation that does not depend on each other must be consistent, otherwise an error will be reported

Note that if the code is written in notepad under window, when indenting with tab, you need to convert the tab build and space bar in notepad, which can be set in the preferences.

two。 Variables are made up of letters, underscores, and numbers

The first character of the identifier must be a letter (both uppercase and lowercase) or an underscore

The rest of the identifier can consist of characters (uppercase and lowercase) *, underscores, or numbers (0-9).

Examples of invalid identifiers are 2Things; this is a man; my-name

3.python compilation process-script interpretation language

After running the script, before you get the result

1) python compiles the script to bytecode (pyc,pyo) first

As long as the import function is in the script, the pyc file will be generated.

2) the python virtual machine interprets and runs the bytecode file

C language-- the Internal Mechanism of compiled language

1) compile the source code into machine code and generate an executable file

2) run the executable file

4. Dynamic semantics-- determining the data type when assigning values

For example, type 5 b=string, then use type (a) = int; type (b) = string

5. The Zen of python

Enter python and type import this to see

All data in python are objects.

A whole that contains a series of data and the methods that manipulate that data is called an object.

All variables are references to data objects

1. Reference count within python, sys.getrefcount

Import sys

A couple of hellos

Sys.getrefcount ('hello')

Output to 3---python with an initial number of 3

Estrangement hello`s

Sys.getrefcount ('hello')

Output is 4

Axi1

Sys.getrefcount ('hello')

The output is 3, indicating that the reference was destroyed once.

two。 Composition of data types

The composition is divided into three parts.

Identity: id method to take a look at his unique identifier, memory address depends on this!

Type: type, take a look.

Value: represents a data item.

Aojiao conclusion in a book: everything in python is pointers, so you don't have to think about pointers anymore!

3. Basic data types are commonly used.

Int integer

Boolean Boolean

A=True

B=False

Note: the first character of a Boolean value must be capitalized

String string

List list

Tuple Yuanzu

Dict dictionary

4. The mutability and immutability of the data type, which means whether the id value changes after the value of the variable changes.

Immutable type: int,string,tuple

For example, a = 5, look at the id value; then a = 6, and then look at the id value, the two values are different.

Variable type: list,dict

For example, a = [1, 2, 2, 3]

A.append (4), at this time a = [1 ~ 1 ~ 2 ~ 3 ~ 4], but the id value of an is unchanged before and after the change.

5. If a variable is assigned a value, its data type is determined and cannot be changed at will

For example, if a = "4", the operation of aq1 cannot be carried out at this time, because "4" and 1 are not of the same data type and cannot be calculated. If you want to calculate, you need to convert the string a to an integer, which is int (a) + 1.

This is the end of "Python programming style and object description". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report