Python as a calculator (mathematical usage)

Source: Internet
Author: User
Tags square root value of pi

1. Basic subtraction and take-rest operations

Print (5+10) Print (5-10) Print (5*10) Print (10/5) Print (5%3)2

2. Ask for n-Squared and square root

Print (5%3) Print (5**3) Print (125** (1/3)) Print (2**4) Print (16** ())4.0>>>

Use of the 3.math function library

Introduce the math library and see the value of pi

Import Math>>> math.pi3.141592653589793

(1) To find sine cosine function

>>> Math.sin (math.pi/2)1.0>>> math.cos (Math.pi/4)0.7071067811865476>> > Math.tan (Math.pi/4)0.9999999999999999>>>

(2) Rounding up and rounding down

>>> Math.floor (5.225)5>>> Math.ceil (5.225)6

Exercise: Apply a problem together

Apple 5 yuan a catty, grape 15 yuan a catty, sold a catty of apples 2.5 catty grapes, asked how much the total cost?

Solution:

# Apple spending ... # Grape Cost ... Print (5*1) Print (15*2.5) # Total Cost ... Print (5+37.5)42.5

Solution Two:

# Apple spending Print (apple_price*apple_weight) # Grape Cost Print (putao_price*putao_weight) # Total Cost Print (5+37.5)42.5>>>

>>> putao_toast = putao_price*putao_weight>>> apple_toast = apple_price*apple_weight  Print(putao_toast + apple_toast)42.5

Solution three: Using the enhanced formatted string function format

" The total price of the grapes is {}, Apple's total price is {}, total cost { } " . Format (putao_toast,apple_toast,putao_toast+apple_toast) ' The total price of the grapes is 37.5, Apple's total price is 5, total cost 42.5 '

{} represents the parameter, there are several {}, pass several parameters, take parameters sequentially

Python as a calculator (mathematical usage)

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.