Python Tutorial – Numbers

Python Numbers
Reading Time: 3 minutes

This is continuation in Python Programming Tutorial Series. From this article I will be introducing practical exercises which you can download and practice on your own.

You can practice python in both command prompt or an IDE, but I suggest using IDE you’re comfortable with. I will be using VS Code for demonstration. If you haven’t already set up VS code for python follow my another article for guidance. I also wrote another article on short introduction of built-in data types and built-in functions please go through those articles if you are absolute beginner before exercises. In this article you’ll notices a word class. I will write blog on Object Oriented Programming in future where I’ll explain the term but for now you can ignore them.

But before that let me tell you a little about comments in python.

Comments

Comments is a means of explaining your codes for programmers. Its not meant to be for computer but for fellow programmers who will work with you or after you. In python there are two types of comments.

  1. Hashtag(#): Its a single line comment. To comment you have to start your comment with #.
  2. Triple Quotes(“””Write here””””): Its a multi-line comment, but used as doc string. You comment between triple quotes, they can be double or single quotes.

Doc string is a information string, to convey a detail about a object like function, its written in Triple quotes in the beginning of object scope.

Numbers

They are basic form of data in any programming language. Lets get right to the coding.

var_1= 25
var_2 = 45

#Lets first print those variables
print(var_1, var_2)

#Now lets check the data types using type()

print("The type of variable var_1", type(var_1))

#Now lets do some basic operations

#Add
addition = var_1+var_2

#Subtraction
subtraction = var_1 - var_2

#Multiply
product = var_1 * var_2

#Division
division = var_1 / var_2

# Getting Dividor Using %(pronounced as mode)
mode = 10%2 


# Lets print the results

print("The sum is:", addition)
print("The subtraction is:", subtraction)
print("The product is:", product)
print("The division is:", division)
print("The mode between 10/2 is:", mode)


#Now lets declare few other variables
int_num= 2356
float_num = 23.67

# Lets check the types again

print("The data types of {} and {} are {} and {} respectively".format(int_num,float_num, type(int_num), type(float_num))) 

Ouput

25 45
The type of variable var_1 <class 'int'>
The sum is: 70
The subtraction is: -20
The product is: 1125
The division is: 0.5555555555555556
The mode between 10/2 is: 0
The data types of 2356 and 23.67 are <class 'int'> and <class 'float'> respectively

See these are some basic operations that goes with numbers. And check out the last line even though we are not declaring floats or integers python interprets those by itself.

Lets see the operations between two class int and floats.

# Now lets declare few other variables
int_num = 2356
float_num = 23.67

# Lets check the types again

print("The data types of {} and {} are {} and {} respectively".format(
    int_num, float_num, type(int_num), type(float_num)))


# Now lets do some basic operations

# Add
addition = int_num+float_num

# Subtraction
subtraction = int_num - float_num

# Multiply
product = int_num * float_num

# Division
division = int_num / float_num


# Lets print the results

print("The sum is:", addition, "and its type is: ", type(addition))
print("The subtraction is:", subtraction,
      "and its type is: ", type(subtraction))
print("The product is:", product, "and its type is: ", type(product))
print("The division is:", division, "and its type is: ", type(division))

Output

The data types of 2356 and 23.67 are <class 'int'> and <class 'float'> respectively
The sum is: 2379.67 and its type is:  <class 'float'>
The subtraction is: 2332.33 and its type is:  <class 'float'>
The product is: 55766.520000000004 and its type is:  <class 'float'>
The division is: 99.5352767215885 and its type is:  <class 'float'>

Even though one is integer another is float, but operation between them results in float. So, while doing operations you have to careful of the data types that you’re using so that result does not alter your expectations.

Int() and Float()

We can inter-convert integers and float by using int() and float() built-in functions respectively.

# Lets convert int_num to float
int_num_to_float = float(int_num)

# Lets convert flaot_num to int
float_num_to_int = float(float_num)


print("Integer {} to float -->{}".format(int_num,int_num_to_float))

print("Float {} to integer --> {} ".format(float_num,float_num_to_int))

Output

Integer 2356 to float --> 2356.0 
Float 23.67 to integer --> 23

One more thing did you noticed the int() function has not rounded to nearest 10 but to 0. Hence, if you want to round to nearest ten please use round() method.

You can also convert string to integer if possible.

print(int('2345'))
# Now for hexadecimal number with base 16
print(int('FFF', base=16))
print(int("Conversion not possible"))

Output

2345
4095
Traceback (most recent call last):
  File "second_python.py", line 51, in <module>
    print(int("Conversion not possible"))
ValueError: invalid literal for int() with base 10: 'Conversion not possible'

The second conversion is possible since its a Hexadecimal(base 16), but third one is not possible.

Leave a Reply

%d bloggers like this: