Zaawansowane napisy


    {% raw %}
    
    1. f-napisy
    def generate_report(points, threshold):
      if points >= threshold:
          return f"You passed the exam with {points} points."
      else:
          return f"You failed the exam with {points} points."


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    2. Ćwiczenie: True i False
    def check_if_true(x, y):
      result = x > y  # Oblicz wartość wyrażenia x > y
      message = f"The value of the expression x > y is {result}"
      print(message)



- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    3. f-napisy: wyrażenia samodokumentujące
    def document(x, y, z):
      return f"{x=}\n{y=}\n{z=}"

    
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    4. Sekwencja ucieczki dla {}
    def dictstring(name, age):
      user_info = f"{{'name': '{name}', 'age': {age}}}"
      return user_info


    
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    5. Mini-język formatowania: wybór reprezentacji
    def stringify(obj, human_readable=False):
      if human_readable:
          result = f"Result: {obj}"
      else:
          result = f"Result: {obj!r}"
      
      return result


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    6. Mini-język formatowania: wyrównywanie, cz. 1: szerokość
    def pretty_print(report):
      max_name_width = max(len(name) for name in report.keys())
      
      for name, score in report.items():
          print(f"{name:<{max_name_width}} {score}")

    
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    7. Wyrównywanie cz. 2: kierunek
    def rgb_dec_code_disp(colors):
      for color_info in colors:
          color_name, r, g, b = color_info
          print(f"{color_name:<10}|{r:>5}|{g:>5}|{b:>5}")


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    8. Ćwiczenie: Imiona
    def disp_names(names):
      names_sorted = sorted(names, key=len)
      max_width = len(names_sorted[-1])
      for name1, name2 in zip(names_sorted, reversed(names_sorted)):
          print(f"{name1:^{max_width}}|{name2:^{max_width}}")


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    9. Wyrównywanie cz. 3: wypełnienie
    def table_of_contents(chapters):
      for i, page in enumerate(chapters, start=1):
          chapter_line = f"{i:.<5}{page:.>10}"
          print(chapter_line)

          
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    10. Wyrównywanie cz. 4: typy numeryczne
    def pretty_print(indexes):
      for i in indexes:
          print(f"{i:010}")


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    11. Ćwiczenie: odchylenie
    def deviation( readings ):
      avg=sum(readings)/len(readings)
      for i in readings:
          print(f"{i-avg:o=25}")

 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    12. Mini-język formatowania: liczby
    def print_report(previous, current):
      if current > previous:
          growth_percent = ((current - previous) / previous) * 100
          print(f"Growth by {growth_percent:.2f}%.")
      elif current < previous:
          loss_percent = ((previous - current) / previous) * 100
          print(f"Loss of {loss_percent:.2f}%.")
      else:
          print("No change in revenue.")
    
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    13. Ćwiczenie: Wykres słupkowy
    def plot(data):
      for period, profit in data:
          bar = '#' * profit
          print(f"{bar}{profit}, {period}")


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    14. Ćwiczenie: Zamiana RGB na Hex
    def rgb_to_hex(colors):
      for color in colors:
          r, g, b = color
          print(f"{r:02X}{g:02X}{b:02X}")
      

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    15. Formatowanie dat
    def printDate(year, month, day):
      formatted_date = f"Date: {year:04d}-{month:02d}-{day:02d}"
      print(formatted_date)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    16. Podsumowanie kolejności określeń formatu
    def pretty_print(x):
      formatted_x = f"{x:+.4e}"
      print(formatted_x)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    17. Ćwiczenie: Centrowanie liczb
    def pretty_print(x):
      print(f"{x:-^20.5}")
    
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    18. Ćwiczenie: Znaki w jednej kolumnie
    def pretty_print(numbers):
      for number in numbers:
          sign = "+" if number >= 0 else "-"
          number_str = "{:>{width}}".format(abs(number), width=9)
          print(f"{sign}{number_str}")


    
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    19. Metoda str.format()
    def print_rows(numbers):
      for i in range(0, len(numbers), 2):
          # Formatuj liczby jako procenty z dokładnością do 3 miejsc po przecinku
          formatted_number1 = '{:.3f}%'.format(numbers[i] * 100)
          formatted_number2 = '{:.3f}%'.format(numbers[i + 1] * 100)
          # Wypisz dwie sformatowane liczby w jednej linii
          print('{:<10}{}'.format(formatted_number1, formatted_number2))

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    20. Ćwiczenie: Metoda str.format()
    def fill_in_data(data):
      # Formatuj dane zamówienia i zwróć wypełniony formularz
      formatted_data = "Nr. Zamówienia: {}\nNr. Klienta: {}\nImię: {}\nNazwisko: {}".format(
          data['order_id'], data['client_id'], data['name'], data['family_name']
      )
      return formatted_data


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    {% endraw %}