Python – Basic Syntax

Basic Syntax of Python Programming IDE

The syntax of the Python Programming Language is fairly easy to learn and you can easily follow.

In this lesson, we are going to write a program in Python and then work through the program line by line to explain the various ingredients that make up a Python Program.

I would recommend you use PyCharm to write and run this codes. PyCharm is free and can be downloaded from https://www.jetbrains.com/pycharm-edu/download

# Your First Python Program
name = "Kindson The Tech Pro"
Print("Name is " + name)
# End of Program

Comments in Python

The first line in the code that begins with a # is a single line comment in Python. Comments are texts you include in your program that are not really part of your program. They are not executed by the interpreter. So you can use comment to add information like:

  • Your Name
  • Date of the Program
  • Description of the Program

 

You can create multi-line comments in Python using triple single quotes (”’). The comment begins with three single quotes and ends with three single quotes.


Identifiers in Python

An identifier in Python is a name given to a variable, function, class or other objects by a programmer. Remember that variables are memory locations.

A Python identifier begins with an alphabet or an underscore followed y a number of letters, digits or underscores. You cannot use punctuation or special characters in an identifier. Also note that identifiers are case sensitive. The following are rules for identifiers in Python:

  • An identifier cannot start with a number
  • An identifiers cannot contain any special character
  • Class names in Python begins with an uppercase letter while other identifiers begins with lower case
  • An identifier that begins with two underscores indicates its a private identifier
  • An identifier that ends with two underscores indicate it is an inbuilt identifier provided by the language

Python Reserved Words

Reserved words are keywords used by the programming language and you cannot used then and names of your variables or identifiers.

Python keywords are all lowercase as listed below:

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

Lines and Indentation in Python

One unique feature of the Python programming language is absence of braces which is normally used to indicate block of code in other programming languages. In Python, blocks of codes are specified using line indentation

Statements in the same level of indentation belong to the same block. An example is given below:

# Executes correctly
a = 10
b = 20
if a == b:
   print("a equals b")
else:
   print("a is not equal to b")

You will notice that the code below compiles fine. Additionally, notice the colon (:) after the if and else statement which means that the following statements should be indented, failure of which would result in error.

If however the , above code is written without indentation as shown below:

# Would generate an error
a = 10
b = 20
if a == b:
print("a equals b")
else:
print("a is not equal to b")

Multi-line Statements in Python

If a statement would require more than one line, then Python provides a functionality to wrap a line of code to the next line. It is the continuation character, the backslash (\). When this is used at the end of a line, then the interpreter will take the next line as part of the the current statement. An example is therefore given below

title = "Mr. "
firstname = "Kindson"
lastnaname = "Munonye"
othername = "Kany"
fullname = title + \
           firstname + \
           lastname + \
           othername 

This is the same as :

title = "Mr. "
firstname = "Kindson"
lastnaname = "Munonye"
othername = "Kany"
fullname = title + firstname + lastname + othername 

Getting User Input

Sometimes, you may require your program to get an input from the user. This is accomplished in Python using the input function. For example

name = input("Please enter your name: ")

The result of the above code is that a prompt is displayed to the output that says: “Please enter your name”. If the users enters his name and then press the enter key, the name the user entered is assigned to the variable name.


Watch the Video