slide 5
slide 5
Integers (int)
In Python, an integer is referred to as an int.
In order to convert any types of variables into integer,
the ‘int()’ function is used. For Example:
x = input("What's x? ")
y = input("What's y? ")
z = int(x) + int(y)
print(z)
// % **
Divide and Modulus Exponent
Round
3
Float
A floating point value is a real number that has a decimal
point in it, such as 0.52.
You can change your code to support floats as follows:
x = float(input("What's x? "))
y = float(input("What's y? "))
print(x + y)
4
Rounding Floats
Looking at the Python documentation for round you’ll see
that the available arguments are round(number[n,
ndigits]).
Those square brackets indicate that something optional
can be specified by the programmer. Therefore, you
could do round(n) to round a digit to its nearest integer.
For example:
x = float(input("What's x? "))
y = float(input("What's y? "))
z = round(x / y, 2)
print(z)
5
Rounding Floats (Alternative Approach)
We could also use fstring to format the previous output
as follows:
x = float(input("What's x? "))
y = float(input("What's y? "))
z=x/y
print(f"{z:.2f}")
6
Including Thousands Separator
What if we wanted to format the output of long
numbers? For example, rather than seeing 1000, you
may wish to see 1,000. You could modify your code as
follows:
x = float(input("What's x? "))
y = float(input("What's y? "))
z = round(x + y)
print(f"{z:,}")
7
Exercise
8
That’s All
For Today!
9