Common operators of 006:python

Source: Internet
Author: User
Tags arithmetic arithmetic operators logical operators

Notes

3 divisions in 1.python: traditional division, Precision division, floor removal.

Traditional Division : If it is an integer except that the law performs a floor addition, if it is a floating-point number, the law performs precise division.

>>>1/2  0  >>>1.0/2.0  0.5  

Exact Division : Division always returns the real quotient, regardless of whether the operand is shaped or floating. This can be done by executing from the future Import Division directive.

>>>fromimport division  >>>1/2  0.5  >>>1.0/2.0  0.5  

floor except ://Division Regardless of the numeric type of the operand, the fractional part is always removed, returning the nearest number in the sequence of numbers that is smaller than the true quotient.

>>>1//2  0  >>>1.0//2  0  >>>-1//2.0  -1  

2. Precedence of Operators: Power arithmetic > Sign > Arithmetic operators > Comparison operators > Logical operators.

Test questions

1. Please say the answer at the fastest speed: not 1 or 0 and 1 or 3 and 4 or 5 and 6 or 7 and 8 and 9.
Answer: 4

The precedence of not OR and is different: not > and > or

We enclose them in parentheses according to priority: (not 1) or (0 and 1) or (3 and 4) or (5 and 6) or (7 and 8 and 9)
= = 0 or 0 or 4 or 6 or 9
= = 4
Short Circuit logic:
If A and B: #如果a是false, then skip B's judgment and result directly false.
If A or B: #如果a为true, then skip B's judgment, direct true

2. Please write a program to print out all the odd numbers of 0~100.
For:

i = 0while i < 101:    if i %2 == 1:        print(i)        i += 1    else:        i += 1

Modify according to the answer:

i = 0while i <= 100:    if i % 2 != 0:        print(i, end=‘ ‘)        i += 1    else:        i += 1

3. Einstein had such an interesting math problem: there is a long ladder, if each step on the 2 order, the last remaining 1 orders, if each step 3 order, the last remaining 2 orders, if each step 5 order, the last 4 orders, if each step 6 order, finally left 5 order;

Title: Please program how many steps to solve the ladder?
For:

i = 0while 1:    if i%2==1 and i%3==2 and i%5==4 and i%6==5 and i%7==0:        print(i)        break    else:        i += 1

Modify according to the answer:

x = 7i = 1flag = 0while i <= 100:    if (x%2 == 1) and (x%3 == 2) and (x%5 == 4) and (x%6==5):        flag = 1    else:        x = 7 * (i+1) # 根据题意,x一定是7的整数倍,所以每次乘以7    i += 1if flag == 1:    print(‘阶梯数是:‘, x)else:    print(‘在程序限定的范围内找不到答案!‘)

Common operators of 006:python

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.