Skip to content

Instantly share code, notes, and snippets.

@marcin119a
Last active October 27, 2019 10:29
Show Gist options
  • Save marcin119a/e8aa8fef38b223d1aab7d10382d4a8cf to your computer and use it in GitHub Desktop.
Save marcin119a/e8aa8fef38b223d1aab7d10382d4a8cf to your computer and use it in GitHub Desktop.
zajecia2

Podstawowe typy danych języka Python. __(1 dzień) ___

  1. Model danych języka Python.

    1. List of immutable types:**

      int, float, decimal, complex, bool, string, tuple, range, frozenset, bytes

      List of mutable types:

      list, dict, set, bytearray, user-defined classes
  2. Typy liczbowe, typy logiczne, typ None.

    • Zmienno pozycyjne
    • @ZADANIE Liczenie PPM
    • @ZADANIE Liczenie BMI
    • @Zadanie Temperatura
  3. Typy sekwencyjne:

    1. Zmienne: listy.

      1. l = [1,2,3,4]
        l[:2] # [1, 2]
      2. [1,2,3] + [3,12,2] # [1, 2, 3, 3, 12, 2]
      3. @BEST practices: https://www.youtube.com/watch?v=cowtgmZuai0dict

        1. # wcięcie kodu 
          my_movies = ['How I Met Your Mother',
                       'Friends',
                       'Silicon Valley', 
                       'Family Guy',
                       'South Park']
            
   2. 
      
- @ZADANIE policz średnią arytmetyczną
      
      - ```python
        sum(a)/len(a)
  - aproach 2:
  
  - ```python
    from statistics import mean
    lista = [1,3,5,6,7,-10,20,100,-12]
    lista.sort()
    print(lista)
    
    wynik = 0
    for x in lista:
        wynik += x
    print(f"suma = {wynik}")
    
    print("suma = {}".format(sum(lista)))
    print("max = {}".format(max(lista)))
    print("min = {}".format(min(lista)))
    print("srednia arytm. = {}".format(wynik/len(lista)))
    print("srednia arytm. = {}".format(statistics.mean(lista)))
    print("mediana = {}".format(statistics.median(lista)))
    print(sorted(lista, reverse=True))
    print(lista[::-1])
    
    ```

​ Zabawy z listami ​

lista = []
pprint(type(lista))
###### dodawanie elementow
lista.append("Ala ma kota")
lista.append(True)
lista.append(-12.34567)
lista.append(True)
lista.append(-12.34567)
print(lista)
obliczanie dlugosci listy
print("Dlugosc list = {}".format(len(lista)))
###### ilosc wystapien elementu
```python

print("Ile razy jest True w liscie = {}".format( lista.count(True) )) print("Ile jest a w pierwszym elemencie listy - {}".format( lista[0].count("a"))) ```

###### wyszukiwanie w liscie
```python

print("Pierwsze wystapienie wartosc to index = {}".format( lista.index(-12.34567) )) ```

###### rozszerzanie listy
lista.extend([-1,-1]); print(lista)
###### usuwanie z listy
lista.pop(1); print(lista)
###### wstawianie do listy
```python

lista.insert(2, 100); print(lista) ```

###### czyszczenie listy
 lista.clear(); print(lista)
  • wyjasnienie z

    a = [1,2,3]
    b = a
    a.append() 
    # copy nie ma w python2
    x = 2
    x = [1,2,3]
    y = x.copy()
    
  • Zaznaczenie_042

  1. Niezmienne: krotki, łańcuchy znaków.

    • a, b = b, a

    • vs

    • a = b, b = a

    • @ZADANIE NA PALINDROM:

      print("Czy palindrom?")
      
      tekst = input("Podaj wyraz: ")
      bez_spacji = tekst.replace(" ","").lower().strip()
      if bez_spacji==bez_spacji[::-1]:
          print("To jest palindrom")
      else:
          print("To nie jest palindrom")
  2. Słowniki.

    1. **https://stackoverflow.com/questions/1247486/list-comprehension-vs-map
    • dict are key-value storage

    • Zmienne - can add, remove, and modify items

      my_dict = {
          'first_name': 'Jan',
          'last_name': 'Twardowski'
      }
      my_dict = dict(
          first_name='Jan',
          last_name='Twardowski'
      )
      #Duplicating items are overridden by latter
      my_dict = {
          'name': 'Twardowski',
          'name': 'Иванович',
      }
  3. Zbiory.

    • some_list = ['a', 'b', 'c', 'b', 'd', 'm', 'n', 'n']
      duplicates = set([x for x in some_list if some_list.count(x) > 1])
      print(duplicates)
    • https://stackoverflow.com/questions/14535730/what-does-hashable-mean-in-python

    • Róznica między & and and

      • w mnożeniu binarnym

      • A bitwise AND is a binary operation that takes two equal-length binary representations and performs the logical AND operation on each pair of the corresponding bits, which is equivalent to multiplying them. Thus, if both bits in the compared position are 1, the bit in the resulting binary representation is 1 (1 × 1 = 1); otherwise, the result is 0 (1 × 0 = 0 and 0 × 0 = 0). For example:

            0101 (decimal 5)
        AND 0011 (decimal 3)
          = 0001 (decimal 1)
        
      • OR

        Bitwise OR of 4-bit integers

        A bitwise OR is a binary operation that takes two bit patterns of equal length and performs the logical inclusive OR operation on each pair of corresponding bits. The result in each position is 0 if both bits are 0, while otherwise the result is 1. For example:

           0101 (decimal 5)
        OR 0011 (decimal 3)
         = 0111 (decimal 7)
        
      • W odniesieniu do set operations, the & operator jest traktowany jako odpowiednik intersection()

      • and i nadal jest tylko funkcją logicznego porównywania i będzie traktować ustawiony argument jako wartość niefałszywą. Zwróci również ostatnią wartość, jeśli żaden z argumentów nie jest fałszywy:

    • @ ZADANIE Imiona, które są w przynajmniej w jednym ze zbiorów A, B i C (czyli zwykła suma);

      • a | b | c
        
    • @ZADANIE Imiona, które są jednocześnie w zbiorach A, B i C;
    • @ZADANIE Imiona, które są albo w zbiorze A, albo w sumie zbiorów B i C.
    {"marcin","jurek"} & {"jan","marcin"}
  • # Obiekt jest hashable, jeśli ma wartość hash, która nigdy nie zmienia się podczas jego życia (potrzebuje metody hash()).
    my_set = {1, 2.0, [3, 4]}
    # TypeError: unhashable type: 'list'
    
    my_set = {1, 2.0, {3, 4}}
    # TypeError: unhashable type: 'set'
  1. Dynamiczne typowanie oraz duck-typing w języku Python.

    @TODO ciekawe zadania

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment