0% found this document useful (0 votes)
6 views24 pages

Python CIA

The document outlines the Python execution process, including lexical analysis, syntactic analysis, bytecode generation, and execution by the Python Virtual Machine. It also distinguishes between statements and expressions, explains operator precedence, and covers data structures like lists, tuples, sets, and dictionaries. Additionally, it discusses classes, constructors, inheritance, and file handling in Python.

Uploaded by

727724eucb022
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)
6 views24 pages

Python CIA

The document outlines the Python execution process, including lexical analysis, syntactic analysis, bytecode generation, and execution by the Python Virtual Machine. It also distinguishes between statements and expressions, explains operator precedence, and covers data structures like lists, tuples, sets, and dictionaries. Additionally, it discusses classes, constructors, inheritance, and file handling in Python.

Uploaded by

727724eucb022
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

1) Python Execution Process: From Source Code to Runtime

1. Lexical Analysis (Tokenization)

• Python interpreter reads source code character by character.

• Breaks code into tokens: keywords (if, for), operators (+, =), identifiers (variable
names), and literals (numbers, strings).

• This step prepares the code for syntactic analysis.

2. Syntactic Analysis (Parsing)

• Tokens are structured into an Abstract Syntax Tree (AST).

• AST represents the grammatical structure of the code.

• Syntax errors are detected here; if found, execution halts with an error message.

3. Bytecode Generation

• If syntax is valid, AST is converted into bytecode.

• Bytecode is a low-level, platform-independent representation of the code.

• Stored in .pyc files for faster future execution.

• Bytecode is not machine code—it needs the Python Virtual Machine (PVM) to run.

4. Execution by Python Virtual Machine (PVM)

• PVM reads and executes bytecode instructions.

• Converts bytecode into machine-executable code dynamically.

• Execution often happens line-by-line, allowing for interactive and flexible


programming.

Comparison: Python vs Compiled Languages

Compiled Languages (e.g., C, C++)

• Entire source code is compiled into machine code before execution.

• Output is a platform-specific executable (e.g., .exe on Windows).

• No interpreter needed at runtime; the OS and CPU run the binary directly.

• Compilation is a one-time process; the executable can be reused.

Interpreted Languages (e.g., Python, JavaScript)

• Source code is translated into intermediate form (like Python’s bytecode).


• Bytecode is executed by an interpreter (e.g., PVM) at runtime.

• Final machine code conversion happens during execution, not before.

• Supports dynamic features and rapid development cycles.

• Changes can be tested immediately without full recompilation.

2)STATEMENT AND EXPRESSION:

Statements vs. Expressions

Expressions are pieces of code that evaluate to a value. They can be composed of literals,
variables, and operators.

• Examples:

o 5 (literal expression)

o x (variable expression, evaluates to the value of x)

o a + b (arithmetic expression, evaluates to the sum of a and b)

o len("hello") (function call expression, evaluates to the length of the string)

Statements are units of code that perform an action. They do not necessarily produce a
value, but rather control the flow of the program or modify its state.

• Examples:

o x = 10 (assignment statement)

o if x > 5: print("Greater") (conditional statement)

o for i in range(5): print(i) (loop statement)

o def my_function(): pass (function definition statement)

Key Difference: Expressions produce values, while statements perform actions. While an
expression can be part of a statement, a statement cannot be used where an expression
is expected.

Python Operator Precedence

Python handles operator precedence in expressions based on a defined hierarchy, similar


to the order of operations in mathematics (PEMDAS/BODMAS). Operators with higher
precedence are evaluated before those with lower precedence.

The general order of precedence (from highest to lowest) includes:

• Parentheses (): Used to explicitly group expressions and override default precedence.

• Exponentiation `: `**
• Unary operators +x, -x

• Multiplication *, Division /, Floor Division //, Modulo %

• Addition +, Subtraction -

• Bitwise shifts <<, >>

• Bitwise AND &

• Bitwise XOR ^

• Bitwise OR |

• Comparison operators ==, !=, >, <, >=, <=, is, is not, in, not in

• Boolean NOT not

• Boolean AND and

• Boolean OR or

• Assignment operators =, +=, -=, etc.

Example:

Python

result = 3 + 4 * 2 - 1

In this expression, multiplication (*) has higher precedence than addition (+) and
subtraction (-). Therefore, 4 * 2 is evaluated first, resulting in 8. Then, the expression
becomes 3 + 8 - 1, which evaluates from left to right, yielding 10.

Parentheses can be used to explicitly control the order of evaluation:

Python

result = (3 + 4) * 2 - 1

Here, (3 + 4) is evaluated first due to the parentheses, resulting in 7. Then, 7 * 2 is


evaluated, yielding 14, and finally 14 - 1 results in 13.

3)PASCAL AND FLOYD TRIANGLE:


n = int(input("Enter number of rows for Floyd's Triangle:
"))
num = 1

for i in range(1, n + 1):


# print numbers in each row
for j in range(1, i + 1):
print(num, end=' ')
num += 1

# move to next line


print()

Step-by-Step Logic:

• Step 1: Take input n for number of rows.

• Step 2: Initialize num = 1 to start counting.

• Step 3: Outer loop i runs from 1 to n, controlling rows.

• Step 4: Inner loop j runs from 1 to i, printing i numbers per row.

• Step 5: Print num, then increment it.

• Step 6: After each row, use print() to move to the next line.
n = int(input("Enter number of rows for Pascal's
Triangle: "))

for i in range(n):
# print spaces for alignment
for space in range(n - i):
print(" ", end='')

val = 1
# print values in each row
for j in range(i + 1):
print(val, end=' ')
val = val * (i - j) // (j + 1)

# move to next line


print()

Step-by-Step Logic:

• Step 1: Take input n for number of rows.

• Step 2: Outer loop i runs from 0 to n-1, controlling rows.

• Step 3: First inner loop prints spaces to align the triangle.

• Step 4: Initialize val = 1 for the first value in each row.

• Step 5: Second inner loop calculates and prints binomial coefficients using:

\text{val} = \text{val} \times \frac{(i - j)}{(j + 1)}

• Step 6: After each row, use print() to move to the next line.

4).RECURSIVE:

Absolutely, Karthika! Here's a complete and well-structured answer for a 16-mark


question on recursive functions in Python, including a recursive GCD program and a clear
explanation of the call stack — without any video references.

What Are Recursive Functions in Python?

A recursive function is a function that calls itself to solve smaller instances of the same
problem. This technique is especially useful for problems that exhibit self-similarity,
meaning they can be broken down into simpler versions of themselves.

Key Features of Recursion:

• Base Case: The condition that stops the recursion.

• Recursive Case: The part where the function calls itself with modified arguments.

Recursion is commonly used in problems like factorials, Fibonacci sequences, tree


traversals, and computing the greatest common divisor (GCD).

Recursive Program to Find GCD of Two Numbers

Python uses Euclid’s Algorithm for an efficient recursive approach to compute GCD.

Code:

a = int(input("Enter first number: ")) b = int(input("Enter second number: ")) def gcd(a,
b): if b == 0: return a else: return gcd(b, a % b) print("GCD is:", gcd(a, b))

Step-by-Step Logic:

1. Input: Two integers a and b are taken from the user.


2. Base Case: If b == 0, then a is the GCD.

3. Recursive Case: Call gcd(b, a % b) to reduce the problem size.

4. The recursion continues until b becomes 0.

5. The final result is returned and printed.

Role of the Call Stack in Recursion

The call stack is a memory structure that stores information about active function calls.
Each time a recursive function calls itself:

• A new frame is pushed onto the stack.

• Each frame holds the function’s parameters and local variables.

• When the base case is reached, the stack unwinds, returning results back through
each previous frame.

Example: gcd(48, 18)

• gcd(48, 18) → calls gcd(18, 12)

• gcd(18, 12) → calls gcd(12, 6)

• gcd(12, 6) → calls gcd(6, 0)

• gcd(6, 0) returns 6

• Each previous call receives 6 and returns it up the stack

This stack-based execution ensures that each recursive call has its own context and
returns correctly once the base case is reached.

5).LIST:

Difference Between Lists, Tuples, Sets, and Dictionaries in Python

Python provides several built-in data structures to store and manage collections of
data. Each has unique properties suited for different use cases.

1. List

• Definition: Ordered, mutable collection that allows duplicates.

• Syntax: my_list = [1, 2, 3, 2]

• Use Case: Ideal for storing sequences where order matters and elements may
change.

2. Tuple
• Definition: Ordered, immutable collection that allows duplicates.

• Syntax: my_tuple = (1, 2, 3)

• Use Case: Used for fixed data, like coordinates or database records.

3. Set

• Definition: Unordered, mutable collection of unique elements.

• Syntax: my_set = {1, 2, 3, 2} → becomes {1, 2, 3}

• Use Case: Useful for membership tests, removing duplicates, and set operations.

4. Dictionary

• Definition: Unordered collection of key-value pairs with unique keys.

• Syntax: my_dict = {'name': 'Karthika', 'age': 20}

• Use Case: Ideal for fast lookups and mappings between keys and values.

Time Complexity Comparison

Operation List Tuple Set Dictionary

O(1) O(1) (new


Insertion O(1) O(1)
(append) tuple)

Not
Deletion O(n) O(1) O(1)
allowed

Lookup O(n) O(n) O(1) O(1)

Note: Time complexities assume average-case performance. Worst-case scenarios


may vary depending on implementation and data distribution.

Summary of Key Differences

Feature List Tuple Set Dictionary

(Python
Ordered
3.7+)

Mutable

Allows (keys
Duplicates only)
Feature List Tuple Set Dictionary

Integer- Integer-
Indexing Key-based
based based

Dynamic Fixed Unique Key-value


Use Case
data data items mapping

6).CLASS AND CONSTRUCTOR:

Class Definition and Constructors in Python

What is a Class?

• A class in Python is a blueprint for creating objects.

• It defines a set of attributes (variables) and methods (functions) that describe the
behavior and state of its objects.

• Classes are defined using the class keyword.

Syntax:
class ClassName:
# Class attributes
# Class methods

What is a Constructor?

• A constructor is a special method named __init__ in Python.

• It is automatically called when a new object is created.

• The constructor initializes the object’s attributes.

• The first parameter is always self, which refers to the current instance of the class.

Syntax:
class ClassName:
def __init__(self, param1, param2):
self.attribute1 = param1
self.attribute2 = param2

Student Class Example


Objective:

Define a class Student with:

• Attributes: name, roll_no, marks

• Methods: calculate_average(), calculate_grade()

Python Code:
class Student:
def __init__(self, name, roll_no, marks):
[Link] = name
self.roll_no = roll_no
[Link] = marks

def calculate_average(self):
if not [Link]:
return 0.0
return sum([Link]) / len([Link])

def calculate_grade(self):
average = self.calculate_average()
if average >= 90:
return "A"
elif average >= 80:
return "B"
elif average >= 70:
return "C"
elif average >= 60:
return "D"
else:
return "F"

Example Usage:
student1 = Student("Alice", 101, [85, 90, 78, 92])
student2 = Student("Bob", 102, [60, 65, 55, 70])
student3 = Student("Charlie", 103, [95, 98, 90, 97])

# Display student1 details


print(f"Student Name: {[Link]}")
print(f"Roll Number: {student1.roll_no}")
print(f"Marks: {[Link]}")
print(f"Average Marks: {student1.calculate_average():.2f}")
print(f"Grade: {student1.calculate_grade()}")
print("-" * 20)

# Display student2 details


print(f"Student Name: {[Link]}")
print(f"Roll Number: {student2.roll_no}")
print(f"Marks: {[Link]}")
print(f"Average Marks: {student2.calculate_average():.2f}")
print(f"Grade: {student2.calculate_grade()}")
print("-" * 20)

# Display student3 details


print(f"Student Name: {[Link]}")
print(f"Roll Number: {student3.roll_no}")
print(f"Marks: {[Link]}")
print(f"Average Marks: {student3.calculate_average():.2f}")
print(f"Grade: {student3.calculate_grade()}")

7).INHERITANCE:

Inheritance in Python

Definition:
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows
one class (child) to acquire the properties and behaviors (methods) of another class
(parent). It promotes code reusability and modular design.

Types of Inheritance:

• Single Inheritance: One child class inherits from one parent class.

• Multiple Inheritance: One child class inherits from more than one parent class.

Method Overriding

Definition:

Method overriding occurs when a child class defines a method with the same name as a
method in its parent class. The child’s version replaces the parent’s version during
execution.

Example 1: Single Inheritance with Method Overriding


class Animal:
def sound(self):
print("This animal makes a sound")

class Dog(Animal):
def sound(self): # Overriding the parent method
print("Dog barks")

# Create object of Dog


d = Dog()
[Link]() # Output: Dog barks

Explanation:

• Dog inherits from Animal.

• The sound() method is overridden in Dog to provide specific behavior.

• When [Link]() is called, the overridden method in Dog is executed.

Example 2: Multiple Inheritance


class Artist:
def skill(self):
print("Can draw and paint")

class Athlete:
def skill(self):
print("Can run and jump")

class AllRounder(Artist, Athlete):


def skill(self): # Overriding both parent methods
print("Excels in both art and sports")

# Create object of AllRounder


a = AllRounder()
[Link]() # Output: Excels in both art and sports

Explanation:

• AllRounder inherits from both Artist and Athlete.

• The skill() method is overridden to combine both abilities.

• Python uses Method Resolution Order (MRO) to determine which parent method to
call if not overridden.

Summary

• Inheritance allows classes to reuse and extend functionality.

• Method overriding customizes inherited behavior in child classes.

• Single inheritance involves one parent class; multiple inheritance involves two or
more.

• Python supports both types and resolves conflicts using MRO.

This concept is essential for building scalable, maintainable, and reusable object-oriented
programs.

8).FILE HANDLING:

File Handling in Python


Python provides built-in functions to read from and write to text files using the open()
function.

Steps to Read Data from a Text File

1. Open the file using open(filename, mode) with mode 'r' (read).

2. Read content using methods like .read(), .readline(), or .readlines().

3. Process the data (e.g., count words, lines, characters).

4. Close the file using .close() or use a with block for automatic closing.

Steps to Write Data to a Text File

1. Open the file in 'w' (write) or 'a' (append) mode.

2. Write content using .write() or .writelines().

3. Close the file to save changes.

Python Program to Count Words, Characters, and Lines


filename = input("Enter the filename: ")

# Initialize counters
word_count = 0
char_count = 0
line_count = 0

try:
with open(filename, 'r') as file:
for line in file:
line_count += 1
char_count += len(line)
words = [Link]()
word_count += len(words)

print("Lines:", line_count)
print("Words:", word_count)
print("Characters:", char_count)

except FileNotFoundError:
print("File not found!")

Explanation:

• open(filename, 'r'): Opens the file in read mode.

• for line in file: Reads line by line.

• [Link](): Splits each line into words.

• len(line): Counts characters including spaces and newline.

• try-except: Handles missing file errors gracefully.

This program demonstrates practical use of file handling and string operations in Python
— a key skill for text processing tasks.

9).BINARY FILE:

Difference Between Text Files and Binary Files

Feature Text Files Binary Files

Raw bytes, not human-


Format Human-readable characters
readable

Extension .txt, .csv, .py .bin, .dat, .jpg, .exe

Storing images, audio,


Usage Storing plain text, logs, configs
serialized objects

Mode in
'r', 'w', 'a' 'rb', 'wb', 'ab'
Python

Requires character encoding No encoding; data stored as


Encoding
(e.g., UTF-8) byte stream

Requires specific programs or


Readability Can be opened in text editors
decoding

Summary:

• Text files store data as readable characters.

• Binary files store data as bytes, ideal for complex or structured data.
• Python uses the 'b' mode to handle binary files accurately.

Python Program: Store and Retrieve Student Records Using pickle

import pickle

# Define student records


students = [
{"name": "Karthika", "roll_no": 101, "marks": [85, 90,
78]},
{"name": "Arjun", "roll_no": 102, "marks": [75, 80,
70]},
{"name": "Meera", "roll_no": 103, "marks": [95, 92, 88]}
]

# Store records in binary file

with open("[Link]", "wb") as file:


[Link](students, file)
print("Student records stored successfully.")

# Retrieve records from binary file

with open("[Link]", "rb") as file:


loaded_students = [Link](file)

print("\nRetrieved Student Records:")


for student in loaded_students:
print(f"Name: {student['name']}, Roll No:
{student['roll_no']}, Marks: {student['marks']}")

Explanation:

• [Link]() serializes Python objects and writes them to a binary file.

• [Link]() deserializes the data back into Python objects.


• The file is opened in 'wb' mode for writing and 'rb' mode for reading.

• This method is ideal for storing structured data like dictionaries or lists.

This program demonstrates how binary files and the pickle module work together to
efficiently store and retrieve complex data structures in Python.

10).DATA FRAME:

Combining and Merging Datasets Using Pandas

In data analysis, it's common to combine multiple datasets. Pandas provides powerful
functions like merge(), concat(), and join() to combine DataFrames.

• merge() is similar to SQL joins and is used to combine two DataFrames based on a
common column or index.

• The how parameter in merge() specifies the type of join: 'inner', 'outer', 'left', or
'right'.

Python Program to Merge Two DataFrames


import pandas as pd

# Define first DataFrame


df1 = [Link]({
'ID': [1, 2, 3, 4],
'Name': ['Alice', 'Bob', 'Charlie', 'David']
})

# Define second DataFrame


df2 = [Link]({
'ID': [3, 4, 5, 6],
'Marks': [85, 90, 75, 80]
})

# Merge using different join types


inner_merge = [Link](df1, df2, on='ID', how='inner')
left_merge = [Link](df1, df2, on='ID', how='left')
right_merge = [Link](df1, df2, on='ID', how='right')
outer_merge = [Link](df1, df2, on='ID', how='outer')

# Display results
print("Inner Join:\n", inner_merge)
print("\nLeft Join:\n", left_merge)
print("\nRight Join:\n", right_merge)
print("\nOuter Join:\n", outer_merge)

Explanation of Join Types

Join
Description Result Includes
Type

Returns only rows with matching keys in both


Inner Intersection of keys
DataFrames

Returns all rows from the left DataFrame and All left keys + matched
Left
matching rows from the right right values

Returns all rows from the right DataFrame All right keys + matched
Right
and matching rows from the left left values

Returns all rows from both DataFrames, fills Union of keys from both
Outer
missing values with NaN DataFrames

11).

Reshaping and Pivoting in Pandas

Reshaping refers to changing the structure or layout of a DataFrame — such as


converting rows to columns or vice versa.
Pivoting is a specific type of reshaping where data is reorganized based on column
values, similar to pivot tables in Excel.

Pandas provides several powerful functions for reshaping:

• pivot(): Converts long-format data into wide-format.

• melt(): Converts wide-format data into long-format.

• stack() / unstack(): Reshape multi-level indexes by rotating rows and columns.


Example DataFrame
import pandas as pd

# Sample data
data = {
'Name': ['Alice', 'Bob', 'Alice', 'Bob'],
'Subject': ['Math', 'Math', 'Science', 'Science'],
'Marks': [85, 90, 88, 92]
}

df = [Link](data)
print("Original DataFrame:\n", df)

1. Using pivot()

pivot_df = [Link](index='Name', columns='Subject',


values='Marks') print("\nPivoted DataFrame:\n", pivot_df)

Output:

Subject Math Science

Name

Alice 85 88

Bob 90 92

Explanation: Rows become student names, columns become subjects, and values are
marks.

2. Using melt()
melted_df = [Link](pivot_df.reset_index(), id_vars='Name',
value_vars=['Math', 'Science'], var_name='Subject',
value_name='Marks')
print("\nMelted DataFrame:\n", melted_df)

Output:
Name Subject Marks

0 Alice Math 85

1 Bob Math 90

2 Alice Science 88

3 Bob Science 92

Explanation: Converts wide format back to long format.

3. Using stack() and unstack()


stacked_df = pivot_df.stack()
print("\nStacked DataFrame:\n", stacked_df)

unstacked_df = stacked_df.unstack()
print("\nUnstacked DataFrame:\n", unstacked_df)

Output:

• stack() converts columns into a multi-level index.

• unstack() reverses it back to column format.

Summary

Function Purpose

pivot() Converts long data to wide format

melt() Converts wide data to long format

stack() Stacks columns into row index

unstack() Unstacks row index back into columns

These reshaping tools are essential for transforming data to suit analysis, visualization, or
reporting needs.

Data Transformation Techniques in Pandas

Data transformation is the process of converting raw data into a clean and usable format
for analysis or modeling. Common techniques include:

1. Handling Missing Values


Missing values can lead to incorrect analysis or errors. Pandas provides several ways to
handle them:

• [Link]() → Identifies missing values.

• [Link]() → Removes rows/columns with missing values.

• [Link](value) → Replaces missing values with a specified value (e.g., mean, median).

2. Normalization

Normalization scales numerical data to a common range, often between 0 and 1. This
ensures that features contribute equally to analysis.

Min-Max Normalization Formula:

\text{Normalized} = \frac{x - \text{min}}{\text{max} - \text{min}}

In Pandas:

df['column'] = (df['column'] - df['column'].min()) / (df['column'].max() -


df['column'].min())

3. Applying Functions with apply()

The apply() method allows you to apply custom or built-in functions to rows or columns.

• [Link](func, axis=0) → Applies function column-wise.

• [Link](func, axis=1) → Applies function row-wise.

Python Program Demonstrating All Three


import pandas as pd

# Sample dataset with missing values


data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Math': [85, None, 78, 92],
'Science': [88, 90, None, 95]
}

df = [Link](data)
print("Original DataFrame:\n", df)

# 1. Handling missing values by filling with column mean


df['Math'].fillna(df['Math'].mean(), inplace=True)
df['Science'].fillna(df['Science'].mean(), inplace=True)

# 2. Normalizing scores using min-max scaling


df['Math'] = (df['Math'] - df['Math'].min()) /
(df['Math'].max() - df['Math'].min())
df['Science'] = (df['Science'] - df['Science'].min()) /
(df['Science'].max() - df['Science'].min())

# 3. Applying function row-wise to calculate average score


df['Average'] = [Link](lambda row: (row['Math'] +
row['Science']) / 2, axis=1)

print("\nTransformed DataFrame:\n", df)

Summary

Technique Purpose

Handling Missing Values Ensures data completeness and consistency

Normalization Scales features to a uniform range

apply() Enables flexible row/column-wise operations

These transformations are essential for preparing clean, consistent, and meaningful
datasets for analysis and machine learning.

Certainly, Karthika! Here's a complete answer for an 8-mark question on string


manipulation in Python, especially in the context of data cleaning. I’ll include common
operations and a sample program that demonstrates them clearly.

String Manipulation in Python for Data Cleaning

String manipulation is essential in data preprocessing to ensure consistency, remove


unwanted characters, and prepare textual data for analysis. Common tasks include:
Key Techniques:

• Removing leading/trailing spaces: [Link]()

• Converting case: [Link](), [Link](), [Link]()

• Replacing substrings: [Link](old, new)

• Splitting and joining: [Link](), 'separator'.join(list)

• Removing special characters: using regex or [Link]()

• Checking patterns: [Link](), [Link]()

Python Program: String Cleaning and Analysis


import re

# Sample raw data


raw_data = [
" Alice Smith ",
"BOB-JOHNSON",
"charlie@[Link]",
"David123",
"Eve!#&*"
]

# Cleaned data list


cleaned_data = []

for entry in raw_data:


# Step 1: Remove leading/trailing spaces
entry = [Link]()

# Step 2: Convert to title case


entry = [Link]()
# Step 3: Remove special characters and digits
entry = [Link](r'[^A-Za-z ]+', '', entry)

# Step 4: Append cleaned entry


cleaned_data.append(entry)

# Display results
print("Cleaned Data:")
for name in cleaned_data:
print(name)

Output:

Cleaned Data: Alice Smith Bobjohnson CharlieDomaincom David Eve

Summary

Operation Purpose

strip() Removes unwanted spaces

title() Standardizes capitalization

[Link]() Removes digits and special characters

lower() / upper() Ensures case consistency

split() / join() Helps restructure text

These techniques are crucial for preparing clean, uniform string data before applying
analysis or machine learning models. Let me know if you'd like to see how this integrates
with Pandas for column-wise cleaning!

------------

You might also like