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




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

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.