Semester I BCA Syllabus BCA101 T – Programming Principles Using Python
Semester I Paper BCA101 T Unit III – Creating Python Programs:
Unit III “Creating Python Programs” is designed to introduce students to the fundamentals of Python programming. Python is a versatile and widely used programming language known for its simplicity and readability. In this course, we will explore various aspects of Python programming, including input and output operations, control statements, function definition, default arguments, and exception handling. You will learn the practical programming skills required for a good career as a developer by going through theory, practicing the exercises and your books.
Objectives:
- To help the student learn Python programming concepts and syntax.
- To enable students to perform input and output operations in Python.
- To teach students how to handle different types of data for input and output.
- To enable learners with real examples of Python programs for day-to-day scenarios.
Outline:
- Input and Output Statements
- Introduction to Python and Basic I/O
- Advanced I/O and Formatting
- Control Statements
- Conditional Statements
- Loop Control Statements
- Exiting Programs
- Defining Functions
- Introduction to Functions
- Default Arguments and Keyword Arguments
- Errors and Handling Exceptions
- Understanding Errors
- Handling Exceptions
- Raising and Custom Exceptions
Input and Output Statements:
Introduction to Python and Basic I/O
- Introduction to Python as a programming language.
- Setting up a Python environment.
- Your first Python program.
- Basic input and output statements.
Input and Output Examples:
name = input(“Enter your name: “)
print(“Hello, ” + name + “!”)
Advanced I/O and Formatting
- Reading and writing files.
- String formatting for output.
- Handling user input.
Reading and Writing to Files
with open(“example.txt”, “w”) as file:
file.write(“This is an example file.”)
with open(“example.txt”, “r”) as file:
content = file.read()
print(content)
Control Statements:
Conditional Statements
- Introduction to conditional statements (if, elif, else).
- Using conditional statements for decision-making.
Conditional Statements Example
age = int(input(“Enter your age: “))
if age < 18:
print(“You are a minor.”)
else:
print(“You are an adult.”)
Loop Control Statements:
- Introduction to loop control statements (for, while).
- Using break, continue, and pass in loops.
Loop Control Statements Example
for i in range(1, 6):
if i == 3:
continue
print(i)
Exiting Programs
- Exiting programs using the exit() function.
- Handling program termination gracefully.
Exiting Programs Example
import sys
choice = input(“Do you want to exit? (yes/no): “)
if choice.lower() == “yes”:
sys.exit()
else:
print(“Continuing program execution.”)
Defining Functions:
Introduction to Functions
- What are functions?
- Defining and calling functions.
Function Definition Example
def greet(name):
print(“Hello, ” + name + “!”)
Error and Exception Handling Example
try:
result = 10 / 0
except ZeroDivisionError:
print(“Division by zero is not allowed.”)
Handling Exceptions
-
- Using try-except blocks for exception handling.
-
- Handling multiple exceptions.
Handling Multiple Exceptions Example
try:
age = int(input(“Enter your age: “))
print(“You entered:”, age)
except ValueError:
print(“Invalid input. Please enter a number.”)
except KeyboardInterrupt:
print(“Program interrupted by user.”)
Raising and Custom Exceptions
-
- Raising exceptions in Python.
-
- Creating custom exceptions.
Raising and Custom Exceptions Example
def divide(a, b):
if b == 0:
raise ZeroDivisionError(“Division by zero is not allowed.”)
return a / b
Note: The information provided above serves as a guide to learning foundation in Python programming with a focus on input and output, control statements, functions, default arguments, and exception handling. Students are encouraged to practice regularly and seek additional resources to deepen their understanding of Python.