Python_01_Basic_syntax
Python_01_Basic_syntax
Example 1:
Example 2:
Hope you’ve understood how to write and run a code in Jupyter Notebook.
So, moving forward, in this section you will be learning about the basic syntax of python 3.x. The
prescribed rule of writing a code is called syntax.
Example:
Outcome:
Example:
Outcome:
Example:
Outcome:
1.2 Indentation
The meaning of indentation is “to start a line/ block of text few spaces away from the
margin than from the main part of the text.”
In C, C++, statements inside curly brackets ‘{’ and ‘}’ are treated as a block.
For example:
void main()
{
Statement 1;
Statement 2;
}
From the above example, Statement 1& Statement 2, are considered as the part of the part
of the ‘main’ function. We could identify it as a part of main function because they were
enclosed in curly brackets.
Explanation:
Explanation of C code Explanation of python code
1. After ‘if’ condition is given 1. After ‘if’ condition, a semicolon (:) if given,
a ‘{’ is placed, indicating indicating the start of ‘if’ statement..
the start of ‘if’ statement.
2. Statement is written inside the ‘if’ condition.
if a = = 10:
2. Statement is written inside
print(“ Value of a is 10 ”)
the ‘if’ condition.
Here, you can see the print statement doesn’t
printf(“ Value of a is 10 ”)
start from the level of ‘if’ condition.
It has few spaces called indent.
3. To close ‘if’ condition, the indent on next
line is removed.
3. To close the ‘if’ condition a if a = = 10:
‘}’ is placed. print(“ Value of a is 10. ”)
. else:
Here, the ‘else’ condition starts from the
level of ‘if’ condition.
4. Similarly an else statement 4. Similarly, the statement is also written in
is also written. ‘else’ condition.
You can also add a block inside a block. In such cases, indentation of inner block will increase.
Example:
a =10; b = 6
if a = = 10:
print(“a is equal 10”)
if b = = 6:
print(“b is equal to 6”)
Example:
Here you can see that the output is only “Hello World”. The comments are not reflected in
the output.
Example:
Example:
Syntax:
input( prompt )
Here, the prompt is the message that you want to display to the user. This message is to be written
between inverted quotes (“message”).
Example 1:
Here, “Enter your college name: ” is the message/prompt, which will be visible to the user once
the code is run.
Once the code is run, you’ll get a block below the cell, with your message/prompt. You need to
type your answer in that block.
Here, the name of the college which you have entered is stored in the variable ‘college’.
So if you print the variable ‘college’, you’ll get the following output.
Example 2:
Writing a code to input 2 numbers from user and perform addition on them.
Note: In the topic ’Getting user’s input’, we learnt that the input( )function always takes inputs in
form of strings. Thus we need to convert it into integer before adding. The ‘int’ written
before input( ) does this conversion for us. This is called ’typecasting’. You will learn about
this in future sections.
Now we will add the values of ‘a’ & ‘b’ and print the result.
4 Keywords
All computer programming languages comprise of a set of predefined words which are
called keywords.
These are predefined words, hence cannot be used for any other purpose, than for which it
is defined.
Some of the keywords which you might come across in this training are mentioned below.
Note: All these are case sensitive, hence while using these keywords make sure that you use
the correct casing.
If you want to know what exactly is the use these keywords, then you can use the following
command on Jupyter Notebook.
Syntax:
help(“name_of_the_keyword”)
Example:
Output:
Output 1:
Example 2:
Printing a value stored in a variable.
Output 2:
Example 3:
Printing multiple strings and variable values using single print.
Output 3:
Example 4:
Printing multiple values separated by separators.
Output 4:
In this example, we have used comma (,) as the separator. You can use any symbol, alphabet,
numbers as a separator
Output 5:
Here you can see that all the elements of same print statement are displayed on the same line.
Example 6:
Display values in separate print functions on single line.
In the previous example you saw that, the elements of separate print functions were displayed
of separate lines.
This is because the output of print function ends with a newline (equivalent to Enter).
Thus to do this we use the parameter ‘end’.
Output 6:
References:
1. https://www.w3schools.com/python/python_ref_keywords.asp
2. https://www.tutorialsteacher.com/python/python-basic-syntax
3. https://www.geeksforgeeks.org/statement-indentation-and-comment-in-python/