Sunday, April 20, 2025

Python Numbers

Python is high level programming language - it will automatically recognize number type in variables:


x = 5
y = 4654655
z = -4656456

print("Value of x: ", x, type(x))
print("Value of y: ", y, type(y))
print("Value of z: ", z, type(z))

Sure, we can use floats. If we need higher precision:


x = 5.00
y = 4654655.123
z = -4656456.435345

print("Value of x: ", x, type(x))
print("Value of y: ", y, type(y))
print("Value of z: ", z, type(z))

It's extremely useful to put result of operations in dedicated variables, so we can use it further:


x = 5
y = 10
addition = x + y

print("Result: ", addition)
print("Type:", type(addition))

If one number in calculation is float, result will be float, too:


x = 5.34
y = 10
addition = x + y

print("Result: ", addition)
print("Type:", type(addition))

Result:


Result:  15.34
Type: <class 'float'>
>>> 

Scientific notation supported by default:


# 5 x 10 on 3

x = 5e3
print(x)

This will work, too:


# 5 x 10 on 12

x = 5E12
print(x)

Result:


5000000000000.0
>>> 

In next tutorial we will convert from one data type to another.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...