"Learning notes--python"python Informal guidance __python

Source: Internet
Author: User
Tags abs integer division stdin
3 Python Unofficial guide

In the example in this section, at the prompt >>>, ..., start with input, otherwise output, #后为python的注释Table of Contents 1 takes python as Calculator 1.1 number 1.2 string 1.3 Unico De-encoded string 1.4 List 2 Preliminary programming 1 think of Python as a calculator 1.1 digits

First go into interactive mode and use Python as a calculator

$ python
python 2.7.3 (default, Aug  1, 05:16:07) 
[GCC 4.6.3]
on linux2 Type ' help ', ' copyright ', ' cre Dits "or" license for the more information.
>>> 1 + 2
3
>>> 1-2
-1
>>> 2 * 3
6
>>> 2/4
0
  >>> 2/0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Zer Odivisionerror:integer division or modulo by zero
>>> (1+2) *3
9

You can also assign a value to a variable and then participate in the operation

>>> x = 3
>>> y = 4
>>> (x + y) * 5
35

You can also assign values consecutively, but you cannot use variables that are not defined

>>> x = y = z = 3.4
>>> x
3.4
>>> y
3.4
>>> C
Traceback (most Recent call last):
  File "<stdin>", line 1, in <module>
nameerror:name ' C ' isn't defined
>> ;> Z
3.4

At the same time, Python also supports complex numbers, which can be represented by a + BJ, or complex (A,B)

>>> 1 + 2j
(1+2j)
>>> 2j * 3j
( -6+0j)
>>> Complex (1+2j) * Complex (1-2J) c15/> (5+0J)

Also, you can assign a complex number to a variable, and then extract the real part, the imaginary part

>>> x = (3+4j)
>>> x.real
3.0
>>> x.imag
4.0

float (), int (), converts values to floating-point and integral types, but does not apply to complex numbers, which can be obtained by an ABS () or as a modulo.

>>> Int (3.4)
3
>>> Int (3.5)
3
>>> float (3)
3.0
>>> A = 1 + 2j
>>> a
(1+2j)
>>> Float (a)
Traceback (most recent call last):
  File "< Stdin> ", line 1, at <module>
Typeerror:can ' t convert complex to float
>>> ABS (a)
2.23606797749979
>>> ABS (3+4J)
5.0

In interactive mode, you can use _ to represent the results of the last calculation

>>> 1 + 2 
3
>>> 4 + _
7
>>> 5 * _
35
1.2 String

In addition to math, Python can manipulate strings, pay attention to escape characters, single quotes, double quotes, and try a few examples to realize

>>> ' Hello,python '
hello,python '
>>> ' Hello,python '
' Hello,python '
> >> ' don ' t '
  File ' <stdin> ', line 1
    ' don '
         ^
syntaxerror:invalid syntax
> >> ' don\ ' t '
"don ' t"
>>> "Dont ' t"
"Dont ' t"
>>> "don\ ' t"
"don ' t"

Strings can be divided into multiple lines and printed with the print function

>>> Hello = "This is a string which is \n\ ...
used to test multi\ ...
lines."
>>> print Hello this is
a string which was 
used to test multilines.

Note the newline character \ n, as well as the use of multiple lines of editing \ These two symbols.

You can also use three quotes so that you do not have to write these two escape characters.

>>> Hello = "" "
... usage:thingy [OPTIONS]
...-h      display this Usage message ...-
h      hostname display host name
... "" "
>>> print Hello

usage:thingy [OPTIONS]-
h      Display this Usage      message-H Hostname Display Host Name

If you do not need an escape character, \ n means \ n, does not represent escape, you can start with an R action string

>>> Hello = r "This is \ n a test line"
>>> print Hello this is
\ n a test line
>>> Hello = "This is \ n a test line"
>>> print Hello this are 
 a test line

Strings can be connected, and can even be "multiplied" (in fact, also connected ~)

>>> words = ' A ' + ' hello ' + ' B '
>>> print words
A Hello B
>>> ' < ' + words*3 + ' > '
<a hello ba hello ba hello b> '

The string can also be extracted, subscript starting from 0, subscript can be negative, representing the reciprocal str[i:j] representing a total of j-i characters from the str subscript i to subscript (j-1). Str[_:3] means Str[0:3] str[2:_] represents subscript 2 beginning to the last

>>> words = "Hello"
>>> words[0]
' h '
>>> Words[0:3]
' hel '
>> > Words[1:2] '
e '
>>> words[:3]
' hel '
>>> words[2:]
' Llo '

> >> words= "Hello"
>>> words[-1]
' o '
>>> words[-2]
' l '

Letters cannot be modified in strings, only strings can be reconstructed

>>> words
' hello '
>>> words[0] = ' A '
traceback (most recent call last):
  File "  <stdin> ", line 1, in <module>
typeerror: ' str ' object does not support item assignment
>>> words= "HI"
>>> words
' Hi '

String length can be obtained by using the Len function

>>> words= "Hello"
>>> len (words)
5
1.3 Unicode-encoded strings

Use the U prefix to define Unicode encoded strings use Unicode functions to convert other forms of encoding to Unicode use encode to convert Unicode form encoding to another form

>>> u ' Hello python! '
U ' Hello python! '
>>> Unicode (' \xc3\xa4\xc3\xb6\xc3\xbc ', ' utf-8 ')
u ' \xe4\xf6\xfc '
>>> u ' äöü '. Encode (' Utf-8 ')
' \XC3\XA4\XC3\XB6\XC3\XBC '
1.4 List

Python has a series of data structures that can be used to combine multiple values, with the most versatile number list

>>> a = [' Spam ', 3, 4.5, "HI"]
>>> a
[' Spam ', 3, 4.5, ' Hi ']
>>> a[0]
' spam '
>>> a[1]
3
>>> a[-1]
' Hi '
>>> a[1:-1]
[3, 4.5]
>>> 2*a[:3] + [' xxx ']
[' Spam ', 3, 4.5, ' spam ', 3, 4.5, ' xxx ']

Unlike strings, the elements of the list can be changed

>>> a
[' Spam ', 3, 4.5, ' Hi ']
>>> a[1] = 6
>>> a
[' Spam ', 6, 4.5, ' Hi ']

The list can also be easily inserted, replaced, and other operations

>>> a
[' Spam ', 6, 4.5, ' Hi ']
>>> a[1:1] = [' xxx ', ' yyy ']
>>> a
[' spam ', ' xxx ' , ' yyy ', 6, 4.5, ' Hi ']

>>> a = [' X ', ' y ', ' z ']
>>> a
[' x ', ' y ', ' z ']
>>> a[0: 2] = [' A ', ' B ']
>>> a
[' A ', ' B ', ' Z ']

Len can also get the number of elements in the list.

>>> a
[' A ', ' B ', ' Z ']
>>> len (a)
3
2 Preliminary Program Design

As you can see from the previous section, Python is quite flexible and easy to use, starting with the first example of program design, from the famous Fibonacci series:

>>> # Fibonacci series ... 
# The sum of two elements defines the next
... a, b = 0, 1
>>> While B <:
...     Print B ...     A, B = B, a+b ... 
1
1
2
3
5
8

The above program is very simple, do not elaborate, from which you can also understand the program written by Python concise and easy to understand, dapper need to note that while the program block is not like c,java those surrounded by parentheses, but the use of indentation if you do not want to change the line in the output, you need to add a comma

>>> A, b = 0, 1
>>> while B <:
...     Print B,
...     A, B = B, a+b ... 
1 1 2 3 5 8

As a Python beginner, I can only say that my heart has fallen deeply into the lake.


Note: Original: http://docs.python.org/2/tutorial/introduction.html

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.