0% found this document useful (0 votes)
2 views9 pages

slide 5

The document provides an overview of basic Python concepts including integers, floats, and arithmetic operations. It explains how to convert variables to integers, perform basic arithmetic, round floats, and format output with thousands separators. Additionally, it includes an exercise for converting hours into minutes.

Uploaded by

emonahmed.kl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

slide 5

The document provides an overview of basic Python concepts including integers, floats, and arithmetic operations. It explains how to convert variables to integers, perform basic arithmetic, round floats, and format output with thousands separators. Additionally, it includes an exercise for converting hours into minutes.

Uploaded by

emonahmed.kl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Python Practical-03

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)

The use of int(x), is called “casting” where a value is


temporarily changed from one type of variable (in this
case a string) to another (here, an integer).
2
Basic Arithmetic Operators
/
+ - *
Add Subtract Multiply Divide

// % **
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

User will provide you a number of


hours. Convert that into minutes.

8
That’s All
For Today!
9

You might also like