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

Comprehensive Python Programming Guide

This document provides comprehensive notes on Python programming, covering its introduction, features, basic syntax, data structures, functions, object-oriented programming, exception handling, modules, file I/O, standard libraries, and advanced topics. It emphasizes Python's readability, simplicity, and versatility, along with best practices for coding. Key concepts such as control flow, data types, and programming paradigms are also discussed.

Uploaded by

nguhoa04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Comprehensive Python Programming Guide

This document provides comprehensive notes on Python programming, covering its introduction, features, basic syntax, data structures, functions, object-oriented programming, exception handling, modules, file I/O, standard libraries, and advanced topics. It emphasizes Python's readability, simplicity, and versatility, along with best practices for coding. Key concepts such as control flow, data types, and programming paradigms are also discussed.

Uploaded by

nguhoa04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python Class Notes

1. Introduction to Python

 Python is a high-level, interpreted, and general-purpose programming language.

 Created by Guido van Rossum in 1991.

 Known for readability, simplicity, and versatility.

 Interpreted & dynamically typed: No need to declare variable types explicitly.

 Cross-platform: Works on Windows, macOS, Linux.

Features

 Simple syntax, easy to learn.

 Interpreted language.

 Object-oriented and supports functional programming.

 Extensive standard library.

 Interactive: Supports REPL (Read-Eval-Print Loop).

 Open-source and widely supported.

2. Python Basics

2.1 Hello World

print("Hello, World!")

2.2 Comments

# Single-line comment

"""

Multi-line comment

"""

2.3 Variables & Data Types

# Numbers

x = 10 # int

y = 3.14 # float

# String

name = "John"

# Boolean
is_active = True

# None

value = None

 Dynamic typing: Python determines type automatically.

2.4 Type Conversion

x = int(3.5) #3

y = float(10) # 10.0

s = str(100) # "100"

3. Operators

 Arithmetic: +, -, *, /, //, %, **

 Comparison: ==, !=, >, <, >=, <=

 Logical: and, or, not

 Assignment: =, +=, -=, *=, /=

 Membership: in, not in

 Identity: is, is not

4. Control Flow

4.1 Conditional Statements

x = 10

if x > 0:

print("Positive")

elif x == 0:

print("Zero")

else:

print("Negative")

4.2 Loops

 for loop

for i in range(5): # 0 to 4

print(i)

 while loop
i=0

while i < 5:

print(i)

i += 1

 break & continue

for i in range(5):

if i == 3:

break

print(i)

5. Data Structures

5.1 Lists

fruits = ["apple", "banana", "cherry"]

[Link]("orange")

fruits[0] # "apple"

5.2 Tuples

 Immutable list.

point = (1, 2)

x, y = point

5.3 Sets

 Unordered, no duplicates.

numbers = {1, 2, 3, 3} # {1, 2, 3}

[Link](4)

5.4 Dictionaries

person = {"name": "John", "age": 25}

print(person["name"])

person["age"] = 26

6. Functions

def greet(name):

return f"Hello, {name}"


print(greet("Alice"))

 Default parameters

def power(x, n=2):

return x ** n

 Variable arguments

def add(*args):

return sum(args)

7. Object-Oriented Programming

7.1 Classes & Objects

class Person:

def __init__(self, name, age):

[Link] = name

[Link] = age

def display(self):

print(f"{[Link]} is {[Link]} years old")

p = Person("Alice", 25)

[Link]()

7.2 Inheritance

class Student(Person):

def __init__(self, name, age, student_id):

super().__init__(name, age)

self.student_id = student_id

7.3 Encapsulation

class Person:

def __init__(self, name):

self.__name = name # private variable

def get_name(self):

return self.__name
7.4 Polymorphism

 Method overriding

class Animal:

def speak(self):

print("Animal speaks")

class Dog(Animal):

def speak(self):

print("Dog barks")

8. Exception Handling

try:

result = 10 / 0

except ZeroDivisionError:

print("Cannot divide by zero")

finally:

print("Done")

 raise: Throw an exception manually.

if x < 0:

raise ValueError("x must be positive")

9. Modules & Packages

# Import module

import math

print([Link](16))

# From module

from math import pi

print(pi)

 Creating module: Save .py file and import it.

 Packages: Folders with __init__.py.


10. File I/O

# Write to file

with open("[Link]", "w") as f:

[Link]("Hello Python")

# Read from file

with open("[Link]", "r") as f:

data = [Link]()

11. Python Standard Libraries

 math – Math functions

 random – Random numbers

 datetime – Date and time

 os – OS operations

 sys – System-specific parameters

 re – Regular expressions

 json – JSON parsing

12. Advanced Topics

12.1 List Comprehensions

squares = [x**2 for x in range(5)]

12.2 Lambda Functions

square = lambda x: x**2

print(square(5))

12.3 Decorators

def decorator(func):

def wrapper():

print("Before function")

func()

print("After function")

return wrapper
@decorator

def say_hello():

print("Hello")

12.4 Generators

def gen():

for i in range(5):

yield i

for val in gen():

print(val)

12.5 Multithreading

import threading

def print_hello():

print("Hello from thread")

t = [Link](target=print_hello)

[Link]()

[Link]()

12.6 Decorators, Context Managers, Iterators

 with keyword for resource management.

 __iter__ and __next__ for custom iterators.

13. Python Best Practices

 Use meaningful variable names.

 Follow PEP 8 coding style.

 Write modular code using functions and classes.

 Handle exceptions carefully.

 Avoid global variables when possible.

 Comment and document code clearly.

You might also like