Python practice-common strings and mathematical functions

Source: Internet
Author: User
Tags mathematical functions cmath

Python is very convenient to process strings. It takes some time to make a summary:

------ Function -------------------- meaning --------------------

ABS (number) returns the absolute value of a number.

Cmath. SQRT (number) returns the square root, which can also be applied to negative numbers.

Float (object) converts string and number to floating point

Help () provides interactive help

Input (prompt) to get user input

INT (object) converts string and number to integer

Long (object) converts string and number to long integer

Math. Ceil (number) indicates the upper integer of the returned number. The type of the returned value is floating point.

Math. Floor (number) returns an integer in the lower house of the number. The type of the returned value is a floating point number.

Math. SQRT (number) returns the square root, not applicable to negative numbers

Pow (X, Y [, Z]) returns the Y Power of X (returns the result modulo Z)

Raw_input (prompt) gets user input. The returned type is string.

String Representation of the Repr (object) Return Value

Round (number [, ndigits]) rounds the number according to the given precision

STR (object) converts a value to a string

------------------------------------------------------

Input (): print the string and use the result as a new prompt.

Then Input 6

>>>x=input("please input x:")please input x:6>>>print(x)6

Raw_input (): all inputs are treated as raw data in the same string.

It is different from input ().

Linux (this is not the case for testing on Windows)

When we enter the name, the following error will occur, and if the input name is in the form of a string, it will be okay.

>>> Name = input ("What is your name? ")

What is your name? "Loulijun"

This is acceptable, but the following method does not work.

>>>name=input("what is your name?")what is your name?loulijunTraceback (most recent class last):    File "<stdin>", line 1, in <module>    File "<string>",line 1, in <module>NameError:name 'loulijun' is not defined

The reason is that input considers the user to input a valid expression, but it is not actually

This problem can be avoided by using raw_input ().

>>>name = raw_input("what is your name?")what is your name?loulijun>>> print(name)loulijun

Pow (): Calculate the multiplication party

Same effect **

>>> 2**38>>>pow(2, 3)8

How to use the extension module

You can import the module by importing math or from math import SQRT.

>>>import math>>>math.floor(32.9)32.0>>>from math import sqrt>>>sqrt(9)3.0

Cmath Module

Because the math module can only process floating-point numbers, errors such as virtual numbers are reported, such as SQRT (-2). An error is reported.

If you import cmath, you can use it. This is also an extension of math.

>>>import cmath>>>cmath.sqrt(-1)1j

  

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.