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

How to get started quickly with Python language

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article to share with you is about Python language how to get started quickly, Xiaobian feel quite practical, so share to everyone to learn, I hope you can read this article after some harvest, not much to say, follow Xiaobian to see it.

[Introduction]

Python is a dynamically interpreted programming language. Python can be used on Windows, UNIX, MAC and many other operating systems, as well as Java and. NET development platforms.

python logo

[Features]

Python is developed in C, but Python no longer has complex data types such as pointers in C.

Python has strong object-oriented features and simplifies object-oriented implementations. It eliminates object-oriented elements such as protected types, abstract classes, interfaces, etc.

3 Python code blocks are separated by spaces or tabs indented.

Python has only 31 reserved words and no semicolons, begin, end, etc.

Python is a strongly typed language, variables will correspond to a data type after creation, and different types of variables appearing in unified expressions need to be type converted.

[Creating a Development Environment]

1 You can download the installation package from www.python.org and install it via configure, make, make install.

2 You can also download the ActivePython package at www.activestate.com. (ActivePython is a binary wrapper around Python's core and common modules, a Python development environment published by ActiveState.) ActivePython makes Python easy to install and can be applied to a variety of operating systems. ActivePython includes some common Python extensions, as well as programming interfaces for Windows environments.) For ActivePython, if you are a Windows user, download the msi package and install it; if you are a Unix user, download the tar.gz package and extract it directly.

Python IDE, including PythonWin, Eclipse+PyDev plugin, Komodo, EditPlus

[Version]

Python2 and Python 3 are the two main versions.

Python2 is recommended for the following two situations:

1. When you have no full control over the environment in which you are about to deploy;

2 When you need to use certain third-party packages or extensions;

Python 3 is officially recommended and is a fully supported version in the future, and many feature improvements are currently only available on Python 3.

【hello world】

1 Create hello.py

2 Writing procedures:

if __name__ == '__main__': print "hello word"

3 Run the program:

python ./ hello.py

[Note]

1 Comment with a #followed by a space, whether it is a line comment or paragraph comment.

2 If you need to use Chinese comments in your code, you must add the following comments at the beginning of your python file:

# -* - coding: UTF-8 -* -

3 The following comments are used to specify the interpreter

#! /usr/bin/python

[File Type]

Python file types are divided into three types: source code, byte code, and optimized code. All of these can be run directly without compilation or concatenation.

2 Source code with.py extension, Python is responsible for interpretation;

3 The source file is compiled to produce a file with the extension.pyc, a compiled byte file. This file cannot be modified using a text editor. Pyc files are platform-independent and run on most operating systems. The following statements can be used to generate pyc files:

import py_compile py_compile.compile(‘hello.py’)

4 Optimized source files are suffixed with.pyo, i.e. optimized code. It also cannot be modified directly with a text editor, and the following commands can be used to generate pyo files:

python -O -m py_complie hello.py

[Variable]

1 Variables in python do not need to be declared, and variable assignment operations are the process of variable declaration and definition.

A new assignment in Python creates a new variable. Even if the names of variables are the same, the identities of variables are different. The id() function can be used to obtain variable identifiers:

x = 1 print id(x) x = 2 print id(x)

If a variable is not assigned, Python assumes that the variable does not exist.

4 Variables defined outside a function can be called global variables. Global variables can be accessed by any function inside the file and by external files.

5 Global variables are recommended to be defined at the beginning of the file.

You can also put global variables in a special file and reference them via import:

The gl.py file reads:

_a = 1 _b = 2

Reference global variables in use_global.py:

import gl def fun(): print gl._ a print gl._ b fun()

[Constant]

Python does not provide reserved words that define constants. You can define a constant class to implement the function of a constant.

class _const: class ConstError(TypeError): pass def __setattr__(self,name,vlaue): if self.__ dict__.has_key(name): raise self.ConstError, "Can’t rebind const(%s)"%name self.__ dict__[name]=value import sys sys.modules[__name__]=_const()

[Data Type]

Python numeric types are divided into integer, long integer, floating point, Boolean, and complex types.

Python has no character type.

Python has no common types, any type is an object.

4 If you need to see the type of a variable, you can use the type class, which returns the type of the variable or creates a new type.

Python has three ways to represent string types: single quotes, double quotes, and triple quotes. Single quotes and double quotes work the same. Python programmers prefer single quotes, C/Java programmers prefer double quotes for strings. Single quotes, double quotes or newlines can be entered in the triple quotes.

[Operators and Expressions]

Python does not support the increment and decrement operators. For example i++/i-is wrong, but i+=1 is OK.

2 1/2 equals 0.5 before Python 2.5 and 0 after Python 2.5.

3 is not equal to!= or

4 equals ==

In logical expressions, and means logical AND, or means logical OR, not means logical NOT.

[Control statement]

1 Conditional statement:

if (expression) : statement 1 else : statement 2

2 Conditional statements:

if (expression) : statement 1 elif (expression) : statement 2 … elif (expression) : statement n else : statement m

3 Conditional nesting:

if (expression 1) : if (expression 2) : statement 1 elif (expression 3) : statement 2 … else: statement 3 elif (expression n) : … else : …

Python itself has no switch statement.

5 Loop statements:

while(expression) : … else : …

6 Loop statements:

for variables in collection: … else : …

7 Python does not support for(i=0;i) like c

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