0% found this document useful (0 votes)
545 views4 pages

Clean Coding Principles in Python

This document outlines the course content for a course on clean coding with Python. The course aims to teach students how to design and program Python applications, use data structures like lists and dictionaries, write loops and functions, handle errors and exceptions, work with files and classes, and implement object-oriented programming concepts. It also covers machine learning algorithms like random forests and support vector machines. The course material will be taught over 4 units that cover Python fundamentals, data handling, advanced concepts, and machine learning algorithms.

Uploaded by

Tanisha Sehgal
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)
545 views4 pages

Clean Coding Principles in Python

This document outlines the course content for a course on clean coding with Python. The course aims to teach students how to design and program Python applications, use data structures like lists and dictionaries, write loops and functions, handle errors and exceptions, work with files and classes, and implement object-oriented programming concepts. It also covers machine learning algorithms like random forests and support vector machines. The course material will be taught over 4 units that cover Python fundamentals, data handling, advanced concepts, and machine learning algorithms.

Uploaded by

Tanisha Sehgal
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
  • Unit - I: Clean Coding
  • Unit - III: Data Handling and Use Cases
  • Unit - II: Introduction to Python
  • Unit - IV: Advance Concepts

Clean Coding with Python

COURSE OUTCOME:
➢ To understand why Python is a useful scripting language for developers
➢ To learn how to design and program Python applications
➢ To learn how to use lists, tuples, and dictionaries in Python programs
➢ To learn how to identify Python object types
➢ To learn how to use indexing and slicing to access data in Python programs
➢ To define the structure and components of a Python program
➢ To learn how to write loops and decision statements in Python
➢ To learn how to write functions and pass arguments in Python
➢ To learn how to build and package Python modules for reusability
➢ To learn how to read and write files in Python
➢ To learn how to design object‐oriented programs with Python classes
➢ To learn data handling and use cases diagrams
➢ To learn how to use class inheritance in Python for reusability
➢ To learn how to use exception handling in Python applications for error handling

Particulars

Unit - I
❖ Chapter - 1
➢ Introduction to Clean Code
■ What is Bad Code?
● Example 1: Avoid
● Example 2: for each code
■ What is Clean Code?
➢ Purpose of Clean Code
➢ Thought of experienced programmers
❖ Chapter - 2
➢ Meaningful Names
■ Intention Revealing Names
● Example 1: Poor Variable Names
● Example 2: Poor Method Names
● Example 3: Variable Name
■ Make Meaningful Distinctions
● Example 1: Usage of Different Words
● Example 2: Distinct Names
■ Use Pronounceable Names
● Example 1: Vocal Names
● Example 2: Short Form Names
● Example 3: Non-Pronounceable Names
● Example 4: Compare
■ Avoid Encodings and Mental Mappings
● Difference between smart and professional programmer
■ Class and Method Names
❖ Chapter - 3
➢ Functions
■ Function Size Matters
■ Blocks and Indenting
■ Do only one thing within a function
■ One level of abstraction per function
■ Use Descriptive Names
● Example 1: Verbal Names
■ Function Arguments
● Advantages of Having Less Arguments
■ Command Query Separation
■ Prefer Exceptions to Returning Error Codes
■ Extract Try/Catch Blocks
■ Error Handling Is One Thing
❖ Chapter - 4
➢ Comments
■ Good Comments
■ Good Names Can Obviate Comments
■ Types of Good Comments
● Legal Comment
Unit - II
❖ Introduction to Python
➢ What is Python?
➢ Advantages and disadvantages
➢ Downloading and installing
➢ Which version of Python
➢ Running Python Scripts
➢ Using the interpreter interactively
➢ Using variables
➢ String types: normal, raw and Unicode
➢ String operators and expressions
➢ Math operators and expressions
➢ Writing to the screen
➢ Reading from the keyboard
➢ Indenting is significant
➢ The if and elif statements
➢ While Loops
➢ Using List
➢ Dictionaries
➢ Using the for statement
➢ Opening, reading and writing a text file
➢ Using Pandas, the python data analysis library and data frames
➢ Grouping, aggregating and applying, merging and joining
➢ Dealing with syntax errors
➢ Exceptions, Handling exceptions with try/except

Unit - III
❖ Data Handling and Use Cases
➢ RE Pattern Matching
➢ Parsing Data
➢ Introduction to Regression
➢ Types of Regression
➢ Use Cases
➢ Exploratory data analysis
➢ Correlation Matrix
➢ Visualization using Matplotlib
➢ Implementing linear regression
Unit - IV
❖ Advance Concepts
➢ Machine Learning - Algorithm
➢ Algorithms – Random forest
➢ Super vector Machine
➢ Random Forest
➢ Build your own model in python
➢ Comparison between random forest and decision tree

TEXTBOOKS-

1. Learning Python 5ed: Powerful Object-Oriented Programming by Mark Lutz


2. Python in Easy Steps by Mike McGrath
3. Python 2.1 Bible by Dave Brueck and Stephen Tanner
4. The Python Coding Book: Understanding what programming really is by Stephen Gruppetta

REFERENCE-

[Link]: The Complete Reference by Martin C. B

2. Python Pocket Reference, 5th Edition,Mark Lutz,ISBN: 1449357016, O'Reilly Media

[Link] 3 Standard Library by Example,Doug Hellman,ISBN: 9780134291055, Addison-Wesley Professional; June 11,
2017; 1456 pages

Common questions

Powered by AI

Exception handling is crucial for maintaining application robustness by allowing programs to handle errors gracefully and continue operations or terminate cleanly . Best practices involve using try-except blocks to catch and manage exceptions, ensuring resource release (e.g., files, network connections) through finally blocks, and avoiding catching generic exceptions unless necessary, which helps in understanding the specific issues . Properly logging exceptions and using them to provide informative error messages also aids in debugging and user communication .

Choosing between function arguments and command-query separation involves understanding their advantages and intended use. Minimizing function arguments enhances function clarity and ease of understanding by reducing complexity and potential for errors . Command-query separation, a design principle whereby methods either perform an action (commands) or return data (queries), promotes code readability and maintainability by distinguishing between actions with side effects and those without. This helps prevent unintended side effects when calling methods that should be non-mutative .

The choice between using a random forest and a decision tree depends largely on the application requirements in terms of accuracy, interpretability, and speed. Random forests, being an ensemble learning method combining multiple decision trees, generally offer higher prediction accuracy and are robust to overfitting compared to a single decision tree . However, decision trees are easier to interpret and visualize, making them preferable for simpler tasks or when model interpretability is crucial . Random forests require more computation and are less suited for real-time predictions compared to decision trees .

Meaningful naming in Python is a core principle of creating clean code as it clarifies the intent and functionality of code elements at a glance. Effective practices include choosing intention-revealing names, making meaningful distinctions between different variables, and using pronounceable and descriptive names. For example, names like 'calculate_interest' are more descriptive than vague alternatives like 'doStuff' . Ineffective practices involve the use of non-descriptive, ambiguous, or overly condensed names that require additional mental effort to understand, such as 'var1' or 'temp', which do not convey meaningful information .

Comments in Python enhance code readability and maintainability by explaining the purpose of complex logic, assumptions, or decisions not apparent from the code alone . 'Good' comments provide context or rationale, such as legal or contractual obligations, clarifying intent without repeating the obvious code semantics . Conversely, 'bad' comments state what the code does, duplicating information that should be self-evident from clean code practices like naming conventions, and thus can become misleading as the code evolves .

Indenting is significant in Python because it defines the structure and block of code, acting as a syntax requirement for the demarcation of code blocks like loops and conditionals . Improper indentation can lead to syntax errors or logical errors, as Python interprets indents as an indication of code scope. Mistakes in indentation can result in blocks being incorrectly associated, which can potentially change the program's logic or cause runtime errors .

Pandas is beneficial for data analysis in Python due to its powerful data manipulation capabilities, including handling large datasets with ease, intuitive syntax, and seamless integration with other data processing libraries . It supports a variety of operations like grouping, merging, and aggregating, which are vital for exploratory data analysis . However, its drawbacks include potential performance issues with very large datasets due to memory overhead and steep learning curve due to its extensive feature set, which can overwhelm new users .

Class inheritance in Python facilitates code reusability by allowing new classes to inherit methods and properties from existing classes, thereby extending their functionality without redundant code duplication . This promotes a hierarchical and organized structure where common functionality is centralized in base classes, and specific features are added in derived classes, enhancing maintainability and reducing errors due to code replication . However, it requires careful design to prevent excessively deep hierarchies, which can complicate program flow and hinder understanding .

Python's lists, tuples, and dictionaries each provide unique properties that cater to different data handling needs, contributing to application efficiency. Lists are versatile, mutable, and suitable for dynamically sized collections, making them ideal for tasks involving frequent updates or needs to maintain order . Tuples, being immutable, allow for faster access and are suitable for fixed collections that don't require modification, providing a layer of data integrity . Dictionaries offer fast lookups due to their hash table implementation, making them optimal for mappings and associations between keys and values, thereby facilitating quick data retrieval operations .

Using exceptions is preferable over returning error codes when precise error handling and propagation are required. Exceptions offer a structured way to handle errors by separating error-handling logic from regular code flow, thus increasing code readability and maintainability . Unlike error codes, exceptions propagate up the call stack, allowing the program to handle errors at appropriate levels without cluttering function interfaces with return values meant for error signaling .

You might also like