Table of contents
1.
Introduction
2.
What is Python Syntax?
3.
Execute Python Syntax
3.1.
Python
4.
Python Indentation
4.1.
Python
5.
Python Variables
5.1.
Python
5.2.
Python
6.
Python Identifiers
7.
Python Keywords
7.1.
Python
8.
Python Multi-Line Statements
8.1.
Python
9.
Quotations in Python
9.1.
Python
10.
Comments in Python
10.1.
Python
11.
Using Blank Lines in Python Programs
11.1.
Python
11.2.
Python
12.
Waiting For The User
12.1.
Python
12.2.
Python
12.3.
Python
13.
Multiple Statements on a Single Line
13.1.
Python
14.
Command Line Arguments in Python
14.1.
Python
15.
First Python Program
15.1.
Python
16.
Also see,   reverse a string in python
17.
Frequently Asked Questions
17.1.
Is Python syntax easy?
17.2.
How to write code in Python?
17.3.
What does syntax mean in Python example?
18.
Conclusion
Last Updated: Mar 27, 2024
Easy

Python - Basic Syntax

Author APURV RATHORE
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

python syntax

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
Run Code

 

OUTPUT

execute python syntax

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

vs code python file output

We can also directly run our Python file from the terminal as follows:

terminal python file run

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
Run Code

 

Python enforces indentation, which makes the code look sleek and tidy. As a result, Python scripts appear to be consistent and similar.

Also see, Python Filter Function

Python Variables

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:-

  • Python

Python

x=5
X=4
abc="Coding Ninjas"
cd='Hello'
z=float(5)
f=4
f="Happy Coding"


print(x)
print(X)
print(abc)
print(cd)
print("Type of x is: ", type(x))
print("Type of cd is: ", type(cd))
print(z)
print(f)
You can also try this code with Online Python Compiler
Run Code

OUTPUT

python variables

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
Run Code

 

OUTPUT

global variable

Python Identifiers

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
Run Code

 

OUTPUT

python keywords

Thus, we can list all the keywords in Python.

Python Multi-Line Statements

For executing multi-line statements in Python, we can use ‘\', Parenthesis, and triple quotes. Let us see its result through the following code:-

  • Python

Python

text="Hello\
Coders"
print(text)
d=[4,
3,2]
print(d)
d=(
f"Hello"
f" Ninjas"
)
print(d)
s="""Happy
Learning
"""
print(s)
You can also try this code with Online Python Compiler
Run Code

 

OUTPUT

python multi-line statements

Quotations in Python

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
Run Code

 

OUTPUT

quotations in python

Comments in Python

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
Run Code

Using Blank Lines in Python Programs

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
Run Code

 

But, if we have to print a blank line using a Python program, we can achieve it in the following program:

  • Python

Python

print(5* '\n')
You can also try this code with Online Python Compiler
Run Code

 

The output will be five blank lines.

Waiting For The User

In Python, we can wait for the user to enter something whenever we have to take some input(s) from the user.

Python has an input() function to take user input. 

After taking input from the user, we assign it to a variable. The syntax of the input function is 

  • Python

Python

variable = input(prompt)
You can also try this code with Online Python Compiler
Run Code

 

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
Run Code

 

The input function works as follows:

  • 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
Run Code

Multiple Statements on a Single Line

To execute multiple statements on a single line in Python, we use the semicolon(;).

Let us see this through the following code:-

  • Python

Python

j=9; k=9; s=10; print(j); print(k); print(s)
You can also try this code with Online Python Compiler
Run Code

 

OUTPUT

Multiple Statements on a Single Line

Command Line Arguments in Python

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
Run Code

 

OUTPUT

sys.argv

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
Run Code

 

OUTPUT

first python program

Also see,   reverse a string in python

Frequently Asked Questions

Is Python syntax easy?

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. 

Live masterclass