Python Syntax

Just like grammar rules in the English language, so is syntax in the Python Language. So said, for you to be able to code properly in python, you must understand what python Syntax is, the types of Python Syntax, their meaning, when to use it, how to use it, and also the Syntax Rules. Interestingly, the aforementioned subjects will be covered on this page. Let’s begin.

What Is Python Syntax?

Python syntax is a set of rules a programmer should write his code on to enable the computer to read and interpret. Python Syntax is similar to Perl, C, and Java.

Python Identifiers:

As the name implies, python Identifiers are used to identify a variable, a function, a class, a module, and other objects in the python language. It can either start with Uppercase A-Z or lowercase a-z. An identifier may be an underscore (_) followed by zero or more letters or an underscore and digits ranging from 0 to 9 (0 to 9). It’s important to note the following:

  • Python is case-sensitive Love and love has da different meanings in python
  • Python does not permit the use of special characters like %, @, $, and lots more.

Reserved Keywords:

Python Reserved keywords are keywords that can’t be used as names of variables, functions, or classes in python code writing, they are used to identify the language structure or syntax. There are 33 reserved Keywords in python. See the table below.

andelseinreturn
asexceptisTrue
assertfinallylambdatry
breakfalsenonlocalWith
classforNonewhile
continuefromnotyield
defglobalor 
delifpass 
Elifimportraise 

Note: True and None begin with Upper case.

We’ll delve deeper into the meaning of each reserved Keyword, for the time being, let’s progress.

Python Indentation:

Python is a unique language that doesn’t use curly braces {} it’s been substituted by indentation of the code lines to form a code block. Indentation simply means the space before the next code. You must add at least a space in every code block, you’ll have error feedback if you skip a gap also use the same number of indentations in one block of code

Example

#!/usr/bin/python3 
import sys   
file_finish = "end" 
file_text = " contents=[]   

file_name=input("Enter filename: ") 
if len(file_name) == 0:
    print("Please enter filename")    
    sys. exit()   

try:    
    # open file stream
    file = open(file_name, "w")   

except for IOError:
    print ("There was an error writing to", file_name)
    sys. exit()   
print ("Enter '", file_finish,) 
print ("' When finished")   

while file_text != file_finish:
    file_text = input("Enter text: ")
    contents. append(file_text)
    if file_text == file_finish:
       # close the file
       file.close()
       break   
print(contents) 
data = ' '.join([str(elem) for elem in contents])  
print(data) 
file.write(data) 
file.close() 
file_name = input("Enter filename: ")   

if len(file_name) == 0:
    print ("Next time please enter something")
    sys. exit()   
try:
    file = open(file_name, "r")   
except for IOError:
    print ("There was an error reading file")
    sys. exit() file_text = file.read() 
file.close() print (file_text)

Pay attention to the spaces given before the next blocks of codes. Without braces () you should be able to understand that the gap between each code formed a code block.

Python Quotation:

Clearly stated, just as a sentence is a string of words written together to make a complete sense, that’s how quotes are used in python to start and end a sequence of characters in coding called String. Example: ‘bye-word’. Python enables the use of single quotes, double quotes, and triple quotes.

Python Single Quotes:

Single quotes are used to mark a string that has another quote within. It’s also used to mark a direct quote as a new storyline. When programming, use single quotes for

  • String literals: ‘hyper-romantic’
  • When your string will contain a double quote within: ‘ “Hello” James

Python Double Quotes:

A double quote is used for string representation, it’s used to mark a sentence like a quotation. Example: “I like the way you smile”. Similar to single quotes, use double quotes to enclose a string with a single quotation.

See the code example below: Input

wish = "Hello World!" 
print(wish) hey = "AskPython says "Hi" 
print(hey) 
famous =" 'Simon Monday' is in London." 
print(famous)

Output:

Hello World!   
Invalid Syntax   
'Simon Monday' is in London.

Triple Quotes:

Triple quotes enable you to write multi-line sentences in python.

Example: Input

sentence1 = ”’She asked, “did you speak with him?””’
print(sentence1)
sentence 2 = ”'”That’s great”, she said.”’
print(sentence2)

Output:

She asked, “did you speak with him?”
“That’s great”, she said.

Once again use enclose a double-quoted string with a triple-quote string.

Python Comment:

A comment is a hash-tag symbol(#) that is not located inside a string literal. After the comment symbol(#) all the words in a string literal till the end of the same string line is part of the comment, and the python interpreter won’t read it. It is only used to explain some complex structures in a code.

If you must comment on python, you must do so on each code line separately, because no multi-comment provision on python

Example 1: Each comment next line

# this is what I mean
#another comment for another line
# now you understand

And not like this

#Hello world! #dont try this #never do this

Example 2: Python ignores quotations

Input:

#!/usr/bin/python3  
# First comment
print (“Hello, World!”) # second comment

Output:

Python only reads and interprets the characters that were not commented on

Hello, world!

Example 3: You can type a comment on the same string literally after a character

“Simon” # This is a comment

Blank Lines in Python:

Blank Lines means leaving a whole line with a character, sometimes a comment might be added. When this is done, the python interpreter ignores it.

Multiline Statements on Python:

On python, a new line indicates a new statement, however, a single statement can be split into two or more lines that bring about the name, of a multiline Statement. This is done to enhance readability among complex characters

Example 1: You can use backlash to split a statement into several lines:

Input:

>>> print(“Hi\  
I love you”)

Output:

Hi I love you

Example 2: Use semicolons (;)to write multiple statements on a single line:

import sys; x = ‘foo’; sys. stdout.write(x + ‘\n’)

Syntax Rules:

You’re already aware that the grammatical rules serve for good sentence construction in the English Language. Similarly, the syntax rules enable the program to code properly in the python language. The syntax rule is international, all python code writers will write the same thing and on the same standard. Some major rules you shouldn’t forget are listed below:

  • Python is case-sensitive, for example, Windows is not windows
  • Use only English names in writing programs
  • All functions should begin with a lowercase
  • All variables must start with a lowercase letter, for example, name=Phillip
  • Classes should be UPPERCASE
  • Numbers and special characters like $, a, % are not allowed
  • Key reserved words are not allowed in naming. See all 33 reserved words here
  • Indentations are used to mark code block segments

Summary:

Python Language couldn’t make code programming any easier, with consistency and passion you can understand the python programming language within hours. Admittedly, it may appear tough at first, but if it wasn’t tough enough, it wouldn’t have elevated like a staircase. Now that you have learned the basic python variable types, what next? It’s python tutorial time!

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment