Python - Basic Concepts

Programming PYTHON CORE SERIES Image

    Welcome to Python!

    Python is a high-level programming language, with applications in numerous areas, including web programming, scripting, scientific computing, and artificial intelligence!
    It is very popular and used by organizations such as Google, NASA, the CIA, and Disney.

    There are almost no limitations on what can be built using Python. These include standalone apps, web apps, games, data science and machine learning models, and much more.
    To use Python, you first need to install it on your computer from here.

    Fun fact: According to the creator Guido Van Rossum, the name of Python was derived from the British comedy series "Monty Python's Flying Circus".


    Printing Text

    Let's start off by creating a short program that displays "Hello world!".
    In Python, we use the print statement to output text.

    print("Hello World!")


    Simple Operations

    Python has the capability of carrying out calculations.
    Enter a calculation directly into the print statement:

    print(2+4)
    print(2+2-3)

    Python also carries out multiplication and division, using an asterisk * to indicate multiplication and a forward slash / to indicate division.
    Use parentheses to determine which operations are performed first.

    print(2*(3+4))
    print(10/2)

    Using a single slash to divide numbers produces a decimal (or float, as it is called in programming). We'll have more about floats in the next part.

     

    Floats

    Floats are used in Python to represent numbers that aren't integers (whole numbers).
    Some examples of numbers that are represented as floats are 0.5 and -7.8237591.
    They can be created directly by entering a number with a decimal point, or by using operations such as division on integers. 

    print(3/4)
    print(0.42)

    Computers can't store floats perfectly accurately, in the same way that we can't write down the complete decimal expansion of 1/3 (0.3333333333333333...). Keep this in mind, because it often leads to infuriating bugs!

    As you saw previously, dividing any two integers produces a float.
    A float is also produced by running an operation on two floats, or on a float and an integer.

    print(8/2)
    print(6*7.0)
    print(4+1.65)

    A float can be added to an integer, because Python silently converts the integer to a float.


    Exponentiation

    Besides addition, subtraction, multiplication, and division, Python also supports exponentiation, which is the raising of one number to the power of another. This operation is performed using two asterisks.

    print(2**5)

    You can chain exponentiations together. In other words, you can rise a number to multiple powers. For example, 2**3**2.

    You can also use floats in exponentiation.
    For example, the following code will result in the square root of 9: 

    print(9**(1/2))

    Note that the result will be a float.


    Quotient

    Floor division is done using two forward slashes and is used to determine the quotient of a division (the quantity produced by the division of two numbers).

    For example:
     

    print(20//6)

    The code above will output 3, because 6 goes into 20 three times.

    You can also use floor division on floats.


    Remainder

    The modulo operator is carried out with a percent symbol (%) and is used to get the remainder of a division.

    For example:

    print(20%6)
    print(1.25%0.25)

    All numerical operators can also be used with floats.


    Conclusion

    These were some basic concepts in Python. In the next blog of this Python Core Series, we'll learn about Strings and Variables.
    Till then, stay tuned. Thanks!

    0 Comments

    To add a comment, please Signup or Login