Compare and Contrast int vs. float
357are integers. The float data type refers to floating-point data. Numbers containing digits to the right of the decimal point such as
300.345567are floating-point numbers.
After you complete this section, you should be able to explain the difference between int and float. There are deeper reasons (beyond the scope of this course) why you might distinguish between these data types; those have to do with how they are represented within a computer system. Our goal for this section is simply to be able to visually identify integer and floating-point numbers. On this page, you will see more examples of int and float.
The int data type refers to integer numerical data. In the rightmost window of the Repl.it IDE, use your mouse to click to the right of the > command prompt. Next, type the integer
2 and press the "enter" key on your keyboard. You should see the
IDE echo the value back to the screen. Try this a few times with other examples to be sure you understand the int data type.
The float data type refers to floating-point numerical data. In the rightmost window of the Repl.it IDE, use your mouse to click to the right of the > command prompt. Next, type the floating-point number
press the "enter" key on your keyboard. You should see the IDE echo the value back to the screen. Try this a few times with other examples that have been provided to be sure you understand the float data type.
It is of the utmost importance that you understand the value
2 is of type int, while
2.0 is of type float. On paper, this might not seem like a big difference, but the decimal point is how a computer tells the difference between these two data types.
||11 plus 56||67|
||23 minus 52|
||4 multiplied by 5||20|
||2 to the power of 5||32|
||9 divided by 2||4.5|
||9 divided by 2||4|
||9 mod 2||1|
Types int and float
A type is a set of values and operations that can be performed on those values.
Two of Python's numeric types:
float: floating-point number (an approximation of a real number)
Arithmetic Operator Precedence
When multiple operators are combined in a single expression, the operations are evaluated in order of precedence.
Syntax and Semantics
Syntax: the rules that describe valid combinations of Python symbols.
Semantics: the meaning of a combination of Python symbols is the meaning of an instruction — what a particular combination of symbols does when you execute it.
A syntax error occurs when an instruction with invalid syntax is executed. For example:
>>>3)+2*4 SyntaxError: invalid syntax
A semantic error occurs when an instruction with invalid semantics is executed. For example:
>>> 89.4/0 Traceback(most recent call last): File"", line 1,in 89.4/0 ZeroDivisionError:float division by zero
Source: MS, https://www.oercommons.org/authoring/5133-a-short-notes-on-python-programming/2/view#h2
This work is licensed under a Creative Commons Attribution 4.0 License.