Module 2: Elementary Python Programming
Installing Python
Step 1: Go to the official Python website: [Link]
Step 2: Download the latest stable version of Python for your operating system
(Windows, macOS, Linux).
Step 3: During installation:
● Check the box “Add Python to PATH”
● Then click Install Now
After installation, you can check that it’s installed correctly:
● Open Command Prompt or Terminal
● Type:
You should see something like:
2. Running Code in the Interactive Shell
After installation, you can use the Python Interactive Shell (also called REPL:
Read-Eval-Print Loop) to test Python commands quickly.
To start it:
● Open Terminal / Command Prompt
● Type:
You’ll see something like:
Python Data Types
Data types in Python are a way to classify data items. They represent the kind of
value, which determines what operations can be performed on that data. Since
everything is an object in Python programming, Python data types are classes and
variables are instances (objects) of these classes.
The following are standard or built-in data types in Python:
● Numeric - int, float, complex
● Sequence Type - string, list, tuple
● Mapping Type - dict
● Boolean - bool
Variables and the Assignment Statement
Variable:
A variable is a named storage location in a program that holds a value.
● It can store data like numbers, text, or other objects.
● The value stored in a variable can be changed during program execution.
Assignment Statement:
Used to assign a value to a variable.
Variable_name=value
The = symbol is the assignment operator.
It does not mean "equals" in math; it means “store this value in the variable.”
Program Comments
Comments:
Comments are notes written in the code to explain what the code is doing. They are
ignored by the Python interpreter.
Types of Comments:
● Single-line: Using #
● Multi-line: Use multiple # lines,
Mixed-Mode Arithmetic and Type Conversion
Mixed-mode arithmetic:
When different data types (e.g., int and float) are used in the same expression.
Python automatically converts int to float if needed.
Type Conversion
Python converts data types automatically in mixed expressions.
type() – Check the data type
Input() – Get user input
Always returns a string by default.
print() – Output to the screen
id() – Memory address of a variable
Returns the unique ID (address in memory) of an object.
int(), float(), str()
Used for type conversion.
PEMDAS Rule | Order of Operations
The PEMDAS rule tells us the sequence in which the expression with multiple
operations is solved. The order is PEMDAS: Parentheses, Exponents,
Multiplication, Division (from left to right), Addition, and Subtraction (from left
to right)
● It begins with brackets, which prioritize the processes encompassed
inside them.
● Following that, exponents and powers are discussed.
● Then, multiplication and division are done from left to right.
● Finally, addition and subtraction are done the same way.
Boolean expressions and Conditional Structure (if-then-else-elif)
1) Practical based on accepting income from the user and finding the income
tax to be paid by the user.
Tax Calculation using Conditional Statements
Practical based on accepting marks from the user and finding the grade obtained.
2) Practical based on accepting a number and checking whether the number is
positive, zero or negative (Application to Law of Trichotomy).
Law of trichotomy - states that every real number is either positive, negative, or zero.
Python program to check whether a number is positive, negative or zero.
3) Practical based on accepting two positive integers and checking whether
they are co-prime or not. (Application to coprime numbers).
Co-prime numbers - Two numbers are said to be co-prime if their GCD is 1.
i.e. GCD(a,b)=1
Python program to check whether the given numbers are co-prime or not -
Math Directory and the built-in functions and values (like, pi, e etc.)
1) Practical based on verification of |a + b| ≤ |a| + |b| and of absolute value
function. (Application to absolute value function).
Absolute value of a number - The absolute value (or modulus) | x | of a real
number x is the non-negative value of x without regard to its sign.
For example, the absolute value of 5 is 5, and the absolute value of −5 is also 5.
The absolute value of a number may be thought of as its distance from zero along
real number line.
Python abs() Function Syntax -
Syntax: abs(number)
● number: Integer, floating-point number, complex number.
Return: Returns the absolute value.
Absolute value of a complex number
Absolute value of a floating number
Python program to verify absolute value property |𝑎 + 𝑏| <= |𝑎| + |𝑏|.
2) Practical based on accepting three positive numbers from the user and
checking whether a triangle is possible with these three numbers as its sides,
and if such a triangle is possible then stating its type (Equilateral, Isosceles,
Scalene).
Triangle
● If the sum of smaller sides is greater than the third side then the given sides forms a
triangle.
Python program -
How to know the given triangle is equilateral, isosceles or scalene triangle based on its
sides?
To determine if a triangle is equilateral, isosceles or scalene triangle based on its side lengths,
you can use the Pythagorean Inequality Theorem, which states that:
1) If all the three sides are equal then the triangle is equilateral.
2) If any two sides of a triangle are equal then the triangle is isosceles.
3) If all the three sides are of different length then the triangle is scalene.
Python program to check whether the given triangle is equilateral, isosceles or scalene -
3) Practical based on accepting sides of a triangle and finding its area.
● Area of the triangle -
Using Heron’s formula, A( ⃤ ABC) =𝑆 (𝑆 − 𝑎)(𝑆 − 𝑏)(𝑆 − 𝑐),
Where S is the semiperimeter of a triangle and is given by
𝑆 = (𝑎 + 𝑏 + 𝑐)/2
Python program to find the area of triangle -
4) Practical based on accepting three positive numbers from the user and checking whether
a triangle is possible with these three numbers as its sides, and if such a triangle is
possible then stating its type(Acute-angled, Right-angled, Obtuse-angled).
How to know the given triangle is acute, obtuse or right angle angled triangle based on its
sides?
To determine if a triangle is acute or obtuse based on its side lengths, you can use the
Pythagorean Inequality Theorem, which states that:
1) 1If the square of the longest side is greater than the sum of the squares of the two shorter
sides, the triangle is obtuse
2) If the square of the longest side is less than the sum of the squares of the two shorter
sides, the triangle is acute
3) If the square of the longest side is equal to the sum of the squares of the two shorter sides,
the triangle is right angled
Python program to state the type of triangle(i.e. Right angled triangle, Obtuse angled triangle OR
Acute angled triangle)
OR
Loop Structure with pre-known number of iterations (for loop)
1) Practical based on accepting a non-negative integer from the user and finding its
factorial.
● Factorial of a number - The factorial of a number is the product of a number and every
natural number below it, down to 1. It is represented by the symbol "!".
For example, the factorial of 3 is 3! = 3 × 2 × 1 = 6.
2) Practical based on finding all the integral roots (lying in some fixed interval, to be
decided by the user) of a given polynomial.
Python program to find all the integral roots of the polynomial -
Short-cut method -
2
Find the roots of the polynomial 𝑥 + 2𝑥 + 1
3 2
Find the roots of the polynomial 𝑥 + 3𝑥 + 2𝑥 + 1
3) Practical based on accepting a positive integer from the user and getting square root of
the same to the desired level of accuracy.
Find the square root of the given number.