Let's Learn Python 🐍steemCreated with Sketch.

in #steemstem5 years ago (edited)

H


ello fellow Steemians!
Today, we'll talk about , and why is it an important programming language to learn!

(Don't try Python on mobile?!...no, you can! | Source: Image by Gerd Altmann from Pixabay.)

Python is comparatively new, and is gaining importance very fast. It is written in C. As many of you might be knowing, C is considered to be the fastest as far as implementation time is concerned.

1. A Brief Intro to Python

Python was conceived in the late 1980's. Python 2.0 was released in 2000...and the current version of Python is 3.8.1


(The guy behind the slither! | Source: WIkipedia)

2. Why Learn Python?

  • Python is easy to use: the main goal of any programming language is to bridge the gap between the programmer’s brain and the computer - and Python nearly is the best in doing that.
  • Is powerful: has the ability to : employ a GUI, process files, and use a variety of data structures and much more.

  • Python is Object Oriented - but, that is optional: using OOP many-a-times causes small programs to become long and complicated.
  • 3. Let's Dive Into Python...

    NOTE: You must first install Python on your computer, you can visit Python.ORG for download and installation instructions (or) you can simply download and install the Community Edition of Pycharm. I'll be using VS.Code.

    • Adding numbers:
    print(2+3)
    print(450/50)
    


    You can see, I got the result as

    5
    9.0
    

    ...as output in the terminal upon running the code.

    In a similar fashion, you can add subtract and and do many other algebraic operations using print() function to print the result as output.

    Here is a list of some of the math operators in Python.

    • Variables in Python:

    Variable: is an object that can be assigned any value of choice.

    Let's try running the code below:

    x=5
    y=6
    print(5*6, "Here x and y are variables which have been assigned values", x, "and", y, "respectively!!")
    

    image.png

    NOTE: Rules for variable nomenclature (basic):

    1. A variable name can contain only numbers, letters, and underscores.
    2. A variable name can’t start with a number.

    Okay!! That was eezy-peezy!! But, don't worry...we got tough stuff for you!
    ...as we proceed, things will get a bit tougher.

    • Augmented Assignment Operators:
      Okay, try guessing the output of the code below:
    import math 
            #this imports the math module
    length=3
    breadth=4
    length=length*length
    breadth=breadth*breadth
    
    hypotenuse=math.sqrt(length+breadth)
    
    print(hypotenuse)
    

    image.png
    Okay, so that's easy we got 5.0 as the hypotenuse.
    ...and that's probably what we were expecting!


    (a=length, b=breadth, c=hypotenuse | Source: lememento.fr)

    NOTE: # is used to denote a comment in python. Comments are not processed by he interpreter, so make no difference. import is used to tell the interpreter to use a special module (in this case, it was math).
    Later on, we'll try learning other modules like numpy, and scipy.

    So, you must have understood that length=length*length actually means: assign "length" a new value which is equal to square of the original value... and similarly for breadth.

    Now, instead of just using augmented assignment with multiplication, we can use it with many other arithmetic operators like:
    image.png

    NOTE: The general form/working of an augmented assignment is: x = f(x), where f(x) is a function of x. The table above shows simpler examples with compound augmented assignment operators!

    • Okay! Enough of boring algebra!! Let's try some slicing !
      Let's try running the following code:
    print("HONOLULU"[:4])
    print("HONOLULU"[4:])
    print("HONOLULU"[4:4])
    print("HONOLULU"[:2])
    
    x=567
    print(x[:1])
    

    image.png
    So, that must have made clear what slicing actually does, and its syntax.

    Notice: We encountered an error in implementing the last line of the code (marked by a red arrow in the output).
    This was because x was an int (integer) datatype, and we treated it as a string during slicing.

    So, now you know why datatypes are important to know about!

      3.1. Datatypes in Python

    Some common datatypes in Python are:

    • Integer --> int
    • Float --> float
    • String --> str

    Now, let's look at an illustration to understand these better:

    import math
    
    x = math.pi
    print(x)
    #This will show us to what precision is the value of pi being stored.
    
    print(int(x))
    print(float(x))
    y=print(str(x), str(x)[:4])
    

    image.png

    NOTE: math.pi will give us the exact value of π upto a given number of precision places.

    So, I hope the example above would have helped you in understanding a little about the working of datatypes in Python and how to inter-convert between them.

    Okay! So, with this much knowledge can you write Python code to print numbers from 1 - 1000?

    print(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...)
    

    ...without explicitly stating them (as above).


    Okay, maybe you can try doing that while I go out and catch something to eat! See you next time!
    This was an introductory post.
    In the next post, we'll try to look at how to use loops in Python.
    Thanks for sparing your valuable time. Hope you learned something new!

    Keep slithering !
    Best
    Aakash

    Sort:  

    Congratulations @aakashsinghbais! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

    You distributed more than 50 upvotes. Your next target is to reach 100 upvotes.

    You can view your badges on your Steem Board and compare to others on the Steem Ranking
    If you no longer want to receive notifications, reply to this comment with the word STOP

    To support your work, I also upvoted your post!

    Vote for @Steemitboard as a witness to get one more award and increased upvotes!
    Hello @aakashsinghbais!
    This is a great post!! We really appreciate your work and efforts.


    To support your work, we have also up-voted you using our Bioportal.steem Curation Trail ✅!

    We'd like to invite you to our Server: SBioP.

    Our project will not be able to operate without support from people like you.


    FROM OUR CURATOR (@tibfox):
    Learning new things every day is a good way to keep the young human inside of your mind!

    Best,
    Team Biodiversity Project 🍄

    By the people ⚪ For the people


    This post has been voted on by the SteemSTEM curation team and voting trail. It is elligible for support from @curie and @minnowbooster.

    If you appreciate the work we are doing, then consider supporting our witness @stem.witness. Additional witness support to the curie witness would be appreciated as well.

    For additional information please join us on the SteemSTEM discord and to get to know the rest of the community!

    Please consider using the steemstem.io app and/or including @steemstem in the list of beneficiaries of this post. This could yield a stronger support from SteemSTEM.

    Coin Marketplace

    STEEM 0.20
    TRX 0.26
    JST 0.039
    BTC 100693.54
    ETH 3647.21
    USDT 1.00
    SBD 3.13