Introduction to arithmetic operations and arithmetic expressions in Python

Source: Internet
Author: User
Tags integer division

The Python language tutorial has a lot of knowledge to learn carefully during our learning process. Next we will introduce the Arithmetic Operators and arithmetic expressions in detail. Hope to help you. No matter what language we use, most of the Code logic lines we write) contain expressions.

  • How to bind a C ++ program to Python
  • Python display UTF-8 Chinese Text specific operation method
  • Main features of Python objects
  • Summary of Python experience
  • Python scripts solve difficulties in Game Development

An expression can be divided into operators and operands. Operators are used to complete a certain task. They are represented by mathematical operators or other specific keywords. Operators need data for computation, such data is called operands. For example, 2 + 3 is a simple expression, where + is an operator, and 2 and 3 are operands.

Arithmetic Operators and arithmetic expressions

Arithmetic Operators are the most basic operators in programming languages. In addition to the plus (+), minus (-), minus (*), minus (/), and minus (%), the following operators are provided: Evaluate the power **) and get the entire division //). Here we use a piece of code to understand the two arithmetic operators:

 
 
  1. #-*-Coding: UTF-8 -*-
  2. X = 3.3
  3. Y = 2.2
  4. A = x ** y
  5. Print a # output 13.827086118, that is, 3.3 power of 2.2. in C #, the Pow method is used to implement power calculation.
  6. B = x // y
  7. Print B # output 1.0, taking the integer part of the divisible return Operator
  8. C = x/y11 print c # output 1.5. Note the differences between common division and integer division.

Value assignment operator and value assignment expression

Assign a value is to assign a new value to a variable. Apart from simple = assign values, both Python and C # support compound assign values. For example, x + = 5 is equivalent to x = x + 5.

The Python tutorial does not support the auto-increment and auto-subtraction operators in C #. For example, statements such as x ++ are prompted with syntax errors in Python. C # programmers may have used this expression or why C ++?) in Python, please write x + = 1 honestly.

Logical operators and expressions

The logical operators of Python are quite different from those of C #. Python replaces the logical operators of C # With the keywords and, or, and not! In addition, the operands involved in logical operations in Pyhton are not limited to boolean types. Values of any type can be involved in logical operations. See section 1.2.2 boolean type.

A logical expression is used to connect operands or expressions. Similar to C #, the logic expression in Python is executed by "Short Circuit", that is, the right value of the logic expression is calculated only when necessary, for example, expression a and B is calculated only when expression a is True. Think about it. if (0 and 10/0): Will this statement cause an exception with Zero Divisor?

In the Python tutorial, the logical operations executed by and or do not return boolean values, but return one of the values actually compared. The following is an example:

 
 
  1. Print 'A' and 'B' # output B
  2. Print ''and 'B' # Empty output string

Relational operators and expressions

Relational operation is actually a type of logical operation. The return value of a relational expression is always a Boolean value. The comparison operators in Python are exactly the same as those in C #, including = ,! =,>, <,> =, And <=.

In addition to the basic variable comparison, Python's Relational operators also include the identity operator is. In Python, is used to check whether two objects point to the same object in the memory. Remember "is everything data an object, and all names are referenced ?). Note that the meaning of is in the Python tutorial is different from that in C #. in C #, the is operator is used to dynamically check whether the object type is compatible with the given type. For example, e is T, where e is an object, T is a type, and the return value is a Boolean value, which indicates whether e can be converted to T type.

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.