2. Toad Python Script learning note Two basic commands to play

Source: Internet
Author: User
Tags pow stdin

2. Toad Python Script learning note Two basic commands to play

This famous article:"Success stems from the discovery of details, no details, no opportunities, attention to detail means to create opportunities." A commonplace trifle may be the key to opening the treasure trove of opportunities! "

Come home from work, let's take a look at some common basic commands.

Welcome reprint, Reprint please indicate source: http://blog.csdn.net/notbaron/article/details/48092873

1. Numbers and Expressions

Look at Figure 1 to illustrate a lot of questions:


addition, divisible, floating-point, modulo, power, etc. is not very direct and very rude.

On the upper limit, toad is not quite sure what the upper limit is, anyway below Figure 2 can be executed


In-process conversion

>>> 0xaf

175

>>> 010

8

>>>

See these, the toad has been conquered, you?

2. Variables and statements

Variables are used like other shell scripts or something.

>>> x=2

>>> x*2

4

>>> x*3

6

>>> Print 3*x

The statement is not an expression that has no value for the interactive interpreter to print out. such as the x=3 statement.

3. Get input

Get input this thing must, all kinds of languages will have. C in the Scanf,shell in read and so on.

Python is input.

Such as:

>>> x=input ("thevalue=")

The value=20

>>> Print X

20

4. Functions

Python When we install the interpreter, in fact, it provides a lot of commonly used functions, we can directly use, and do not need to go to their own implementation.

Such as:

>>> Pow (2,3)

8

>>> 10+pow (2,3*5)/3.0

10932.666666666666

>>> ABS (-10)

10

>>> Round (1.0/2.0)

1.0

5. Module

Modules are extensions that are imported into Python to enhance their functionality. You need to use the Import command.

>>> Math.floor (32.9)

Traceback (most recent):

File "<stdin>", line 1, in <module>

Nameerror:name ' math ' is not defined

>>> Importmath

>>> Math.floor (32.9)

32.0

There is no error when importing, you can use it directly after importing.

Call here to enter the module name, if you want to not enter the module name, you can import

>>> frommath Import sqrt

>>> sqrt (9)

3.0

>>> Math.sqrt (9)

3.0

>>> Floor (32.9)

Traceback (most recent):

File "<stdin>", line 1, in <module>

Nameerror:name ' floor ' are not defined

Of course we find that floor is still not directly available because we just imported the SQRT function with the command from math import sqrt.

It's interesting to have a library of complex numbers, you guys can see.

>>> Import Cmath

>>> (1+3j) * (9+4j)

( -3+31j)

Can handle the plural, toad smiled.

6. Save and execute

Like a shell script, just put a stack of commands in a file, and then call the file directly with the Python command to execute the program.

Comments are similar to many other scripts, such as # and Shell.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

2. Toad Python Script learning note Two basic commands to play

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.