Functions:
- Function: A block of organized, reusable code that performs a specific task.
def greet(name): return "Hello, " + name
- Function Call: Executing a function to perform its defined task
message = greet("Alice")
- Arguments: Values passed to a function when it’s called.
def add(x, y): return x + y result = add(5, 3) # Result is 8
- Parameters: Variables in a function definition that receive arguments.
def multiply(a, b): return a * b
- Return Statement: Defines what a function should return.
def square(x): return x * x
- Default Arguments: Arguments with predefined values.
def power(base, exponent=2): return base ** exponent
- Variable Scope: The part of the code where a variable can be accessed.
def my_func(): x = 10 # Local variable
- Global Variable: A variable defined outside any function and accessible everywhere.
global_var = 100 def access_global(): print(global_var)
- Lambda Function: An anonymous function defined using the
lambda
keyword.double = lambda x: x * 2
Data Types:
- Integer: Whole numbers without decimal points.
age = 25
- Float: Numbers with decimal points.
pi = 3.14
- String: A sequence of characters.
message = "Hello, Python"
- Boolean: Represents truth values
True
orFalse
.-
is_valid = True
-
- List: An ordered collection of elements.
numbers = [1, 2, 3, 4, 5]
- Tuple: Similar to a list but immutable.
coordinates = (3, 5)
- Dictionary: A collection of key-value pairs.
student = {"name": "Alice", "age": 20, "grade": "A"}
- Set: An unordered collection of unique elements.
unique_numbers = {1, 2, 3, 4, 5}
- DataFrame: A 2-dimensional labeled data structure in pandas.
import pandas as pd data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]} df = pd.DataFrame(data)
- Indexing: Accessing elements in a data structure using indices.
first_name = student["name"]
- Slicing: Extracting a subset of elements from a sequence.
sublist = numbers[1:4]
- Appending: Adding an element to the end of a list.
numbers.append(6)
- Updating: Modifying elements in a list.
numbers[0] = 0
- Keys: The unique identifiers in a dictionary.
keys = student.keys()
- Values: The data associated with the keys in a dictionary.
values = student.values()
- DataFrame Columns: Named series within a DataFrame.
ages = df['Age']
- DataFrame Rows: Individual records in a DataFrame.
row = df.iloc[0] # First row
- Concatenation: Joining two or more sequences together.
combined_list = numbers + unique_numbers
- Sorting: Arranging elements in a specific order.
sorted_list = sorted(numbers)
- List Comprehension: Creating a new list using concise syntax.
squares = [x ** 2 for x in numbers]
- Tuple Packing and Unpacking: Assigning multiple values in a single operation.
x, y = coordinates
- Key-Value Pair Manipulation: Adding, updating, or deleting entries in a dictionary.
student["grade"] = "A+"
- Set Operations: Union, intersection, and difference between sets.
common_numbers = numbers & unique_numbers
- DataFrame Operations: Filtering, grouping, and aggregating data in a DataFrame.
filtered_df = df[df['Age'] > 25]
- Data Type Conversion: Changing one data type to another.
num_str = str(42) # Convert to string
- Membership Testing: Checking if an element is present in a sequence.
is_present = 3 in numbers
- Dictionary Iteration: Looping through keys, values, or items in a dictionary.
for key, value in student.items(): print(key, value)
- List Length: Counting the number of elements in a list.
num_elements = len(numbers)
- Tuple Immutability: Preventing modification of tuple elements.
coordinates[0] = 4 # Error
- Dictionary Keys Immutability: Using immutable objects as dictionary keys.
immutable_dict = {(1, 2): 'value'}
- Index Out of Range Handling: Avoiding accessing elements beyond the list’s length.
if index < len(numbers): element = numbers[index]
- List Appending and Extending: Adding elements from another sequence to a list.
new_numbers = [6, 7, 8] numbers.extend(new_numbers)
- List Deletion: Removing elements by index or value.
del numbers[0] # Delete by index numbers.remove(5) # Delete by value
- DataFrame Filtering: Selecting rows based on certain conditions.
adults_df = df[df['Age'] >= 18]
- DataFrame Grouping: Grouping data by a certain column.
grouped = df.groupby('Age') ``