Sunday, 11 January 2015

Getting Started with Python Programming

The articles on python will be succinct. It is up to you to delve deeper. This post talks about the very fundamental concepts. There is a summary slide, followed by a program and its output.


Program
__author__ = 'techmightsolutions.blogspot.com'


def integer_representations():
    print("Decimal 10 = ", 10)
    print("Binary  10 = ", 0b10)
    print("Octal  10 = ", 0o10)
    print("Hexadecimal 10 = ", 0x10)

    # Constructing int using int() constructor
    print("Converting to int from other numeric type = ", int(5.49))

    #Rounding will always be towards zero
    print("int(-5.49) = ", int(-5.49))

    #Converting to base 10, from base 2
    print("0b100 = ", int("100", 2))


def floating_point_numbers():
    """IEEE-754 double precision (64-bit)
    53 bits of binary precision
    15 to 16 bits of decimal precision
    """
    print("Speed of Light = ", 3e8)
    print("Plank's constant = ", 1.616e-35)

    # using float constructor for conversion
    print(float("56"))

    #nan, inf and -inf
    print(float("nan"))
    print(float("inf"))
    print(float("-inf"))


def None_value():
    """Special null value = None
    :return:
    """

    # checking if a variable is None using is operator
    a = None
    print(a is None)


def bool_types():
    """
    represents logical state
    either True or False
    :return:
    """

    # bool constructor for conversion
    print(" 0 = ", bool(0))
    print(" 1.23 = ", bool(1.23))
    print("Empty String = ", bool(""))
    print("'techmightsolutions.blogspot.com' = ", bool(['techmightsolutions.blogspot.com']))
    print("Empty List [] = ", bool([]))
    print("List [1, 'Hello', 3] = ", bool([1, 'Hello', 3]))
    #same is applicable for tuple() and dictionary {}


def main():
    while True:
        print("-" * 20)
        print("What you want to test? ")
        print("1. Integer")
        print("2. Floats")
        print("3. None values")
        print("4. Boolean types")
        response = int(input("Enter your choice: "));

        if (response == 1):
            print("Integer Section: ")
            print("-" * 10)
            integer_representations()
        elif (response == 2):
            print("Floating point Section: ")
            print("-" * 10)
            floating_point_numbers()
        elif (response == 3):
            print("None value Section: ")
            print("-" * 10)
            None_value()
        elif (response == 4):
            print("bool Section: ")
            print("-" * 10)
            bool_types()
        else:
            break;


if __name__ == '__main__':
    main()

Output
--------------------
What you want to test? 
1. Integer
2. Floats
3. None values
4. Boolean types
Enter your choice: 1
Integer Section: 
----------
Decimal 10 =  10
Binary  10 =  2
Octal  10 =  8
Hexadecimal 10 =  16
Converting to int from other numeric type =  5
int(-5.49) =  -5
0b100 =  4
--------------------
What you want to test? 
1. Integer
2. Floats
3. None values
4. Boolean types
Enter your choice: 6

Process finished with exit code 0

No comments:

Post a Comment

Your comments are very much valuable for us. Thanks for giving your precious time.