py2nb
This commit is contained in:
1 parent
95d0233676
commit
c95c4b9148
2 files changed
+328
-1
No files matched your search
+102
-1
@@ -93,7 +93,7 @@ that is some values, we also have containers, which contain values
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
my_list = [5, "list", "items", ("with", "different"), type]
|
my_list = [5, "list", "items", ("with", "different"), type]
|
||||||
# type() is a function: you could put literally everthing
|
# type() is a function: you could put literally everything in a list it does not care
|
||||||
|
|
||||||
"""
|
"""
|
||||||
values, including lists. yes, thats powering your 2d matrix.
|
values, including lists. yes, thats powering your 2d matrix.
|
||||||
@@ -396,6 +396,11 @@ for number in one_to_20:
|
|||||||
|
|
||||||
# quick notice: this number variable can not be accessed outside the for loop.
|
# quick notice: this number variable can not be accessed outside the for loop.
|
||||||
|
|
||||||
|
# actually it will be the last element actually i dont know
|
||||||
|
# coming from literally any other language:
|
||||||
|
# a variable made for the loop should only stay in the loop
|
||||||
|
# why is python like this? is this a bug?
|
||||||
|
|
||||||
"""
|
"""
|
||||||
while.
|
while.
|
||||||
|
|
||||||
@@ -435,6 +440,12 @@ while True:
|
|||||||
|
|
||||||
# wtf
|
# wtf
|
||||||
|
|
||||||
|
"""
|
||||||
|
match case
|
||||||
|
|
||||||
|
like switch case but it is match case and it doesnt have fallthroughs
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
errors. you get those when you use a thing and it doesnt run how you want, so it crashes and print:
|
errors. you get those when you use a thing and it doesnt run how you want, so it crashes and print:
|
||||||
@@ -607,5 +618,95 @@ bungee = BungeeJumper(68.1, 0.25)
|
|||||||
bungee.dt = 2
|
bungee.dt = 2
|
||||||
print(bungee.vt_next())
|
print(bungee.vt_next())
|
||||||
|
|
||||||
|
|
||||||
# wow it so works
|
# wow it so works
|
||||||
# trailing off
|
# trailing off
|
||||||
|
|
||||||
|
"""so much over here. a lot of it is boilerplate,
|
||||||
|
only a few: namely vt_step and current_dvdt is doing all the math.
|
||||||
|
|
||||||
|
yes. thats code for you. a lot of structure for idk 5 lines of actual calculation or something idfk
|
||||||
|
"""
|
||||||
|
|
||||||
|
"""
|
||||||
|
Class. The heart of python. literally everything is a class.
|
||||||
|
|
||||||
|
you are either working with a function, which is a class,
|
||||||
|
a data type, which is a class,
|
||||||
|
or like an external module / package, which is (oh no) also a class.
|
||||||
|
|
||||||
|
|
||||||
|
class in python is more lenient than java:
|
||||||
|
|
||||||
|
first up: everything need not be a class:
|
||||||
|
in java you have to declare the main class and the main function.
|
||||||
|
in python they see statement they execute statement. no need for class.
|
||||||
|
|
||||||
|
while java you can only inherit from only one base class, in python
|
||||||
|
you can have multiple base classes, classes that double as interfaces, etc.
|
||||||
|
|
||||||
|
you can also have directly editable attributes.
|
||||||
|
No getter / setter functions. if you want you could make them, like i do, but rn you dont
|
||||||
|
|
||||||
|
you also have no private attributes. i can only hope you wont touch my
|
||||||
|
bungee._BungeeJumper__time and mess it up.
|
||||||
|
|
||||||
|
Class is yk classic OOP: you have class, you have instances of class.
|
||||||
|
class BungeeJumper is a class, bungee is an instance.
|
||||||
|
int is a class, and 1 is an instance.
|
||||||
|
|
||||||
|
|
||||||
|
everywhere else they call a function function, but in a class they call them Methods.
|
||||||
|
|
||||||
|
methods have this kinda thing they call dot notation.
|
||||||
|
basically instance.method is just another function. but somethings going on:
|
||||||
|
|
||||||
|
instance instead of doing nothing will be passed into methods as first argument.
|
||||||
|
yes, not javas `this`. `this` is a set name.
|
||||||
|
python `self` i could change it to whatever. instance will be passed to it.
|
||||||
|
|
||||||
|
That is a classmethod.
|
||||||
|
a staticmethod has no such rules.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# do you know you can declare a class without even touching the word class at all? its the power of type().
|
||||||
|
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def lda_set_example_arg(self, arg):
|
||||||
|
self.lda_example_instance_arg = arg
|
||||||
|
|
||||||
|
|
||||||
|
" __init__ is a magic function that will run when you make a class instance"
|
||||||
|
|
||||||
|
LdaExampleClass = type(
|
||||||
|
"LdaExampleClass",
|
||||||
|
(object,),
|
||||||
|
{
|
||||||
|
"__init__": lda_set_example_arg,
|
||||||
|
"lda_example_class_arg": 1,
|
||||||
|
"quadratic_builder": quadratic_builder,
|
||||||
|
"hello_world": hello_world,
|
||||||
|
"hello_to": hello_to,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
LdaExampleClass.hello_to("lda") # prints hello lda!
|
||||||
|
|
||||||
|
|
||||||
|
def lda_print_example_arg(self):
|
||||||
|
print("instance:", self.lda_example_instance_arg)
|
||||||
|
print("class:", self.lda_example_class_arg)
|
||||||
|
|
||||||
|
|
||||||
|
# objects can freely add properties
|
||||||
|
LdaExampleClass.lda_print_example_arg = classmethod(lda_print_example_arg)
|
||||||
|
|
||||||
|
lda1 = LdaExampleClass(3) # this must fit the args of self.__init__()
|
||||||
|
lda1.lda_print_example_arg() # pylint: disable=no-member # ide freak out
|
||||||
|
# this will print instance which we just passed as 3,
|
||||||
|
# and class which is from the above which is 1.
|
||||||
|
# Nothing was changed: i dont assign anything to anything in the functions
|
||||||
|
# except for lda_set_example_arg
|
||||||
|
|
||||||
|
# dont do this, however, as ide will freak out.
|
||||||
@@ -0,0 +1,226 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Python tutorial\n",
|
||||||
|
"\n",
|
||||||
|
"this is a python tutorial in jupyter notebook! this will serve as a continuation of that last one."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## but last one is ugh\n",
|
||||||
|
"\n",
|
||||||
|
"ok mr complainer here we go:\n",
|
||||||
|
"\n",
|
||||||
|
"### basic"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"x = 3 # variable = value\n",
|
||||||
|
"x = 31.28 # float value\n",
|
||||||
|
"\n",
|
||||||
|
"x = 3 + 5\n",
|
||||||
|
"assert x == 8"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"above is an interactive piece of code. you will press run and it will remember.\n",
|
||||||
|
"\n",
|
||||||
|
"If you didnt press run this one will show error:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"try:\n",
|
||||||
|
" x\n",
|
||||||
|
"except NameError:\n",
|
||||||
|
" print(\"play the last code block broski\")\n",
|
||||||
|
"else:\n",
|
||||||
|
" print(\"thats right\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"try:\n",
|
||||||
|
" x\n",
|
||||||
|
"except NameError:\n",
|
||||||
|
" print(\"play the last code block broski\")\n",
|
||||||
|
"else:\n",
|
||||||
|
" print(\"thats right\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### containers\n",
|
||||||
|
"\n",
|
||||||
|
"these are some of the containers.\n",
|
||||||
|
"\n",
|
||||||
|
"list, dict, set is mutable, \n",
|
||||||
|
"dict keys and set items are unique, \n",
|
||||||
|
"index list and tuple by index and dict by keys."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"my_list = [\"this\", \"is\", \"a\", \"list\"]\n",
|
||||||
|
"my_dict = {\n",
|
||||||
|
" \"my key\": \"my value\",\n",
|
||||||
|
" \"my life\": \"my rules\",\n",
|
||||||
|
" \"my style\": \"my attitute\"\n",
|
||||||
|
"}\n",
|
||||||
|
"\n",
|
||||||
|
"assert my_list[1] == \"is\" # not \"this\"\n",
|
||||||
|
"assert my_list[-1] == \"list\""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"my_longer = list(range(20)) # 0 -> 19\n",
|
||||||
|
"\n",
|
||||||
|
"assert my_longer[2:5] == [2, 3, 4] # slice\n",
|
||||||
|
"assert my_longer[:4] == [0, 1, 2, 3] # blank start for 0\n",
|
||||||
|
"assert my_longer[3::5] == [3, 8, 13, 18] # blank stop for past end\n",
|
||||||
|
"assert my_longer[18:6:-4] == [18, 14, 10] # empty/no step for 1\n",
|
||||||
|
"assert my_longer[:-3:8] == [0, 8, 16]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### function\n",
|
||||||
|
"\n",
|
||||||
|
"function is declared with `def`:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def my_function(a: int, b: int) -> int:\n",
|
||||||
|
" return a + b"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"you can violate type hints:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"my_function(\"hello \", \"world!\") # dont do this\n",
|
||||||
|
"my_function(2, 6)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### control flow, conditional"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"if 3 + 5 == 6:\n",
|
||||||
|
" print(\"3 + 5 = 6\")\n",
|
||||||
|
"elif 3 + 5 == 7:\n",
|
||||||
|
" print(\"3 + 5 = 7\")\n",
|
||||||
|
"elif 3 + 5 == 8:\n",
|
||||||
|
" print(\"3 + 5 == 8\")\n",
|
||||||
|
"else:\n",
|
||||||
|
" print(\"D: None of the above\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"container = [1, 11, 5, 17]\n",
|
||||||
|
"sum_ = 0\n",
|
||||||
|
"for element in container:\n",
|
||||||
|
" sum_ += element\n",
|
||||||
|
"sum_"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"x = 0\n",
|
||||||
|
"while True:\n",
|
||||||
|
" x += 1\n",
|
||||||
|
" \n",
|
||||||
|
" if x > 20:\n",
|
||||||
|
" break"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.12.5"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 2
|
||||||
|
}
|
||||||
Reference in new issue
Block a user