The most basic formula of the Python list Example

Source: Internet
Author: User
Tags python list

Many of the Python list examples do not know what is going on. In fact, this is a very interesting thing. This is also one of the reasons why many developers are infatuated with the Python language. Next, let's take a detailed look at how to solve the relevant problems.

  • Basic Environment Test for Python Programming
  • How does Python IDE adapt to the current turbulent market
  • Performance Comparison Between the Python programming language and Java
  • Summary of advantages in the use of Python Programming Language
  • Python for beginners

Generally, programmers Fall In Love With Python because it increases productivity. Since there is no compilation process, the edit-test-debugging cycle is quite fast. Debugging a Python program is simple: An error will never lead to a segment error. When the interpreter finds an error, it raises an exception. When the program does not catch exceptions, the interpreter prints a stack trace. A source code-level debugger allows us to check local and global variables, calculate expressions, set breakpoints, and perform single-step tracking. The debugger is written in Python, which proves Python's ability. In addition, the fastest way to debug the program is to add several print statements: Quick edit-test-debugging cycle makes this simple method very effective.

Basic calculation formula of the Python list Example

Let's start with the subject and teach you how to use Python. I assume that the reader has the foundation of other languages and can directly focus on the syntax.

 
 
  1. 1 a = 0
  2. 2 B = 7
  3. 3 aa = a + 1
  4. 4 a = B *
  5. 5 print
  6. Result: 7

The above is a simple example of python. I believe that if you have learned other languages (such as C/C ++ and Java), you can easily understand it.

 
 
  1. A + B
  2. A-B A minus B
  3. A * B a multiplied by B
  4. A/B a except B
  5. A % B obtain the remainder of A/B (for example, 8% 3 = 2)
  6. -A: Obtain the inverse number of a (if A = 7,-A =-7)
  7. String
  8. 1 a = 'hello'
  9. 2 B = "world"
  10. 3 c = a + ''+ B + '!! '
  11. 4 print c

Result: hello world !!

String can be enclosed by the 'or "symbol. Row 3 is an example of merging four string objects and connecting the four strings to a single string.

 
 
  1. 1 a = '%s=%d' % ('test', 16)   
  2. 2 print a  

Result: test = 16

Similar to C/C ++'s printf or sprintf. In line 1, python provides the string format function. String '% s = % d' specifies the format of the string, followed by % after the string, followed by the format parameter, these parameters will replace % s and % d in format in sequence. % S indicates that the string is to be replaced, and % d is to be replaced with an integer.

 
 
  1. a = 'This is a rather long string containing\n\   
  2. several lines of text just as you would do in C.\n\   
  3. Note that whitespace at the beginning of the line is\   
  4. significant.\n'  

String can be extended to several rows, but escape \ is required at the end of each row to ignore newline. You can also use "" Or '''

 
 
  1. a = '''This is a rather long string containing   
  2. several lines of text just as you would do in C.   
  3. Note that whitespace at the beginning of the line is   
  4. significant.'''  

You do not need to create an escape when using ''' or ", but newline will contain string content.

 
 
  1. List   
  2. 1 a = []   
  3. 2 a[0] = 'aoo'   
  4. 3 a[1:3] = [10, 11]   
  5. 4 b = [1, 2, 3, 'foo']   
  6. 5 print a, b, b[:3], b[1:]   

The results show that [9, 10, 11] [1, 2, 3, 'foo'] [1, 2, 3] [2, 3, 'foo']

The above is the use of the Python list Example. List is a sequence data type, which is similar to the array of C/C ++, but array is fixed length, while list is not. Its length can be changed at any time. Line 1 shows bind a as an empty list. Row 2 specifies that index 0 is 'aoo' string object. Example of using slice with row 3 as list. Replace item (index 1 and 2) between index 1 and index 3 with 10 and 11. B [: 3] of Row 5 is equivalent to B [0: 3], while B [1:] is equivalent to B []. The items in the list do not need to be of the same type. In the above example, items in a list object can contain integers and items of different types.

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.