Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!
This data type supports only numerical values like 1 , 31.4 .
Python 3 support 3 different numerical types.
- int – for integer values like 45 .
- float – for floating point values like 2.3 .
- complex – for complex numbers like 3+2j .
Integer literals in python belong to int class.
Floating points are values with decimal point like.
One point to note that when one of the operands for numeric operators is a float value then the result will be in float value.
As you may now complex number consists of two parts real and imaginary, and is denoted by j .You can define complex number like this:
Python has type() inbuilt function which is use to determine the type of the variable.
Python has the different operators which allows you to carry out required calculations in your program.
+ , - and * works as expected, remaining operators require some explanation.
/ – Float Division : / operator divides and return result as floating point number means it will always return fractional part. For e.g
// – Integer Division : // perform integer division i.e it will truncate the decimal part of the answer and return only integer.
** – Exponentiation Operator : This operator helps to compute ab (a raise to the power of b). Let’s take an example:
% operator : % operator also known as remainder or modulus operator. This operator return remainder after division. For e.g:
In python every expression are evaluated using operator precedence. Let’s take an example to make it clear.
In the above expression which operation will be evaluated first addition or multiplication? To answer such question we need to refer to operator precedence list in python. Image below list python precedence order from high to low.
Operator precedence top (high) to bottom(low)
as you can see in table above * is above + , so * will occur first then addition. Therefore the result of the above expression will be 13 .
Let’s,take one more example to illustrate one more concept.
In above expression which will occur first addition or subtraction. As we can see from the table + and - have same precedence, then they will be evaluated from left to right, i.e addition will be applied first then subtraction.
The only exception to this rule is assignment operator ( = ) which occur from right to left.
You can change precedence by using parentheses () , For e.g
As you can see from the precedence table () has highest priority so in expression 3*(4+1) , (4+1) is evaluated first then multiplication. Hence you can use () to alter order of precedence.
Augmented Assignment Operator
These operator allows you write shortcut assignment statements. For e.g:
by using Augmented Assignment Operator we can write it as:
similarly you can use - , % , // , / , * , ** with assignment operator to form augmented assignment operator.
In the next post we will learn about python strings.
Other TutorialsThis site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!
>>>x=2+3j# where 2 is the real part and 3 is imaginary
>>>2**3# is same as 2 * 2 * 2
Augmented assignment (or compound assignment) is the name given to certain assignmentoperators in certain programming languages (especially those derived from C). An augmented assignment is generally used to replace a statement where an operator takes a variable as one of its arguments and then assigns the result back to the same variable. A simple example is which is expanded to . Similar constructions are often available for various binary operators.
In general, in languages offering this feature, most operators that can take a variable as one of their arguments and return a result of the same type have an augmented assignment equivalent that assigns the result back to the variable in place, including arithmetic operators, bitshift operators, and bitwise operators.
For example, the following statement or some variation of it can be found in many programs:x = x + 1
This means "find the number stored in the variable x, add 1 to it, and store the result of the addition in the variable x." As simple as this seems, it may have an inefficiency, in that the location of variable x has to be looked up twice if the compiler does not recognize that two parts of the expression are identical: x might be a reference to some array element or other complexity. In comparison, here is the augmented assignment version:x += 1
With this version, there is no excuse for a compiler failing to generate code that looks up the location of variable x just once, and modifies it in place, if of course the machine code supports such a sequence. For instance, if x is a simple variable, the machine code sequence might be something likeLoad x Add 1 Store x
and the same code would be generated for both forms. But if there is a special op code, it might beMDM x,1
meaning "Modify Memory" by adding 1 to x, and a decent compiler would generate the same code for both forms. Some machine codes offer INC and DEC operations (to add or subtract one), others might allow constants other than one.
More generally, the form isx ?= expression
where the ? stands for some operator (not always +), and there may be no special op codes to help. There is still the possibility that if x is a complicated entity the compiler will be encouraged to avoid duplication in accessing x, and of course, if x is a lengthy name, there will be less typing required. This last was the basis of the similar feature in the ALGOL compilers offered via the Burroughs B6700 systems, using the tilde symbol to stand for the variable being assigned to, so thatLongName:=x + sqrt(LongName)*7;
would becomeLongName:=x + sqrt(~)*7;
and so forth. This is more general than just x:=~ + 1; Producing optimum code would remain the province of the compiler.
In expression-oriented programming languages such as C, assignment and augmented assignment are expressions, which have a value. This allows their use in complex expressions. However, this can produce sequences of symbols that are difficult to read or understand, and worse, a mistype can easily produce a different sequence of gibberish that although accepted by the compiler does not produce desired results. In other languages, such as Python, assignment and augmented assignment are statements, not expressions, and thus cannot be used in complex expressions. For example, the following is valid C, but not valid Python:
As with assignment, in these languages augmented assignment is a form of right-associative assignment.
In C, C++, and C#, the assignment operator is =, which is augmented as follows:
|Left bit shift|
|Right bit shift|
|Bitwise exclusive OR|
|Bitwise inclusive OR|
Each of these is called a compound assignment operator in said languages.
The following list, though not complete or all-inclusive, lists some of the major programming languages that support augmented assignment operators.