[Python learning note 1] Python language base mathematical operator string list

Source: Internet
Author: User
Tags integer division

This series is my note in the process of learning the Python language, mainly a number of knowledge points, rather than learning tutorials, for a certain programming basis for reference. The prejudice and insufficiency in the text are unavoidable, for reference only, and the criticism is welcome.

The main references in this series of notes are official documents: http://docs.python.org/. Thank the document editor here. Do not use this article for commercial purposes.

First, the Python language introduction

First, Python is a widely used general-purpose advanced programming language with a high level of abstraction and support for object-oriented programming methods. It has advanced data structure and many convenient library files, which can complete file IO, system call, network programming and even GUI library. Python is highly readable and can improve development efficiency relative to other programming languages (C,c++,java). However, as an interpreted language (interpreted language), Python's advantages in operational efficiency are not obvious. So people often use Python to do simple, high-level tasks, in large-scale development often as "glue language" to connect the modules.

Python runs on both the Windows and Linux series operating systems. Some versions of Linux have their own Python programs, and the binary versions are available for download on their official website: http://www.python.org. (the Python version described in this article series is 3.4.3)

Ii. using the Python interpreter

In the console, enter the command "python3.4" (or "Python") to run the Python interpreter, the console prints out the Python version information, and gives a prompt >>>

 1  $ python3.42  Python 3.4 (default, Mar, 09:25:04)  3  [GCC 4.8.2] on Linux  4  Type  " help  , "   Copyright  , "  credits   or   " license  "  for   more information.  5  >>> 

Python's Prompt ">>>" can enter a statement, press ENTER to execute the statement. If the interpreter requires more than one statement, it gives a "..." prompt, prompting for subsequent statements.

Variables in Python do not need to be declared (or even typed) to be used directly. Enter the statement here:

>>>a=1>>>

That is, a variable is defined. This time input expression A, you can display the value of a:

>>> a1>>>

Third, expression calculation

Here, you can try using Python as a calculator to do some simple numerical calculations.

Operator Explain A similar expression in C Example

A+b

A-B

A*b

A/b

Subtraction, Division is floating point division

A+b

A-B

A*b

A/b

>>> a/30.3333333333333333>>> 2.3+6.99.2>>>

a//b

A%b

Divisible, return integer division

Modulo, returns the remainder of A/b

A/b
>>> 1//30>>> 50//316>>>

A**b Power operation Pow (A, B)
>>> 2**0.51.4142135623730951>>> ( -1) **0.5(6.123233995736766e-17+1j)>> > _**2(-1+1.2246467991473532e-16j)>>> 2**1001267650600228229401496703205376> >>1J**1J (0.20787957635076193+0J) >>>

Where the underscore _ represents the result of the previous operation, (A+BJ) represents the complex number (a+bi).

( ) Brackets, same as C language ( )

Four, string

The string (str) in Python can be enclosed in single or double quotation marks, and single and double quotes can be nested with each other. Examples of official documents are:

1>>>'Spam eggs'  #Single quotes2 'Spam eggs'3>>>'doesn\ ' t'  #Use "to escape " the single quote ...4 "doesn ' t"5>>>"doesn ' t"  #... or use double quotes instead6 "doesn ' t"7>>>'"Yes," he said.'8 '"Yes," he said.'9>>>"\ "Yes,\" he said."Ten '"Yes," he said.' One>>>'"Isn\ ' t," she said.' A '"Isn\ ' t," she said.'

    • Where the section begins with # as a line comment
    • Single quotes in single quotes need to use escape character \ Bootstrap
    • Note \ is an escape character in line 12

The escape character can also escape the backslash ' \ \ ' and the line break ' \ n '. You can use the source string (raw strings) to avoid backslashes being treated as escape characters, just add R before the string.

 1  >>> print  ( '  ) #   here \ n means newline!  2  c:\some  3  4  >>> print  (R " c:\some\name  ) #   Note the R before the quote  5  C:\some\name 
    • The print function prints the value of the parameter. For numeric values, print their values, and for strings, print their contents.
    • The print function can take multiple parameters and print a space between the parameters. Like what:
      Print (1, ( -1) **0.5,'Hello,world')1 (6.123233995736766e-17+1j) Hello, World >>>
    • The print function prints a newline character at the end of the line, and at the end of the last argument, you can specify the end of the row (string):
      Print ' Hello, World ', end='| ' )1 (6.123233995736766e-17+1j) Hello, world|>>>

There are other ways to represent strings. In the same way as C, two strings with a space or tab interval are automatically merged together:

>>>"Hi,"'Py' 'thon' ' Hi,python '

A multiline string can be enclosed in three quotes. A string enclosed in three quotation marks can include a visual line break. If a backslash is added at the end of the line, the backslash, along with the following newline character, is ignored:

1>>>Print("""2 ... usage:thingy [OPTIONS]3 ...-h Display this usage message4 ...-h hostname hostname to connect to5 ... """)6 usage:thingy [OPTIONS]7-h Display This usage message8-H hostname hostname to connect to9 Ten>>>
    • 2-5 rows of three points is a prompt
    • Line 1th has been used \ implements the continuation function, so u becomes the first character of the string
    • The last character in the string is the line break (line break at the end of line 4th), which is printed to the end of line 8th and the line break at the end of line 9th is the print default

The string and the list mentioned below are all sequences (sequence), and the supported operations are referred to in the introduction list.

V. List

A list is a data structure in Python, similar to a generalized table. In the Python syntax, the list is represented as a comma-separated set of elements enclosed in brackets. The type of the element can also be a list. The types of different elements allow different.

>>> squares = [1*1, 2*2, 3*3, 4*4, 5*5]>>> squares[1, 4, 9, +]>>&G T [squares,0][[1, 4, 9, +, +], 0]>>>

The list object has some "methods" that can be changed by invoking the contents of the listing:

Method Example

list. Append (item)

Inserts the item element into the last face of the list

>>> squares=[1,4,9,16,25]>>> squares.append >>> squares[  1, 4, 9, approx., +-->>>

list. Insert (n,item)

Inserts the item into the nth position of the list and if n is out of range, insert to both sides

>>> squares=[1,4,9,16,25]>>> squares.insert (3, 0)>>> Squares [1, 4, 9, 0, +,]>>>

The following are the operations that are common to strings and lists.

Operation Example

seq1+seq2

Connect Two sequences

seq*int

int*seq

repeats a sequence several times
'+'new year! ' ' Happy New year! '>>> [2]+[3]*2[2, 3, 3]>>>

seq [ i ]

Extract the element labeled I in the sequence. The subscript here is similar to the C language, the first element is labeled 0, the second element is labeled 1, and so on. Python also has the penultimate element labeled-1, the second-to-last element is labeled-2, and so on.

  • generates an error if the subscript is out of bounds
  • specifically, elements inside strings are read-only, and statements like ' Hello ' [0]=0] produce errors
 >>&G T   hello    [1   e    >>> a=[1,2,3,4,4"  >> > A[4]=5>>> a[ 1, 2, 3, 4, 5 >>> 

seq [begin:end]

intercept (slice) The elements from begin (inclusive) to end (not included) in a sequence. (a friend who has used a standard library of C + + should be familiar with the rule that begin is the first subscript of the Intercept section, and end is the subscript after the end.) )

  • This operation does not produce an subscript out-of-bounds error, but may return an empty sequence (empty string ' or empty list [])
  • The operation returns a copy of the original sequence, that is, assigning the intercept to other variables, and modifying the new sequence with the new variable does not affect the contents of the original sequence.
  • You can implicitly specify the beginning and end by omitting the begin and/or end (line 7th of the example)
  • The contents of the original sequence can be changed by assigning a value to the expression (example line 7th)

1>>>a2[2, 3, 4]3>>> a=[1,2,3,4,5]4>>> B=a[1:3]5>>>b6[2, 3]7>>> b[:]=[]8>>>b9 []Ten>>>a One[1, 2, 3, 4, 5] A>>>

In addition to the above operators, a frequently used built-in function len (seq) can calculate the length of a string or list.

[Python learning note 1] Python language base mathematical operator string list

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.