In this blog, I am going to discuss with you some basics of Python syntax. Python is a simple language to learn. But if you are going to start coding in any particular language, you need to know the syntax or the rules of that particular language.
If you are reading this blog, I will ask you to create a .py file and write code so that your confidence will increase if you study in some practical way.
There are two ways to write and run a basic Python program:
- In Interactive mode – Where you write and execute a program
- Using Script mode – Where you have executed the Python program and have already saved (.py file)
Basics Of Python Syntax: Step By Step
#1. Print Statement
While learning a new programming language we always start with a Print statement so let’s check out how this print statement works in Python.
The syntax of the print statement in the version of Python 2.x is:
print "Hello World" # python version 3.x does not support
The syntax of the print statement in the version of Python 3.x is:
#2. Writing your first Python Program in scripting mode
Let’s write our Hello World program in scripting format and see how to create and execute any scripting file.
First of all, the file you saved should be made executable. Create a hello_world.py file and write the below code in that file.
Now that your file is executable, let us execute the program in scripting mode.
Once you execute this command, your “Hello World” program will print to your terminal.
Script program output:
And now you have learned how to write a program in scripting mode. Now let’s move on to another syntax that is Python Variables.
#3. Python Variables
Variables are nothing but reserved memory locations for storing values. This means that when you create a variable you have some space reserved in memory.
Python has no command to declare a variable.. A variable is created at the same time that you first specify its value.
Variables do not need to be declared in any particular way like other languages and can change once they are set.
x = 7 # x is of type int y = "Test" # x is of type str print(x) print(y)
#4. Multi-line Statement in Python
Usually, each Python statement ends with a newline character. However, we can extend it into multiple lines using the line continuation character (\).
total = 1+ \ 2+ \ 3
#5. Quotes in Python
Single and double quotes are used to represent the strings in Python as well as Single triple quotes can be used to represent a string
print('test') print("test") print('''test''') print("""test""")
test test test test
#6. Comments in Python
It is important to comment on all types of projects, be they small, medium, or large. This is an essential part of our workflow and is seen as good practice for developers.
Types of Comments
- Single-Line Comments
- Multi-line Comments
- Docstring Comments
Such a comment starts with a hash character (#) followed by a text containing the following explanation.
These comments are used as in-line documentation for your code reader and usually explain things in more detail.
DocString is added as a comment below the function, module, or object head and describes what the function, module, or object does.
#7. Multiple Statements in a single line in Python
Multiple statements in a block of unit indents form compound statements. Normally each statement is written on a separate physical line in the editor. However, if the semicolon is split, the statements in the block can be written in one line. The code of the three statements written in separate lines below is as follows:
a=10 b=20 c=a+b print (c)
This statement can be written very well in one line with this semicolon in the middle.
a=10; b=20; c=a+b; print (c)
#8. How to swap two integers without using a third variable
Usually, we swap two integers with the third variable which is easy to do. But in Python, it is much easier.
Check the below code to swap two integers without using the third variable:
a=1 b=2 a,b = b,a print(a,b)
These are some basics of python syntax. Check out Python’s official site for more information.