Python Glossary serves as a comprehensive reference guide to key terms, concepts, and features in Python programming. Whether you're a beginner looking to build your foundational understanding of Python or an experienced developer seeking to deepen your knowledge, this glossary provides A to Z information on all things Python. Let's dive in!
In this section, we’ll cover a wide range of Python terms, concepts, and features, from basic to advanced.
The assignment operator (=
) is used to assign a value to a variable.
x = 10
10
is assigned to the variable x
.Boolean is a data type that represents true or false values.
x = True
y = False
x
is assigned the value True
, and y
is assigned the value False
.Conditional statements are used to perform different actions based on different conditions.
x = 10
if x > 0:
print("Positive number")
else:
print("Non-positive number")
if
statement checks if x
is greater than 0
, and if it is, it prints “Positive number”; otherwise, it prints “Non-positive number”.A dictionary is a data structure that stores key-value pairs.
my_dict = {'name': 'John', 'age': 30}
my_dict
is a dictionary with keys 'name'
and 'age'
mapped to the values 'John'
and 30
, respectively.Exception handling is the process of responding to and managing errors that occur during the execution of a program.
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
try
block attempts to perform a division operation, and if a ZeroDivisionError
occurs, the except
block catches and handles the exception by printing a message.A function is a block of reusable code that performs a specific task.
def greet(name):
print("Hello, " + name + "!")
greet("Alice")
greet
function takes a parameter name
and prints a greeting message using that name.A generator is a function that generates a sequence of values lazily, one at a time.
def my_generator():
yield 1
yield 2
yield 3
gen = my_generator()
print(next(gen)) # Output: 1
my_generator
is a generator function that yields values 1
, 2
, and 3
when iterated over.A high-order function is a function that takes one or more functions as arguments or returns a function as its result.
def apply(func, x):
return func(x)
def square(x):
return x ** 2
result = apply(square, 5)
print(result) # Output: 25
apply
is a high-order function that takes a function func
and applies it to the argument x
.Inheritance is a mechanism in Python where a class inherits properties and behaviors from another class.
class Animal:
def sound(self):
print("Some sound")
class Dog(Animal):
def sound(self):
print("Woof!")
my_dog = Dog()
my_dog.sound() # Output: Woof!
Dog
class inherits the sound
method from the Animal
class and overrides it with its own implementation.JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate.
import json
data = '{"name": "John", "age": 30}'
parsed_data = json.loads(data)
print(parsed_data['name']) # Output: John
json.loads
function is used to parse a JSON string into a Python dictionary.A keyword is a reserved word in Python that has a special meaning and cannot be used as an identifier.
if True:
print("This is a keyword")
if
is a keyword used to perform conditional execution in Python.A lambda function is an anonymous function defined using the lambda
keyword.
add = lambda x, y: x + y
print(add(3, 5)) # Output: 8
add
is a lambda function that takes two arguments x
and y
and returns their sum.A module is a file containing Python code that can define functions, classes, and variables.
# Module: mymodule.py
def greet(name):
print("Hello, " + name + "!")
json.loads
function is used to parse a JSON string into a Python dictionary.JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate.
import json
data = '{"name": "John", "age": 30}'
parsed_data = json.loads(data)
print(parsed_data['name']) # Output: John
mymodule
containing a greet
function.A namespace is a container that holds a set of names defined in a specific context.
x = 10
def func():
y = 20
print(x) # Accessing global variable
print(y) # Accessing local variable
func()
x
and y
exist in different namespaces: x
is in the global namespace, while y
is in the local namespace of the func
function.An object is an instance of a class that encapsulates data and behavior.
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
my_car = Car("Toyota", "Camry")
print(my_car.make) # Output: Toyota
my_car
is an object of the Car
class, representing a specific car instance with a make and model.A package is a hierarchical directory structure that contains Python modules and other packages.
my_package/
__init__.py
module1.py
module2.py
my_package
containing two modules module1
and module2
.A queue is a data structure that follows the First-In-First-Out (FIFO) principle, where the element added first is the one to be removed first.
from queue import Queue
q = Queue()
q.put(1)
q.put(2)
print(q.get()) # Output: 1
Queue
object is created, and elements are added to it using the put
method. The get
method retrieves and removes the first element from the queue.Recursion is a programming technique where a function calls itself to solve smaller instances of the same problem.
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
print(factorial(5)) # Output: 120
factorial
that calculates the factorial of a number.A set is an unordered collection of unique elements.
my_set = {1, 2, 3}
my_set
is a set containing the elements 1
, 2
, and 3
.A tuple is an ordered and immutable collection of elements.
my_tuple = (1, 2, 3)
my_tuple
is a tuple containing the elements 1
, 2
, and 3
.Unpacking is the process of extracting values from a sequence and assigning them to variables.
my_list = [1, 2, 3]
x, y, z = my_list
print(x, y, z) # Output: 1 2 3
my_list
into variables x
, y
, and z
.A virtual environment is an isolated Python environment that allows you to install and manage dependencies for a project without affecting the system-wide Python installation.
$ python -m venv myenv
$ source myenv/bin/activate
(myenv) $ pip install package_name
myenv
and installing a package within it using pip
.A while loop is used to repeatedly execute a block of code as long as a specified condition is true.
x = 0
while x < 5:
print(x)
x += 1
0
to 4
using a while loop.XML (eXtensible Markup Language) parsing is the process of extracting data from XML documents.
import xml.etree.ElementTree as ET
tree = ET.parse('data.xml')
root = tree.getroot()
for child in root:
print(child.tag, child.attrib)
data.xml
is an XML file that is parsed using the ElementTree
module to extract its elements and attributes.The yield
keyword is used in generator functions to produce a series of values lazily.
def my_generator():
yield 1
yield 2
yield 3
gen = my_generator()
print(next(gen)) # Output: 1
my_generator
is a generator function that yields values 1
, 2
, and 3
when iterated over.The zip
function is used to combine multiple iterables into a single iterable of tuples.
names = ['Alice', 'Bob', 'Charlie']
ages = [30, 25, 35]
for name, age in zip(names, ages):
print(name, age)
zip
function to iterate over names
and ages
simultaneously, printing each name and age pair.This glossary provides a comprehensive reference guide to key Python terms, concepts, and features. Whether you're a beginner looking to build your foundational understanding or an experienced developer seeking to deepen your knowledge, this topic covers all the essentials. Happy Coding!❤️