0% found this document useful (0 votes)
10 views29 pages

Python Programming Unit-1 Notes

The document provides comprehensive study material for Unit-1 of the BCS302 Python Programming course at AKTU University, covering topics such as the introduction to Python, programming cycle, basic operators, data types, and expressions. It includes detailed notes, examples, and diagrams to aid academic preparation. Additionally, it features practice questions and important exam tips for students.

Uploaded by

yswautc
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)
10 views29 pages

Python Programming Unit-1 Notes

The document provides comprehensive study material for Unit-1 of the BCS302 Python Programming course at AKTU University, covering topics such as the introduction to Python, programming cycle, basic operators, data types, and expressions. It includes detailed notes, examples, and diagrams to aid academic preparation. Additionally, it features practice questions and important exam tips for students.

Uploaded by

yswautc
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

BCS302: Python Programming

Unit-1: Introduction to Python


AKTU University Detailed Notes
For Academic Preparation

Complete Unit-1 Study Material


Comprehensive Notes with Examples and Diagrams

[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 Python Basic Operators 8


2.1 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
aft
2.1.1 Detailed Examples . . . . . . . . . . .
2.2 Comparison Operators . . . . . . . . . . . . .
2.2.1 Examples with Comparison Operators
2.3 Logical Operators . . . . . . . . . . . . . . . .
A
rk .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
8
9
9
9
2.3.1 Examples with Logical Operators . . . . . . . . . . . . . . . . . . 9
ic

2.4 Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10


m

2.4.1 Examples with Assignment Operators . . . . . . . . . . . . . . . . 10


2.5 Identity Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
de
Dr
2.6 Membership Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
ca

2.7 Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12


A

2.7.1 Examples of Operator Precedence . . . . . . . . . . . . . . . . . . 12

3 Understanding Python Blocks 13


3.1 Indentation in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.1.1 Rules for Indentation . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.1.2 Example: Proper Indentation . . . . . . . . . . . . . . . . . . . . 13
3.2 Block Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.2.1 Conditional Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.2.2 Loop Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.2.3 Function Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.3 Block Visualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

4 Python Data Types 15


4.1 Overview of Python Data Types . . . . . . . . . . . . . . . . . . . . . . . 15
4.2 Numeric Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.2.1 Integer (int) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.2.2 Float . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.2.3 Numeric Type Conversion . . . . . . . . . . . . . . . . . . . . . . 17
4.3 Boolean Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.3.1 Boolean Examples . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.4 String Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

2
[Link]
Unit-1: Introduction to Python BCS302: Python Programming

4.4.1 String Creation and Access . . . . . . . . . . . . . . . . . . . . . 18


4.5 Declaring and Using Numeric Data Types . . . . . . . . . . . . . . . . . 19
4.5.1 Variable Declaration . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.5.2 Practical Examples . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.6 Type Identification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

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

7 Boolean Expressions aft


7.1 Simple Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . .
7.2 Compound Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . .
rk
7.3 Short-Circuit Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . .
A 24
24
24
25

8 Summary and Key Points 26


ic

8.1 Important Formulas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26


m

8.2 Quick Reference Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27


de
Dr
9 Practice Questions for Exam Preparation 28
ca

9.1 Short Answer Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . 28


A

9.2 Programming Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

10 Important Exam Tips 29

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.

1.1.1 Key Characteristics of Python


Important Features
• Easy to Learn: Simple syntax similar to English language

• Interpreted Language: Code executed line-by-line


aft
• Dynamically Typed: No need to declare variable types explicitly

• Object-Oriented: Supports classes and objects


rk
A

• Functional Programming: Supports lambda functions, map, filter, reduce


ic

• Cross-Platform: Runs on Windows, Linux, macOS, etc.


m
de

• Open Source: Free to use and modify


Dr ca

• Rich Library Support: Extensive standard library and third-party pack-


A

ages

1.2 The Programming Cycle for Python


The programming cycle consists of several important phases:

4
[Link]
Unit-1: Introduction to Python BCS302: Python Programming

1. Problem Analysis 2. Algorithm Design

3. Coding 4. Compilation/Testing

5. Execution 6. Maintenance

1.2.1 Detailed Steps


aft
Figure 1: Programming Cycle for Python
A
rk

Step-by-Step Process
ic
m

1. Problem Analysis: Understand the problem requirements, inputs, outputs,


de

and constraints.
Dr ca

2. Algorithm Design: Create a step-by-step solution approach using flowcharts


or pseudocode.
A

3. Coding: Write the Python code based on the algorithm.


4. Compilation/Testing: Test the code for errors and verify correctness.
5. Execution: Run the program with test data.
6. Maintenance: Update and optimize the code as needed.

1.3 Python IDE and Environment


Example: Popular Python IDEs

IDE Name Description


IDLE Built-in Python IDE, beginner-friendly
PyCharm Professional IDE by JetBrains
Visual Studio Code Lightweight code editor with extensions
Jupyter Notebook Interactive notebook for data science
Anaconda Distribution with IDE and package manager
Spyder Scientific Python IDE
Thonny Beginner-friendly IDE with debugger

5
[Link]
Unit-1: Introduction to Python BCS302: Python Programming

1.4 Interacting with Python Programs


Python programs can be executed in two ways:

1.4.1 Interactive Mode


In interactive mode, Python acts as an interpreter. You can type commands directly and
see results immediately.
1 >>> print ( " Hello , Python ! " )
2 Hello , Python !
3 >>> x = 10
4 >>> y = 20
5 >>> print ( x + y )
6 30
7 >>>

1.4.2 Script Mode

3
# File : hello . py

x = 10
aft
In script mode, you write code in a file and execute it.

print ( " Hello , Python ! " )


A
rk

4 y = 20
ic

5 print ( x + y )
m

Execute using: python [Link]


de
Dr ca

1.5 Elements of Python


A

Core Elements of Python


Python programs consist of the following fundamental elements:

1. Keywords: Reserved words with specific meanings (if, else, for, while, def,
class, etc.)

2. Identifiers: Names given to variables, functions, classes, etc.

3. Literals: Actual values assigned to variables (numbers, strings, etc.)

4. Operators: Symbols for performing operations (+, -, *, /, ==, etc.)

5. Statements: Instructions that Python executes

6. Comments: Annotations that explain code

6
[Link]
Unit-1: Introduction to Python BCS302: Python Programming

1.6 Type Conversion


Type Conversion

Type conversion (also called type casting) is the process of converting a value from
one data type to another.

1.6.1 Implicit Type Conversion


Python automatically converts one data type to another in certain operations:
1 x = 10 # int
2 y = 3.14 # float
3 z = x + y # z becomes 13.14 ( float )
4 print (z , type ( z ) ) # Output : 13.14 < class ’ float ’>

1.6.2 Explicit Type Conversion

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

9 print (b , type ( b ) ) # Output : 10.0 < class ’ float ’>


10
A

11 # int to str
12 num = 42
13 s = str ( num )
14 print (s , type ( s ) ) # Output : 42 < class ’ str ’>
15

16 # float to int ( truncates decimal )


17 f = 3.99
18 i = int ( f )
19 print ( i ) # Output : 3
20

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

2 Python Basic Operators


Definition: Operators
Operators are special symbols in Python that perform computations on values and
variables. These values and variables are called operands.

2.1 Arithmetic Operators


Arithmetic operators are used to perform mathematical operations.

Arithmetic Operators

Operator Description Example


+ Addition 10 + 5 = 15
- Subtraction 10 - 5 = 5
* Multiplication 10 * 5 = 50
/ Division (float result) 10 / 4 = 2.5
//
%
**
aft Floor Division
Modulus (remainder)
Exponentiation
ic
A
rk 10 // 4 = 2
10 % 3 = 1
2 ** 3 = 8

2.1.1 Detailed Examples


m

# 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

12 # Division ( returns float )


13 print ( a / b ) # Output : 1.3333...
14

15 # Floor Division ( returns integer )


16 print ( a // b ) # Output : 1
17

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

2.2 Comparison Operators


Comparison operators compare two values and return boolean results (True/False).

Comparison Operators

Operator Description Example


== Equal to 5 == 5 returns True
!= Not equal to 5 != 3 returns True
> Greater than 5 > 3 returns True
< Less than 5 < 3 returns False
>= Greater than or equal 5 >= 5 returns True
<= Less than or equal 5 <= 3 returns False

2.2.1 Examples with Comparison Operators

1 x = 10
2

4
y = 20
z = 10

print ( x == z ) # Output : t
True
af
5

print ( x != y ) # Output : True


k
6
Ar

7 print ( x > y) # Output : False


print ( x < y) # Output : True
ic

9 print ( x >= z ) # Output : True


em

10 print ( y <= x ) # Output : False


ad

11

# Comparing strings
Ac

12
Dr

13 name1 = " Alice "


14 name2 = " Bob "
15 print ( name1 == name2 ) # Output : False
16 print ( name1 < name2 ) # Output : True ( alphabetical order )

2.3 Logical Operators


Logical operators combine conditional statements and return boolean values.

Logical Operators

Operator Description Example


and Returns True if both conditions are True 5 > 3 and 3 > 1 returns True
or Returns True if one condition is True 5 > 10 or 3 > 1 returns True
not Reverses the result not(5 > 3) returns False

2.3.1 Examples with 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

2.4 Assignment Operators

t
Assignment operators are used to assign values to variables.
af
Assignment Operators
k
Ar

Operator Description Example


ic

= Simple assignment x = 5
em

+= Add and assign x += 3 (equivalent to x = x + 3)


ad

-= Subtract and assign x -= 3 (equivalent to x = x - 3)


*= Multiply and assign x *= 3 (equivalent to x = x * 3)
Dr Ac

/= Divide and assign x /= 3 (equivalent to x = x / 3)


%= Modulus and assign x %= 3 (equivalent to x = x % 3)
**= Exponent and assign x **= 2 (equivalent to x = x ** 2)
//= Floor divide and assign x //= 3 (equivalent to x = x // 3)

2.4.1 Examples with Assignment Operators

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

2.5 Identity Operators


Identity operators compare objects, not just values.

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

6 print ( a is c) # Output : True ( same object )


ic

7 print ( a is not b ) # Output : True


m

8 print ( a == b) # Output : True ( same content )


de
Dr ca

2.6 Membership Operators


A

Membership operators test if a value is in a sequence.

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

2.7 Operator Precedence


Operator Precedence (High to Low)

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

5 # Example 2: Exponentiation before multiplication


de

print (2 ** 3 ** 2) # Output : 512


Dr
6
ca

7 # 3 ** 2 = 9 , then 2 ** 9 = 512
A

9 # Example 3: Multiplication before addition


10 print (10 + 2 * 5) # Output : 20
11 # 2 * 5 = 10 , then 10 + 10 = 20
12

13 # Example 4: Parentheses override precedence


14 print ((10 + 2) * 5) # Output : 60
15 # 10 + 2 = 12 , then 12 * 5 = 60
16

17 # Example 5: Logical operators precedence


18 print ( True or False and False ) # Output : True
19 # and is higher precedence
20

21 # Example 6: Comparison and logical operators


22 x = 5
23 print ( x < 10 and x > 0) # Output : True

12
[Link]
Unit-1: Introduction to Python BCS302: Python Programming

3 Understanding Python Blocks


Definition: Python Blocks
A block is a group of statements in Python code that belongs together. Blocks are
identified using indentation (whitespace at the beginning of a line). All statements
in a block must have the same indentation level.

3.1 Indentation in Python


Importance of Indentation

Unlike many programming languages that use braces {} or keywords to denote


blocks, Python uses indentation. This makes Python code more readable and
enforces consistent code style.

3.1.1 Rules for Indentation

t
1. Use spaces or tabs (preferably 4 spaces per indent level)

2. All statements within a block must have the same indentation


af k
3. Blocks must be preceded by a colon (:)
Ar
ic

4. Mixing tabs and spaces can cause errors


em

3.1.2 Example: Proper Indentation


ad
Dr Ac

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

7 # Indentation for loops


8 for i in range (3) :
9 print ( i )
10 print ( " Inside loop " )
11 print ( " Outside loop " )
12

13 # Indentation for functions


14 def greet ( name ) :
15 print ( " Hello , " + name )
16 print ( " Welcome ! " )

3.2 Block Structures


3.2.1 Conditional Blocks

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

11 # if - elif - else blocks


12 if score >= 90:
13 print ( " Grade A " )
14 elif score >= 80:
15 print ( " Grade B " )
16 elif score >= 70:
17 print ( " Grade C " )
18

19
else :
print ( " Grade F " )

t
af
3.2.2 Loop Blocks
k
Ar

1 # for loop block


ic

2 for i in range (5) :


em

3 print ( i )
print ( " Loop iteration " )
ad

5
Dr Ac

6 # while loop block


7 count = 0
8 while count < 5:
9 print ( count )
10 count += 1
11

12 # Nested blocks
13 for i in range (3) :
14 for j in range (2) :
15 print ( f " i ={ i } , j ={ j } " )

3.2.3 Function Blocks

1 def calculate_sum (a , b ) :
2 result = a + b
3 return result
4

5 def greet_user ( name ) :


6 print ( " Hello , " + name )
7 print ( " Welcome to Python ! " )
8

9 class Student :

14
[Link]
Unit-1: Introduction to Python BCS302: Python Programming

10 def __init__ ( self , name ) :


11 self . name = name
12

13 def display ( self ) :


14 print ( self . name )

3.3 Block Visualization

Main Program
if Block
Statement 1
Statement 2
Statement 3

aft for Block


Statement A
Statement B
rk
Statement C
A
ic

Main statements outside blocks


m
de
Dr
Figure 2: Python Block Structure
ca
A

4 Python Data Types


Definition: Data Types
Data types specify the kind of value that a variable can store. Python is dynam-
ically typed, meaning the type is determined at runtime based on the assigned
value.

4.1 Overview of Python Data Types

Python Data Types

Numeric Sequence Mapping

int float str list dict set

Figure 3: Python Data Types Hierarchy

15
[Link]
Unit-1: Introduction to Python BCS302: Python Programming

4.2 Numeric Data Types


4.2.1 Integer (int)

Integer Data Type


An integer is a whole number without a decimal point. It can be positive, negative,
or zero.

1 # Integer examples
2 x = 10
3 y = -5
4 z = 0
5

6 print ( type ( x ) ) # Output : < class ’ int ’>


7 print ( type ( y ) ) # Output : < class ’ int ’>
8

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

20 print ( a ** b ) # Output : 1000


Dr Ac

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

6 print ( type ( x ) ) # Output : < class ’ float ’>


7

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

16 print ( a * b ) # Output : 33.6


17 print ( a / b ) # Output : 3.28125
18

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

4.2.3 Numeric Type Conversion

1 # int to float
2 x = 10
3 y = float ( x )
4 print (y , type ( y ) ) # Output : 10.0 < class ’ float ’>
5

6 # float to int ( truncates decimal )


7 f = 3.99
8 i = int ( f )
9 print (i , type ( i ) ) # Output : 3 < class ’ int ’>
10

11

12
# String to int
s = " 42 "
n = int ( s ) t
af
13

print (n , type ( n ) ) # Output : 42 < class ’ int ’>


k
14
Ar

15

# String to float
ic

16

17 s = " 3.14 "


em

18 f = float ( s )
ad

19 print (f , type ( f ) ) # Output : 3.14 < class ’ float ’>


Ac

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

4.3 Boolean Data Type


Boolean Data Type
Boolean is a data type that can have one of two values: True or False. Booleans
are used in conditional statements and logical operations.

4.3.1 Boolean Examples

1 # Boolean values
2 is_student = True
3 is_employed = False
4

17
[Link]
Unit-1: Introduction to Python BCS302: Python Programming

5 print ( type ( is_student ) ) # Output : < class ’ bool ’>


6

7 # Boolean from comparisons


8 print (5 > 3) # Output : True
9 print (5 < 3) # Output : False
10 print (5 == 5) # Output : True
11

12 # Boolean from logical operations


13 print ( True and False ) # Output : False
14 print ( True or False ) # Output : True
15 print ( not True ) # Output : False
16

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 :

4.4 String Data Type t True


af Ar
k

String Data Type


ic

A string is a sequence of characters enclosed in single quotes, double quotes, or


em

triple quotes. Strings are immutable in Python.


ad
Dr Ac

4.4.1 String Creation and Access

1 # Single quotes
2 name = ’ Alice ’
3

4 # Double quotes
5 message = " Hello , World ! "
6

7 # Triple quotes ( multi - line )


8 text = " " " This is a
9 multi - line string " " "
10

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

22 last_name = " Doe "


23 full_name = first_name + " " + last_name
24 print ( full_name ) # Output : John Doe
25

26 # String repetition
27 print ( " Ha " * 3) # Output : HaHaHa

4.5 Declaring and Using Numeric Data Types


4.5.1 Variable Declaration
Example: Declaring Variables

1 # Declaring integer variables


2 age = 25
3 year = 2025
4

# Declaring float variables


5

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

12 # Declaring with same value


x = y = z = 0
em

13

14
ad

15 # Type checking
Ac

print ( type ( age ) ) # Output : < class ’ int ’>


Dr
16

17 print ( type ( height ) ) # Output : < class ’ float ’>

4.5.2 Practical Examples

1 # Example 1: Calculate area and circumference


2 import math
3 radius = 5
4 area = math . pi * radius ** 2
5 circumference = 2 * math . pi * radius
6 print ( f " Area : { area :.2 f } " )
7 print ( f " Circumference : { circumference :.2 f } " )
8

9 # Example 2: Temperature conversion


10 celsius = 25
11 fahrenheit = ( celsius * 9/5) + 32
12 print ( f " { celsius } C = { fahrenheit :.2 f } F " )
13

14 # Example 3: Simple calculator


15 num1 = 10
16 num2 = 3

19
[Link]
Unit-1: Introduction to Python BCS302: Python Programming

17 print ( f " Addition : { num1 + num2 } " )


18 print ( f " Subtraction : { num1 - num2 } " )
19 print ( f " Multiplication : { num1 * num2 } " )
20 print ( f " Division : { num1 / num2 :.2 f } " )
21 print ( f " Floor Division : { num1 // num2 } " )
22 print ( f " Modulus : { num1 % num2 } " )
23 print ( f " Exponentiation : { num1 ** num2 } " )
24

25 # Example 4: Calculate average


26 marks1 = 85
27 marks2 = 90
28 marks3 = 78
29 average = ( marks1 + marks2 + marks3 ) / 3
30 print ( f " Average marks : { average :.2 f } " )

4.6 Type Identification


Using type() Functionaft
The type() function returns the data type of a variable.
rk
1 # Checking types
A

2 x = 10
ic

3 y = 3.14
m

4 z = " Hello "


de

5 flag = True
Dr ca

7 print ( type ( x ) ) # Output : < class ’ int ’>


A

8 print ( type ( y ) ) # Output : < class ’ float ’>


9 print ( type ( z ) ) # Output : < class ’ str ’>
10 print ( type ( flag ) ) # Output : < class ’ bool ’>
11

12 # Using isinstance () function


13 print ( isinstance (x , int ) ) # Output : True
14 print ( isinstance (y , float ) ) # Output : True
15 print ( isinstance (z , str ) ) # Output : True

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

5.1 Types of Expressions


5.1.1 Arithmetic Expressions

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

6 pattern = " Ha " * 3


de

7 print ( pattern ) # Output : HaHaHa


Dr ca

9 # String length
A

10 text = " Python "


11 length = len ( text )
12 print ( length ) # Output : 6

5.1.3 Boolean Expressions

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

5.2 Evaluating Expressions


Expression Evaluation Process
Expressions are evaluated left to right, respecting operator precedence:

1. Parentheses

2. Exponentiation

3. Multiplication, Division, Floor Division, Modulus

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

3 print ((2 + 3) * 4) # 2 + 3 = 5 , then 5 * 4 = 20


A

4 print (10 - 5 - 2) # Left to right : 10 - 5 = 5 , then 5 - 2 = 3


ic

5 print (2 ** 3 ** 2) # Right to left : 3 ** 2 = 9 , then 2 ** 9 =


m

512
de
Dr ca

6 Assignment Statement
A

Definition: Assignment Statement


An assignment statement assigns a value to a variable. The assignment operator is
=. The right side is evaluated first, and the result is stored in the variable on the
left.

6.1 Simple Assignment


1 # Basic assignment
2 x = 10
3 name = " Alice "
4 pi = 3.14
5

6 # Assignment from expression


7 result = 5 + 3
8 price = 100 * 1.5
9

10 # Assignment from function


11 length = len ( " Python " )

22
[Link]
Unit-1: Introduction to Python BCS302: Python Programming

6.2 Multiple Assignment


1 # Same value to multiple variables
2 a = b = c = 10
3 print (a , b , c ) # Output : 10 10 10
4

5 # Different values to multiple variables


6 x, y, z = 1, 2, 3
7 print (x , y , z ) # Output : 1 2 3
8

9 # Swapping values
10 a = 5
11 b = 10
12 a, b = b, a
13 print (a , b ) # Output : 10 5

6.3 Compound Assignment


1

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.

7.1 Simple Boolean Expressions


1 # Comparison expressions
2 print (5 > 3) # Output : True
3 print (5 < 3) # Output : False
4 print (5 == 5) # Output : True
5

8
print (5 != 3)
print (5 >= 5)
print (3 <= 2)
aft
# Output :
# Output :
# Output :
True
True
False
rk
# String comparisons
A

print ( " apple " == " apple " ) # Output : True


ic

10

11 print ( " apple " < " banana " ) # Output : True ( alphabetical )
m
de
Dr ca

7.2 Compound Boolean Expressions


A

1 # Using ’ and ’ operator


2 age = 25
3 income = 50000
4 print ( age > 18 and income > 30000) # Output : True
5 print ( age > 30 and income > 30000) # Output : False
6

7 # Using ’ or ’ operator
8 is_member = False
9 is_employee = True
10 print ( is_member or is_employee ) # Output : True
11

12 # Using ’ not ’ operator


13 is_active = True
14 print ( not is_active ) # Output : False
15

16 # Complex boolean expressions


17 x = 10
18 print ( x > 5 and x < 20 and x % 2 == 0) # Output : True
19 print ( x < 0 or x > 100 or x == 10) # Output : True

24
[Link]
Unit-1: Introduction to Python BCS302: Python Programming

7.3 Short-Circuit Evaluation


Short-Circuit Evaluation
Python uses short-circuit evaluation for boolean expressions:

• In and: If the first operand is False, the second is not evaluated

• In or: If the first operand is True, the second is not evaluated

1 # Short - circuit with ’ and ’


2 x = 5
3 y = 10
4 print ( x < 0 and y > 5) # False ( second part not evaluated )
5

6 # Short - circuit with ’ or ’


7 print ( x > 0 or y < 5) # True ( second part not evaluated )
8

9 # Practical example
10

11

12

13

14
def is_positive ( n ) : aft
print ( " Checking ... " )
return n > 0

print ( False and is_positive (5) )


rk
# " Checking ..." not printed
A

15 print ( True or is_positive (5) ) # " Checking ..." not printed


ic
m
de
Dr A
ca

25
[Link]
Unit-1: Introduction to Python BCS302: Python Programming

8 Summary and Key Points


Summary of Unit-1
Key Concepts Covered:

1. Introduction to Python: Python is a high-level, interpreted, dynamically-


typed language that emphasizes code readability.

2. Basic Operators: Python supports arithmetic, comparison, logical, assign-


ment, identity, and membership operators.

3. Python Blocks: Code blocks are identified by indentation, which is crucial


for Python syntax.

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

functions like int(), float(), str().


m
de
Dr
8.1 Important Formulas
ca

Key Formulas and Relationships


A

Operator Precedence (from highest to lowest):

Parentheses → ∗∗ → ∗, /, //, % → +, − → Comparisons → not → and → or

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

8.2 Quick Reference Table


Concept Example Output
Integer x = 42 42
Float x = 3.14 3.14
String x = "Hello" Hello
Boolean x = True True
Addition 10 + 5 15
Modulus 10 % 3 1
Exponentiation 2 ** 3 8
Comparison 10 > 5 True
Logical AND True and False False
Logical OR True or False True
Type Checking type(42) <class ’int’>

aft de
m
ic
A
rk
Dr A
ca

27
[Link]
Unit-1: Introduction to Python BCS302: Python Programming

9 Practice Questions for Exam Preparation


9.1 Short Answer Questions
Practice Questions
1. What is Python? Name five features of Python.

2. Define operator precedence and explain its importance in Python.

3. What is the difference between / and // operators?

4. Explain the difference between implicit and explicit type conversion.

5. What is the purpose of indentation in Python?

6. How does Python use blocks? Give examples.

7. Define boolean expressions and give examples.

aft
8. What are the different data types in Python? Explain each.

9. Write a program to calculate simple interest.


ic
A
rk
10. Write a program to swap two numbers using tuple assignment.

9.2 Programming Problems


m
de

1. Write a Python program to calculate the area and circumference of a circle given
Dr
the radius.
ca
A

2. Write a Python program to convert temperature from Celsius to Fahrenheit.

3. Write a Python program to calculate the compound interest.

4. Write a Python program to find the largest of three numbers.

5. Write a Python program that demonstrates all arithmetic operators.

28
[Link]
Unit-1: Introduction to Python BCS302: Python Programming

10 Important Exam Tips


AKTU Exam Preparation Tips
• Understand Concepts: Focus on understanding concepts rather than
memorizing.

• Practice Coding: Write lots of Python code to get comfortable with syntax.

• Indentation: Always remember Python’s strict indentation rules.

• Type Conversion: Practice type conversion problems as they frequently


appear in exams.

• Operator Precedence: Know the operator precedence order thoroughly.

• Real-World Examples: Practice real-world problems like temperature con-


version, area calculation, etc.

• Multiple-Choice: Be careful with tricky questions about output and type


conversions.
aft m
ic
A
rk

Good Luck with Your Exam!


de
Dr A
ca

For more resources visit: [Link]

29
[Link]

You might also like