Python Concepts Explanation
Python Concepts Explanation
Global variables are defined outside any function and can be accessed anywhere in the code. Local
variables are defined inside a function and can only be accessed within that function.
Example:
x = 10 # Global variable
def func():
y = 5 # Local variable
print("Local:", y)
print("Global:", x)
Inheritance allows a class (child) to inherit attributes and methods from another class (parent).
Example:
class Animal:
def sound(self):
print("Animal Sound")
class Dog(Animal):
def bark(self):
print("Bark")
d = Dog()
d.sound()
d.bark()
Example:
name = "Alice"
_age = 25
Example: mylist.append(4)
if num > 0:
print("Positive")
print("Negative")
else:
print("Zero")
NumPy is a Python library used for numerical operations. It supports large, multi-dimensional arrays
Example:
import numpy as np
A module is a file containing Python code (functions, classes). It can be imported using import.
Example (mymodule.py):
def greet(name):
print("Hello", name)
Usage:
import mymodule
mymodule.greet("John")