{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "Z7apiBJbNP0R" }, "source": [ "Son de\u011fi\u015ftirilme tarihi:21.09.2024" ] }, { "cell_type": "markdown", "metadata": { "id": "r2qH7XpGNP0U" }, "source": [ "\u00d6NS\u00d6Z" ] }, { "cell_type": "markdown", "metadata": { "id": "N2GwLaANNP0V" }, "source": [ "NOT:Dok\u00fcmanda zaman zaman g\u00fcncellemeler olaca\u011f\u0131 i\u00e7in arada bir g\u00fcncel versiyon kontrol\u00fc yapman\u0131z\u0131 tavsiye ederim.\n", "
\n",
"from IPython.core.interactiveshell import InteractiveShell\n",
"InteractiveShell.ast_node_interactivity = \"all\"\n",
"\n",
"\n",
"\u00d6rne\u011fin bu interactive kodlar\u0131 girilmeden a\u015fa\u011f\u0131daki kod \u00e7al\u0131\u015f\u0131t\u0131r\u0131l\u0131rsa sadece 2 sonucunu al\u0131rken\n",
"\n",
"a=1\n",
"b=2\n",
"a\n",
"b\n",
"\n",
"\n",
"Yukar\u0131daki iki sat\u0131rl\u0131k kod girilirse hem 1 hem 2 sonucu g\u00f6r\u00fcn\u00fcr."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2021-05-15T15:32:54.059281Z",
"start_time": "2021-05-15T15:32:54.055292Z"
},
"id": "3zhPcl7VNP0h"
},
"outputs": [],
"source": [
"from IPython.core.interactiveshell import InteractiveShell\n",
"InteractiveShell.ast_node_interactivity = \"all\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 54,
"status": "ok",
"timestamp": 1727018585087,
"user": {
"displayName": "Volkan Yurtseven",
"userId": "15726953944641946140"
},
"user_tz": -180
},
"id": "RkfLENr4NP0i",
"outputId": "07ef44cc-1992-4f73-f19c-33619a12cfe3"
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a=1\n",
"b=2\n",
"a\n",
"b"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "EEPX3axvNP0i"
},
"source": [
"## \u00c7e\u015fitli operat\u00f6rler"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 45,
"status": "ok",
"timestamp": 1727018585087,
"user": {
"displayName": "Volkan Yurtseven",
"userId": "15726953944641946140"
},
"user_tz": -180
},
"id": "rsnrNJINNP0i",
"outputId": "678084f6-0737-4a0d-a16e-2c5f3d879975"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"drive sample_data\n"
]
}
],
"source": [
"#! i\u015fareti ile i\u015fletim sistemi komutlar\u0131 kullan\u0131labilir. Windows'ta cmd'den, Linux'ta terminalden yazar gibi olur\n",
"# !dir #windowsta olsayd\u0131m\n",
"!ls #colab'te oldu\u011fum i\u00e7in, colabde Linux \u00fczerinde \u00e7al\u0131\u015ft\u0131\u011f\u0131 i\u00e7in ls"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 34,
"status": "ok",
"timestamp": 1727018585087,
"user": {
"displayName": "Volkan Yurtseven",
"userId": "15726953944641946140"
},
"user_tz": -180
},
"id": "MDmA-sdXCLMF",
"outputId": "820a5932-a2e9-4c7a-a9a8-8e6c3c56bb58"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/content\n"
]
}
],
"source": [
"!pwd"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 2533,
"status": "ok",
"timestamp": 1727018587601,
"user": {
"displayName": "Volkan Yurtseven",
"userId": "15726953944641946140"
},
"user_tz": -180
},
"id": "AZwHtBQDCAYx",
"outputId": "1d45d904-00a3-418a-9ff9-d0c157fc5e14"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[Errno 2] No such file or directory: 'sample_data #! ile d\u011feil % ile. sebebi: The !cd command only changes the directory for that specific line. To change the directory for the whole notebook, use the %cd command instead.'\n",
"/content\n",
"drive sample_data\n"
]
}
],
"source": [
"%cd sample_data #! ile d\u011feil % ile. sebebi: The !cd command only changes the directory for that specific line. To change the directory for the whole notebook, use the %cd command instead.\n",
"!ls"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ZbIQGdp4NP0j"
},
"outputs": [],
"source": [
"# \"#\" i\u015fareti ile yorum yazar\u0131z"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"executionInfo": {
"elapsed": 89,
"status": "ok",
"timestamp": 1727018587601,
"user": {
"displayName": "Volkan Yurtseven",
"userId": "15726953944641946140"
},
"user_tz": -180
},
"id": "mFFxeJGrNP0j",
"outputId": "3aa3274d-fdc9-4619-c9bb-2f2df01be85c"
},
"outputs": [
{
"data": {
"text/html": [
"Farkl\u0131 dil se\u00e7eneklerin kullanabiliyoruz. Burada HTML kullanm\u0131\u015f olduk.\n"
],
"text/plain": [
"\n",
"matrix = [[0, 1, 2, 3, 4],\n",
" [0, 1, 2, 3, 4],\n",
" [0, 1, 2, 3, 4],\n",
" [0, 1, 2, 3, 4],\n",
" [0, 1, 2, 3, 4]]\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 55,
"status": "ok",
"timestamp": 1727018724604,
"user": {
"displayName": "Volkan Yurtseven",
"userId": "15726953944641946140"
},
"user_tz": -180
},
"id": "OFfwnWT5NP1V",
"outputId": "6d14e9aa-858d-4ba9-8a3a-495f7e807a1b"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]\n"
]
}
],
"source": [
"matrix = []\n",
"\n",
"for i in range(5):\n",
"\n",
"\t# Append an empty sublist inside the list\n",
"\tmatrix.append([])\n",
"\n",
"\tfor j in range(5):\n",
"\t\tmatrix[i].append(j)\n",
"\n",
"print(matrix)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 46,
"status": "ok",
"timestamp": 1727018724604,
"user": {
"displayName": "Volkan Yurtseven",
"userId": "15726953944641946140"
},
"user_tz": -180
},
"id": "pTZObgruNP1V",
"outputId": "87fc9d2b-96d8-4a04-824d-c914309e2a6d"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]\n"
]
}
],
"source": [
"# Nested list comprehension\n",
"matrix = [[j for j in range(5)] for i in range(5)]\n",
"\n",
"print(matrix)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "bOPaavfoNP1W"
},
"source": [
"### Generators"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "JVGurksxNP1W"
},
"source": [
"Bu konu biraz daha advanced bi konu olup ben sadece toplam al\u0131nmas\u0131 gereken durumlar i\u00e7in bir \u00f6enride bulunucam. Bi list comprehension sonunda(\u00f6zellikle \u00e7ok b\u00fcy\u00fck bi list s\u00f6zkonusuya) toplam al\u0131nacaksa [] kullanamya gerek yok, b\u00f6ylece memory tasarrufu yapm\u0131\u015f olursunuz. Detaylar i\u00e7in \u015fu sayfaya bakabilirsiniz: https://www.johndcook.com/blog/2020/01/15/generator-expression/"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 37,
"status": "ok",
"timestamp": 1727018724604,
"user": {
"displayName": "Volkan Yurtseven",
"userId": "15726953944641946140"
},
"user_tz": -180
},
"id": "5wo-U8QoNP1X",
"outputId": "a535516b-294b-47ab-d45b-ae7ebfc5bcb7"
},
"outputs": [
{
"data": {
"text/plain": [
"285"
]
},
"execution_count": 126,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#list comprehension: t\u00fcm liste elemanlar\u0131 bellekte tutuluyor\n",
"sum([x**2 for x in range(10)])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 28,
"status": "ok",
"timestamp": 1727018724604,
"user": {
"displayName": "Volkan Yurtseven",
"userId": "15726953944641946140"
},
"user_tz": -180
},
"id": "tVNCqHRENP1X",
"outputId": "93732567-cba1-476c-a131-381e128270c6"
},
"outputs": [
{
"data": {
"text/plain": [
"285"
]
},
"execution_count": 127,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#generator expression: liste elemanlar\u0131 bellekte tutulmuyor\n",
"sum(x**2 for x in range(10))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "w_aH2KmvNP1X"
},
"source": [
"## Stack"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "koT7RrSyNP1X"
},
"source": [
"Normalde b\u00f6yle bi s\u0131n\u0131f yok. list'i stack gibi kullan\u0131r\u0131z. append ve pop sayesinde. ilk giren ilk \u00e7\u0131kar"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 18,
"status": "ok",
"timestamp": 1727018724604,
"user": {
"displayName": "Volkan Yurtseven",
"userId": "15726953944641946140"
},
"user_tz": -180
},
"id": "AOYlOLN2NP1X",
"outputId": "b546321f-c5e3-41a5-914f-cf564248245d"
},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 128,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"[1, 2, 3]"
]
},
"execution_count": 128,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"stack=[1,2,3]\n",
"stack.append(4)\n",
"stack.pop()\n",
"stack"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "m9mI5senNP1Y"
},
"source": [
"## Queue"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fhGjV581NP1Y"
},
"source": [
"Bunu da istersek listten yapar\u0131z, ilk giren son \u00e7\u0131kar. ama bunun i\u00e7in collections mod\u00fcl\u00fcnde bi s\u0131n\u0131f var"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 818,
"status": "ok",
"timestamp": 1727018725412,
"user": {
"displayName": "Volkan Yurtseven",
"userId": "15726953944641946140"
},
"user_tz": -180
},
"id": "yWhScMN7NP1Y",
"outputId": "76ffb7b1-8748-426f-bf3d-33c5331c3868"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"deque([2, 3, 4])\n"
]
}
],
"source": [
"from collections import deque\n",
"kuyruk=deque([1,2,3])\n",
"kuyruk.append(4)\n",
"s\u0131radaki=kuyruk.popleft()\n",
"print(s\u0131radaki)\n",
"print(kuyruk)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Cdl9pcc0NP1Y"
},
"source": [
"## Dictionary"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "SUa2Se8GNP1Y"
},
"source": [
"Key-value ikililerini tutarlar. S\u0131ras\u0131zd\u0131rlar(EDIT:Python 3.7den itibaren girdilen s\u0131ray\u0131 korur), indeksle ula\u015famay\u0131z. key'lerle valuelara ula\u015f\u0131r\u0131z veya d\u00f6ng\u00fc i\u00e7inde dolanarak ikisine birden tek seferde de ula\u015fabiliriz."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "kwb7sDe9NP1Y"
},
"source": [
"### Yarat\u0131m"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "nJUUXyxBNP1Z"
},
"source": [
"#### Klasik"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 203,
"status": "ok",
"timestamp": 1727018725413,
"user": {
"displayName": "Volkan Yurtseven",
"userId": "15726953944641946140"
},
"user_tz": -180
},
"id": "o_7JO3-LNP1Z",
"outputId": "ab3f9c0c-f6c8-4e24-a158-9a6a4a603349"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(5, ['printy(dict_.keys())\\n']) \n",
"----------\n",
"dict_keys(['one', 'two'])\n",
" \n",
"(6, ['printy(dict_.values())\\n']) \n",
"----------\n",
"dict_values(['bir', 'zwei'])\n",
" \n",
"(7, ['printy(dict_.items())\\n']) \n",
"----------\n",
"dict_items([('one', 'bir'), ('two', 'zwei')])\n",
" \n",
"bir\n",
"N/A\n"
]
}
],
"source": [
"dict_={}\n",
"dict_[\"one\"]=\"bir\" #add,append, insert gibi bir metodu yok, direkt atan\u0131yor\n",
"dict_[\"two\"]=\"iki\"\n",
"dict_[\"two\"]=\"zwei\"\n",
"printy(dict_.keys())\n",
"printy(dict_.values())\n",
"printy(dict_.items())\n",
"print(dict_[\"one\"])\n",
"#print(dict_[\"three\"]) # hata al\u0131r, almamas\u0131 i\u00e7in get kullan\n",
"print(dict_.get(\"three\",\"N/A\"))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "uDsCwp-BNP1Z"
},
"source": [
"#### dict metodu ile ikili elemanlardan olu\u015fan bir yap\u0131dan"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "knXEsR2TNP1Z"
},
"source": [
"bu ikili yap\u0131lar genelde zip veya enumerate olacakt\u0131r. bak\u0131n\u0131z ilgili fonksiyonar."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 53
},
"executionInfo": {
"elapsed": 189,
"status": "ok",
"timestamp": 1727018725413,
"user": {
"displayName": "Volkan Yurtseven",
"userId": "15726953944641946140"
},
"user_tz": -180
},
"id": "I4q3Wwo7NP1Z",
"outputId": "1388f47f-3570-4675-dfba-2ce248167b92"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"| Ama\u00e7 | \n", "K\u0131sayol | \n", "
|---|---|
| Sayfalar aras\u0131nda dola\u015fmak | \n", "CTRL + PgUp/PgDn | \n", "
| Bug\u00fcn\u00fcn Tarihini yazmak | \n", "CTRL + SHIFT +, | \n", "
| T\u00fcm a\u00e7\u0131k dosyalarda calculation yapmak | \n", "F9 | \n", "
| Se\u00e7ili k\u0131sm\u0131n de\u011ferini hesaplay\u0131p g\u00f6stermek | \n", "H\u00fccre i\u00e7indeki form\u00fcl se\u00e7ilip F9 | \n", "
| Aktif sayfada calculation yapmak | \n", "SHIFT+F9 | \n", "
| Sadece belli range i\u00e7in calculation yapmak | \n", "VBA ile yap\u0131l\u0131r. Burdan bak\u0131n. | \n", "
| Bulundu\u011fun h\u00fccrenin CurrentRegion'\u0131n\u0131 se\u00e7me | \n", "CTRL+ A | \n", "
| Bulundu\u011fun h\u00fccreden CurrentRegion'\u0131n u\u00e7 noktlar\u0131na gitmek | \n", "CTRL+ Ok tu\u015flar\u0131 | \n", "
| Bulundu\u011fun h\u00fccreden itibaren belli bir y\u00f6ne do\u011fru se\u00e7im yapmak | \n", "SHIFT+Ok tu\u015flar\u0131 | \n", "
| Bulundu\u011fun h\u00fccreden itibaren CurrentRegion bir ucuna do\u011fru toplu se\u00e7im yapmak | \n", "CTRL+SHIFT+Ok tu\u015flar\u0131 | \n", "
| Bulundu\u011fun h\u00fccreden CurrentRegion'\u0131n Sa\u011f A\u015fa\u011f\u0131 u\u00e7 noktlas\u0131na gitmek | \n", "CTRL+END | \n", "
| Bulundu\u011fun h\u00fccreden CurrentRegion'\u0131n Sa\u011f A\u015fa\u011f\u0131 u\u00e7 noktlas\u0131na kadar se\u00e7mek | \n", "CTRL+SHIFT+END | \n", "
| Bulundu\u011fun h\u00fccreden A1 h\u00fccresine kadar olan alan\u0131(sol yukar\u0131) se\u00e7mek | \n", "CTRL+SHIFT+HOME | \n", "
| Bir h\u00fccre i\u00e7inde veri girerken, ayn\u0131 h\u00fccre i\u00e7inde yeni bir sat\u0131r a\u00e7\u0131p oradan devam etmek | \n", "ALT+ENTER | \n", "
| Veri/Form\u00fcl giri\u015fi yapt\u0131\u011f\u0131n\u0131z h\u00fccrede alt h\u00fccreye ge\u00e7meden giri\u015f tamamlamak | \n", "CTRL+ENTER | \n", "
| Ekranda bir sayfa sa\u011fa kaymak. | \n", "ALT+PGE DOWN | \n", "
| AutoFilter'\u0131 aktif/pasif hale getirmek | \n", "CTRL+SHIFT+L | \n", "
| Bulundu\u011funuz h\u00fccrenin sat\u0131r ve s\u00fctununa ayn\u0131 anda freeze uygulamak/kald\u0131rmak | \n", "Alt+W+FF | \n", "
| VBA edit\u00f6r\u00fcn\u00fc a\u00e7mak | \n", "Alt+F11 | \n", "
| Ribbonu k\u00fc\u00e7\u00fclt\u00fcp/b\u00fcy\u00fctmek | \n", "CTRL+F1 | \n", "
| \u00dcst h\u00fccrelerdeki t\u00fcm rakamlar\u0131n toplam\u0131n\u0131 almak | \n", "ALT+= | \n", "
| Flash Fill uygulamak | \n", "CTRL+E | \n", "
| Sadece g\u00f6r\u00fcnen h\u00fccreleri se\u00e7mek | \n", "ALT+; | \n", "
G\u00f6rsel bu sayfadan al\u0131nm\u0131\u015ft\u0131r
\"\"\")\n", " \n", "def pythonSomeInfo():\n", " print(\"system packages folder:\",sys.prefix, end=\"\\n\\n\")\n", " print(\"pip install folder:\",site.getsitepackages(), end=\"\\n\\n\") \n", " print(\"python version:\", sys.version, end=\"\\n\\n\")\n", " print(\"executables location:\",sys.executable, end=\"\\n\\n\")\n", " print(\"pip version:\", os.popen('pip version').read(), end=\"\\n\\n\")\n", " pathes= sys.path\n", " print(\"Python pathes\")\n", " for p in pathes:\n", " print(p)\n", "\n", "\n", "def showMemoryUsage():\n", " dict_={}\n", " global_vars = list(globals().items())\n", " for var, obj in global_vars:\n", " if not var.startswith('_'):\n", " dict_[var]=sys.getsizeof(obj)\n", " \n", " final={k: v for k, v in sorted(dict_.items(), key=lambda item: item[1],reverse=True)} \n", " print(final)\n", " \n", "def readfile(path,enc='cp1254'):\n", " with io.open(path, \"r\", encoding=enc) as f:\n", " return f.read()\n", "\n", "def getFirstItemFromDictionary(dict_):\n", " return next(iter(dict_)),next(iter(dict_.values()))\n", " \n", "\n", "\n", "def removeItemsFromList(self,list2,inplace=True): \n", " \"\"\"\n", " Extension method for list type. Removes items from list2 from list1.\n", " First, forbiddenfruit must be installed via https://pypi.org/project/forbiddenfruit/\n", " \"\"\" \n", " if inplace:\n", " for x in set(list2):\n", " self.remove(x)\n", " return self\n", " else:\n", " temp=self.copy()\n", " for x in set(list2):\n", " temp.remove(x)\n", " return temp\n", " \n", "curse(list, \"removeItemsFromList\", removeItemsFromList)\n", "\n", "\n", "\n", "\n", "\n", "\n", "----\n" ] }, { "data": { "text/plain": [ "0" ] }, "execution_count": 294, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "fr\n" ] } ], "source": [ "#oku\n", "#import io #normalde bu sat\u0131ra gerek yok, open=io.open i\u00e7in bi alias\n", "dosya = io.open(\"pythonutility.py\", \"r\") #yine bunda da ba\u015fta io yazmazd\u0131k normalde ama os'nin open'\u0131ndan ay\u0131rmak i\u00e7in ekledik\n", "print(dosya.readline(1))\n", "print(\"----\")\n", "print(dosya.read()) #ilk sat\u0131r\u0131 okudu\u011fumuz i\u00e7in ikinci sat\u0131rdan okumaya devam ediyor\n", "print(\"----\")\n", "dosya.seek(0) #ba\u015fa konumlanal\u0131m tekrar\n", "print(dosya.readline(2)) #ba\u015ftan ilk 2 karakter" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 189, "status": "ok", "timestamp": 1727018868501, "user": { "displayName": "Volkan Yurtseven", "userId": "15726953944641946140" }, "user_tz": -180 }, "id": "NYJPrI_9NP2M", "outputId": "cda78192-025c-4574-baa5-472ee9fc3b99" }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 295, "metadata": {}, "output_type": "execute_result" }, { "name": "stdout", "output_type": "stream", "text": [ "1-from __future__ import print_function\n", "2-import inspect\n", "3-import os, sys, site\n", "4-import functools\n", "5-import time\n", "6-from forbiddenfruit import curse\n", "7-\n", "8-try:\n", "9- import __builtin__\n", "10-except ImportError:\n", "11- import builtins as __builtin__\n", "12-\n", "13-# *************************************************************************************************************\n", "14-#Module level methods\n", "15-\n", "16- \n", "17-def lineno():\n", "18- previous_frame = inspect.currentframe().f_back.f_back\n", "19- (filename, line_number, function_name, lines, index) = inspect.getframeinfo(previous_frame)\n", "20- return (line_number, lines)\n", "21- #return inspect.currentframe().f_back.f_back.f_lineno, str(inspect.currentframe().f_back)\n", "22-\n", "23-def printy(*args, **kwargs):\n", "24- print(lineno(),\"\\n----------\")\n", "25- print(*args, **kwargs)\n", "26- print(\" \",end=\"\\n\")\n", "27-\n", "28-\n", "29-def timeElapse(func):\n", "30- \"\"\"\n", "31- usage:\n", "32- @timeElapse\n", "33- def somefunc():\n", "34- ...\n", "35- ...\n", "36-\n", "37- somefunc()\n", "38- \"\"\"\n", "39- @functools.wraps(func)\n", "40- def wrapper(*args,**kwargs):\n", "41- start=time.time()\n", "42- value=func(*args,**kwargs)\n", "43- func()\n", "44- finito=time.time()\n", "45- print(\"Time elapsed:{}\".format(finito-start))\n", "46- return value\n", "47- return wrapper \n", "48-\n", "49-\n", "50-def multioutput(type=\"all\"):\n", "51- from IPython.core.interactiveshell import InteractiveShell\n", "52- InteractiveShell.ast_node_interactivity = type\n", "53- \n", "54-def scriptforReload():\n", "55- print(\"\"\"\n", "56- %load_ext autoreload\n", "57- %autoreload 2\"\"\")\n", "58- \n", "59-def scriptforTraintest():\n", "60- print(\"X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.25,random_state=42)\")\n", "61- \n", "62-def scriptForCitation():\n", "63- print(\"\"\"G\u00f6rsel bu sayfadan al\u0131nm\u0131\u015ft\u0131r
\"\"\")\n", "64- \n", "65-def pythonSomeInfo():\n", "66- print(\"system packages folder:\",sys.prefix, end=\"\\n\\n\")\n", "67- print(\"pip install folder:\",site.getsitepackages(), end=\"\\n\\n\") \n", "68- print(\"python version:\", sys.version, end=\"\\n\\n\")\n", "69- print(\"executables location:\",sys.executable, end=\"\\n\\n\")\n", "70- print(\"pip version:\", os.popen('pip version').read(), end=\"\\n\\n\")\n", "71- pathes= sys.path\n", "72- print(\"Python pathes\")\n", "73- for p in pathes:\n", "74- print(p)\n", "75-\n", "76-\n", "77-def showMemoryUsage():\n", "78- dict_={}\n", "79- global_vars = list(globals().items())\n", "80- for var, obj in global_vars:\n", "81- if not var.startswith('_'):\n", "82- dict_[var]=sys.getsizeof(obj)\n", "83- \n", "84- final={k: v for k, v in sorted(dict_.items(), key=lambda item: item[1],reverse=True)} \n", "85- print(final)\n", "86- \n", "87-def readfile(path,enc='cp1254'):\n", "88- with io.open(path, \"r\", encoding=enc) as f:\n", "89- return f.read()\n", "90-\n", "91-def getFirstItemFromDictionary(dict_):\n", "92- return next(iter(dict_)),next(iter(dict_.values()))\n", "93- \n", "94-\n", "95-\n", "96-def removeItemsFromList(self,list2,inplace=True): \n", "97- \"\"\"\n", "98- Extension method for list type. Removes items from list2 from list1.\n", "99- First, forbiddenfruit must be installed via https://pypi.org/project/forbiddenfruit/\n", "100- \"\"\" \n", "101- if inplace:\n", "102- for x in set(list2):\n", "103- self.remove(x)\n", "104- return self\n", "105- else:\n", "106- temp=self.copy()\n", "107- for x in set(list2):\n", "108- temp.remove(x)\n", "109- return temp\n", "110- \n", "111-curse(list, \"removeItemsFromList\", removeItemsFromList)\n", "112-\n", "113-\n", "114-\n", "115-\n", "116-\n" ] } ], "source": [ "#her sat\u0131r\u0131n ba\u015f\u0131na sat\u0131r no ekleyelim\n", "dosya.seek(0)\n", "i=1\n", "for sat\u0131r in dosya.readlines():\n", " print(\"{}-{}\".format(i,sat\u0131r),end=\"\")\n", " i+=1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Obwq_iNjNP2M" }, "outputs": [], "source": [ "#yarat\n", "yenidosya=io.open(\"test.txt\",\"w\")\n", "yenidosya.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 168, "status": "ok", "timestamp": 1727018868502, "user": { "displayName": "Volkan Yurtseven", "userId": "15726953944641946140" }, "user_tz": -180 }, "id": "jWwIjeIKNP2M", "outputId": "f2b5d520-bd8a-49f1-87e1-deb9155ba69e" }, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 297, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#varolana yaz, sonuna ekleme\n", "yenidosya=io.open(\"test.txt\",\"a\")\n", "yenidosya.write(\"\\nselam\")\n", "yenidosya.flush() #hemen yazs\u0131n. bunu kullanmazsak yapt\u0131\u011f\u0131m\u0131z de\u011fi\u015fiklikleri hemen g\u00f6rmeyiz" ] }, { "cell_type": "markdown", "metadata": { "id": "_FPZJr39NP2N" }, "source": [ "G\u00fcvenli dosya i\u015flemleri" ] }, { "cell_type": "markdown", "metadata": { "id": "xjGs2hFlNP2N" }, "source": [ "Dosyalarla i\u015finiz bitince kapatmak \u00f6nemlidir. Kapand\u0131\u011f\u0131ndan emin olmak i\u00e7in with blo\u011fu i\u00e7inde yazmak gerekir" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 153, "status": "ok", "timestamp": 1727018868502, "user": { "displayName": "Volkan Yurtseven", "userId": "15726953944641946140" }, "user_tz": -180 }, "id": "TuNhM5TlNP2N", "outputId": "7fed268e-fadd-4228-dcc9-cc917e176789", "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "selam\n" ] } ], "source": [ "with io.open(\"test.txt\", \"r\") as dosya:\n", " print(dosya.read())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 141, "status": "ok", "timestamp": 1727018868502, "user": { "displayName": "Volkan Yurtseven", "userId": "15726953944641946140" }, "user_tz": -180 }, "id": "gefRFUIKNP2N", "outputId": "6228198b-92de-4558-e601-4e48c71e5477" }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 299, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/plain": [ "13" ] }, "execution_count": 299, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#hem okuma hem yazma moduyla a\u00e7\u0131p ba\u015fa bilgi ekleme\n", "with io.open(\"test.txt\", \"r+\") as f:\n", " content = f.read()\n", " f.seek(0) #Dosyay\u0131 ba\u015fa sar\u0131yoruz\n", " f.write(\"volkan\\n\"+content)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "6tyKo6fQSnRi" }, "outputs": [], "source": [ "!rm test.txt" ] }, { "cell_type": "markdown", "metadata": { "id": "OijGzgTxNP2N" }, "source": [ "**T\u00fcrk\u00e7e karakter**" ] }, { "cell_type": "markdown", "metadata": { "id": "WJ1vPazkNP2O" }, "source": [ "NOT:sqlite3 \u00e7ok basit bir veritaban\u0131 olup, oracle veya sql server gibi g\u00fc\u00e7l\u00fc veritabanlar\u0131n\u0131 sorgulamak i\u00e7in sqlalchemy veya pyodbc gibi mod\u00fclleri kullan\u0131r\u0131z ve buradan ald\u0131\u011fm\u0131z datay\u0131 pandas ile i\u015fleyebiliriz. Bunun i\u00e7in benim github repomdaki Python Veri Analizi notebookuna bakman\u0131z\u0131 tavsiye ederim.
" ] }, { "cell_type": "markdown", "metadata": { "id": "7yl4D60NNP2S" }, "source": [ "http://sqlitebrowser.org/. sitesi de incelenebilir" ] }, { "cell_type": "markdown", "metadata": { "id": "aWdQ3O35NP2S" }, "source": [ "# Classlar" ] }, { "cell_type": "markdown", "metadata": { "id": "dsnWXgT5NP2T" }, "source": [ "Python nesne y\u00f6nelimli(oo) bir dildir ve t\u00fcm oo dillerde oldu\u011fu gibi s\u0131n\u0131flar yarat\u0131labilir. \u00d6rnek bir s\u0131n\u0131f yarat\u0131m\u0131 a\u015fa\u011f\u0131daki gibi olup detaylar i\u00e7in googlelaman\u0131z\u0131 rica ederim." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 58, "status": "ok", "timestamp": 1727018868504, "user": { "displayName": "Volkan Yurtseven", "userId": "15726953944641946140" }, "user_tz": -180 }, "id": "ISjW6xL4NP2T", "outputId": "3a6022fe-62c6-464f-e478-3f7c80b24c16" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yeni ara\u00e7 haz\u0131r\n", "yeni ara\u00e7 haz\u0131r\n", "yeni ara\u00e7 haz\u0131r\n", "<__main__.Araba object at 0x7d45a4bfff70>\n", "\u00e7al\u0131\u015f\u0131yor\n", "durdu\n", "Mekanik\n", "Mekanik\n" ] } ], "source": [ "class Araba:\n", " aractipi=\"Mekanik\" #class seviyesinde, t\u00fcm Arabalar taraf\u0131ndan payla\u015f\u0131lan bir de\u011fer\n", " def __init__(self,model,marka,km):\n", " self.model=model\n", " self.marka=marka\n", " self.km=km\n", " print(\"yeni ara\u00e7 haz\u0131r\")\n", " def run(self):\n", " print(\"\u00e7al\u0131\u015f\u0131yor\")\n", " def stop(self):\n", " print(\"durdu\")\n", "\n", "bmw0=Araba(2011,\"bmw\",0)\n", "bmw1=Araba(2014,\"bmw\",0)\n", "audi=Araba(2011,\"audi\",0)\n", "print(bmw0)\n", "bmw0.run()\n", "bmw0.stop()\n", "print(bmw0.aractipi)\n", "print(audi.aractipi)" ] }, { "cell_type": "markdown", "metadata": { "id": "jy6MGCozNP2T" }, "source": [ "## Paralelle\u015ftirme" ] }, { "cell_type": "markdown", "metadata": { "id": "4vVeILX-NP2T" }, "source": [ "\u00d6zellikle analitik model kurma s\u0131ras\u0131nda \u00e7ok i\u015fimize yarayan bir olgudur. Bunun i\u00e7in ayr\u0131 bir notebook'um olacak. \u00d6nden ara\u015ft\u0131rmak isteyenler \u015fu kavramlar\u0131 ara\u015ft\u0131rabilir. Multi-threading, multiprocessing, conccurency ve parallelism" ] }, { "cell_type": "markdown", "metadata": { "id": "fabSEm7ANP2T" }, "source": [ "# Verimlilik ve Di\u011fer" ] }, { "cell_type": "markdown", "metadata": { "id": "ocn1iv8JNP2U" }, "source": [ "## debugging" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 5035, "status": "ok", "timestamp": 1727018873489, "user": { "displayName": "Volkan Yurtseven", "userId": "15726953944641946140" }, "user_tz": -180 }, "id": "CDamCKWINP2U", "outputId": "ce10d6b7-bded-43af-80a1-84bda466c6ca", "scrolled": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\n", "PYDEV DEBUGGER WARNING:\n", "sys.settrace() should not be used when the debugger is being used.\n", "This may cause the debugger to stop working correctly.\n", "If this is needed, please check: \n", "http://pydev.blogspot.com/2007/06/why-cant-pydev-debugger-work-with.html\n", "to see how to restore the debug tracing back correctly.\n", "Call Location:\n", " File \"/usr/lib/python3.10/bdb.py\", line 336, in set_trace\n", " sys.settrace(self.trace_dispatch)\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "4\n", "--Call--\n", "> \u001b[0;32m/usr/local/lib/python3.10/dist-packages/IPython/core/displayhook.py\u001b[0m(252)\u001b[0;36m__call__\u001b[0;34m()\u001b[0m\n", "\u001b[0;32m 250 \u001b[0;31m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstdout\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mflush\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0m\u001b[0;32m 251 \u001b[0;31m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0m\u001b[0;32m--> 252 \u001b[0;31m \u001b[0;32mdef\u001b[0m \u001b[0m__call__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0m\u001b[0;32m 253 \u001b[0;31m \"\"\"Printing with history cache management.\n", "\u001b[0m\u001b[0;32m 254 \u001b[0;31m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0m\n", "ipdb> c\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\n", "PYDEV DEBUGGER WARNING:\n", "sys.settrace() should not be used when the debugger is being used.\n", "This may cause the debugger to stop working correctly.\n", "If this is needed, please check: \n", "http://pydev.blogspot.com/2007/06/why-cant-pydev-debugger-work-with.html\n", "to see how to restore the debug tracing back correctly.\n", "Call Location:\n", " File \"/usr/lib/python3.10/bdb.py\", line 347, in set_continue\n", " sys.settrace(None)\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "asda\n" ] } ], "source": [ "import pdb\n", "print(4)\n", "pdb.set_trace() #c devam, n:next gibi se\u00e7enekler var\n", "print(\"asda\")" ] }, { "cell_type": "markdown", "metadata": { "id": "won9XpBlNP2U" }, "source": [ "## memory y\u00f6netimi" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "executionInfo": { "elapsed": 320, "status": "ok", "timestamp": 1727018887406, "user": { "displayName": "Volkan Yurtseven", "userId": "15726953944641946140" }, "user_tz": -180 }, "id": "AdmijMKPNP2V", "outputId": "e654c22f-8181-4b11-b6fa-64b2e449a166", "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "64\n", "88\n", "104\n" ] } ], "source": [ "import sys\n", "import array\n", "t=(1,2,3)\n", "l=[1,2,\"3\"]\n", "a=array.array(\"l\",[1,2,3])\n", "print(sys.getsizeof(t)) #immutabel oldu\u011fu i\u00e7in daha az\n", "print(sys.getsizeof(l)) #mutable oldu\u011fu i\u00e7in tupldan daha \u00e7ok, i\u00e7ine farkl\u0131 tipler alabilece\u011fim i\u00e7in arraydan daha \u00e7ok\n", "print(sys.getsizeof(a)) #eleman tipi belli oldu\u011fu i\u00e7in listten daha az" ] }, { "cell_type": "markdown", "metadata": { "id": "C0XtcQ6ONP2V" }, "source": [ "# Cheatsheet" ] }, { "cell_type": "markdown", "metadata": { "id": "UE-hIG00NP2V" }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": { "id": "1YeCwWllNP2V" }, "source": [ "# Kendinizi test edin" ] }, { "cell_type": "markdown", "metadata": { "id": "uj1XC9KjNP2V" }, "source": [ "A\u015fa\u011f\u0131daki adreslerden birinden challange sorular\u0131n\u0131 g\u00f6rebilirsiniz.\n", "* https://mybinder.org/v2/gh/VolkiTheDreamer/PythonRocks/master (Interaktiftir, download etmenize gerek yok)\n", "* \u00fcstteki a\u00e7\u0131lmazsa: https://nbviewer.org/github/VolkiTheDreamer/PythonRocks/blob/master/Python%20Challenges.ipynb\n" ] }, { "cell_type": "markdown", "metadata": { "id": "bI3tbqMJNP2V" }, "source": [ "Bunun d\u0131\u015f\u0131nda \u015fu sitelerde de pratik yapma imkan\u0131 bulabilirsiniz.\n", "\n", "- https://www.w3resource.com/python-exercises/\n", "- https://www.practicepython.org/\n", "- https://www.w3schools.com/python/python_exercises.asp\n", "- https://pynative.com/python-exercises-with-solutions/" ] } ], "metadata": { "colab": { "provenance": [], "toc_visible": true }, "hide_input": false, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.8.6" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": { "height": "708px", "left": "300px", "top": "148px", "width": "305.625px" }, "toc_section_display": true, "toc_window_display": true }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 0 }