Python Module 1 Notes
Python Module 1 Notes
PYTHON PROGRAMMING
Code: 25BPLC105A/205A
2025 Scheme
MODULE 1
Introduction, Variables,
Expressions and Statements,
Iterations and Functions
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
MODULE 1
Syllabus:
The way of the program: The Python programming language, what is a
program? What is debugging? Syntax errors, Runtime errors, Semantic
errors, Experimental debugging.
Variables, Expressions and Statements: Values and data types, Variables,
Variable names and keywords, Statements, Evaluating expressions,
Page 1
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
The >>> is called the Python prompt. The interpreter uses the prompt to
indicate that it is ready for instructions. If you typed 2 + 2, and the interpreter
evaluated our expression, and replied 4, and on the next line it gave a new
prompt, indicating that it is ready for more input.
Python interpreter modes
Page 2
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
Page 3
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
Page 4
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
Page 5
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
Page 6
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
3. String Type(str):
o Python programs can also have text values called strings, or str.
o Strings in Python are surrounded by either single quotation marks or
double quotation marks. Ex: 'Hello' is the same as "Hello".
Page 7
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
o Example 3: The values like "17" and "3.2". They look like numbers, but they
are in quotation marks like strings.
o In the above, that’s not what we expected at all! Because of the comma,
Python chose to treat this as a pair of values. Hence not to put commas or
spaces in your integers, no matter how big they are.
o In formal languages are strict, the notation is strict, and even the smallest
change might mean something quite different from what you intended.
2.2 Variables
o Variables are pices of computer’s memory for storing data values that can be
changed or manuplate.
Page 8
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
o In above example makes three assignments. The first assigns the string
value "What's up, Doc?" to a variable named message. The second gives
2.3 Variable names and Keywords
Page 9
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
o Following Table 1-3: gives examples of valid and invalid variable names
2.4 Statements
o In Python, a statement is an instruction that the Python interpreter can
execute to perform a specific action.
o When you type a statement on the command line, Python executes it.
Statements don’t produce any result.
o Common types of statements in Python are:
1. Assignment Statements 2. Expression Statements
3. Conditional Statements 4. Looping Statements
[Link] Flow Statements 7. Import Statements etc.
Page 10
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
o In the above example, len is a built-in Python function that returns the
number of characters in a string.
values or variables.
o These values or variables are known as operands.
o Operators indicate the specific action to be performed, such as addition,
comparison, or logical evaluation.
o Examples of operators include:
Arithmetic operators: + (addition), - (subtraction), * (multiplication), /
(division), ** (exponentiation).
Comparison Operators: == (equal to), != (not equal to), > (greater
than), < (less than).
Logical Operators: and, or, not.
Assignment Operators: = (assignment), += (add and assign).
o Operands are the values or variables upon which an operator acts. They
Page 11
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
Page 12
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
Page 13
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
o Python follows the same precedence rules for its mathematical operators
that mathematics does.
o The order of operations in Python, from highest to lowest precedence, is:
1. Parentheses (): Operations within parentheses are always evaluated
first, regardless of the operators inside
Example: result = (5 + 3) * 2 # (5 + 3) is evaluated first,
then multiplied by 2. Similarly 2 * (3-1) is 4, and (1+1)**(5-
2) is 8.
2. Exponents**:Exponentiation is evaluated after parentheses.
Exponentiation has the next highest precedence,
Example: 2**1+1 is 3 and not 4, and 3*1**3 is 3 and not 27.
Page 14
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
o Interestingly, the + operator does work with strings, but for strings, the +
operator represents concatenation, not addition.
o String concatenation and replication operation can be performed.
Concatenation is the process of joining two or more strings together and
Example :
2.10 Input
o The input() function allows takes a user input. By default, it returns the
user input in the form of a string.
o The input() function waits for the user to type some text on the keyboard
and press ENTER.
Example 1:
Example 2:
Page 15
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
2.11 Composition
o One of the most useful features of programming languages is their ability
to take small building blocks and compose them into larger chunks.
o We know that, the elements of a program — variables, expressions,
statements, and function calls — in isolation, without talking about how
to combine them.
o For example, consider a small four-step program that asks the user to
input a value for the radius of a circle, and then computes the area of the
circle from the formula 𝑨 = 𝝅𝒓 𝟐
o Firstly, we’ll do the four steps one at a time:
Page 16
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
3.3 Iteration:
o Iteration in Python refers to the process of repeatedly executing a block
of code for each item in a sequence or until a certain condition is met.
o It is a fundamental concept in programming for processing collections of
data or performing repetitive tasks.
3.3.1 Assignment
o Python's assignment operators are used to assign values to variables.
o The most fundamental is the simple assignment operator, =, which
assigns the value on its right to the variable on its left.
o A new assignment makes an existing variable refer to a new value.
o Example:
Program Output
o The third line changes the value of a but does not change the value of b,
so they are no longer equal.
Page 17
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
o Before you can update a variable, you have to initialize it to some rting
value, usually with a simple assignment
Page 18
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
o The for loop processes each item in a list. Each item in turn is (re-)assigned
to the loop variable, and the body of the loop is executed
o Example 1:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
output=
o Example 2:
for friend in ["Joe", "Zoe", "Zuki", "Thandi", "Paris"]:
invite = "Hi " + friend + ". Please come to my party!"
print(invite)
Output:
3. A colon
Page 19
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
Page 20
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
o Notice first that the print function on line 4 has an extra argument
end=", ". This tells the print function to follow the printed string with
whatever the programmer chooses (in this case, a comma followed by a
space), instead of ending the line.
o So each time something is printed in the loop, it is printed on the same
output line, with the numbers separated by commas.
o The call to print(n, end=".\n") at line 9 after the loop terminates will then
print the final value of n followed by a period and a newline character.
o Example 2:
Page 21
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
Output sequence:
3.3.9 Tables
One of the things loops are good for is generating tables. For some
operations, computers use tables of values to get an approximate answer
and then perform computations to improve the approximation.
The following program outputs a sequence of values in the left column
and 2 raised to the power of that value in the right column. Example
Output:
The string "\t" represents a tab character.
The backslash character in "\t" indicates the
beginning of an escape sequence.
Escape sequences are used to represent
invisible characters like tabs and newlines. The
sequence \n represents a newline.
An escape sequence can appear anywhere in a
string; in this example, the tab escape sequence
is the only thing in the string.
The tab character shifts the cursor to the right
until it reaches one of the tab stops.
o Because of the tab characters between the columns, the position of the
second column does not depend on the number of digits in the first
column.
Page 22
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
Page 23
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
Example 2:
Page 24
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
Page 25
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
o Nested loops in Python are used to iterate over nested data structures,
such as lists of lists, lists of dictionaries, or nested dictionaries.
o The concept involves placing one loop inside another, where the inner
loop executes completely for each iteration of the outer loop.
o Consider a case, we have a list of students. Each student has a name
which is paired up with another list of subjects that they are enrolled for:
o Now we’d like to ask how many students are taking CompSci. This needs
a counter, and for each student we need a second loop that tests each of
the subjects in turn:
Page 26
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
Chapter 4: Functions
4.4 Functions that require arguments
o In Python, functions can be defined to require arguments, meaning that
values must be provided for these arguments when the function is
called.
o Most functions require arguments: For example, if we want to find the
absolute value of a number, we have to indicate what the number is.
Python has a built-in function for computing the absolute value.
o Example 1: functions with one argument
- In above example, the arguments to the abs function are 5 and -5.
o Example 2: Some functions take more than one [Link] example
the built-in function pow takes two arguments, the base and the
exponent. Inside the function, the values that are passed get assigned to
variables called parameters.
o Example 3: Another built-in function that takes more than one argument
Page 27
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
Page 28
Python Programming(25BPLC205A) - Module 1: Introduction, Variables, Expressions and Statements, Iterations and Functions
Page 29