Numbers in python example 2022

    By: Thad Mertz
    2 years ago

    In this guide we are going to explore numbers in python, what functions we can use and how we perform operations using numbers. In python we can perform various mathematical operations.

    So first of all we are going to create a file index.py.

    and then run this file with some code in it.

    Here is how the code looks in this file

    // Importing random modules from Python for generating random number
    
    import random
    
    // Here we created a variable and assigned value 2 to it.
    
    difficulty = 2
    
    // Here we created another variable and assgined value 10 to it.
    
    health = 10
    
    // Here we are performing operation so generating random number between 25 to 50 and type integer not float.
    
    recovers_health = int(random.randint(25, 50));
    
    // here we add values and devide with difficulity variable, addition will take place first because of bodmas rule.
    
    
    health = (health + recovers_health) / difficulty
    
    // Printing value.
    
    print(health)
    



    What is Bodmas Rule, Explained


    So there is a order when execution happens on numbers.

    Anything in brackets will be executed first and subtraction will be done at last.


    So here is a example

    // Here 2 and 3 will be added then divided because they are in brackets.
    
    (2+3)/2
    
    Output is 2.5
    
    // Here divide will take place first then multiply and then addition. 
    
    3 + 5 * 3 / 2
    
    Output is 10.5