Python Basic Syntax

In this section of the Python tutorial, we will have a look at the basic syntax of Python programming language. This tutorial introduces the reader to the Python language and basic syntax.

Basic Syntax of Python

Python is a versatile programming language that is easy to learn. It has efficient high-level data structures and an objective-oriented programming approach that is simple but effective.

Together with its interpreted nature, Python’s elegant syntax and dynamic typing make it an ideal language for scripting and rapid application development on most platforms in many areas.

Python is a case-sensitive language. It means Variables and variables are not the same.

Keywords in Python

Keywords are Python’s reserved words. We can not use a keyword as the name of the variable, the name of the method or any other identifier. We are used to describing the Python language’s syntax and structure.

Please note that the keywords in Python are case sensitive.

Python contains 33 keywords. Over time, this number can vary slightly.

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

Identifiers in Python

An identifier is name for objects such as class functions, variables, etc. 

  • Identifiers may be a combination of lower case (a to z) or uppercase (A to Z) letters or digits (0 to 9) or underscore letters.
  • A digit can not be used to begin an identifier.
  • It is not possible to use keywords as identifiers.

Lines and Indentation in Python

For class and function definitions or flow control, Python does not provide braces to signify blocks of code. Line indentation signifies blocks of code, which is rigidly enforced.

The number of spaces in the indentation varies, but the same amount must be indented for all statements within the row.

Example:

# Python Program - Add Two Numbers

print("Enter '0' for exit.");
print("Enter two numbers: ");
val1 = int(input());
val2 = int(input());
if val1 == 0:
    exit();
else:
    sum = val1 + val2;
    print("Sum of the given two number:",sum);

For more complicated tasks, we can use Python than putting two numbes. For example, we may write an initial Fibonacci series sub-sequence as follows:

>>> # Fibonacci series:
... # the sum of two elements defines the next
... a, b = 0, 1
>>> while b < 10:
...     print b
...     a, b = b, a+b
...
1
1
2
3
5
8

Comments in Python

It starts with the hash character, #, and continues to the physical line’s end. At the start of a line or following white space or code, a statement can appear, but not within a literal sequence. A hash character is just a hash character within a literal string. Because comments are meant to explain code and are not interpreted by Python, while typing in examples, they may be omitted.

Example:

# this is the first comment
spam = 1  # and this is the second comment
          # ... and now a third!
text = "# This is not a comment because it's inside quotes."

Leave a Reply

Your email address will not be published. Required fields are marked *

Shopping cart