Python Glossary
This glossary provides definitions and examples for common Python terms and concepts, helping you understand the language's key features and usage.
Glossary Terms
- Function: A block of reusable code that performs a specific task. Functions are defined using the
def
keyword. Functions can accept input parameters and return a result.Pythondef greet(name): return "Hello, " + name print(greet("Alice")) # Output: Hello, Alice
- Loop: A programming construct that repeats a block of code. Common loops in Python are
for
andwhile
loops.Python# For loop example for i in range(5): print(i) # Output: 0, 1, 2, 3, 4 # While loop example count = 0 while count < 5: print(count) # Output: 0, 1, 2, 3, 4 count += 1
- List: An ordered collection of items in Python, which can contain elements of different types. Lists are mutable, meaning their elements can be changed.
Python
my_list = [1, "apple", 3.14] my_list[1] = "orange" print(my_list) # Output: [1, "orange", 3.14]
- Tuple: Similar to a list, but immutable. Tuples cannot be modified after they are created, making them useful for data that should not change.
Python
my_tuple = (1, "apple", 3.14) # my_tuple[1] = "orange" # This will raise a TypeError
- Dictionary: A collection of key-value pairs, where each key is unique. Dictionaries are mutable, and keys are used to access the associated values.
Python
my_dict = {"name": "Alice", "age": 25} print(my_dict["name"]) # Output: Alice
- Set: An unordered collection of unique elements. Sets are useful for removing duplicates from a sequence and performing set operations like unions and intersections.
Python
my_set = {1, 2, 3, 3} print(my_set) # Output: {1, 2, 3}
- Module: A file containing Python code that can be imported and used in other Python scripts. Modules help organize and reuse code.
Python
# math_module.py def add(a, b): return a + b # main.py import math_module print(math_module.add(5, 3)) # Output: 8
- Class: A blueprint for creating objects, providing initial values for state (attributes) and implementations of behavior (methods).
Python
class Dog: def __init__(self, name, breed): self.name = name self.breed = breed def bark(self): return "Woof!" my_dog = Dog("Buddy", "Golden Retriever") print(my_dog.bark()) # Output: Woof!
- Object: An instance of a class. Objects can have attributes (variables) and methods (functions) defined by their class.
Python
my_dog = Dog("Buddy", "Golden Retriever") print(my_dog.name) # Output: Buddy
- Exception: An error that occurs during the execution of a program. Python provides a way to handle exceptions gracefully using
try
,except
, andfinally
blocks.Pythontry: result = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero") finally: print("Execution complete")
Additional Terms
- Variable: A named location in memory used to store data. Variables are created by assigning a value to a name.
Python
x = 10 y = "Hello, World!"
- Immutable: An object whose state cannot be modified after it is created. Examples of immutable objects in Python include strings, tuples, and numbers.
Python
text = "hello" # text[0] = "H" # This will raise a TypeError
- Mutable: An object that can be modified after it is created. Examples of mutable objects in Python include lists, dictionaries, and sets.
Python
my_list = [1, 2, 3] my_list[0] = 0 # Now my_list is [0, 2, 3]
Import Links
Here are some useful import links for further reading: