Python_Unit-1
Python_Unit-1
Python Introduction
What is Python?
Python is a general-purpose,high-level, compiled and interpreted, garbage-collected,
and purely object-oriented programming language that supports procedural, object-
oriented, and functional programming
Python is a popular programming language. It was created by Guido van Rossum, and released in
1991.
It is used for:
Why Python?
Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
Python has a simple syntax similar to the English language.
Python has syntax that allows developers to write programs with fewer lines than some other
programming languages.
Python runs on an interpreter system, meaning that code can be executed as soon as it is written.
This means that prototyping can be very quick.
Python can be treated in a procedural way, an object-oriented way or a functional way.
Python was designed for readability, and has some similarities to the English language with
influence from mathematics.
Python uses new lines to complete a command, as opposed to other programming languages
which often use semicolons or parentheses.
Python relies on indentation, using whitespace, to define scope; such as the scope of loops,
functions and classes. Other programming languages often use curly-brackets for this purpose.
According to python:
print("Hello, World!")
According to java:
Class A{
System.out.println(“Hello world”);
According to C:
#include<stdio.h>
void main(){
printf(“Hello world”);
History of Python:.
Understand the problem: Clearly define the problem, inputs, and expected outputs.
Break down the problem: Decompose the problem into smaller, more manageable components or steps.
Plan the solution: Develop a strategy using algorithms or logical sequences. Consider control flow structures like
loops, conditionals, and functions.
Choose data structures: Use appropriate data structures such as lists, dictionaries, sets, or tuples to store and
manipulate data efficiently.
Write Python code: Implement the solution in Python using its syntax, functions, modules, and libraries.
Test and debug: Test the solution with various inputs to ensure correctness and debug any issues or errors.
Optimize: Refine the solution for efficiency, reducing time and space complexity if needed.
Defination of problem:
problem refers to a task or challenge that needs to be solved using programming techniques. It typically involves:
Program Design:
Program design in Python involves planning and structuring the components of a program to solve a specific problem
efficiently. The process generally follows these steps:
1. Problem Definition:
Clearly define what the program is supposed to do, including inputs, outputs, and constraints.
2. Algorithm Design:
Plan the sequence of steps (algorithm) needed to solve the problem. Break the task into smaller sub-tasks.
Identify reusable parts of the code and group them into functions or classes. This modularity enhances code
clarity, reuse, and maintainability.
Select suitable data structures like lists, dictionaries, sets, tuples, etc., to store and manipulate data efficiently.
5. Write Pseudocode/Flowchart:
Use pseudocode or flowcharts to visualize the program’s flow and logic before coding. This helps in identifying
logical errors early.
6. Write Code:
Translate your pseudocode or flowchart into actual Python code. Use Python’s syntax, functions, and libraries
to implement your solution.
7. Testing:
Test the program with different inputs to ensure correctness. Include unit testing for individual functions and
overall testing for program integrity.
Fix any bugs or logical errors encountered during testing. Implement error handling using try-except blocks for
robust code.
9. Optimization:
Analyze the performance of your code and optimize it if necessary. This may involve reducing time complexity
or improving memory usage.
10. Documentation:
Add comments and documentation to explain the purpose and functioning of the code, making it easier to
understand and maintain.
Debugging:
Debugging refers to the process of locating the place of error ,cause of error and
correcting the code accordingly.
Error in the program:
An error some times called a bug is anything in the code that prevents a program
from compiling and running correctly.
a=10
b=a/0
Print(b)
Here we will get the ZeroDivisonError at the run time because any number can not be
divisible by zero..
3)Logical error: It occurs when the program does not give desired output due to wrong
logic.
Ex:
Write python program to add two number
a=12;
b=29;
c=a-b;
print(c)
In the above program we will get logical error because we need out put as adition of
two number but we are getting substraction of two number because of logic.
Documentation:
Code Maintainability
Onboarding New Developers
Code Collaboration
Quality Assurance
Ways to Document Python Code :
1. Inline Comments
Purpose: To describe parts of code to make it easier to understand.
Syntax: Comments start with a # symbol.
Example:
# This function adds two numbers
def add(a, b):
return a + b
2. Docstrings
Purpose: To provide a structured way to document functions, classes, and modules.
Syntax: Docstrings are written using triple quotes (""" or ''') directly after the function, class, or module definition.
Types of Docstrings:
python
Copy code
def multiply(a, b):
"""
Multiply two numbers and return the result.
Args:
a (int): The first number.
b (int): The second number.
Returns:
int: The result of multiplication.
"""
return a * b
3. Module Documentation
Purpose: To document the overall purpose and functionality of a Python module.
Location: At the top of the module file.
Example:
python
Copy code
"""
This module contains functions for basic arithmetic operations.
Functions included:
- add(a, b): Adds two numbers.
- multiply(a, b): Multiplies two numbers.
"""
4. Class Documentation
Purpose: To describe the purpose and functionality of a class.
Example:
python
Copy code
class Calculator:
"""
A simple calculator class for basic arithmetic operations.
"""
python
Copy code
def divide(a, b):
"""
Divide two numbers and return the quotient.
Args:
a (int): The dividend.
b (int): The divisor.
Returns:
float: The quotient of the division.
Raises:
ZeroDivisionError: If b is zero.
"""
if b == 0:
raise ZeroDivisionError("Division by zero is not allowed.")
return a / b
6. Tools for Generating Documentation
Sphinx: A tool that can generate documentation from your Python code, using docstrings.
pydoc: The built-in Python tool that automatically generates documentation from your code.
7. Best Practices for Documentation
Keep comments and docstrings up to date with code changes.
Use clear and concise language.
Avoid obvious comments .
Include information on edge cases, exceptions, and return types in docstrings.