0% found this document useful (0 votes)
5 views7 pages

Python_Unit-1

python

Uploaded by

sahoobrijeskumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
5 views7 pages

Python_Unit-1

python

Uploaded by

sahoobrijeskumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

PYTHON

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:

 web development (server-side),


 software development,
 system scripting.

What can Python do?

 Python can be used on a server to create web applications.


 Python can be used alongside software to create workflows.
 Python can connect to database systems. It can also read and modify files.
 Python can be used to handle big data and perform complex mathematics.
 Python can be used for rapid prototyping, or for production-ready software development.

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 Syntax compared to other programming languages

 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{

Public static void main(String[]args){

System.out.println(“Hello world”);

According to C:

#include<stdio.h>

void main(){

printf(“Hello world”);

History of Python:.

 Python laid its foundation in the late 1980s.


 The implementation of Python was started in December 1989 by Guido Van Rossum at CWI in
Netherland.
 In February 1991, Guido Van Rossum published the code (labeled version 0.9.0) to alt.sources.
 In 1994, Python 1.0 was released.
 Python 2.0 added new features such as list comprehensions, garbage collection systems.
 On December 3, 2008, Python 3.0 (also called "Py3K") was released. It was designed to rectify
the fundamental flaw of the language.

Concept of problem solving:

 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:

1. Input: Data provided to the program.


2. Output: The expected result or solution.
3. Constraints: Conditions or limitations that the solution must satisfy.
4. Process: The steps or logic (algorithm) used to transform the input into the desired output.

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.

3. Decompose into Modules/Functions:

 Identify reusable parts of the code and group them into functions or classes. This modularity enhances code
clarity, reuse, and maintainability.

4. Choose Appropriate Data Structures:

 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.

8. Debugging and Error Handling:

 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.

There are three types of errors:


1)Compile time error :
2)Run time error
3)Logical error

1)Compile time error :


The errors occurs during compile time we call it as compile time error.
 There are 2 types of compile time error
i) Syntax error: It occurs when rules of programming language is misused.
Ex: print(hello)
Here we will get error because we have not mention any variable with the
name of hello .
ii) Semantic error: it occurs when statements are not meaningfull
EX: x=10;
Y=20;
X*y=z
Here we will get error because the statement we have written in the
above is meaningless.
2)Runtime error: Error occurs during the execution of a program are called runtime error.
Ex:
Suppose we are watching movie in laptop by connecting pendrive suddenly we remove the
pendrive from the laptop then the movie will be stop there because in the location there will
not be any movie ,there we will get run time error.
Ex:

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:

 Documentation in python refers to the written text that accompanies a python


software project .This text explain the purpose of code making it easier for other
 Documentation in Python refers to the written text that accompanies a Python software project. This text
explains the purpose and use of the code, making it easier for others (and often yourself in the future) to
understand and maintain. Python documentation can come in many forms, from inline comments and
docstrings within the code itself, to external documentation like user manuals and API references.

 Why Documenting Your Python Code Is Important :

 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:

Single-line docstring: Describes the function briefly in a single line.


Multi-line docstring: Provides a more detailed explanation of the function or class.
Example:

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.
"""

def add(self, a, b):


"""Add two numbers."""
return a + b
5. PEP 257 – Python Docstring Conventions
PEP 257 provides conventions for writing docstrings in Python:
Use triple quotes even for one-liners.
The first line should be a short summary of the object’s purpose.
If there are more details, they should be separated from the summary by a blank line.
Each function's docstring should describe its arguments and return values (if any).
Example of PEP 257 convention:

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.

You might also like