Home - BrunaHeleno/Python GitHub Wiki

Documentation

List of things that I felt were interesting or new to me

Most of the examples below are from "Apostila Python Impressionador" available in a online course https://www.hashtagtreinamentos.com/curso-python. The course is Portuguese (Brazil); therefore, I translated the examples into English

Tuplas

  • Collection of data - unchangeable, ordered (has index), allow duplicates, can contain different data types
  • Lists [ ] Tuplas ( )
sales = [("20/08/2024", "iphone x", "blue", "128g", 350, 4000), 
         ("20/08/2024", "iphone x", "silver", "128g", 1500, 4000), 
         ("20/08/2024", "ipad", "silver", "256g", 127, 6000),
         ("20/08/2024", "ipad", "silver", "128g", 981, 5000), 
         ("21/08/2024", "iphone x", "blue", "128g", 397, 4000), 
         ("21/08/2024", "iphone x", "silver", "128g", 1017, 4000),
         ("21/08/2024", "ipad", "silver", "256g", 50, 6000),
         ("21/08/2024", "ipad", "silver", "128g", 4000, 5000)]

quantity_best_selling = 0

for sale in sales:
    day, product, colour, memory, units, price = sale
    if day == "21/08/2024":
        if units > quantity_best_selling:
            best_selling = product
            quantity_best_selling = units
print ("My best selling product at 21/08/2024 was {} with {} units" .format(best_selling, quantity_best_selling))

Dictionaries

  • Collection of key-value pairs, each key is associated with a value
  • Each key is unique
  • They are mutable
products = ['iphone', 'samsung galaxy', 'tv samsung', 'ps5', 'tablet', 'ipad']
sales = [15000, 12000, 10000, 14300, 1720, 1000]

my_tuplas = zip(products, sales)
dictionary = dict(my_tuplas)
print(dictionary)

Set

  • Collection data type that is unordered, unchangeable, and indexed
  • Unique elements, do not support duplicates
  • Usam { }
products = {'iphone', 'samsung galaxy', 'tv samsung', 'ps5', 'tablet', 'ipad'}

Docstring

  • String literals after the definition of a function, method, class or module
  • They are used to document/explain the code, enclosed within triple quotes
  • doc allows to retrieve the content of the docstrings
def my_function(arg1, arg2, arg3):
    """Explanation about the function
    arg1: does whatever
    arg2: does another thing
    arg3: does this
    return: return that
    """
    
    sum = ar1 + arg2 + arg3
    return sum

Annotation

  • Type hints, simple way to explain the code, it's written inside the code
  • The example shows the type of variables and ->float explains the return value
def my_function(arg1: int, arg2: int, arg3: float) -> float:
    
    return = ar1 + arg2 + arg3

Try-Except

  • Handle errors
def my_function(email):
   try:
      position_a = email.index('@')
   except:
      raise ValueError('Email doesn't contain @, try again') #shows error to user
   else:
      #rest of the code

Function

  • Multiple arguments use * before the variable
def my_function(*scores):
   sum = 0
   for score in scores:
      sum += score
   return sum/(len(scores))
  • Kwargs use ** to indicate that are different keywords
def my_function(price, **additional):
   if 'word' in additional:
      price *= (1 - additional['word'])
   if 'word2' in additional:
      price *= (1 - additional['word2'])
   if 'word3' in additional:
      price *= (1 - additional['word3'])
   return price
  • If a function contain arg and kwargs the order to put them are: simple arg, multiple args, kwarg and multiple kwargs
def my_function(arg1, arg2, *arg3, k = kwarg1, k2 = kwarg2, **kwargs):