Step-python-03

Source: Internet
Author: User

Operators are symbols that tell the Python interpreter to do some mathematical or logical operations. Some of the basic math operators are as follows:

>>> a=8+9>>> a17>>> b=1/3>>> b0.3333333333333333>>> c=3*5>>> C15>>> 4%31

Here is a script that calculates the number of days

#/usr/bin/env python3#coding:utf8days = Int (input ("Please enter Days:")] mouth = days//30day0 = Days%30print "you enter %s Days "%daysprint (" Total is {} mouths and {} days ". Format (MOUTH,DAY0))
[Email protected]:~/file/1# python days.pythonplease Enter days:1000you Enter daystotal is mouths and ten days

can also be implemented with the Divmod function, Divmod (NUM1, num2) returns a tuple, the tuple contains two values, the first is the NUM1 and num2 to divide the resulting value, the second is NUM1 and num2 to obtain the value of the remainder, and then we use the * operator to unpack the tuple, get these two A value.

>>> Import sys>>> c=divmod (100,30) >>> C (3, 10)
#!/usr/bin/env python3days = Int (input ("Enter Days:")), print ("Total was {} mouths and {} days". Format (*divmod (days,30))) [E Mail protected]:~/file/1# python3 daysv2.py Enter days:100total is 3 mouths and ten days

The effect is the same

Relational Operators
Operator meaning
< is less than #小于
<= is less than or equal to #小于等于
> is greater than# greater than
>= is greater than or equal to# greater than or equal
== is equal to #等于
!= Is isn't equal to# not equal to
>>> 1 = = 1true>>> 2 > 3false>>> 24false>>>!! = 35True
logical Operators

For logic with, or, not, we use and,or,not these few keywords.

The logical operators and and or are also known as short-circuiting operators: Their parameters are parsed from left to right, and once the results are determined, they stop. For example, if A and C are true and B is false, A and B and C do not resolve C. When acting on a normal non-logical value, the return value of the short-circuiting operator is usually the one that can determine the result first.

relational operations can be combined by logical operators and and OR, and the results of comparisons can be reversed with not. Logical operators are also less privileged than relational operators, where not has the highest priority, or has the lowest priority, so a and not B or C equals (A and (NOTB)) or C. Of course, parentheses can also be used to compare expressions .

>>> 4 and 33>>> 0 and 20>>> False or 4 or 54>>> 2 > 1 and not 3 > 5 or 4true> >> false and 3false>>> false and 4False
shorthand operator

x op= expression   is the syntax form for shorthand operations. It is equivalent to   x = x op expression

>>> A = 3>>> A + = 20>>> a23>>> a/= 1>>> a23.0>>> a/= 2>>> a11.5
An expression

Usually when we write an expression, we put a space around each operator.

>>> A = 123 * (32-66/8) >>> a2921.25
Type Conversions

type conversion function Conversion Path
float(string) Floating-point value, String
int(string) String-Integer value
str(integer) Integer value-string
str(float) Floating-point value-string
>>> a2921.25>>> Str (a) ' 2921.25 ' >>> b=str (a) >>> B ' 2921.25 '

Do a math problem

This program calculates the salary for a digital camera salesperson. His base salary is 2500, and every camera sold he can get 200 and get 2% of the pumped. The program requires you to enter the number of cameras and the total monthly sales .

#!/usr/bin/env python3#coding:utf8basic = 2500saleone  = 200ticheng = 0.02count = int (Input ("Enter number of sold cameras:")) Price = float ( Input ("Enter the total price of the camera:")) If price >=2500:        saleone_money  =  (Saleone*count)         ticheng_money =  (Ticheng *price*count)         print ("Performance    = {:6.2f}". Format (Saleone_money))         print ("commission    = {:6.2f}". Format (ticheng_ Money)         print ("Total revenue  = {:6.2f}". Format (basic +  saleone_money + ticheng_money)) Else:        print   "Camera price must be greater than or equal to 2500 RMB" 


Run the next script

[Email protected]:~/file/1# python sale.py input number of sold cameras: 100 Total price of the camera: 2500 results = 20000.00 Commission = 5000.00 Gross income = 27500.00[email prote cted]:~/file/1# python sale.py Enter the number of cameras to sell: 100 Total price of the camera: 2488 the camera price must be greater than or equal to 2500 RMB [email protected]:~/file/1#



This article is from the "interested people, things become" blog, please be sure to keep this source http://wuxinglai.blog.51cto.com/9136815/1882025

Step-python-03

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.