bing
Flat 10% & upto 50% off + 10% Cashback + Free additional Courses. Hurry up
×
UPTO
50%
OFF!
Intellipaat
Intellipaat
  • Live Instructor-led Classes
  • Expert Education
  • 24*7 Support
  • Flexible Schedule

Python Coding with Examples

Python was specifically designed to be a highly readable language. It’s ease of use and neat syntax have made it one of the most used language among freshers as well as experts. Python has English like syntax which makes it much easier to write, read and understand a python script as compared to a similar script written in other languages such as C or Java.

A syntax of any language refers to the structure of that language which includes a certain set of rules, subjective to that respective language. The Python syntax also comprises of some set of rules and elements that collectively make up the Python Syntax.

Following is the list of topics that we will cover in this module, in case you want to jump to a specific one.

Watch this Python basics video

 

Python Line Structure

Python coding style comprises of physical lines as well as logical lines or statements. A physical line in a python program is a sequence of characters and the end of the line terminates the line sequence as opposed to some other languages such as C and C++ where a semi colon is used to mark the end of the statement. A logical line on the other hand is composed of one or more physical lines. The use of semi colon is not prohibited in Python although it’s not mandatory either. The NEWLINE token denotes the end of the logical line.

Python Code Example:

Variable = 6

NOTE: A logical line that only contains spaces, comments or tabs are called blank lines and they are ignored by the interpreter.

Python Multiline Statements

As we saw that in python, a new line simply means that a new statement has started. Although, Python does provide a way to split a statement into a multiline statement or to join multiple statements into one logical line. This can be helpful to increase the readability of the statement. Following are the two ways to split a line into two or more lines

Explicit Line joining

In Explicit line joining you use a backward slash to split a statement into a multi-line statement.

Example:

print ("this\
is a python coding example")

You can also use explicit line joining to join two or more physical lines into a logical line, using back slash.

Implicit Line Joining

Statements that reside inside [], {} or () brackets can be broken down into two or more physical lines without using back slash.

Example:

months = [' January', 'February',
'March', 'April']

Multiple statements on single line

In Python, it is possible to club multiple statements in same line using a semi colon, however most programmers do not consider this good practice as it reduces the readability of the code.

Example:

a=10; b=30;
print(a); print(b);

Whitespaces and Indentation

Unlike most of the programming languages python uses indentation to mark a block of code. According to python coding style guideline or PEP8, you should keep an indent size of four.

Whitespaces and Indentation

Most of the programming languages provide indentation for better code formatting and don’t enforce to have it. But in Python it is mandatory. This is why indentation is so crucial in Python.

For example, if you run the following code, you will get an error of “expected indentation”

a=1
if a>0 :
print("There is no indentation in this statement")

Identifiers

Identifiers in python are nothing but user-defined names to represent programmable entity like variables, functions, classes, modules or any other objects. But there are a few rules that we need to follow while defining an identifier. They are:

  • You can use a sequence of letters (lowercase (a to z) or uppercase (A to Z)). You can also mix up digits (0 to 9) or an underscore (_) while defining an identifier.
  • You can’t use digits to begin an identifier name.
  • You should not use Reserved Keywords to define an identifier.
  • Other than underscore (_) you are not allowed to use any other special characters.
  • Even though python doc says that you can name an identifier with unlimited length. But it is not entirely True.

Using a large name (more than 79 chars) would lead to the violation of a rule set by the PEP-8 standard. It says.

Reserved Words

Reserved words are nothing but a set of special words, which are reserved by python and have specific meanings. Remember that we are not allowed to use keywords as variables or constants in python.

Reserved words in python are case sensitive. Following table shows all the reserved words in Python.

False class finally is return
None continue for lambda try
True def from non local while
and del global not with
as el if or yield
assert else import pass
break except in raise

Python Quotations

In Python, single quotes as well as double quotes both are supported for strings. If you have started a string with a single quote, it mandatory to end it with single quote only. The same goes for double quotes.

Example:

print (' singles quoted string')
print ("double quoted string")

This brings us to the end of this module to Learn Python. Next module highlights Python variables. See you there!

Further, check our offers for Python training and prepare yourself with our free interview questions and answers for Python.

Previous Next

Download Interview Questions asked by top MNCs in 2019?

"0 Responses on Python Syntax"

Leave a Message

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.
top

Sales Offer

Sign Up or Login to view the Free Python Syntax.