Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Python Programming Notes

This document provides comprehensive notes on Python programming fundamentals, including basic syntax, data types, operators, control structures, data structures, functions, object-oriented programming, modules, exception handling, and file operations. It covers essential concepts such as dynamic typing, list comprehensions, class inheritance, and common libraries for various applications. The notes serve as a quick reference guide for Python programming techniques and best practices.

Uploaded by

singomillionaire
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Programming Notes

This document provides comprehensive notes on Python programming fundamentals, including basic syntax, data types, operators, control structures, data structures, functions, object-oriented programming, modules, exception handling, and file operations. It covers essential concepts such as dynamic typing, list comprehensions, class inheritance, and common libraries for various applications. The notes serve as a quick reference guide for Python programming techniques and best practices.

Uploaded by

singomillionaire
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Python Programming Notes

Python Fundamentals
Basic Syntax

 Python uses indentation (whitespace) to define code blocks


 Statements don't require semicolons
 Comments use # symbol
 Case-sensitive language

Variables and Data Types

 Dynamic Typing: No need to declare variable types


 Basic Types:
o Numeric: int, float, complex
o Sequence: str, list, tuple
o Mapping: dict
o Set: set, frozenset
o Boolean: True, False
o None: None

Operators

 Arithmetic: +, -, *, /, // (floor division), % (modulo), ** (exponentiation)


 Comparison: ==, !=, >, <, >=, <=
 Logical: and, or, not
 Assignment: =, +=, -=, *=, /=

Control Structures
Conditional Statements
if condition:
# code block
elif another_condition:
# code block
else:
# code block

Loops
# For loop
for item in iterable:
# code block

# While loop
while condition:
# code block
Control Statements

 break: Exit loop


 continue: Skip to next iteration
 pass: Null operation

Data Structures
Lists

 Ordered, mutable collections


 Methods: append(), extend(), insert(), remove(), pop(), sort(), reverse()
 List comprehensions: [x for x in range(10) if x % 2 == 0]

Dictionaries

 Key-value pairs
 Methods: keys(), values(), items(), get(), update()
 Dictionary comprehensions: {x: x**2 for x in range(5)}

Tuples

 Ordered, immutable collections


 Used for unchanging data groupings

Sets

 Unordered collections of unique elements


 Methods: add(), remove(), union(), intersection()

Functions
Function Definition
def function_name(parameters):
"""Docstring"""
# code block
return value

Parameters

 Default parameters: def func(x=10)


 Variable-length arguments: def func(*args, **kwargs)
 Keyword-only arguments: def func(*, keyword=value)

Lambda Functions

 Anonymous functions: lambda x: x**2


Object-Oriented Programming
Classes
class ClassName:
"""Docstring"""

def __init__(self, parameters):


self.attribute = value

def method(self):
# code block

Inheritance
class Child(Parent):
def __init__(self, parameters):
super().__init__(parameters)
# additional initialization

Special Methods

 __str__, __repr__, __len__, __getitem__, etc.

Modules and Packages


Importing
import module
from module import function
from package.module import function as alias

Common Libraries

 Data Science: NumPy, Pandas, Matplotlib


 Web: Flask, Django, Requests
 Machine Learning: Scikit-learn, TensorFlow, PyTorch

Exception Handling
try:
# code that might raise exception
except ExceptionType:
# handle exception
else:
# runs if no exception occurred
finally:
# runs regardless of exception

File Operations
with open('filename.txt', 'r') as file:
content = file.read()

with open('filename.txt', 'w') as file:


file.write('Hello, world!')

You might also like