Amity Institute of Information Technology
Python Syntax
Dr. Rajiv Pandey,
Senior Member IEEE
Amity University Uttar Pradesh,
Lucknow Campus,
Amity Institute of Information Technology
• Python statement ends with the token NEWLINE
character (carriage return). It means each line in a
Python script is a statement. The following Python
script contains three statements in three separate
lines.
2
Amity Institute of Information Technology
Use backslash character \ to join a statement span over
multiple lines, as shown below.
3
Amity Institute of Information Technology
Please note that the backslash character spans a
single statement in one logical line and multiple
physical lines, but not the two different statements in
one logical line.
4
Amity Institute of Information Technology
Use the semicolon ; to separate multiple
statements in a single line.
5
Amity Institute of Information Technology
Expressions in parentheses (),
square brackets [ ], or curly braces { } can be
spread over multiple lines without using
backslashes.
6
Amity Institute of Information Technology
Indentation Rules
• Use the colon : to start a block and press
Enter.
• All the lines in a block must use the same
indentation, either space or a tab.
• Python recommends four spaces as
indentation to make the code more readable.
Do not mix space and tab in the same block.
• A block can have inner blocks with next level
indentation.
7
Amity Institute of Information Technology
The elif keyword is pythons way of saying
"if the previous conditions were not true,
then try this condition".
8
Amity Institute of Information Technology
The following function contains a block
with two statements.
• def SayHello(name):
• print("Hello ", name)
• print("Welcome to Python Tutorials")
9
Amity Institute of Information Technology
Python Naming Conventions
• The Python program can contain variables,
functions, classes, modules, packages, etc.
Identifier is the name given to these
programming elements. An identifier should
start with either an alphabet letter (lower or
upper case) or an underscore (_). After that,
more than one alphabet letter (a-z or A-Z),
digits (0-9), or underscores may be used to
form an identifier. No other characters are
allowed.
10
Amity Institute of Information Technology
• Identifiers in Python are case sensitive, which means variables named
age and Age are different.
• Class names should use the TitleCase convention. It should begin with
an uppercase alphabet letter e.g. MyClass, Employee, Person.
• Function names should be in lowercase. Multiple words should be
separated by underscores, e.g. add(num), calculate_tax(amount).
• Variable names in the function should be in lowercase e.g., x, num,
salary.
• Module and package names should be in lowercase e.g., mymodule,
tax_calculation. Use underscores to improve readability.
• Constant variable names should be in uppercase e.g., RATE,
TAX_RATE.
• Use of one or two underscore characters when naming the instance
attributes of a class.
• Two leading and trailing underscores are used in Python itself for a
special purpose, e.g. __add__, __init__, etc.
11
Amity Institute of Information Technology
Display Output
• The print() serves as an output
statement in Python. It echoes the
value of any Python expression on the
Python shell.
12
Amity Institute of Information Technology
Getting User's Input
• The input() function is a part of the core
library of standard Python distribution. It
reads the key strokes as a string object
which can be referred to by a variable
having a suitable name.
13
Amity Institute of Information Technology
Python input() Method
• The input() method reads the user's input
and returns it as a string.
• Parameters:
– prompt: (Optional) A string, representing
the default message before taking input.
• Return Value:
– A string.
14
Amity Institute of Information Technology
15
Amity Institute of Information Technology
Input with prompt and return type
16
Amity Institute of Information Technology
• Age = Int(age)
17
Amity Institute of Information Technology
Lab Task for 03-10-2022
• Take two numbers as input from the user
• Def a function to add, verify which is
greater
• Redirect the user input to the above
defined functions.
• Show the results.
18
Amity Institute of Information Technology
Reference
• Chapter 4 - Introduction to Problem Solving – NCERT, https://ncert.nic.in
• https://www.geeksforgeeks.org/difference-between-bottom-up-model-and-top-down-m
odel/
• https://gawron.sdsu.edu/python_for_ss/course_core/book_draft/anatomy/import_state
ments.html
• https://www.tutorialspoint.com/python/index.htm.
• https://www.tutorialsteacher.com/python/python-interective-shell
• https://www.tutorialsteacher.com/python/python-syntax
19