Basics Of Python Syntax For Beginners

basics of python syntax

Hello Techies,

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)

Let’s learn some basics of Python syntax. If you want a Python installation guide with Django(Ubuntu & Windows) then, check out my previous blog.

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:

print("Hello World")

Output:

Hello World

#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.

print("Hello World")

Now that your file is executable, let us execute the program in scripting mode.

python hello_world.py

Once you execute this command, your “Hello World” program will print to your terminal.

Script program output:

Hello World

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.

Example

x = 7
          # x is of type int
y = "Test"
     # x is of type str
print(x)
print(y)

Output

7
Test

#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 (\).

Example

total = 1+ \
        2+ \
        3

Output:

6

#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

Example:

print('test')
print("test")
print('''test''')
print("""test""")

Output

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
Single-Line Comments

Such a comment starts with a hash character (#) followed by a text containing the following explanation.

Example:

Multi-line Comments

These comments are used as in-line documentation for your code reader and usually explain things in more detail.

Example

DocString Comments

DocString is added as a comment below the function, module, or object head and describes what the function, module, or object does.

Example


#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)

Output

2 1

These are some basics of python syntax. Check out Python’s official site for more information.

Leave a Comment