Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Python's popularity among programmers is unrivalled by any other programming language. Python Syntax is so straightforward and clean that it is famously called the pseudocode language. Reading, understanding, and writing Python code is much easier than other well-known languages like C++ or Java.
Python has a number of built-in functions that can be used to do specific tasks.
A built-in function is a sequence of statements that performs a task that is already defined in a programming framework. As a result, users can utilize this function directly in their program or application without having to construct it.
In this blog, we will understand input and output functions in Python-Basic Syntax.
What is Python Syntax?
Python syntax signifies the set of rules or instructions on accurately writing a Python program. The syntax has to be correct in each line of each program; else, the code will not work. Thus it is essential to know it.
Execute Python Syntax
If we want to execute our Python syntax, we can do it in the terminal like the below program is executed:
Python
Python
print("Coding Ninjas")
You can also try this code with Online Python Compiler
Or we can also make a file with the extension .py and can run it in an IDE or the terminal itself again. For example, we have created a file named firstt.py with the same code as above and we run it in VS Code giving the following output.
OUTPUT
We can also directly run our Python file from the terminal as follows:
Python Indentation
Braces are used to define a code block in most programming languages, including C, C++, and Java. Python, on the other hand, employs indentation.
Indentation begins with the first unindented line and ends with the first indented line in a code block (body of a function, loop, etc.). You can choose the amount of indentation, but it must be consistent across the block.
Indentation is usually done with four whitespaces and is favoured over tabs.
Python
Python
for i in range(5): print(i)
You can also try this code with Online Python Compiler
Variables are basically containers that can store some data. A variable can store integers, characters, strings, boolean values, etc. In Python, there is no syntax or command for declaring a variable. We also do not assign a type to a variable in Python. A variable is created when it is assigned some value. Also, variable names are case-sensitive. We can look at the following examples:-
We can see that ‘x’ and ‘X’ are stored differently. Also, we can print the data type of the variables using the type() function. We can also specify a data type to the variable as we specified float to ‘z’. Also, we can overwrite a variables' data anytime we want, like we did with ‘f’.
Also, there are certain rules for naming a variable in Python, as follows:-
A variable name can start with a letter or an underscore only. It cannot start with a number.
Variable names are case-sensitive.
Variable names can only contain alphanumeric characters.
A variable name cannot be the same as a keyword.
Also, when you want to declare a global variable inside a function, you have to use the keyword ‘global’ next to it. Else, if a variable is initialized outside any function, it is a global variable itself. We can see it in the following code:-
Python
Python
def temp(): global temp temp="Hello Coders!"
temp() print(temp)
You can also try this code with Online Python Compiler
Identifiers in Python are the names that are given to a variable, function, class, etc. You should give meaningful names for better readability of your code. Let us note down the rules for naming an identifier in Python.
It cannot be a keyword's name.
It can only be a combination of a-z, A-Z, _, and 0-9.
It can start with a letter or underscore only.
Some examples of valid Python identifiers are:-
temp1
_temp1
_1_temp
temp_1
Python Keywords
Python Keywords are some reserved words that have a special meaning to them and cannot be used as a variable's name. Following is the code to get the list of all the keywords in Python.
Python
Python
import keyword print(keyword.kwlist)
You can also try this code with Online Python Compiler
In Python, you can use single, double, or triple inverted commas for a string variable as long as you end it with the same kind of inverted comma(s) you started it with. We can see it with the following example:-
Python
Python
s="Hello" j='There' k="""How are you """ print(s) print(j) print(k)
You can also try this code with Online Python Compiler
When writing a program, comments are crucial. They explain what happens inside a program so that someone viewing the source code doesn't have trouble figuring it out.
In a short time, you might forget the crucial details of the program you just built. As a result, taking the time to explain these concepts through comments is always beneficial.
To begin creating a comment in Python, we use the hash (#) symbol.
It goes all the way to the newline character. Comments help programmers understand a program better. The Python interpreter ignores comments.
Python
Python
# this is a comment # the below line is a for loop for i in range(5): print(i)
You can also try this code with Online Python Compiler
Generally, blank lines in Python programs are used to separate two blocks of code to increase the readability of the code. We can see it in the following code
Python
Python
f=4 f="Happy Coding"
print(f)
You can also try this code with Online Python Compiler
Here prompt is a string that gets displayed on the screen while the program waits for the user to enter the input. The prompt string can be empty.
This function takes the user's input and then evaluates the expression, which means Python recognizes if the user typed a text, an integer, or a list automatically. Python will raise a syntax error or an exception if the input provided is incorrect.
For example -
Python
Python
var = input("Enter the value of variable")
You can also try this code with Online Python Compiler
The program execution is halted when the input() function is called until the user provides the input.
The prompt string is displayed on the screen. The prompt is optional, ie., it is not compulsory to provide a prompt to the input() function.
The input function converts whatever you enter into a string. Even if an integer value is entered, the input() function converts it to a string. You must use typecasting to convert it to an integer in your code explicitly.
For example, consider a program that takes two integers as input and prints their sum. We first need to convert them to integers for them to be added.
Python
Python
a = int(input("Enter number 1: ")) b = int(input("Enter number 2: ")) print("The sum is ", a+b)
You can also try this code with Online Python Compiler
Command line arguments are given after the program's names in the command line terminal. The three common ways of dealing with them are sys.argv, getopt, and argparse. sys.argv is a list of command line arguments. When we use this, the arguments passed are in a line. Let us see the code below for the syntax.
Python
Python
import sys
m = len(sys.argv) print("Total arguments passed are:", m-1)
print("\nName of the file:", sys.argv[0])
print("\nArguments passed are :", end = " ") for i in range(1, m): print(sys.argv[i], end = " ")
multiply = 1
for i in range(1, m): multiply *= int(sys.argv[i])
print("\n\nProduct: ", multiply)
You can also try this code with Online Python Compiler
Thus, we have seen how to pass arguments in the command line in Python.
First Python Program
Now that we have covered the basic syntax of Python in deep, we hope that you will be able to code your first Python program keeping in mind all the rules. One such code is as follows:-
Python
Python
print("Hello World!") print("Let the count down begin") for i in range (0, 6): print(5-i) print("Thank"); print("You!")
You can also try this code with Online Python Compiler
Yes, if we compare it to languages like C++ and Java, Python syntax is comparatively easy. The syntax is very much similar to the English language and it is easy to understand for beginners.
How to write code in Python?
To write code in Python, first of all, you should know the correct syntax of different things in it. Firstly correctly study the syntax, then download a Python compiler or IDE, and finally, design the code logically. You can also use the terminal.
What does syntax mean in Python example?
The syntax is a set of rules and instructions on the format of the code. It is mandatory to follow it for correct results and no errors. For example, print("Hello") is correctly written in Python, whereas print "Hello"; is invalid.
Conclusion
Congratulations on making it this far.
In this blog, we understood Python’s input and output functions. We also understood some more basic Python syntax.
If you want to become proficient with Python programming, I suggest you take the Coding Ninjas Python Course, which will teach Python basics with Data Structures and Algorithms.