Python Programming Unit-1 Notes
Python Programming Unit-1 Notes
[Link]
aft Last Updated: October 2025
rk
A
ic
m
de
Dr A
ca
1
Unit-1: Introduction to Python BCS302: Python Programming
Contents
1 Introduction to Python 4
1.1 Overview of Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.1.1 Key Characteristics of Python . . . . . . . . . . . . . . . . . . . . 4
1.2 The Programming Cycle for Python . . . . . . . . . . . . . . . . . . . . . 4
1.2.1 Detailed Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 Python IDE and Environment . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4 Interacting with Python Programs . . . . . . . . . . . . . . . . . . . . . . 6
1.4.1 Interactive Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4.2 Script Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.5 Elements of Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.6 Type Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.6.1 Implicit Type Conversion . . . . . . . . . . . . . . . . . . . . . . 7
1.6.2 Explicit Type Conversion . . . . . . . . . . . . . . . . . . . . . . 7
2
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
5 Expressions 20
5.1 Types of Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5.1.1 Arithmetic Expressions . . . . . . . . . . . . . . . . . . . . . . . . 21
5.1.2 String Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5.1.3 Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . . 21
5.2 Evaluating Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
6 Assignment Statement 22
6.1 Simple Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
6.2 Multiple Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
6.3 Compound Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
1 Introduction to Python
1.1 Overview of Python
Definition: What is Python?
Python is a high-level, interpreted, general-purpose programming language created
by Guido van Rossum. It emphasizes code readability and simplicity, making it
ideal for both beginners and professionals. Python is dynamically typed, supports
multiple programming paradigms (procedural, object-oriented, functional), and has
extensive built-in libraries.
ages
4
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
3. Coding 4. Compilation/Testing
5. Execution 6. Maintenance
Step-by-Step Process
ic
m
and constraints.
Dr ca
5
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
3
# File : hello . py
x = 10
aft
In script mode, you write code in a file and execute it.
4 y = 20
ic
5 print ( x + y )
m
1. Keywords: Reserved words with specific meanings (if, else, for, while, def,
class, etc.)
6
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
Type conversion (also called type casting) is the process of converting a value from
one data type to another.
3
# str to int
x = " 25 "
y = int ( x )
aft
You can convert types using built-in functions:
rk
print (y , type ( y ) ) # Output : 25 < class ’ int ’>
A
5
ic
6 # int to float
m
7 a = 10
de
8 b = float ( a )
Dr ca
11 # int to str
12 num = 42
13 s = str ( num )
14 print (s , type ( s ) ) # Output : 42 < class ’ str ’>
15
21 # bool conversion
22 print ( bool (0) ) # Output : False
23 print ( bool (1) ) # Output : True
24 print ( bool ( " " ) ) # Output : False
25 print ( bool ( " text " ) ) # Output : True
7
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
Arithmetic Operators
# Addition
de
1
Dr
2 a = 20
ca
3 b = 15
A
4 print ( a + b ) # Output : 35
5
6 # Subtraction
7 print ( a - b ) # Output : 5
8
9 # Multiplication
10 print ( a * b ) # Output : 300
11
18 # Modulus ( remainder )
19 print ( a % b ) # Output : 5
20 print (17 % 5) # Output : 2
21
22 # Exponentiation
23 print (2 ** 3) # Output : 8
24 print (5 ** 2) # Output : 25
8
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
Comparison Operators
1 x = 10
2
4
y = 20
z = 10
print ( x == z ) # Output : t
True
af
5
11
# Comparing strings
Ac
12
Dr
Logical Operators
1 # and operator
2 a = 5
9
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
3 b = 10
4 print ( a > 3 and b > 5) # Output : True
5 print ( a > 10 and b > 5) # Output : False
6
7 # or operator
8 print ( a > 10 or b > 5) # Output : True
9 print ( a < 3 or b < 5) # Output : False
10
11 # not operator
12 print ( not ( a > b ) ) # Output : True
13 print ( not ( a < b ) ) # Output : False
14
15 # Combined example
16 age = 25
17 income = 50000
18 print ( age > 18 and income > 30000) # Output : True
t
Assignment operators are used to assign values to variables.
af
Assignment Operators
k
Ar
= Simple assignment x = 5
em
1 x = 10
2 print ( x ) # Output : 10
3
4 x += 5 # x = x + 5
5 print ( x ) # Output : 15
6
7 x -= 3 # x = x - 3
8 print ( x ) # Output : 12
9
10 x *= 2 # x = x * 2
11 print ( x ) # Output : 24
12
13 x /= 4 # x = x / 4
14 print ( x ) # Output : 6.0
10
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
15
16 x %= 4 # x = x % 4
17 print ( x ) # Output : 2.0
18
19 x **= 3 # x = x ** 3
20 print ( x ) # Output : 8.0
Identity Operators
Operator Description
is Returns True if both variables are the same object
is not Returns True if variables are not the same object
5
a = [1 , 2 , 3]
b = [1 , 2 , 3]
c = a
print ( a is b)
aft
# Output :
rk
False ( different objects )
A
Membership Operators
Operator Description
in Returns True if value is in sequence
not in Returns True if value is not in sequence
1 fruits = [ " apple " , " banana " , " cherry " ]
2 print ( " apple " in fruits ) # Output : True
3 print ( " grape " in fruits ) # Output : False
4 print ( " grape " not in fruits ) # Output : True
5
6 # String membership
7 text = " Hello World "
8 print ( " H " in text ) # Output : True
9 print ( " xyz " in text ) # Output : False
11
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
Precedence Operator
1 (Highest) ** (Exponentiation)
2 +x, -x, x (Unary plus, minus, NOT)
3 *, /, //, % (Multiplication, division, floor, modulus)
4 +, - (Addition, subtraction)
5 <<, >> (Bitwise shifts)
6 & (Bitwise AND)
7 ^ (Bitwise XOR)
8 | (Bitwise OR)
9 Comparison operators (==, !=, <, >, etc.)
10 not (Logical NOT)
11 and (Logical AND)
12 (Lowest) or (Logical OR)
1
2.7.1
aft
Examples of Operator Precedence
A
rk
# Example 1: Exponentiation before multiplication
2 print (2 + 3 * 4) # Output : 14 ( not 20)
ic
3 # 3 * 4 = 12 , then 2 + 12 = 14
m
7 # 3 ** 2 = 9 , then 2 ** 9 = 512
A
12
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
t
1. Use spaces or tabs (preferably 4 spaces per indent level)
1 # Correct indentation
2 if x > 5:
3 print ( " x is greater than 5 " )
4 print ( " This is still in the if block " )
5 print ( " This is outside the if block " )
6
13
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
1 # if block
2 if age >= 18:
3 print ( " You are an adult " )
4
5 # if - else blocks
6 if marks >= 60:
7 print ( " Pass " )
8 else :
9 print ( " Fail " )
10
19
else :
print ( " Grade F " )
t
af
3.2.2 Loop Blocks
k
Ar
3 print ( i )
print ( " Loop iteration " )
ad
5
Dr Ac
12 # Nested blocks
13 for i in range (3) :
14 for j in range (2) :
15 print ( f " i ={ i } , j ={ j } " )
1 def calculate_sum (a , b ) :
2 result = a + b
3 return result
4
9 class Student :
14
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
Main Program
if Block
Statement 1
Statement 2
Statement 3
15
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
1 # Integer examples
2 x = 10
3 y = -5
4 z = 0
5
9 # Large integers
10
11
12
13
big_num = 12 34 56 789 12 34 56 78 9
# Operations on integers
a = 10
t
af
14 b = 3
k
print ( a + b ) # Output : 13
Ar
15
16 print ( a - b ) # Output : 7
ic
17 print ( a * b ) # Output : 30
em
18 print ( a // b ) # Output : 3
print ( a % b ) # Output : 1
ad
19
4.2.2 Float
Float Data Type
A float is a number with a decimal point. It represents a floating-point number.
1 # Float examples
2 x = 3.14
3 y = -2.5
4 z = 0.0
5
8 # Scientific notation
9 sci_num = 1.5 e -3 # 0.0015
10
11 # Operations on floats
12 a = 10.5
13 b = 3.2
14 print ( a + b ) # Output : 13.7
15 print ( a - b ) # Output : 7.3
16
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
19 # Float precision
20 print (0.1 + 0.2) # Output : 0 .3 0 0 00 0 0 0 00 0 0 00 0 0 4
1 # int to float
2 x = 10
3 y = float ( x )
4 print (y , type ( y ) ) # Output : 10.0 < class ’ float ’>
5
11
12
# String to int
s = " 42 "
n = int ( s ) t
af
13
15
# String to float
ic
16
18 f = float ( s )
ad
20
Dr
21 # Invalid conversions
22 try :
23 num = int ( " 3.14 " ) # This will raise an error
24 except :
25 print ( " Cannot convert ’3.14 ’ directly to int " )
26 num = int ( float ( " 3.14 " ) ) # Correct way
27 print ( num ) # Output : 3
1 # Boolean values
2 is_student = True
3 is_employed = False
4
17
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
17 # Converting to boolean
18 print ( bool (0) ) # Output : False
19 print ( bool (1) ) # Output : True
20 print ( bool ( " " ) ) # Output : False
21 print ( bool ( " text " ) ) # Output : True
22 print ( bool ([]) ) # Output : False
23 print ( bool ([1 , 2]) ) # Output :
1 # Single quotes
2 name = ’ Alice ’
3
4 # Double quotes
5 message = " Hello , World ! "
6
11 # Accessing characters
12 text = " Python "
13 print ( text [0]) # Output : P
14 print ( text [1]) # Output : y
15 print ( text [ -1]) # Output : n ( last character )
16
17 # String length
18 print ( len ( text ) ) # Output : 6
19
20 # String concatenation
21 first_name = " John "
18
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
26 # String repetition
27 print ( " Ha " * 3) # Output : HaHaHa
8
height = 5.9
weight = 75.5
t
af
9 # Declaring multiple variables
k
10 a , b , c = 10 , 20.5 , " Python "
Ar
11
ic
13
14
ad
15 # Type checking
Ac
19
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
2 x = 10
ic
3 y = 3.14
m
5 flag = True
Dr ca
5 Expressions
Definition: Expression
An expression is a combination of variables, operators, and values that evaluates
to a single value. Expressions do not modify state; they only compute and return
a result.
20
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
1 # Simple arithmetic
2 result = 10 + 5
3 print ( result ) # Output : 15
4
5 # Complex arithmetic
6 result = 2 * 3 + 4 / 2 - 1
7 print ( result ) # Output : 7.0
8
9 # Using variables
10 a = 10
11 b = 3
12 result = a * b + a / b
13 print ( result ) # Output : 33.333...
2
5.1.2 aft
String Expressions
# String concatenation
greeting = " Hello " + " " + " World "
rk
print ( greeting ) # Output : Hello World
A
4
ic
5 # String repetition
m
9 # String length
A
1 # Comparison expressions
2 print (10 > 5) # Output : True
3 print (10 == 5) # Output : False
4
5 # Logical expressions
6 print ( True and False ) # Output : False
7 print ( True or False ) # Output : True
8
9 # Mixed expressions
10 x = 10
11 print ( x > 5 and x < 20) # Output : True
21
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
1. Parentheses
2. Exponentiation
4. Addition, Subtraction
5. Comparison operators
6. Logical NOT
7. Logical AND
8. Logical OR aft
1 # Step - by - step evaluation
print (2 + 3 * 4) # 3 * 4 = 12 , then 2 + 12 = 14
rk
2
512
de
Dr ca
6 Assignment Statement
A
22
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
9 # Swapping values
10 a = 5
11 b = 10
12 a, b = b, a
13 print (a , b ) # Output : 10 5
2
# += operator
x = 10
t
af
3 x += 5 # x = x + 5
k
4 print ( x ) # Output : 15
Ar
5
ic
6 # -= operator
y = 20
em
8 y -= 3 # y = y - 3
ad
9 print ( y ) # Output : 17
Dr Ac
10
11 # *= operator
12 z = 4
13 z *= 3 # z = z * 3
14 print ( z ) # Output : 12
15
16 # /= operator
17 w = 20
18 w /= 4 # w = w / 4
19 print ( w ) # Output : 5.0
20
21 # %= operator
22 a = 10
23 a %= 3 # a = a % 3
24 print ( a ) # Output : 1
25
26 # **= operator
27 b = 2
28 b **= 4 # b = b ** 4
29 print ( b ) # Output : 16
30
31 # //= operator
32 c = 17
23
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
33 c //= 5 # c = c // 5
34 print ( c ) # Output : 3
7 Boolean Expressions
Definition: Boolean Expression
A boolean expression is an expression that evaluates to either True or False.
Boolean expressions are used in conditional statements and loops.
8
print (5 != 3)
print (5 >= 5)
print (3 <= 2)
aft
# Output :
# Output :
# Output :
True
True
False
rk
# String comparisons
A
10
11 print ( " apple " < " banana " ) # Output : True ( alphabetical )
m
de
Dr ca
7 # Using ’ or ’ operator
8 is_member = False
9 is_employee = True
10 print ( is_member or is_employee ) # Output : True
11
24
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
9 # Practical example
10
11
12
13
14
def is_positive ( n ) : aft
print ( " Checking ... " )
return n > 0
25
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
4. Data Types: Python supports numeric (int, float), string, boolean, and
collection data types.
5. Variables: Variables are dynamically typed and can store values of any data
type.
duce results.
aft
6. Expressions: Expressions combine variables, operators, and values to pro-
A
rk
7. Type Conversion: Python allows implicit and explicit type conversion using
ic
Type Conversion:
int(x) = Convert x to integer
float(x) = Convert x to floating-point
str(x) = Convert x to string
Boolean Logic:
True and True = True
True or False = True
not(True) = False
26
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
aft de
m
ic
A
rk
Dr A
ca
27
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
aft
8. What are the different data types in Python? Explain each.
1. Write a Python program to calculate the area and circumference of a circle given
Dr
the radius.
ca
A
28
[Link]
Unit-1: Introduction to Python BCS302: Python Programming
• Practice Coding: Write lots of Python code to get comfortable with syntax.
29
[Link]