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

Cloud Computing Development Technology Python Automation Operation and maintenance Development practice II

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Cloud computing development technology Python knowledge is indispensable, Python programming is profound, knowledge points are numerous, you need to first understand some of the basic usage of Python as a whole and then study each knowledge point carefully, so that the learning speed will be much faster. So let's look at some Python basics you need to know beforehand.

Interactive mode programming:

Interaction is interacting with the user, that is, the person we use python. You give instructions or code, and the python interpreter gives the result. The call interpreter does not go through the script file as an argument and displays the following prompt:

# python

Python 2.7.10 (default, Jul 14 2015, 19:46:27)

[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>>

Type the following text at the Python prompt and press Enter:

>>> print "Hello, Python! " #python2.0

>>> print ("Hello, Python! ") #python3.0

Hello, Python!

IPython is highly recommended and installed at blog.51cto.com/fklinux/2046741

Scripted mode programming:

The so-called script programming, that is, python instructions or code written in text files, and then to these instructions specify a command interpreter, this file file is python script.

For example: Write the following code in a test.py file

print "Hello, Python! "

Run the program:

# python test.py

Hello, Python!

Another way to execute Python scripts is to modify the test.py file:

#!/ usr/bin/python

print "Hello, Python! "

Run the program:

# chmod +x test.py

#./ test.py

Hello, Python!

Using Chinese in Script Mode Programming

Python uses ascii code by default and does not support Chinese. To use Chinese, you need to declare a character set that supports Chinese, generally utf8, in the following way:

#!/ usr/bin/python

#coding=utf8

#encoding:utf-8

#_*_ coding:utf-8 _*_

print "Hello China! "

Why are there so many ways to write? Python is a regular way to detect whether there is anything he wants to see in your character set definition, as long as it meets the following rules:

coding[:=]\s*([-\w.]+)

[root@wing python]# cat a.py

#!/ usr/bin/env python

#fdsf coding=utf8 fdaf For example, fdsf fdaf here is something I randomly wrote as long as there is coding utf8 in it, etc.

print "Hello China"

Note: Python3 can support Chinese directly, and it is not necessary to specify a character set that supports Chinese

Python identifier:

1. A name used to identify a variable, function, class, module, or other object.

2. An identifier starts with the letters A to Z or a to z followed by zero or more letters underscore (_) and digits (0 to 9), punctuation characters such as @,$, %, etc. are not allowed within Python identifiers.

3. Python is case-sensitive.

Identifier naming conventions:

Class names in upper case and all other identifiers in lower case.

An identifier that begins with a single leading underscore indicates that the meaning agreed upon by the identifier is private.

An identifier with two leading underscores indicates a strongly private identifier.

If the identifier also ends with two underscores, the identifier is a language-defined special name.

Avoid starting variable names with underscores:

Because underscores have special meaning to interpreters and are symbols used by built-in identifiers, it is recommended that programmers avoid starting variable names with underscores.

In general, variable names '_xxx' are considered "private" and may not be used outside modules or classes.

When variables are private, it is a good practice to use_xxx to represent variables.

Because variable names__xxx__have special meanings for Python, this naming style should be avoided for ordinary variables

Key words:

Reserved words may not be used as constants or variables, or any other identifier. All Python keywords contain only lowercase letters.

Query all keywords in the current version of Python:

>>> import keyword #If you don't have this module in your system, you need to install python-docs

>>> keyword.kwlist

['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

Determine whether it is a keyword:

>>> keyword.iskeyword ('False')

True is returned.

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

Servers

Wechat

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

12
Report