Python operators and common data type conversions

Source: Internet
Author: User
Tags arithmetic operators

Nine. Operators1. Arithmetic Operators
operator Description Example
+ Add Two objects added a + B output result 30
- Reducing Get negative numbers or a number minus another number A-B output 10
* By Two number multiplied or returns a string that is repeated several times a * b output result 200
/ Except B/A Output Results 2
// Take the Divide Returns the integer portion of the quotient 9//2 output result 4, 9.0//2.0 output 4.0
% Take surplus Returns the remainder of division B% A output result 0
** Index A**b is 10 of 20, output 100000000000000000000

Note: When blending operations, the order of precedence is: ** higher * / % // + than c25> - , in order to avoid ambiguity, it is recommended () to use 0 to handle operator precedence.

Also, when different types of numbers are mixed, the integers are converted to floating point numbers for operation.

#+ Add two objects add a + b output result#Minus a negative number or one number minus the other. A-B output result -10#* Multiply by two number or return a string that is repeated several times a * b output result#/except b/a output result 2#Please enter the first number a:a = Int (input ("Please enter the first number a:")) b= Int (Input ("Please enter a second number B:"))#additionRet1 = A +bPrint("Result of addition operation:%d"%Ret1)#SubtractionRet2 = A-bPrint("subtraction Result:%d"%Ret2)#multiplicationRet3 = A *bPrint("multiplication Result:%d"%Ret3)#DivisionRet4 = A/bPrint("Division Result:%d"%ret4)#//Take the integer portion of the returned quotient 9//2 the output result 4, 9.0//2.0 the output 4.0#% of remainder returns division B% A output result 0#* * Index A**B is 10 of 20, output results 0000 0000 0000 0000NUM1 = 10num2= 2#Take the DivideRET5 = NUM1//num2Print(RET5)#Take surplusRet6 = num1%num2Print(RET6)#IndexRet7 = 10**2Print(RET7)Print(Type (RET7))
2. Assignment Operators 1) Assignment operator:
operator Description Example
= Assignment operators Assigns the result to the right of the = number to the left variable, such as num = 1 + 2 * 3, and the result Num has a value of 7

2) Compound assignment operator

operator Description Example
+= Addition assignment operator c + = A is equivalent to C = C + A
-= Subtraction assignment operator C-= A is equivalent to C = c-a
*= Multiplication assignment operator C *= A is equivalent to C = c * A
/= Division assignment operator C/= A is equivalent to C = c/a
%= Modulo assignment operator C%= A is equivalent to C = c% A
**= Power assignment operator C * *= A is equivalent to C = C **a
//= Take the divisible assignment operator C//= A is equivalent to C = c//A
#+ = addition Assignment operator C + = A is equivalent to C = C + a#- = Subtraction assignment operator C-= A is equivalent to C = c-a#*= multiplication assignment operator C *= A is equivalent to C = c * a#/= Division assignment operator C/= A is equivalent to C = c/a#%= modulo assignment operator C%= A is equivalent to C = c% a#**= Power assignment operator C **= A is equivalent to C = c * * A#//=-divisible assignment operator C//= A is equivalent to C = c//aa= 10b= 20C= A +b#addition, commonly usedRet1 = A +bPrint(RET1) a= A +BA+=b#Get 30 ResultsB + =aPrint(b)#SubtractionA-=BB-=aPrint(b)#multiplicationA *=bPrint(a)#DivisionA/=bPrint(a)
10. Common Data type Conversions
function Description
int (x [, Base]) Convert x to an integer
Float (x) Convert x to a floating-point number
Complex (real [, Imag]) Create a complex number, real is the real part, Imag is the imaginary part
STR (x) Convert an object x to a string
REPR (x) Convert an object x to an expression string
eval (str) Used to evaluate a valid Python expression in a string and return an object
Tuple (s) Converting a sequence s to a tuple
List (s) Convert the sequence s to a list
Chr (x) Converts an integer to a Unicode character
Ord (x) Converts a character to its ASCII integer value
Hex (x) Converts an integer to a hexadecimal string
Oct (x) Converts an integer to an octal string
Bin (x) Converts an integer to a binary string
#Python Object-oriented language#Everything in Python is Object#convert x to an integer#Define a stringMy_str ="1234"My_num=Int (MY_STR)Print(Type (my_num))Print(My_num)#convert x to a floating-point numberMY_STR1 ="3.14"My_f=float (MY_STR1)Print(Type (my_f))Print(My_f)#convert an object x to a stringnum = 123MY_STR2=str (num)Print(Type (MY_STR2))Print(MY_STR2)#Understand#used to calculate a "valid" Python expression in a string#and returns an objectMY_STR3 ="2323"ret=eval (MY_STR3)Print(Type (ret))Print(ret)#eval is often used in conjunction with inputresult = eval (input ("Please enter a number:"))Print(Type (result))Print(Result)

Python operators and common data type conversions

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.