Select Git revision
PITCHME.md 8.76 KiB
- Python3 alapok
- The Zen of Python, by Tim Peters
- Története
- Interpretált
- Miért szeretjük a pythont?
- Tökéletes?!
- Tökéletes?!
- Tökéletes?!
- Note: https://en.wikipedia.org/wiki/N-body_simulation
- Csomagok
- Pip
- Virtualenv
- Flake8
- Black
- Hello World
- Szintaxis
- Változók
- List
- Tuple
- Dictionary
- Set
- Függvények
- Alap beépített fgv-ek
- Ciklusok
- While
- For(each)
- Elágazások
- Osztályok
- HF
- Dekorátor
Python3 alapok
import this
The Zen of Python, by Tim Peters
-
@size[24px](
- Beautiful is better than ugly. ) @size[24px](
- Explicit is better than implicit. ) @size[24px](
- Simple is better than complex. ) @size[24px](
- Complex is better than complicated. ) @size[24px](
- Flat is better than nested. ) @size[24px](
- Sparse is better than dense. ) @size[24px](
- Readability counts. ) @size[24px](
- Special cases aren't special enough to break the rules. ) @size[24px](
- Although practicality beats purity. ) @size[24px](
- Errors should never pass silently. ) @size[24px](
- Unless explicitly silenced. ) @size[24px](
- In the face of ambiguity, refuse the temptation to guess. )
-
@size[24px](
- There should be one-- and preferably only one --obvious way to do it. ) @size[24px](
- Although that way may not be obvious at first unless you're Dutch. ) @size[24px](
- Now is better than never. ) @size[24px](
- Although never is often better than right now. ) @size[24px](
- If the implementation is hard to explain, it's a bad idea. ) @size[24px](
- If the implementation is easy to explain, it may be a good idea. ) @size[24px](
- Namespaces are one honking great idea -- let's do more of those! )
Története
- Guido van Rossum
- 1991-ben jött létre
- Magas szintű programozási nyelv
Note:
- BDFL (Benevolent dictator for life)
- Monty Python, karácsony
- könnyen olvasható, tanulható, gyors fejlesztés
- lassú
Interpretált
- A kódot egy értelmező dolgozza fel
- Nem kell fordítani
- Platformfüggetlen
Note:
Miért szeretjük a pythont?
Note:
- Magasszintű:
- Könnyen tanulható, gyors fejlesztés
- Interpretált:
- Bárhol lefut, nem kell lefordítani
Tökéletes?!
+++
Tökéletes?!
NEM!
+++
Tökéletes?!
- Viszonylag lassú
- Nagy memóriaigény
Note:
+++
https://en.wikipedia.org/wiki/N-body_simulation
Note:Csomagok
- Webfejlesztés:
- Django
- Flusk
- Requests
+++
- IT Security:
- Scapy
- Nmap
+++
- AI:
- TensorFlow
- Keras
+++
- Data Science:
- Pandas
- StatsModels
- NumPy
+++
- GUI:
- PyQt
- Tkinter
- Flexx
+++
- Data visualization
- matplotlib
- Plotly
- geoplotlib Note:
Pip
- Csomagkezelő
- Egy helyen van minden
- Könnyen használható
Note:
- könnyen használható
- C++ ban jóval nehezebb lib import
+++
pip install <package>
Virtualenv
- Saját környezet minden projekthez
Note:
- Dependency, egy csomag több verzió
+++
- Windows:
python -m venv venv
venv\Scripts\activate.bat
+++
- Linux:
python3 -m venv venv
source venv/bin/activate
Flake8
- Linter
- Segít jobb kódot írni:
- Syntax error
- Typo
- Rossz formázás
- pep8(Style Guide)
pip install flake8
flake8 <filename>
Note:
- Style Guide
- átlátható
- egységes
- kevesebb merge conflict
Black
- Atomatikusan formázza a kódot.
pip install black
black <filename>
Hello World
+++
print("Hello World!")
+++
pip install colorama
from colorama import init
from colorama import Fore, Back, Style
init()
print(Fore.RED + "some red text")
print(Back.GREEN + "and with a green background")
print(Style.DIM + "and in dim text")
print(Style.RESET_ALL)
print("back to normal now")
Szintaxis
"""This is the start of the program """
while 1:
print("Spam")
answer = input("Press y for large fries ")
if answer == "y":
print("Large fries with spam, mmmm, yummy ")
continue
answer = input("Had enough yet? ")
if answer == "y":
break
print("Have a ")
print("nice day!") # Bye bye
Note:
- Nincs sorvég jel
- A kód blokkok indentálva vannak nincs kapcsoszárójel
- 4 space
- komment #, '''alam'''
Változók
- number (integer, floating point, complex)
- boolean
- string
- list, tuple, dictionary, ...
+++
a = 2
b = 3.2
d = 2 + 4j
x = False
str = "string"
str2 = str * 2
str3 = str2 + "abc"
print(a ** 3)
print(b / 2)
print(d * 2)
print(x)
print(str)
print(str2)
print(str3)
Output
8
1.6
(4+8j)
False
string
stringstring
stringstringabc
List
+++
- Bármilyen típust tartalmazhat (általában egyforma)
- Az elemek indexelhetőek
- Módosítható(mutable)
+++
list = ["a", "b", "c", 1, 2, 3]
for a in list:
print(a)
list[4] = 22
list.append(50)
del list[3]
print(list[2:])
Output:
a
b
c
1
2
3
['c', 22, 3, 50]
Tuple
+++
- Ugyanaz, mint egy lista
- De nem módosítható(immutable)
+++
tuple = ("a", "b", 3)
item = tuple[2]
for a in tuple:
print(a)
a, b, c = tuple
print(c)
tuple2 = (34, 56, "d")
tuple3 = tuple + tuple2
print(tuple3)
Output:
a
b
3
3
('a', 'b', 3, 34, 56, 'd')
Dictionary
+++
- Kulcs érték pár
- A kulcs egyedi, érték bármi lehet
+++
dict = {"Name": "Zara", "Age": 10, "Class": "First"}
dict["Age"] = 8
for key, value in dict.items():
print(key, " : ", value)
del dict["Name"]
print("Name" in dict)
Output:
Name : Zara
Age : 8
Class : First
False
Set
+++
- Mint a lista, csak minden érték egyszer szerepelhet
- Nem biztos, hogy sorban tárolja
+++
set = {1, 3, 5}
print(1 in set)
print(2 in set)
set.add(2)
set.update([6, 4])
for a in set:
print(a)
Output:
True
False
1
2
3
4
5
6
Függvények
+++
def fibonacci():
if n == 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)
print(fibonacci(10))
Output:
55
+++
- Opciónális paraméter
def fibonacci(n=10):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)
print(fibonacci())
Output:
55
+++
- Tetszőlegesen sok paraméter, list
def minimum(first, *args):
acc = first
for x in args:
if x < acc:
acc = x
return acc
print(minimum(3, 4, 2, -8, 6))
Output:
-8
+++
- Tetszőlegesen sok paraméter, dictionary
def bar(**kwargs):
for key, value in kwargs.items():
print(key, value)
print(bar(Kutya="vau", Cica="miaaau"))
Output:
-8
+++
- Névszerinti paraméterátadás
def print_rectangle(w=0, h=0, x=0, y=0):
print("Rectangle")
print("- size: {}×{}".format(w, h))
print("- position: ({};{})".format(x, y))
print_rectangle(20, 30, 40, 50)
print_rectangle(x=10, w=19, h=25)
Output:
Rectangle
- size: 20×30
- position: (40;50)
Rectangle
- size: 19×25
- position: (10;0)
Alap beépített fgv-ek
dir(__builtins__)
+++
- input() Allowing user input
- len() Returns the length of an object
- abs() Returns the absolute value of a number
- slice() Returns a slice object
- ...
- https://www.w3schools.com/python/python_ref_functions.asp
Ciklusok
While
+++
i = 1
while i < 6:
print(i)
i += 1
Output:
1
2
3
4
5
For(each)
+++
a = [1, 2, 3]
for x in a:
print(x)
print("-")
for x in range(1, 6, 2): # for (int i = 1; i < 6; i += 2)
print(x)
Output:
1
2
3
-
1
3
5
Elágazások
+++
b = False
x = 3
if not b and x <= 6:
print("Hello")
elif b or x > 5:
print("World!")
else:
print("Hello World!")
Output:
Hello
Osztályok
+++
class A:
b = "Foo"
def __init__(self):
self.a = 1
def print(self, str):
for x in range(self.a):
print(self.b, str)
class B(A):
def __init__(self, a):
self.a = a
a = A()
a.print("Fighters")
b = B(5)
b.print(u"\u03C1" + " Fej")
Output
Foo Fighters
Foo ρ Fej
Foo ρ Fej
Foo ρ Fej
Foo ρ Fej
Foo ρ Fej
HF
Dekorátor
+++
def my_decorator(func):
def wrapper():
print("Before")
func()
print("After")
return wrapper
@my_decorator
def hello():
print("Hello")
Output:
Before
Hello
After