forked from tdwojak/Python2019
14 KiB
14 KiB
Python: podstawy, cz. 3 (ostatnia)
9 lutego 2019
Kilka uwag do zadania domowego:
def even_elements(list):
output = []
for element in range(len(list)):
if element % 2 == 0:
output.append(list[element])
return output
def even_elements(elems):
output = []
for element in range(len(elems)):
if element % 2 == 0:
output.append(elems[element])
return output
def leet_speak(text):
text2 = ""
i = 0
suma = 0
for e in text:
if text[i] == "e":
text2 = text2 + "3"
i = i + 1
else:
if text[i] == "l":
text2 = text2 + "1"
i = i + 1
else:
if text[i] == "o":
text2 = text2 + "0"
i = i + 1
else:
if text[i] == "t":
text2 = text2 + "7"
i = i + 1
else:
text2 = text2 + text[i]
i = i + 1
return (text2)
def leet_speak(text):
text2 = ""
i = 0
suma = 0
for e in text:
if text[i] == "e":
text2 = text2 + "3"
elif text[i] == "l":
text2 = text2 + "1"
elif text[i] == "o":
text2 = text2 + "0"
elif text[i] == "t":
text2 = text2 + "7"
else:
text2 = text2 + text[i]
i = i + 1
return (text2)
Do tej pory:
- podstawowe typy danych
- pętla
for
i wyrażenie warunkoweif
- listy, słowniki
- funkcje
Wbudowane funkcje w Pythona
elems = [1, 5, 10, 18, 2]
print(min(elems), max(elems), sum(elems))
1 18 36
elems = [1, 5, 10, 18, 2]
for idx, elem in enumerate(elems):
print("Index:", idx, "wartość:", elem)
for idx in range(len(elems)):
elem = elems[idx]
print("Index:", idx, "wartość:", elem)
Index: 0 wartość: 1 Index: 1 wartość: 5 Index: 2 wartość: 10 Index: 3 wartość: 18 Index: 4 wartość: 2 Index: 0 wartość: 1 Index: 1 wartość: 5 Index: 2 wartość: 10 Index: 3 wartość: 18 Index: 4 wartość: 2
ages = [1, 5, 10, 18, 2, 1]
names = ["Alicja", "Beata", "Cecylia", "Danuta", "Eryka"]
for age, name in zip(ages, names):
print(name, "lat", age, ".")
Alicja lat 1 . Beata lat 5 . Cecylia lat 10 . Danuta lat 18 . Eryka lat 2 .
def circle_length(radius):
return 2 * 3.14 * radius
radii = [3, 4, 5, 6]
results = []
for r in radii:
results.append(circle_length(r))
results = list(map(circle_length, radii))
print(results)
[18.84, 25.12, 31.400000000000002, 37.68]
radii = [3, 4, 5, 6]
f = lambda radius: 2 * 3.14 * radius
print(f(2))
results = map(f, radii)
print(list(results))
12.56 [18.84, 25.12, 31.400000000000002, 37.68]
Funkcje lambda
- składnia:
lambda nazwa_zmiennej: zwracana_wartość
code = input("Co mam zrobić?\n")
eval(code)
Co mam zrobić? print("ala ma kota.") ala ma kota.
Listy składane
elems = [10, 50, 40, 30, 100, 19990]
lens = []
for elem in elems:
lens.append(len(str(elem)))
print(lens)
[2, 2, 2, 2, 3, 5]
elems = [10, 50, 40, 30, 100, 19990]
lens = [len(str(elem)) for elem in elems]
print(lens)
[2, 2, 2, 2, 3, 5]
elems = [10, 50, 40, 30, 100, 19990]
lens = [len(str(elem)) for elem in elems
if str(elem)[0] == '1']
print(lens)
[2, 3, 5]
elems = [10, 50, 40, 30, 100, 19990]
lens = [len(str(elem))
if str(elem)[0] == '1' else 0
for elem in elems]
print(lens)
[2, 0, 0, 0, 3, 5]
def times(elem, n=2):
if isinstance(elem, list):
return [times(item, n) for item in elem]
elif isinstance(elem, float) or isinstance(elem, int):
return elem * n
else:
raise ValueError("błąd")
print(times([5, 7, [8, 9.0]]), 2)
[10, 14, [16, 18.0]] 2
Formatowanie napisów
- procentowe formatowanie z Pythona 2
- metoda format
- f-stringi (Python 3.6)
imie = "Jan"
nazwisko = "Kowalski"
wiek = 34
full_name = imie + " " + nazwisko + ", lat " + str(wiek)
full_name = "%s %s, lat %d" % (imie, nazwisko, wiek)
print(full_name)
Jan Kowalski, lat 34
imie = "Jan"
nazwisko = "Kowalski"
wiek = 34
full_name = "{0} {0}, lat {2}".format(imie, nazwisko, wiek)
print(full_name)
full_name = f"{imie} {nazwisko}, lat {wiek}"
print(full_name)
Jan Kowalski, lat 34 Jan Kowalski, lat 34
OOP: Programowanie obiektowe
class Sentence:
pass
sentence = Sentence()
class Sentence:
def __init__(self, text):
self.text = text
sentence = Sentence("This is a little test.")
sent_1 = Sentence("This is a little test.")
sent_2 = Sentence("This is a little test.")
print(sent_1 == sent_2)
print("text: {}".format(sent_1.text))
False text: This is a little test.
class Sentence:
def __init__(self, text):
self.text = text
def word_number(self):
return len(self.text.split(' '))
def is_question(self):
return self.text[-1] == "?"
sent = Sentence("To jest test .")
print("Liczba słów: {}".format(sent.word_number()))
Liczba słów: 4
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def raise_salary(self, amount):
self.salary += amount
employee = Employee("Franek", 6000)
employee.raise_salary(500)
print(employee.salary)
6500
class Manager(Employee):
def __init__(self, name, salary, team):
super().__init__(name, salary)
self.team = team
man = Manager("Joanna", 6000, "super-team")