Előadás: Damásdi Gábor damasdigabor@caesar.elte.com
Gyakorlat:
Matematikai algoritmusok implementálása
Alapozás későbbi órákhoz(Matematikai algoritmusok és felfedezések II, Algoritmusok tervezése és elemzése, Adatbányászat, Deep learning)
Három fő témakör lesz:
Az előadás anyaga mindig elérhető lesz a honlapon. damasdigabor.web.elte.hu/teaching
def vegsojegy(a,b):
if a==1 or b==1:
return 1
if (a+b)%2==0:
return (a+b)/2
else:
if b>a:
return (a+b+1)/2
else:
return (a+b-1)/2
Tehát a két jegy átlaga, és ha az nem egész, akkor a második beadandó jegye dönt.
.ipynb
print("Hello world!")
Hello world!
Az utolsó parancs visszatérési értéke kiíródik. (Általában)
1 + 3
1 + 4
3 * 2
6
Ez akár egy több értékből álló "tuple" is lehet. (Ahogy sok mindenre, a "tuple" szóra sem találtam jó fordítást. Talán "többes"?)
print("süsü")
2 + 3, 3 + 4, "hello " + "world"
süsü
(5, 7, 'hello world')
Ez itt félkövér
Ez meg dőlt
Ez | meg itt |
---|---|
egy | táblázat |
egy | táblázat |
Még a Latex is működik:
Speciális parancsok, amik módosítják a cella működését.
%%time
for x in range(1000000):
pass
Wall time: 27.9 ms
%%timeit
x = 2
10.3 ns ± 0.147 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
%%writefile hello.py
print("Hello world xyxcxy")
Writing hello.py
!python hello.py
Hello world xyxcxy
Az elérhető mágiák listája:
%lsmagic
Available line magics: %alias %alias_magic %autoawait %autocall %automagic %autosave %bookmark %cd %clear %cls %colors %conda %config %connect_info %copy %ddir %debug %dhist %dirs %doctest_mode %echo %ed %edit %env %gui %hist %history %killbgscripts %ldir %less %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %macro %magic %matplotlib %mkdir %more %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %pip %popd %pprint %precision %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %ren %rep %rerun %reset %reset_selective %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode Available cell magics: %%! %%HTML %%SVG %%bash %%capture %%cmd %%debug %%file %%html %%javascript %%js %%latex %%markdown %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile Automagic is ON, % prefix IS NOT needed for line magics.
help()
Welcome to Python 3.7's help utility! If this is your first time using Python, you should definitely check out the tutorial on the Internet at https://docs.python.org/3.7/tutorial/. Enter the name of any module, keyword, or topic to get help on writing Python programs and using Python modules. To quit this help utility and return to the interpreter, just type "quit". To get a list of available modules, keywords, symbols, or topics, type "modules", "keywords", "symbols", or "topics". Each module also comes with a one-line summary of what it does; to list the modules whose name or summary contain a given string such as "spam", type "modules spam". help> exit No Python documentation found for 'exit'. Use help() to get the interactive help utility. Use help(str) for help on the str class. help> exit() No Python documentation found for 'exit()'. Use help() to get the interactive help utility. Use help(str) for help on the str class. help> quit You are now leaving help and returning to the Python interpreter. If you want to ask for help on a particular object directly from the interpreter, you can type "help(object)". Executing "help('string')" has the same effect as typing a particular string at the help> prompt.
Kernel -> Restart & Run All
parancsot mielőtt beadnád a házifeladatot/beadandót, hogy megbizonyosodj róla, hogy minden rendben működikprint("Ez fut először asd")
Ez fut először asd
print("Aztán meg ez. Figyeld a számot bal oldalt.")
Aztán meg ez. Figyeld a számot bal oldalt.
a=3
a
mutasson a 2 értékre.
a+a
6
a
még mindig a 2-re mutat
42
42
_
42
Utolsó előtti output:
"first"
'first'
"second"
'second'
__
'first'
__
'second'
Utolsó előtti előtti:
___
'second'
Az n. output is elérhető a _output_count
változón keresztül. Ez csak akkor definiált ha az n. cellának volt kimenete.
Itt egy módszer az összes elérhető output listázására. (A kódot majd később megérted)
list(filter(lambda x: x.startswith('_') and
x[1:].isdigit(), globals()))
['_3', '_4', '_9', '_14', '_15', '_16', '_17', '_18', '_19', '_20', '_21']
Előző input:
_i
"list(filter(lambda x: x.startswith('_') and \n x[1:].isdigit(), globals()))"
N. input:
_i2
'print("Hello world!") '
import wikipedia
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) <ipython-input-25-743b025fe736> in <module> ----> 1 import wikipedia ModuleNotFoundError: No module named 'wikipedia'
wikipedia.search('Budapest')
import antigravity
from IPython.display import HTML
HTML('<iframe width="900" height="600" src="https://www.youtube.com/embed/NoJQHwylvew" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>')
c:\users\user\anaconda3\envs\clean2021\lib\site-packages\IPython\core\display.py:717: UserWarning: Consider using IPython.display.IFrame instead warnings.warn("Consider using IPython.display.IFrame instead")
Valójában sok nyelv használ vegyes stratégiát, így a python is. Mi főleg interpretált nyelvként fogjuk használni.
n = 11
if n % 2 == 0:
print("n páros")
print("n páros")
print("n páros")
else:
print("n páratlan")
print('alma')
if 2==0:
pass
else:
print('citrom')
print('körte')
n páratlan alma citrom körte
n = 2
print(type(n))
n = 2.1
print(type(n))
n = "foo"
print(type(n))
<class 'int'> <class 'float'> <class 'str'>