Press "Enter" to skip to content

Python basic syntax

Zigya Acadmey 0

Before you start writing your first Python program, you’ve got to learn the basics. We will walk you through Python syntax basics that will help as a building block for your Python career.

To get started, let’s first write a very basic Python program.

Writing your First Python Program

There are two ways in which you can write and execute a basic Python program:

  1. In Interactive mode – where you write a program and execute it
  2. Using Script mode – where you execute and already saved Python program(.py file)

Let’s see those in action.

Writing “Hello, World!” on Interpreter

To enter the interactive Python mode enter the following command on your Terminal.

C:\Users\Zigya>python
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

And, now you should be in the interactive mode.

But, if you are using an IDE you don’t need to type the command above to get into interactive programming mode.

Here is Basic Python syntax for executing a Hello World Program.

When you write this program and press enter, your IDE should display “Hello, Word!”

print("Hello World”)

Anything that you write within “ “ of print(“ ”) gets printed on your IDE.

In our case, the output that you should see on your screen would be
Hello, World!

Running Your First Python Program in Scripting Mode

When you wrote your Hello World program earlier, let’s assume that you saved it in a Python file. When you save your first program, look for app .py extension file.

Assuming that you saved it as Hello_Python.py, here’s how you would execute this code in a scripting mode.

In Linux

First of all, the file you saved has to be made executable. We ideally do that by the command:

$ chmod +x test.py

Now that your file is executable, let’s run the program in scripting mode.

$ python Hello_Python.py

Once you execute this command, you will see “Hello, World!” was printed on your Terminal.

"Hello, World”

And, there you have learned your first Python Syntax.

In Windows

You can directly write python along with the file you want to execute

C:\Users\Zigya>python Hello_Python.py

Once you execute this command, you will see “Hello, World!” was printed on your Terminal.

"Hello, World”

And, there you have learned your first Python Syntax.

Python Keywords and Identifiers

There are a total of 31 keywords and 5 major identifiers that you should know in Python.

What are Identifiers in Python

A Python identifier usually is a variable, a function, a class, a module or it can be any other object. You give a name to an entity in Python, it is called an identifier. A valid identifier starts with a letter(A-Z, a-z both apply) or an underscore(_) which can be followed by zero, letters, underscores or numbers(0-9).

Types of Identifiers in Python:

  1. Variables
  2. Functions
  3. Modules
  4. Class
  5. Other Objects

Let’s go over and check what Keywords are available in Python

Python Syntax Basics – Keywords

To see all 3 Python Keywords, open your IDE and type the following:

>>>import keyword

>>> keyword.kwlist

You should see something like this as the output

['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

These keywords may often change as new Python versions are released. A couple things in mind:

  1. These are fixed and cannot be used as identifiers
  2. They are case sensitive

These Keywords are also often referenced as Reserved words.

Lines and Indentation

Python provides no braces to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced.

The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount. For example −

if True:
   print "True"
else:
   print "False"

Multi-Line Statements

Statements in Python typically end with a new line. Python does, however, allow the use of the line continuation character (\) to denote that the line should continue. For example −

total = item_one + \
        item_two + \
        item_three

Statements contained within the [], {}, or () brackets do not need to use the line continuation character. For example −

days = ['Monday', 'Tuesday', 'Wednesday',
        'Thursday', 'Friday']

Quotation in Python

Python accepts single (‘), double (“) and triple (”’ or “””) quotes to denote string literals, as long as the same type of quote starts and ends the string.

The triple quotes are used to span the string across multiple lines. For example, all the following are legal −

word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is
made up of multiple lines and sentences."""

Comments in Python

A hash sign (#) that is not inside a string literal begins a comment. All characters after the # and up to the end of the physical line are part of the comment and the Python interpreter ignores them.

# First comment
print "Hello, Python!" # second comment

This produces the following result −

Hello, Python!

You can type a comment on the same line after a statement or expression −

name = "zigya" # This is again comment

You can comment multiple lines as follows −

# This is a comment.
# This is a comment, too.
# This is a comment, too.
# I said that already.

Following triple-quoted string is also ignored by Python interpreter and can be used as a multiline comments:

'''
This is a multiline
comment.
'''

Using Blank Lines

A line containing only whitespace, possibly with a comment, is known as a blank line and Python totally ignores it.

In an interactive interpreter session, you must enter an empty physical line to terminate a multiline statement.

Conclusion

This brings the end of this Blog. We really appreciate your time.

Hope you liked it.

Do visit our page www.zigya.com/blog for more informative blogs on Data Science

Keep Reading! Cheers!

Zigya Academy
BEING RELEVANT

Leave a Reply

Your email address will not be published.