Python Tutorial Learning (iii)--an informal Introduction to Python

Source: Internet
Author: User

3.1. Use Python as the calculator 3.1.1. Number of Numbers
    • As a calculator, Python supports simple operations,
    • ' + ', '-', ' * ', '/' The earth people know subtraction.
    • () can be used to change the priority, just like the arithmetic priority in math.
    • ' = ' is used to establish the relationship between the expression and the variable, the popular point is to assign value. Afterwards, no result is displayed before the next interactive prompt (not see ...)
    • Variables must be defined before they are used.
    • Floating-point support: When using Python for mathematical calculations, Python will help you deal with the relationship between decimal (float) and integer (integers), and the resulting result must be a floating-point type.
    • Complex number support: the plural virtual step in Python needs to take the suffix J or J, the real part is not 0 of the complex number is expressed as (real + imagj), or can also be used complex (real, imag) function to generate.

The real and imaginary parts of complex z can be obtained by z.real and Z.imag

Type conversion function float (), Int () and long () are invalid for complex numbers

Use abs (z) to obtain its magnitude (as a float) or to obtain its real part by means of z.real

    • Interactive mode (command line), the final output of an expression will be assigned to the variable ' _ ', so sometimes you can simplify the operation by using ' _ '

This variable should is treated as read-only by the user. Don ' t explicitly assign a value to It-you would create an independent local variable with the same name masking the Buil T-in variable with its magic behavior.

3.1.2. Strings string

In addition to the number, Python also has a number of ways to handle strings.

Three ways to express a string:

    • ' This is a string '
    • "This is another string"
    • "" This is also a string "" "

Expand it.

' Hello I ' m zhangsan ' wrong

' Hello i\ ' m Zhangsan ' right

"Hello I ' m Zhangsan" right

In addition, there is a string representation that needs to be mentioned, which is

R ' This was a string and something like \ n won't work it's you wanna to converted it to newline '

"R" is the meaning of raw, the string surrounded by it will be the output of the original, like \ T and so on this time will not be converted to newline and carriage return, but the direct carriage return will take effect, such as

Print R ' Hello, I am \

Zhangsan '

The output will be like this.

Hello, I am

Zhangsan

Strings are available with the + and * operations

For example, ' This is a string ' and ' this is ' + ' a string ' gets the same result

' AAA ' can also be represented as ' a ' * 3

Like List, string can also be sliced, for example

print ' ABCDE ' [: 3] # ABC

print ' ABCDEFG ' [0:4:2] #ac

For W in ' words ':

Print W,

#output:

W o r d S (note I used a comma after print, so it won't wrap)

Len (' abc ') Gets the result of the string ' abc ' Length (3)

So a for loop above can do the same thing:

For I in range (len (' Words ')):

print ' words ' [i],

This will get the same result.

3.1.3. Unicode Strings

Defines a Unicode string as simple as defining a normal string

Creating Unicode strings in Python are just as simple as Creating normal strings:

Because it is too simple, so I do not write (haha, actually I do not know how to say ...)

3.1.4. Lists

The sequence as one of the basic Python formats is fantastic. Here are some simple examples of how to use the list.

Defining a sequence that looks a bit complicated is actually not complicated.

LST = [0, 1, 2, 3, 4, 5, ' A ', ' B ', [8, 888], ' 9 ', {' 10 ': 10, 10:100}]

LST[1] # 11 integers

LST[8] # [8, 888] a sequence

LST[9] # ' 9 ' a string

LST[10] # {' 10 ': 10, 10:100} A dictionary

It seems to be very flexible, it is so capricious.

Slice of List

Lst[2:6] #[2, 3, 4, 5, ' a ']

Lst[2:6:2] #[2, 4, ' a ']

Lst[-1:0:-1] #[{': Ten, 10:100}, ' 9 ', [8, 888], ' b ', ' A ', 5, 4, 3, 2, 1] is actually a reverse order

Lst[-1:0:-2] #[{': Ten, 10:100}, [8, 888], ' a ', 4, 2]

Len (LST) # 10

3.2. First Steps Towards programming

Python is not just for subtraction, for example, where we can use it to implement a Fibonacci sequence (a pair of rabbits, a three-month-old bunny ...)

 >>> # Fibonacci series: ... # the sum of the elements defines the Next ... ab = 0 1>>> while b < 10: ... print b, ... ab = b, a+b 
#output
8 about this function, there will be a more detailed introduction (directly define a function came out)

Python Tutorial Learning (iii)--an informal Introduction to Python

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.