MDM-III
UNIT-I
Python
• Python is an easy to learn, powerful programming language.
• It has efficient high-level data structures and a simple but effective approach to object-oriented
programming.
• Python elegant syntax and dynamic typing, together with its interpreted nature, make it an
ideal language for scripting and rapid application development in many areas on most
platforms.
• Python is a high-level, interpreted, interactive and object-oriented scripting language.
• Python is designed to be highly readable. It uses English keywords frequently where as other
languages use punctuation, and it has fewer syntactical constructions than other languages.
Python Features
Easy to Learn
• It is more expressive means that it is more understandable and readable.
• More emphasis on the solution of the problem rather than the syntax
High-level Language
• In Python, no need to take care about low-level details such as managing the memory used by the
program.
Compiled Interpreted and Platform Independent
• Internally, Python converts the source code into an intermediate form called byte code.
• This byte code is then executed by interpreter on PVM (Python virtual machine).
Free and Open Source
• Python language is freely available at official web address.
• The source-code is also available. Therefore it is open source.
Identifiers
• Identifiers are the names used to identify variables, functions, classes,
etc.
)
Rules:
•Must start with a letter (A–Z or a–z) or underscore _
•Cannot start with a digit
•Cannot be a keyword
•Case-sensitive
Example:
name = "Alice"
_age = 25
2num = 10 # ❌ Invalid
Keywords
Python keywords are the words that are reserved. That means you can’t use them as name
of any entities like variables, classes and functions.
There are 33 keywords in Python programming language
Statement and Expression
•A statement is an instruction that the interpreter can execute (e.g., assignment,
print).
•An expression is a combination of values, variables, and operators that results
in a value.
Example
x=5 # Statement
y = x + 3 # Expression within a statement
print(y) # Statement
Variables
• A variable is a location used to store data in the memory.
• It is helpful to think of variables as a container that holds data which can be
changed later throughout programming.
• In Python, we don't need to specify the type of variable because Python is a
dynamically-typed. It automatically decides the datatype depending on type of
value.
Example:
name = "Alice" # String
age = 25 # Integer
height = 5.6 # Float
is_student = True # Boolean
Data Types
• Variables can store data of different types, and different types can do different things.
• Every value in Python has a datatype.
• Since everything is an object in Python programming, data types are actually classes and
variables are instance (object) of these classes.
Indentation
Indentation in Python refers to the whitespaces at the start of the line to indicate a block of code. We can create an
indentation using space or tabs. When writing Python code, we have to define a group of statements for functions and
loops. This is done by properly indenting the statements for that block.
The leading whitespaces (space and tabs) at the start of a line are used to determine the indentation level of the line. We
have to increase the indent level to group the statements for that code block. Similarly, reduce the indentation to close the
grouping.
Indentation
Python Indentation Rules
•The first line of Python code can’t have an indentation, it will throw Indentation Error.
•You should avoid mixing tabs and whitespaces to create an indentation. It’s because text
editors in Non-Unix systems behave differently and mixing them can cause the wrong
indentation.
•It is preferred to use whitespace than the tab character.
•The best practice is to use 4 whitespaces for the first indentation and then keep adding
additional 4 whitespaces to increase the indentation.
Indentation
Benefits of Indentation in Python
•Indentation is important to increase the readability of the code by clearly indicating the
hierarchy (i.e. if statements belong to the same block or not).
•This avoids the need to use delimiters such as braces or brackets, which is mandatory in
most programming languages like C, C++, Java, etc.
•Python indentation is consistent throughout the program, making it easier to debug.
•In most programming languages, indentation makes the structure of the code proper.
Python uses it for grouping, making the code automatically beautiful.
•Python indentation rules are very simple. Most of the Python IDEs automatically indent
the code for you, so it’s very easy to write the properly indented code.
Indentation
Indentation
Indentation Error Examples
Comments
Comments are hints that we add to our code to make it easier to understand.
Python comments start with #
Ex:
# print a number
print(25)
Comments are completely ignored and not executed by code editors.
Why Use Comments?
•For future references, as comments make our code readable.
•For debugging.
•For code collaboration, as comments help peer developers to understand each other's code.
Comments
Types of Comments
Comments
Types of Comments
Comments
Types of Comments
Operators
Operators
Operators
Operators
Operators
Input Function
Input Function
Input Function
Control Statements
Control Statements
Examples :
WAP to read a number and check if it is an even no or odd no
Control Statements
Examples :
WAP to read 3 angles and check if triangle can be formed or not.
Control Statements
if…elif…else Statements:
The elif is short for else if. It allows us to check for multiple expressions. If the condition for if is False, it checks the
condition of the next elif block and so on. If all the conditions are False, body of else is executed. Only one block
among the several if...elif...else blocks is executed according to the condition. The if block can have only one else
block. But it can have multiple elif blocks.
Control Statements
if…elif…else Statements:
Control Statements
if…elif…else Statements:
Examples :
WAP to read 3 different numbers and find greatest of them.
Control Statements
if…elif…else Statements:
Examples :
WAP to read a number and check if it is positive, negative or zero
Control Statements
if…elif…else Statements:
Example: WAP to check the given number (3) is divisible 2 or 3
Control Statements
Nested-if statement:
A nested if statement in Python is an if statement located within another if or
else clause. This nesting can continue with multiple layers, allowing
programmers to evaluate multiple conditions sequentially. It's particularly useful
in scenarios where multiple criteria need to be checked before taking an action.
Control Statements
Nested-if statement:
Control Statements
Nested-if statement:
Control Statements
While Loop :
Python while loop is used to repeatedly execute some statements until the condition is true. The
while statement works with a condition rather than a sequence. The condition states that the
while statement should perform a task until the condition is no longer true. First condition is
checked If condition is true, then statement within while loop are executed and again condition
is checked The above process is repeated while the condition is true. Program control is
transferred to next statements only when condition becomes false.
Control Statements
While Loop :
Output
Control Statements
While Loop :
Output
Control Statements
While Loop :