Docsity

Python notatki dla nieinformatyków, Notatki z Informatyka

Toggle sidebar
bg1
1. \n
Do następnej linijki
2. \t
Tab
3. a = 1
str(a) konwertuje to co jest w nawiasie na stringa (czyli tutaj będzie to 1)
4. greetings = ‘’Greetings”
print("greetings = " + str(greetings))
greetings = Greetings
5. len(Word) – liczy ilość liter w słowie
6. print(f"Decimal part: {decimal:.2f}")
wtedy program wypisuje liczbę z dwoma cyframi po przecinku
7. score += 100 to to samo co score = score + 100
8. == to porównanie ze sobą dwóch zmiennych
9. the_count = list(input("the_count=").split(','))
tworzy listę I suposse
10. for number in the_count:
print(f"This is count {number}")
This is count 1
This is count 2 (czyli jakby dla każdego Mount wypisuje
to)
11. for i in range(2): (wtedy dodaje liczby 0 i 1
12. fruits = ['apple', 'banana', 'cherry']
fruits.append("orange") #wynikiem jest to, ze dodaje Orange do
listy
13. numbers = [] #tak się ustala tablice
14. i = 0, jeśli chcesz, żeby wskaźnik zacynal się od zera i szedł wyżej
15. while True #pętlę powtarza tak długo dopóki jest prawdziwa
16. if __name__ == "__main__": #wywołanie funkcji
17. int(a) bierze int z tego a
18. try:
else:
raise ValueError
except ValueError:
print("That must be a mistake") #to jest dla
użytkownika idioty w zadaniach
19. story = "" ??????????????????????? chyba ważne
FUNKCJE
20. kwarg to value (keyword arguments)
21. *args kiedy nie wiesz, ile będzie argumentow => będzie tuple
22. **kwargs kiedy nie wiesz, ile kwargów będzie => będzie słownik
23. A[::-1] wtedy wypisuje ten wyraz na odwrót (czyli na przykład a = ‘1234’ to mamy ‘4321’)
pf3
pf4
pf5

Podgląd częściowego tekstu

Pobierz Python notatki dla nieinformatyków i więcej Notatki w PDF z Informatyka tylko na Docsity!

  1. \n Do następnej linijki
  2. \t Tab
  3. a = 1 str(a) konwertuje to co jest w nawiasie na stringa (czyli tutaj będzie to 1)
  4. greetings = ‘’Greetings” print("greetings = " + str(greetings)) greetings = Greetings
  5. len(Word) – liczy ilość liter w słowie
  6. print(f"Decimal part: {decimal:.2f}") wtedy program wypisuje liczbę z dwoma cyframi po przecinku
  7. score += 100 to to samo co score = score + 100
  8. == to porównanie ze sobą dwóch zmiennych
  9. the_count = list(input("the_count=").split(',')) tworzy listę I suposse
  10. for number in the_count: print(f"This is count {number}") This is count 1 This is count 2 (czyli jakby dla każdego Mount wypisuje to)
  11. for i in range( 2 ): (wtedy dodaje liczby 0 i 1
  12. fruits = ['apple', 'banana', 'cherry'] fruits.append("orange") #wynikiem jest to, ze dodaje Orange do listy
  13. numbers = [] #tak się ustala tablice
  14. i = 0, jeśli chcesz, żeby wskaźnik zacynal się od zera i szedł wyżej
  15. while True #pętlę powtarza tak długo dopóki jest prawdziwa
  16. if name == "main": #wywołanie funkcji
  17. int(a) bierze int z tego a
  18. try: else: raise ValueError except ValueError: print("That must be a mistake") #to jest dla użytkownika idioty w zadaniach
  19. story = "" ??????????????????????? chyba ważne

FUNKCJE

  1. kwarg to value (keyword arguments)
  2. *args kiedy nie wiesz, ile będzie argumentow => będzie tuple
  3. **kwargs kiedy nie wiesz, ile kwargów będzie => będzie słownik
  4. A[::-1] wtedy wypisuje ten wyraz na odwrót (czyli na przykład a = ‘1234’ to mamy ‘4321’)
  1. list3.append(x) #dodaje x na koncu list3 (jak lista jest pusta to ja wypelnia po prostu)
  2. s[offset:] + s[:offset] #łączenie dwóch zbiorów (od offset do konca i od początku do offset)

LISTY

  1. print(thislist[ 2 : 5 ]) The search will start at index 2 (included) and end at index 5 (not included).
  2. thislist[: 4 ] #it does not include the 4th value
  3. zapisujemy z nawiasem [], np. mylist = ["apple", "banana"]
  4. The insert() method inserts an item at the specified index: thislist.insert( 2 , "watermelon") #wtedy to co dajesz jest drugim elementem
  5. To add an item to the end of the list, use the append() method:
  6. To append elements from another list to the current list, use the extend() method. #wtedy do naszej aktualnej listy dodaje się cała inna lista na końcu.
  7. The extend() method does not have to append lists, you can add any iterable object (tuples, sets, dictionaries etc.). #czyli w lisatch można łączyć typu
  8. If there are more than one item with the specified value, the remove() method removes the first occurrence: #a jak jest jedno to to usuwa po prostu
  9. The pop() method removes the specified index. #np thislist.pop(1), a bez indeksu usuwa ostatni
  10. del thislist[ 0 ] #usuwa konkretny indeks, a można też cała listę usunąc tak
  11. thislist.clear() #zostaje pusta lista
  12. thislist = ["apple", "banana", "cherry"] for x in thislist: print(x) to wydrukuje się takie coś albo tak można napisać też thislist = ["apple", "banana", "cherry"] i = 0 while i < len(thislist): print(thislist[i]) i = i + 1 thislist = ["apple", "banana", "cherry"] [print(x) for x in thislist]

split() Splits the string at the specified separator, and returns a list title() Converts the first character of each word to up translate() Returns a translated string

  1. x**y to jak x do potęgi y
  2. map(function, iterables)
  3. my_list = list(map(int,input("my_list = ").split('- '))) #to robi z ciągu liczb podanych z myślikami listę z tych liczb

TUPLES

  1. nawias (), np. mytuple = ("apple", "banana", "cherry")
  2. można dodawać tuple do tuple, albo można Zamienic tuple na listę dodać coś i znowu na tuple,
  3. If the number of variables is less than the number of values, you can add an * to the variable name and the values will be assigned to the variable as a list:
  4. index() – indeks danego elementu, count() – ilość elementow w tuple
  5. W sumie podobne do list

SETS

  1. A set is a collection which is unordered, unchangeable*, and unindexed, np. myset = {"apple", "banana", "cherry"}.
  2. Nie ma duplikwatów w secie
  3. set() – tak można zrobić set
  4. To add one item to a set use the add() method.: set.add(‘coś’) i się gdzie randomowo doda
  5. To add items from another set into the current set, use the update() method., set1.update(set2), set2 to może być tez lista
  6. remove(), discard(), pop() – cokolwiek się nam usunie, del i clear też działają
  7. set3 = set1.union(set2), set3 = set1 | set2, myset = set1.union(set2, set3, set4), union pozwala łączenie nie tylko sets
  1. set3 = set1.intersection(set2) #tylko powielenie zostaje & też tak działa, ale tylko dla setów
  2. set3 = set1.difference(set2) ,set3 = set1 - set2 #różnica setów
  3. set3 = set1.symmetric_difference(set2), set3 = set1 ^ set #różnica symetryczna

DICTIONARIES

  1. thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 }
  2. key:value pairs. A dictionary is a collection which is ordered*, changeable and do not allow duplicates.
  3. thisdict = dict(name = "John", age = 36 , country = "Norway") print(thisdict) #tak konstruujemy słownik
  4. x = thisdict["model"] , x = thisdict.get("model") #jak chcesz jakąś konkretną wartość ze słownika to piszesz tak key Word
  5. x = thisdict.keys() #wszytskie klucze ze słownika (values, items)
  6. car["color"] = "red" #tak się dodaje coś do słownika
  7. thisdict.update({"year": 2020 }) #updatujemy kochani
  8. thisdict.pop("model") #usuwamy dany cuś
  9. mydict = dict(thisdict) #powielanie słownika