MyInternships.in

Python Basics

Python Numbers

Python supports three built-in numeric types — int, float and complex — and gives you simple operators to perform math on them.


Number Types in Python

An int is a whole number with no decimal point, a float is a number with a decimal point, and a complex number has a real and imaginary part, written with a j suffix. Complex numbers are rarely needed outside scientific computing.

The three number types
Python
whole = 10          # int
decimal = 10.5      # float
imaginary = 2 + 3j  # complex

print(type(whole), type(decimal), type(imaginary))

Arithmetic Operations

Python supports all standard math operations, plus a couple of handy extras like floor division and exponents.

OperatorMeaningExampleResult
+Addition5 + 27
-Subtraction5 - 23
*Multiplication5 * 210
/Division5 / 22.5
//Floor Division5 // 22
%Modulus (remainder)5 % 21
**Exponent (power)5 ** 225
Python
a = 7
b = 2
print(a + b, a - b, a * b, a / b, a // b, a % b, a ** b)

Converting Between Number Types

Use int() to convert a value to a whole number, and float() to convert it to a decimal number. This is common when working with user input, since input() always returns text.

Python
text_number = "42"
as_int = int(text_number)
as_float = float(text_number)
print(as_int, as_float)

Rounding Numbers

The built-in round() function rounds a float to the nearest whole number, or to a given number of decimal places if you pass a second argument.

Python
price = 19.6789
print(round(price))       # 20
print(round(price, 2))    # 19.68
💡

Use / for normal division that keeps decimals, and // when you specifically need a whole-number result, like splitting items evenly.

Related Python Topics

Keep learning with these closely related lessons.

Ready to use your Python skills?

Find Python, data science and software internships and fresher jobs across India.

Browse Python Internships