An introduction to the Numbers data type in Python
In Python, like in all programming languages, data types are used to classify one particular type of data. This is important because the specific data type you use will determine what values you can assign to it and what you can do to it (including what operations you can perform on it).
Code examples will be shown in this style!! Click code icon to see output…
The output will be shown like this!!
Any number you enter in Python will be interpreted as a number; you are not required to declare what kind of data type you are entering. Python will consider any number written without decimals as an integer (as in 138) and any number written with decimals as a float (as in 138.0).
Like in math, integers in computer programming are whole numbers that can be positive, negative, or 0 (…, -1, 0, 1, …). An integer can also be known as an int. As with other programming languages, you should not use commas in numbers of four digits or more, so when you write 1,000 in your program, write it as 1000.
We can print out an integer in a simple way like this:
Or, we can declare a variable, which in this case is essentially a symbol of the number we are using or manipulating, like so:
my_int = -25 print(my_int)
We can do math with integers in Python, too:
int_ans = 116 - 68 print(int_ans)
Integers can be used in many ways within Python programs, and as you continue to learn more about the language you will have a lot of opportunities to work with integers and understand more about this data type.
A floating-point number or a float is a real number, meaning that it can be either a rational or an irrational number. Because of this, floating-point numbers can be numbers that can contain a fractional part, such as 9.0 or -116.42. Simply speaking, for the purposes of thinking of a float in a Python program, it is a number that contains a decimal point.
Like we did with the integer, we can print out a floating-point number in a simple way like this:
We can also declare a variable that stands in for a float, like so:
my_flt = 17.3 print(my_flt)
And, just like with integers, we can do math with floats in Python, too:
flt_ans = 564.0 + 365.24 print(flt_ans)
With integers and floating-point numbers, it is important to keep in mind that 3 ≠ 3.0, as 3 refers to an integer while 3.0 refers to a float.
Write a program that demonstrates the use of both floats and integers.
Write a program that creates a variable for your age in years and then outputs it on the screen.
Write a program that has a variable for how much money you have with you today and adds it to a variable for how much money a friend has today. It should then output the total to the screen.
Thanks [name] from [whichclass][location], Mr McG will get your message…"
[message]"…and will get back to you ASAP via [email]. Catch you soon.
Have a great day!!
K McGuinness - 2018
No personal data will be shared with any third party. The only data processed via this website is anonymous browsing data to best manage your user experience.