Section One Python basics

Source: Internet
Author: User

Chicken Soup of the Soul:
1, today's bitter force is to not so always bitter force down!
2, the reason why today is hard, because it is not enough effort!
3, today's you, is a few years ago you decided!
4,be a loser for Now,or forever! (Momentary cock silk or forever cock silk)
5, do not and silly to reason, the idiot will pull you to and he a level, and then use his experience to beat you, to persuade.
############################
########### python feature #################
Python Benefits:
Simple, elegant, clear
Powerful modules (third party libraries)-----SMTP, Math, crawler, server programming, Web development
Easy to transplant
Object oriented
Extensible (can be combined with other languages, called Glue language)
Disadvantages:
Code cannot be encrypted
Slow (see applications, 0.1s and 0.0001s people are unable to feel, no difference)
What do you do with Python?
--Software development
Game background, search, graphical interface
Website
c\s Software
Scientific operations
--System Management
Script
It automation tools
C\c++,java (can be developed on Windows and Linux cross-platform), python,php (Web development language), Ruby,go (Google's language, Docker developed using the Go language)
Dynamic languages: Python, PHP, Ruby
Static language: C\c++,java,go
A static language needs to be compiled once before it is executed, which translates the plaintext code into machine instructions that the CPU can execute directly (dealing directly with the CPU, so it's very fast)
Dynamic language is an explanatory language, that is, the edge of the execution side of the interpretation (the first transformation of the CPU can be directly executed machine instructions, execute a little, then convert, and then in the execution ... ), so it's a little bit slower than a one-time compilation process.
Python version
python2.6 compatible with python2.4, but also compatible with some features of python3.0. python2.7 is the last version in 2.X, followed by version 3.0.
Third-party python development environment such as: Pythonxy,portable python (free installation version, U disk copy used in the past can be used directly)
View version: Python-v
>>> print ' Hello world! ' ----Python 2.X syntax
Hello world!
>>> print (' Hello world! ') -----python 3.X syntax
Hello world!
All Python scripts end with a. py
#!/usr/bin/env python--------Tell the system what language to use to execute this script
Python interpreter
Role: When executing a python script, the interpreter translates the written plaintext code into machine code that the machine's CPU can recognize, and the machine code is not a true binary instruction, but a coding between a human-readable code and a real binary instruction. Python is a dynamic language, so it is an edge execution and a side explanation.
-cpython Official standards
-ipython is a python interactive shell that works much better than the default Python shell, supports variable auto-completion, auto-indent, supports bash shell commands, and has many useful functions and functions built into it. Can be understood to do some retouching on the basis of CPython, but the actual execution of the time is called CPython.
-jython is a complete language, not a Java translator or just a python compiler, it's a full implementation of the Python language in Java (with Java completely rewritten in Python, and the interpreter as a Java-written interpreter, Not a C interpreter). Jython also has many module libraries that inherit from Cython. The most interesting thing is that Jython is not like CPython or other high-level languages, it provides all access to its implementation language---so Jython not only provides you with a Python library, but also provides all of the Java classes, which gives it a huge repository of resources.
-ironpython is similar to Jython, but an interpreter written in. Net
-pypy is a python-written interpreter (it is said that the speed is 6 times times faster than the CPython, fast because: Dynamic compilation (just in time), before executing, the code in the class and functions first compiled, so the execution will be faster "understand." Simple understanding: A Chinese sentence a sentence to the other side, into a Chinese 10 sentence 10 translation to tell each other)
########## Programming Style Requirements ##################
-Indent Unity
Code that is not at the same level must be indented! Code indentation in the same level must be consistent!
Functions defined in Python are not executed by default and will only be executed if they are called in the appropriate place.
A function is defined, even if it is not called, and it is checked for syntax. Before CPU execution, Python will pass functions to the interpreter to program CPU executable instructions, so before the CPU executes, in order to avoid the crash, the whole code will be checked for syntax error, indentation error, variable definition error, etc.
-variables (i.e. identifiers)
Generally all uppercase variable names to represent constants (constants are not variable amount, but if you must change the value of the constant, no one can stop you, just a habit), lowercase letters to represent variables, for example:
pai=3.1415926
x=2
The first character of an identifier must be a letter in the alphabet (uppercase or lowercase) or an underscore ('_'), which cannot begin with a number.
Other parts of the identifier name can consist of a letter (uppercase or lowercase), a number (0-9), and an underscore (_).
The identifier name is case-sensitive. For example, MyName and myname are not identifiers.
Examples of valid identifiers are I, __my_name, name_23, and ALB2_C3.
An example of an invalid identifier is 2things, this is spaced out, and my-name.

Note: variable names generally use nouns instead of verbs, and if it is a combination of two nouns, use the following example:
Taskdetail (not recommended)
Task_detail (OK)
Taskdetail (OK)
Taskdetail (OK Hump shape)

eg
[email protected] robin]# cat hello.py
#!/usr/bin/env python
#--Coding:utf-8--#
def main ():
print ' Hello '
Main ()
[email protected] robin]# python hello.py
Hello
The indentation is treated as follows:
[email protected] robin]# cat hello.py
#!/usr/bin/env python
#--Coding:utf-8--#
def main ():
print ' Hello '
Main ()
[email protected] robin]# python hello.py
File "hello.py", line 4
print ' Hello '
^
Indentationerror:expected an indented block
Note: Indentation is the meaning of indentation
[email protected] robin]# cat hello.py
#!/usr/bin/env python
#--Coding:utf-8--#
def main ():
print ' Hello '
print ' = = ' #把这行和def的缩进即使相同也会报错. This is not a nested case must be the same and the head can!
Main ()
[email protected] robin]# python hello.py
File "hello.py", line 3
def main ():
^
indentationerror:unexpected Indent
Note: Code that is not at the same level must be indented! The same level of code indentation must be consistent, otherwise the error is as above!
A diagram of a variable point in Python:
650) this.width=650; "id=" image_operate_47221428724070141 "src=" http://s16.sinaimg.cn/mw690/005XFiLizy6Rpln9yUf6f &690 "alt=" 005xfilizy6rpln9yuf6f&690 "height=" 536 "width=" 552 "/>
###### #python的数据类型 ##########

1, according to the characteristics of the division:

Number type:

Integral type : Boolean (true| False), long Integer (L), standard integer type

Note: Python will automatically convert the growth integer (L) when the stored data is large

Eg:1

>>> a=2**5
>>> A
32
>>> type (a)
<type ' int ' >
>>> a=2**100
>>> A
1267650600228229401496703205376L
>>> type (a)
<type ' Long ' >

non-integral : double-precision floating-point, plural, decimal (not built-in type)

sequence Type : string (str), tuple (tuple), list

EG1:

>>> name= ' Alex '
>>> type (name)
<type ' str ' >
>>> if Type (name) is Str:print ' DDD '----determine variable type by type built-in function
...
Ddd

EG2:

>>> name_list = [' Alex ', ' Robin ', ' Rock ']
>>> type (name_list)
<type ' list ' >

Image Type : Dictionary (dict)

EG1:

>>> name={' Alex ': [+, ' IT ']}
>>> name[' Alex ']
[The ' IT ']
>>> type (name)
<type ' Dict ' >

collection type: mutable set (set), immutable set (Frozenset)

2, according to the variability divided :

Hash, immutable data type: numeric type, String (str), tuple (tuple), immutable collection (Frozenset)

Variable Data types: Dictionary (dict), List, mutable collection (set)

######## #运算 ###################

>>> 1+1*3/2
2
>>> 1+1*3.0/2
2.5

>>> (*3.0/2)
3.0
>>> 2**32
4294967296

>>> a=14
>>> b=12
>>> a>b
True
>>> a<=b
False
>>> a!=b
True

For more Python operations, please refer to: http://www.w3cschool.cc/python/python-operators.html

Video: 30:51

Section One Python basics

Related Article

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.