Let's Learn Python 🐍
H
ello fellow Steemians!
Today, we'll talk about
![](https://steemitimages.com/85x85/https://steemitimages.com/DQmVQ2fifXxMkw9xQ8d5kRSoxf1zdjfnAHgRUBwKyMFFMbQ/image.png)
![](https://steemitimages.com/500x500/https://steemitimages.com/DQmc2TDhGSU8FyUEJJmT4CHGmKqL8REV5wovhNEoiWcDrwy/mobile-phone-1513945_1920.jpg)
(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![](https://steemitimages.com/400x500/https://steemitimages.com/DQmYEzMZipntZFJwpw8RXm1uhDnXSKZiM6nF3uzdQbUZqyu/image.png)
(The guy behind the slither! | Source: WIkipedia)
2. Why Learn Python?
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!!")
NOTE: Rules for variable nomenclature (basic):
- A variable name can contain only numbers, letters, and underscores.
- 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)
Okay, so that's easy we got 5.0
as the hypotenuse.
...and that's probably what we were expecting!
![](https://steemitimages.com/640x0/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 wasmath
).
Later on, we'll try learning other modules likenumpy
, andscipy
.
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:
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])
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 becausex
was anint
(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])
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
Congratulations @aakashsinghbais! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :
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!
@tipu curate
Upvoted 👌 (Mana: 10/15 - need recharge?)
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.
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.