0% found this document useful (0 votes)
2 views

Complete Python Guide

This document serves as a comprehensive guide to learning Python, covering fundamental concepts such as installation, data types, control structures, functions, and object-oriented programming. It also includes advanced topics like exception handling, file handling, and unit testing. The guide emphasizes best practices for writing clean code and provides practical examples throughout.

Uploaded by

veerapakuraja
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)
2 views

Complete Python Guide

This document serves as a comprehensive guide to learning Python, covering fundamental concepts such as installation, data types, control structures, functions, and object-oriented programming. It also includes advanced topics like exception handling, file handling, and unit testing. The guide emphasizes best practices for writing clean code and provides practical examples throughout.

Uploaded by

veerapakuraja
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
You are on page 1/ 5

Complete Python Learning Guide

1. Introduction to Python

Python is a high-level, interpreted programming language known for its simplicity and readability.

Example:

print("Hello, World!")

2. Installing Python and Running Code

- Download from https://python.org

- Run in terminal or use an IDE like VSCode or PyCharm.

Example:

Save as hello.py and run: python hello.py

3. Comments and Indentation

# This is a single-line comment

'''

This is a

multi-line comment

'''

Indentation defines code blocks in Python.

4. Variables and Data Types

Variables store data. Types: int, float, str, bool, list, tuple, dict, set

Example:

name = "Alice"

age = 25

5. Type Casting

Convert between data types.

Example:

int("5")

str(10)

6. Input and Output


input() to take input, print() to display.

Example:

name = input("Name: ")

print("Hello", name)

7. Operators

Arithmetic: + - * / % // **

Comparison: == != > < >= <=

Logical: and, or, not

8. Conditional Statements (if, elif, else)

age = 20

if age >= 18:

print("Adult")

elif age > 12:

print("Teen")

else:

print("Child")

9. Loops (for, while, nested)

for i in range(5): print(i)

i=0

while i < 5: print(i); i += 1

Nested:

for i in range(3):

for j in range(2): print(i,j)

10. do-while Equivalent

while True:

val = int(input())

if val > 0:

break

11. break, continue, pass


for i in range(5):

if i == 2: continue

if i == 4: break

print(i)

12. Functions and Recursion

def greet(name): return "Hello " + name

def fact(n): return 1 if n==0 else n*fact(n-1)

13. Variable Scope (local, global, nonlocal)

x=5

def func():

global x

x = 10

14. Data Structures: list, tuple, dict, set

List: [1,2,3]

Tuple: (1,2)

Dict: {"a": 1}

Set: {1,2,3}

15. List/Dict/Set Comprehension

[x for x in range(5)]

{x: x**2 for x in range(3)}

{x for x in range(3)}

16. Object-Oriented Programming

class Dog:

def __init__(self,name): self.name = name

def bark(self): print("Woof")

Dog("Tommy").bark()

17. Inheritance, super(), Overriding

class A: def show(self): print("A")


class B(A):

def show(self): super().show(); print("B")

18. Encapsulation and Abstraction

Encapsulation hides data: self.__data

Abstraction via abc module.

19. Classmethod, Staticmethod, Dunder Methods

@classmethod def cls_method(cls): ...

@staticmethod def stat(): ...

def __str__(self): return self.name

20. Exception Handling

try: 1/0

except ZeroDivisionError: print("Error")

finally: print("Done")

21. File Handling

with open("file.txt", "w") as f: f.write("Hello")

22. Modules and Libraries

import math, os, sys, random

print(math.sqrt(16))

23. Datetime and JSON

import datetime, json

dt = datetime.datetime.now()

json.dumps({'a':1})

24. Command Line Arguments

import sys

print(sys.argv)

25. Lambda, map, filter, reduce


lambda x: x*2

map(lambda x:x+1, [1,2])

filter(lambda x:x%2==0, [1,2,3])

26. Generators and Decorators

def gen(): yield 1

def deco(f): def wrap(): print("Before"); f(); print("After"); return wrap

27. Virtual Environment

python -m venv env

source env/bin/activate

28. Unit Testing

import unittest

class T(unittest.TestCase):

def test_add(self): self.assertEqual(1+1,2)

29. Debugging with pdb

import pdb; pdb.set_trace()

30. Writing Clean Code (PEP8)

Use snake_case, 4-space indents, proper naming. Use linters like flake8 or black.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy