PYTHON NOTES
Detailed Explanation
Term Functionality
The add() method is used to add a single element to a
Adds an element to a set in set. If the element already exists, it does not add it
add()
Python. again since sets contain only unique elements.
The append() method adds a new item to the end of a
Adds an element to the end of a list. It modifies the original list in place and does not
append()
list in Python. return a new list.
def is a keyword that begins a function definition. The
Used to define a function in code block indented under it will be executed when the
def
Python. function is called with any required parameters.
The else: block is executed if the condition in an if
Defines a block of code to execute statement evaluates to False. It provides an alternative
else:
if the if condition is False. set of instructions when the if condition is not met.
enumerate() is used to loop over an iterable and
retrieve both the index (counter) and the value from the
Adds a counter to an iterable and
enumerate() iterable. It helps when you need both index and value
returns it as an enumerate object.
in a loop.
f is typically used as a shorthand variable for files
when working with file operations, like open(). It can
Commonly used as a variable for
f also represent functions or other objects in various
file objects or function aliases.
contexts.
if is used to make decisions in the program. If the
A conditional statement used to
condition provided evaluates to True, the following
if execute code only if a specified
indented block of code is executed.
condition is True.
import is used to include external libraries or modules
Brings external modules or so that their functions and classes can be used in the
import
libraries into your Python script. current script. Without importing, you can't use them.
The input() function pauses the program and waits for
Takes user input from the console user input. The input is returned as a string, even if the
input()
and returns it as a string. user enters numbers or other data types.
int() is used to convert a value (usually a string or
float) into an integer. If the value cannot be converted
(e.g., "abc"), it raises a ValueError.
int() Converts a value to an integer.
Detailed Explanation
Term Functionality
The in operator is used to check membership. It returns
Checks if a value exists in an
True if the value is found in the iterable (e.g., a list or
in iterable (e.g., list, string,
string), otherwise False.
dictionary).
len() is used to find the length of a collection, such as
the number of elements in a list, the number of
Returns the number of items in an
len() characters in a string, or the number of keys in a
object like a list, string, or tuple.
dictionary.
While not mandatory, it's a convention in many Python
Conventionally used as the entry
programs to use a main() function to group the core
main() point of a Python script or
logic. This function is called at the start of the script.
program.
not is used to reverse the truth value of a condition. If
A logical operator that negates a the condition is True, not makes it False, and if it's
not
boolean value. False, it becomes True.
open() is used to open a file in Python. The mode (e.g.,
Opens a file and returns a file 'r' for read, 'w' for write) specifies how the file will be
open()
object for reading or writing. handled, and it returns a file object for interaction.
pop() removes and returns an element from a list or
dictionary. In lists, it removes the last item by default
Removes and returns an element
pop() or an item at a specific index, and in dictionaries, it
from a list or dictionary.
removes a key-value pair.
print() is used to display text or values to the console
Outputs the specified message or or terminal. It's often used for debugging or showing
print()
value to the console. information to users.
readlines() is used to read all lines from a file and
Reads all lines from a file and returns them as a list of strings, with each string
readlines()
returns them as a list of strings. representing a line from the file.
remove() searches for and removes the first occurrence
Removes the first occurrence of a of the given element from the list. If the element is not
remove()
specified element from a list. found, it raises a ValueError.
return is used to send a result from a function back to
Sends back a value from a the caller, which can be used in expressions or stored
return
function to the caller. in variables.
strip() removes spaces, tabs, and newline characters
from both ends of a string. You can also specify
Removes leading and trailing
characters to remove.
strip() whitespace characters (or
specified characters) from a string.
Detailed Explanation
Term Functionality
The with statement manages resources like files or
Used to simplify resource
network connections. It automatically handles opening
management, such as opening
with and closing resources, even if errors occur in the code
files, ensuring they are properly
block.
closed.
Curly braces { } are used to define sets (unordered
Used to define dictionaries and collections of unique elements) and dictionaries
{}
sets in Python. (collections of key-value pairs).
Square brackets [ ] are used to define lists, which are
ordered collections of items that can be modified. Lists
[] Used to define lists in Python.
support indexing, slicing, and various methods.
Parentheses ( ) are used to call functions, group
Used to group expressions, call expressions for precedence, or define tuples
()
functions, or define tuples. (immutable ordered collections of items).
The else: block is executed if none of the preceding if
Executes the block of code when or elif conditions are true, providing an alternative set
else:
the if condition is not met. of instructions to run.
as is used to alias a module (e.g., import numpy as np)
Used to create an alias for a or to rename a file object within a with statement (e.g.,
as
module or resource. with open('file.txt') as f).