0% found this document useful (0 votes)
3 views22 pages

Module 3 & 4 (Notes)

Modules 3 and 4 cover key programming concepts in Python, including comparison operators, conditionals, loops, computer logic, bitwise operators, list processing, tuples, and user-defined functions. Each topic is explained with definitions, syntax, and practice problems to reinforce learning. The document emphasizes the importance of understanding these foundational elements for effective programming in Python.

Uploaded by

aayan.work.only
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views22 pages

Module 3 & 4 (Notes)

Modules 3 and 4 cover key programming concepts in Python, including comparison operators, conditionals, loops, computer logic, bitwise operators, list processing, tuples, and user-defined functions. Each topic is explained with definitions, syntax, and practice problems to reinforce learning. The document emphasizes the importance of understanding these foundational elements for effective programming in Python.

Uploaded by

aayan.work.only
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module # 3 & 4 (Notes)

The main topics that are going to be covered in module 3 are as


follows:
1. Comparison Operators
2. Conditionals (if, if else, if elif else)
3. Nested Conditionals (if statement in the body of another if )
4. Loops (Such as For Loop and while Loop)
5. Nested Loops ( A for loop within the body of another for loop)
6. Computer logics ( Logic gates (and, or, not, xor)
7. Bitwise Operators
8. List processing
9. Tuples and User defined functions

1. Comparison Operators:
As it has been already discussed in our previous lessons as well
that the comparison operators are as follows:
 >
 <
 >=
 <=
 ==
 !=
They can be used in a variety of ways in our python programs and some
of the examples are as follows:
Word Problem:

Solution:
2. Conditionals
In python conditionals are mainly of 3 types:
1. If
2. If/else
3. If/elif/ else
The detail about each and every type is explained as follows:
Nested if-else statement:
Nested conditionals means that when and if statement if used within
the body of another if statement.
Practice Problems (Conditionals):
1. Write a program to find greatest of two numbers and take
numbers as input from the user?
Solution

2. Write a program to find greatest of three number also take


numbers as input from the user.
Solution
3. Find the greatest of three numbers by using max function
Solution:
3. Loops in Python
In Python programming, we mainly discuss two types of loops and those are
explained as follows:
1. While Loop
2. For Loop
Syntax of While Loop:
while (condition):
Body of while loop
Syntax of For Loop:
for variable_name in range ( starting value, ending value, increment/decrement)

Explanation of While loop:


Practice Problem (While Loop):
1. Print the series of even numbers by using While Loop.

2. Print the series of odd numbers by using while loop.

3. Find number of even and odd numbers using while loop and
conditionals:

Explanation of for Loop:


Practice Problem (For Loop):
1. Print series of times table of a number in both forward and
reverse order by using for loop.
Forward Order:

Reverse Order:

2. Find the exponential power of 2 up to 15 using for loop.

3. Use Nested for loop to print the times table of both 4 and 3 at the
same time?
Solution:

4. Computer Logics:
Computer logics are made by using the basic logical operators
some of them are listed as follows:
a) AND
b) OR
c) NOT
d) XOR
Before discussing the coding section, we should understand that how
each of the logical operators works on the basis of their Truth Table.
Programs:
1. Write a program to apply all basic logical operators.
Solution:

2. Take two numbers from user and apply basic logical operators
on them as explained as follows:
a) If a is greater or b is lesser, Print the product of a, b
b) If b is greater and a is lesser, Print the difference of a and b.
c) If a and b are equal, Calculate a mod b.
Solution:
5. Bitwise Operators:
Bitwise operators are very much alike logical operators but the
basic difference is that all bitwise operators deals with the
numbers in binary form and then we can apply these operations
on each and every bit of a certain value.
Table:

6. List Processing:
List processing in Python involves working with lists, which are ordered,
mutable, and versatile data structures. Python provides a rich set of built-in
functions and methods to manipulate and process lists efficiently. Here are
some common list processing operations in Python:
1. Creating Lists.

2. Accessing List Elements.

3. Modifying Lists.
4. List Slicing.

5. List Concatenation

6. List Length.

7. Iterating Over Lists.

8. Finding Elements.

9. Reversing a List.
7. Tuples:
Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types
in Python used to store collections of data.

A tuple is a collection which is ordered and unchangeable.

Tuples are written with round brackets. ( )

Examples:
8. Functions:
In Python we have two types of functions

1. Built-in- Functions.
2. User defined Functions.

Built-in-Functions:
Built-in-Functions are the functions which are already defined in Python. In simple words,
we can directly use these functions without creating them. Some of the examples of built-
in-functions are as follows:
 Print ( )
 Input ( )
 Int ( )
 Float ( )
 List ( )
 Tuple ( )

User Defined Functions:


Those functions which are created by the user him\herself are known as user defined functions
User defined functions have three major parts which are listed as below:
1. Function Declaration
2. Function Definition
3. Function Calling

Function Declaration
The method to initialize (Start) your user defined function is known as function declaration
Example:
def function_name ( ):
In the above line, the syntax to create a user defined function is written which means that if a
user wants to use a function, the one must follow the exact same syntax.

Function Definition
The content present inside of a user defined function is known as function definition. It allows you
to write or modify your code inside of a function body.

Example:

In the above snippet, the simple addition code inside the function body can be called as function
definition.

Function Calling (Invocation)


The third or the most important part of a user defined function’s structure is known as function
calling. Its concept is very simple which can be understood with the help of a simple example.

If a teacher wants to call a specific student from a class so he\she will address that student by
his\her first name which means, without calling a student the one cannot listen to the teacher.
Same is the case in user defined functions. If a user wants to run a specific function code out of
many so, that particular function should be called by its name.
Example:

It can be seen very clearly that once the add () function is called, it has produced the output
otherwise it will give no result.

Parameterized vs Non-Parameterized functions:


User defined functions are further categorized into two parts known as
1. Parameterized functions
2. Non-parameterized functions
In the above section, all the examples which we have discussed were of non-parameterized
functions. The basic difference between the two can be understood easily with the help of
following example
Let us look at these codes

Both of these codes will do the same function but, the one on the right side takes the value of a
and b as parameters inside the function’s parenthesis and during function calling, we have
assigned the value of 4 to a and 3 to b.
Examples (Non-parameterized functions):
1. Write a program to add, subtract, multiply and divide two
numbers using non-parameterized user defined functions.

2. Implement a code to find the volume of a cylindrical sphere


using functions.
3. Create a simple password using functions.

Examples (Parameterized functions):


1. Calculate cube of a number using a parameterized function.

2. Find the result of whole square formula using parameterized


function.

3. Find the following


S = vit + ½ at2
Using parameterized function.
4. Find that weather a student is pass or fail by using his marks as
parameter.

You might also like