← All articles
PythonAug 18, 20243 min read

Python Basics

1. Introduction to Python Python is a high-level, interpreted programming language known for its simplicity and readability. It was invented by Guido van Rossum and first released in 1991. Python supports multiple…

2. Python Syntax Python uses indentation to define code blocks, eliminating the need for braces {} or keywords like end. The standard indentation is four spaces.

3. Comments in Python Python supports both single-line and multi-line comments. Single-line comments start with #. Multi-line comments are typically done using triple quotes (''' ... ''' or """ ... """).

4. Data Types Python has several built-in data types:

  • Numeric types: int, float, complex
  • Sequence types: str, list, tuple
  • Mapping type: dict
  • Set types: set, frozenset
  • Boolean type: bool
  • None type: NoneType

5. Variables Variables in Python are dynamically typed, meaning you don’t need to declare their type. Assignment is done using the = operator. Variable names should be descriptive and follow the snake_case convention.

6. Control Flow Python uses if, elif, and else statements for decision-making. Loops include for and while. The break statement exits a loop, while continue skips the current iteration.

7. Functions Functions in Python are defined using the def keyword. They can take arguments and return values using the return statement. Functions promote code reusability and modularity.

8. Modules and Packages Modules are Python files that can be imported into other Python files using the import keyword. A package is a collection of modules in a directory. Python’s standard library provides many useful modules, such as math, datetime, and os.

9. File Handling Python makes file handling easy with built-in functions like open(). Files can be opened in different modes, including r (read), w (write), a (append), and b (binary). Always ensure you close files using close() or by using a with statement to handle files automatically.

10. Error Handling Python uses try-except blocks to handle errors gracefully. The finally block can be used to execute code regardless of whether an error occurred.

11. Object-Oriented Programming (OOP) OOP in Python is centered around classes and objects. A class is a blueprint for creating objects, which are instances of a class. Key OOP concepts include:

  • Inheritance: One class can inherit attributes and methods from another.
  • Encapsulation: Hiding the internal state of an object and requiring all interactions to be performed through methods.
  • Polymorphism: Different classes can be treated as instances of the same class through inheritance.

12. List Comprehensions List comprehensions provide a concise way to create lists. They consist of brackets containing an expression followed by a for clause. They are used for transforming or filtering data in lists.

13. Lambda Functions Lambda functions are small, anonymous functions defined using the lambda keyword. They are typically used for short operations without defining a full function.

14. Map, Filter, and Reduce

  • Map applies a function to all items in a list.
  • Filter extracts items from a list based on a condition.
  • Reduce applies a rolling computation to sequential pairs of items, cumulatively reducing the list to a single value.

15. Common Built-in Functions Python comes with many built-in functions, including len(), sum(), max(), min(), sorted(), type(), and range(). These functions perform common operations efficiently.

16. Debugging Debugging in Python can be done using print() statements or the pdb module for step-by-step debugging. Good debugging practices help in identifying and fixing bugs efficiently.

17. Operators

  • Arithmetic Operators: +, -, *, /, %, **, //
  • Comparison Operators: ==, !=, >, <, >=, <=
  • Logical Operators: and, or, not
  • Membership Operators: in, not in
  • Identity Operators: is, is not
  • is checks if two variables point to the same object.
  • is not checks if two variables point to different objects.
MK
Mohankrishna PodileDevOps Engineer & Cloud Architect · Irving, Texas

Comments

Questions, corrections, war stories — all welcome. Sign in with GitHub to join the discussion.