Youssef Samir

Get in touch

Python

Dive into Python, a versatile programming language widely used for web development, data science, automation, and more.

Lists

  • append(item): Adds item to the end of the list.
  • insert(index, item): Inserts item at the specified index.
  • pop(index=-1): Removes and returns the item at index. Default is the last item.
  • remove(item): Removes the first occurrence of item.
  • clear(): Removes all items from the list.
  • sort(reverse=False, key=None): Sorts the list in place.
  • slicing: list[start:end:step] returns a sliced portion of the list.
  • list comprehension: [expression for item in iterable if condition]

Tuples

  • index(item): Returns the index of the first occurrence of item.
  • count(item): Returns the number of occurrences of item.
  • unpacking: a, b, c = my_tuple

Dictionary

  • pop(key, default=None): Removes and returns the value of key.
  • popitem(): Removes and returns the last (key, value) pair.
  • keys(): Returns a view object of dictionary keys.
  • values(): Returns a view object of dictionary values.
  • items(): Returns a view object of (key, value) pairs.
  • copy(): Returns a shallow copy of the dictionary.
  • update(other_dict): Updates the dictionary with elements from another dictionary.

Set

  • add(item): Adds item to the set.
  • remove(item): Removes item from the set. Raises KeyError if not found.
  • discard(item): Removes item from the set if present.
  • union(other_set): Returns a set containing all elements from both sets.
  • intersection(other_set): Returns a set with elements common to both sets.
  • difference(other_set): Returns a set with elements in the current set but not in other_set.
  • update(other_set): Updates the set with elements from other_set.

String

  • strip(): Removes leading and trailing whitespace.
  • upper(): Converts all characters to uppercase.
  • lower(): Converts all characters to lowercase.
  • startswith(prefix): Returns True if the string starts with the specified prefix.
  • endswith(suffix): Returns True if the string ends with the specified suffix.
  • find(substring): Returns the lowest index of the substring. Returns -1 if not found.
  • count(substring): Returns the number of occurrences of substring.
  • replace(old, new): Replaces all occurrences of old with new.
  • split(delimiter): Splits the string into a list using delimiter.
  • join(iterable): Joins elements of an iterable into a string, separated by the string.

Functions

  • len(): Returns the length of an object.
  • type(): Returns the type of an object.
  • tuple(): Converts an iterable to a tuple.
  • copy(): Returns a shallow copy of an object.
  • list(): Converts an iterable to a list.
  • dict(): Creates a new dictionary.
  • sys.getsizeof(): Returns the size of an object in bytes.
  • del: Deletes an object.
  • set(): Creates a new set.

Collections

  • Counter: A dict subclass for counting hashable objects.
  • namedtuple(): Returns a tuple subclass with named fields.
  • OrderedDict: A dict subclass that maintains the order of items.
  • defaultdict: A dict subclass that returns a default value for missing keys.
  • deque: A double-ended queue optimized for fast appends and pops.

Lambda

  • lambda arguments: expression: Anonymous function.

Map Function

  • map(function, sequence): Applies function to every item of sequence and returns an iterator.

Filter Function

  • filter(function, sequence): Filters elements in sequence for which function returns True.

Exceptions

  • raise: Raises an exception.
  • assert: Tests an expression, raises AssertionError if false.
  • try...except...finally: Handles exceptions.
  • else block: Executes if the try block does not raise an exception.
  • exception classes: Custom exceptions by inheriting from Exception.

Logging

  • basicConfig(): Configures the logging system.
  • debug(): Logs a message with level DEBUG.
  • info(): Logs a message with level INFO.
  • warning(): Logs a message with level WARNING.
  • error(): Logs a message with level ERROR.
  • critical(): Logs a message with level CRITICAL.
  • getLogger(name): Returns a logger with the specified name.
  • StreamHandler(): Logs to the console.
  • FileHandler(filename): Logs to a file.
  • formatter: Defines the log message format.
  • addHandler(handler): Adds a handler to the logger.
  • logging.conf file: Configuration file for logging.
  • traceback.format_exc(): Returns a string with the traceback of the last exception.
  • RotatingFileHandler: Logs to a file, rotating logs when the file reaches a certain size.
  • TimedRotatingFileHandler: Rotates logs at a set interval.

JSON

  • dump(obj, file, ...): Serializes obj to a file-like object.
  • dumps(obj, ...): Serializes obj to a JSON-formatted string.
  • load(file, ...): Deserializes JSON from a file-like object.
  • loads(s, ...): Deserializes JSON from a string.

Random

  • random(): Returns a random float between 0.0 and 1.0.
  • uniform(a, b): Returns a random float between a and b.
  • randint(a, b): Returns a random integer between a and b.
  • randrange(start, stop, step): Returns a randomly selected element from range(start, stop, step).
  • sample(population, k): Returns a list of k unique elements from population.
  • choices(population, k): Returns a list of k elements from population, allowing repeats.
  • shuffle(sequence): Shuffles the sequence in place.

Secrets

  • randbelow(n): Returns a random integer below n.
  • randbits(k): Returns a random integer with k random bits.
  • choice(sequence): Returns a randomly chosen element from sequence.

Handling Files Reading/Writing

  • open(filename, mode): Opens a file for reading/writing.
  • read(), readline(), readlines(): Reading from a file.
  • write(), writelines(): Writing to a file.

Decorators

  • function decorators: @decorator to modify function behavior.
  • class decorators: @decorator to modify class behavior.

Generators

  • generator functions: Functions that use yield to return values one at a time.
  • generator expressions: (expression for item in iterable if condition)

Function Args and Kwargs

  • *args: Allows passing a variable number of positional arguments.
  • **kwargs: Allows passing a variable number of keyword arguments.