Python Programming Basics

In this lesson, We will study the Fundamental Python syntax to create Python programs. The Python line structure, whitespace, indentation, identifiers, and other Python syntax components will all be covered in this lesson.


Python Syntax

Perl, C, and Java are all closely related to Python in syntax. There are some definite distinctions between the languages, though. Python is a highly readable language, and Python is one of the most commonly used languages among learners and experts because of its simple syntax. Python syntax is close to English, making it far simpler to write, read, and understand python scripts than similar ones written in other languages like C or Java.


Python Modes of Programming 

  • Python Programming in Interactive Mode 
  • Python Programming in Script Mode 

Python Programming in Interactive Mode

The following prompt appears when the interpreter is called without giving a script file as a parameter. If you are using a newer version of Python, you must use the print statement within parenthesis, as in print (“Hello! World”). However, this results in the following in Python version 2.4.3.

print "Hello! World"

Otherwise, it will return an error.

File "", line 1
print "Hello! World"
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Hello! World")?
>

Python Programming in Script Mode

When the interpreter is invoked with a script parameter, the script’s execution starts and continues until it is finished. The interpreter turns off after the script is finished.

Let’s create a script for a short Python program. The extension for Python files is “.py”. In a “helloworld.py” file, enter the following source code. We rely on the fact that the Python interpreter is set in the PATH variable. Try running this program now as follows:

$ Python helloworld.py

This results in the following outcome:

Hello! World.

Let’s try running a Python script with a different approach. Here is the updated file for helloworld.py. We assume the “/usr/bin” directory includes the Python interpreter. Try running this program now as follows:

Test.py: $ chmod +x to make the file executable, type $./helloworld.py.

This results in the following outcome:

Hello! World.

This concludes the Python Programming Basics lesson. In the next lesson, you will learn about different data types in Python and their usage.