Basic Syntax of Python
Basic Syntax of Python
Basic Syntax of Python
program in Python
In this Lesson we will try to understand the syntax of
python programming. Although, syntax is something that
you will understand as you will see more and more
programs and examples, but there are a few things that
you must know before hand.
12. # this is a
# multiline comment
15. 456 + \
789
'o', 'u']
if True:
if True:
if True:
if True:
So these are some basic rules that you must know so that
it becomes easier for us to learn various concepts of
python programming in the coming tutorials.
You can write and execute this code in IDLE, or you can
save this code in a python code file, name it test.py(you
can name it anything, just keep the extension of the file
as .py).
To run the test.py python script, open IDLE, go to the
directory where you saved this file using the cd command,
and then type the following in command prompt or your
terminal:
python test.py
Copy
This will execute the python script and will show you the
output in the line below.
From the next tutorial we will start learning the various
concepts of python programming language.
Python Numbers and built-in Math Functions
In this section, we will be learning about Numbers and
various Math functions available in python language. In
Numbers, we will see some of the most commonly used
math operators that we can use to perform various
operations on the numbers in python. Under Math
functions section, we will learn about some shortcuts
(called functions), which are very helpful in calculating
some of the complex mathematical expressions
like power, sine/cosine, factorials etc. So, let's begin. We
recommend keeping the IDLE open, while reading, so
that you can practice and learn simultaneously.
Numbers
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulo
6. Power
Most of you must be familiar with all of the above
operators except for the modulo operator. Don't worry we
will explain it. Let's start from the beginning.
Addition
>>> 8+19
27
Subtraction
Multiplication
>>> 16/2
Power
>>> 2**10
1024
With that, we now know about all the commonly used
mathematical operators of python. Now you can try to
combine multiple operators and use them to form one
expression. We will recommend using brackets so that
python can understand what you want as the answer, i.e.
instead of writing 2-9.0/2, write 2-(9.0/2).
Remember BODMAS, how a mathematical expression
with multiple operators is solved in mathematics.
Modulo
Power - pow(x,y)
>>> pow(3,2)
Copy
Now let's analyse what we did and what will happen.
First, we wrote pow, which is simply the name of the
function that we are trying to call. This will tell the
python compiler to look out for an inbuilt function
named pow and discover what it can do. Next, within the
brackets we wrote two numbers separated with a comma,
i.e. 3 and 2. Here the first number 3 is base and the
second number 2 is an exponent, and we are trying to
calculate 32.
Once the python compiler has ensured that all the syntax
(the grammar of programming) is correct, it will look for
the implementation of the function pow and use it to
find 32. So as you might have expected, the output would
be:
Copy
The values inside brackets that had been separated by
commas, which we mentioned to you as input to
functions, are called Arguments. As in pow(x,
y) example given above, 3 and 2 were the arguments.
There can be any number of arguments in a function. And
as we discussed earlier, for a mathematical function there
usually is at least one argument present. Let's see some
another inbuilt mathematical functions.
Absolute - abs(x)
Absolute function, also known as Modulus (not to be
confused with Modulo), returns the non-negative value
of the argument value. Therefore, absolute value of any
non-negative number is the same, while for negative
numbers, their positive value is returned.
Example: absolute value of -3 will be 3, absolute value
of -8.74 will be 8.74 and so on.
Syntax:
>>> abs(-99.99)
Sine - sin(x)
>>> math.sin(3.14159)