Python Concepts With Examples Cleaned
Python Concepts With Examples Cleaned
- Variables:
Used to store data.
Example 1: x = 10
Example 2: name = "Python"
- Data Types:
Handle different types of data.
Example 1: type(10) -> int
Example 2: type([1,2,3]) -> list
- Input/Output:
Example 1: input("Enter your name: ")
Example 2: print("Welcome to Python")
- Operators:
Example 1: 10 + 5 -> 15
Example 2: 7 % 2 -> 1
2. CONTROL FLOW
- if-else:
Example 1: if x > 0: print("Positive")
Example 2: if age >= 18: print("Adult") else: print("Minor")
- for loop:
Example 1: for i in range(3): print(i)
Example 2: for item in ['a','b']: print(item)
- while loop:
Example 1: while x < 5: x += 1
Example 2: while True: break
- break/continue:
Example 1: for i in range(5): if i==3: break
Example 2: for i in range(5): if i==2: continue
3. FUNCTIONS
Python Concepts with 2 Examples Each (Basic to Advanced)
- Defining Functions:
Example 1: def add(a,b): return a+b
Example 2: def greet(): print("Hello")
- Built-in Functions:
Example 1: len("Python") -> 6
Example 2: max(3,9,1) -> 9
4. DATA STRUCTURES
- List:
Example 1: [1,2,3].append(4)
Example 2: mylist[0]
- Tuple:
Example 1: t = (1,2); print(t[1])
Example 2: type((1,)) -> tuple
- Dictionary:
Example 1: d = {"a":1}; d["b"]=2
Example 2: d.get("a")
- Set:
Example 1: s = set([1,1,2])
Example 2: s.add(3)
5. OOPS
- Class/Object:
Example 1:
class Car: pass
c = Car()
Example 2:
class Dog:
def bark(self): print("Woof")
- Inheritance:
Example 1:
class Animal: pass
Python Concepts with 2 Examples Each (Basic to Advanced)
- Encapsulation:
Example 1: self.__hidden = "secret"
Example 2: access using getters/setters
- Polymorphism:
Example 1:
def add(x, y): return x + y
Example 2:
class Bird:
def sound(self): print("Chirp")
6. FILE HANDLING
- Reading:
Example 1:
with open("file.txt") as f: print(f.read())
Example 2:
f = open("file.txt", "r"); f.close()
- Writing:
Example 1:
with open("out.txt", "w") as f: f.write("Data")
Example 2:
f = open("log.txt", "a"); f.write("New Line"); f.close()
7. ERROR HANDLING
- try/except:
Example 1:
try: x = 1/0 except: print("Error")
Example 2:
try: int("abc") except ValueError: print("Invalid")
- finally:
Python Concepts with 2 Examples Each (Basic to Advanced)
Example 1:
try: print("run") finally: print("cleanup")
Example 2:
try: x=5 finally: print("Always")
- Import:
Example 1: import math; math.sqrt(16)
Example 2: import os; os.getcwd()
- Custom:
Example 1: import mymodule
Example 2: from utils import helper
9. LIBRARIES
- Pandas:
Example 1: import pandas as pd; pd.read_csv('data.csv')
Example 2: df.head()
- NumPy:
Example 1: import numpy as np; np.array([1,2])
Example 2: np.mean([1,2,3])
- Matplotlib:
Example 1: plt.plot([1,2],[3,4])
Example 2: plt.show()
- Flask:
Example 1: @app.route('/')
Example 2: return render_template('index.html')
10. PROJECTS
- Chatbot:
Example 1: Use NLP for reply prediction
Example 2: Telegram/WhatsApp Bot with Python
Python Concepts with 2 Examples Each (Basic to Advanced)
- Tracker:
Example 1: Use Pandas for logs and summary
Example 2: Automate monthly report with Excel writer
- Resume Screener:
Example 1: Use PDF parsing to read resumes
Example 2: Match skills using keyword search
- Automation Bot:
Example 1: Use Selenium to fill forms
Example 2: Email sender using smtplib