4th Street Bar Hive-Bar

Hive-Bar Powered by Hive beta-fdb5b5b

Community post

Let's Learn Python 🐍

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. ![](https://cdn.steemitimages.com/DQmR2dt2sepp6GavdKGDTGmn8M7aZbJvav6XUajjNwNbb8C/image.png) - Variables in Python: > Variable: is an object that can be assigned any value of choice. > Let's try running the code below: ```python x=5 y=6 print(5*6, "Here x and y are variables which have been assigned values", x, "and", y, "respectively!!") ``` ![image.png](https://files.steempeak.com/file/steempeak/medro-martin/864Y2cYI-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!

    ![](https://cdn.steemitimages.com/DQmR7DLX8atX5sgFnf5DD5fW1zbpvu5niQWfdSPm9UXkCFT/image.png)
    (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](https://files.steempeak.com/file/steempeak/medro-martin/bEIUwS8i-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?

    ```python 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
    288 upvotes $2.02

    Replies (5)

    Review before signing

    Posting as . Signing with . Keychain permission: Posting. Hive Keychain will ask you to approve this action next.

    
      
    Technical details

    Operation fingerprint: