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.
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.
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.
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鈥檛 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.
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!
NOTE:
#is used to denote a comment in python. Comments are not processed by he interpreter, so make no difference.importis 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!
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 becausexwas anint(integer) datatype, and we treated it as a string during slicing.
So, now you know why datatypes are important to know about!
intfloatstrNow, 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.piwill 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.
print(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...)