/
fedorhub
/
Python_Le
Обзор
Документация
Войти
/
fedorhub
/
Python_Le
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
python/functions-06/index.htm
3 485 строк
348 KB
fedorhub
upload files
17 дек 2025, 14:35
17 дек 2025, 14:35
50de53d
Код
Авторство
О чём код?
<!DOCTYPE html> <html class="no-js" lang="ru-RU"> <head> <meta http-equiv="content-type" content="text/html" charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" > <link rel="profile" href="http://gmpg.org/xfn/11"> <meta name='robots' content='index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1' /> <script> MathJax = { tex: { inlineMath: [['$','$'],['\\(','\\)']], processEscapes: true }, options: { ignoreHtmlClass: 'tex2jax_ignore|editor-rich-text' } }; </script> <!-- This site is optimized with the Yoast SEO plugin v23.8 - https://yoast.com/wordpress/plugins/seo/ --> <title>Функции в Питоне | Программирование на Питоне</title> <meta name="description" content="Важно уметь использовать уже готовые функции в Питоне, а также писать свои собственные обычные def() и lambda-функции." /> <link rel="canonical" href="https://www.dmitrymakarov.ru/python/functions-06/" /> <meta property="og:locale" content="ru_RU" /> <meta property="og:type" content="article" /> <meta property="og:title" content="Функции в Питоне | Программирование на Питоне" /> <meta property="og:description" content="Важно уметь использовать уже готовые функции в Питоне, а также писать свои собственные обычные def() и lambda-функции." /> <meta property="og:url" content="https://www.dmitrymakarov.ru/python/functions-06/" /> <meta property="og:site_name" content="Машинное обучение" /> <meta property="article:modified_time" content="2024-10-12T18:20:08+00:00" /> <meta property="og:image" content="https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/f_math_prog.jpg" /> <meta property="og:image:width" content="1948" /> <meta property="og:image:height" content="1081" /> <meta property="og:image:type" content="image/jpeg" /> <meta name="twitter:card" content="summary_large_image" /> <meta name="twitter:label1" content="Примерное время для чтения" /> <meta name="twitter:data1" content="20 минут" /> <script type="application/ld+json" class="yoast-schema-graph">{"@context":"https://schema.org","@graph":[{"@type":"WebPage","@id":"https://www.dmitrymakarov.ru/python/functions-06/","url":"https://www.dmitrymakarov.ru/python/functions-06/","name":"Функции в Питоне | Программирование на Питоне","isPartOf":{"@id":"https://www.dmitrymakarov.ru/#website"},"primaryImageOfPage":{"@id":"https://www.dmitrymakarov.ru/python/functions-06/#primaryimage"},"image":{"@id":"https://www.dmitrymakarov.ru/python/functions-06/#primaryimage"},"thumbnailUrl":"https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/f_math_prog-1024x568.jpg","datePublished":"2021-11-16T18:46:32+00:00","dateModified":"2024-10-12T18:20:08+00:00","description":"Важно уметь использовать уже готовые функции в Питоне, а также писать свои собственные обычные def() и lambda-функции.","breadcrumb":{"@id":"https://www.dmitrymakarov.ru/python/functions-06/#breadcrumb"},"inLanguage":"ru-RU","potentialAction":[{"@type":"ReadAction","target":["https://www.dmitrymakarov.ru/python/functions-06/"]}]},{"@type":"ImageObject","inLanguage":"ru-RU","@id":"https://www.dmitrymakarov.ru/python/functions-06/#primaryimage","url":"https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/f_math_prog.jpg","contentUrl":"https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/f_math_prog.jpg","width":1948,"height":1081},{"@type":"BreadcrumbList","@id":"https://www.dmitrymakarov.ru/python/functions-06/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Главная страница","item":"https://www.dmitrymakarov.ru/"},{"@type":"ListItem","position":2,"name":"Программирование на Питоне","item":"https://www.dmitrymakarov.ru/python/"},{"@type":"ListItem","position":3,"name":"Функции в Питоне"}]},{"@type":"WebSite","@id":"https://www.dmitrymakarov.ru/#website","url":"https://www.dmitrymakarov.ru/","name":"Машинное обучение","description":"","publisher":{"@id":"https://www.dmitrymakarov.ru/#/schema/person/3fc221e2d391cc5cfd9d31c9c251c2a4"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://www.dmitrymakarov.ru/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ru-RU"},{"@type":["Person","Organization"],"@id":"https://www.dmitrymakarov.ru/#/schema/person/3fc221e2d391cc5cfd9d31c9c251c2a4","name":"DmitryMakarov","image":{"@type":"ImageObject","inLanguage":"ru-RU","@id":"https://www.dmitrymakarov.ru/#/schema/person/image/","url":"https://secure.gravatar.com/avatar/2c5f922d990c667feffc00f99517cc0e?s=96&d=mm&r=g","contentUrl":"https://secure.gravatar.com/avatar/2c5f922d990c667feffc00f99517cc0e?s=96&d=mm&r=g","caption":"DmitryMakarov"},"logo":{"@id":"https://www.dmitrymakarov.ru/#/schema/person/image/"}}]}</script> <!-- / Yoast SEO plugin. --> <link rel='dns-prefetch' href='//cdn.jsdelivr.net' /> <link rel="alternate" type="application/rss+xml" title="Машинное обучение » Лента" href="https://www.dmitrymakarov.ru/feed/" /> <link rel="alternate" type="application/rss+xml" title="Машинное обучение » Лента комментариев" href="https://www.dmitrymakarov.ru/comments/feed/" /> <script type="text/javascript"> /* <![CDATA[ */ window._wpemojiSettings = {"baseUrl":"https:\/\/s.w.org\/images\/core\/emoji\/15.0.3\/72x72\/","ext":".png","svgUrl":"https:\/\/s.w.org\/images\/core\/emoji\/15.0.3\/svg\/","svgExt":".svg","source":{"concatemoji":"https:\/\/www.dmitrymakarov.ru\/wp-includes\/js\/wp-emoji-release.min.js?ver=6.6.2"}}; /*! This file is auto-generated */ !function(i,n){var o,s,e;function c(e){try{var t={supportTests:e,timestamp:(new Date).valueOf()};sessionStorage.setItem(o,JSON.stringify(t))}catch(e){}}function p(e,t,n){e.clearRect(0,0,e.canvas.width,e.canvas.height),e.fillText(t,0,0);var t=new Uint32Array(e.getImageData(0,0,e.canvas.width,e.canvas.height).data),r=(e.clearRect(0,0,e.canvas.width,e.canvas.height),e.fillText(n,0,0),new Uint32Array(e.getImageData(0,0,e.canvas.width,e.canvas.height).data));return t.every(function(e,t){return e===r[t]})}function u(e,t,n){switch(t){case"flag":return n(e,"\ud83c\udff3\ufe0f\u200d\u26a7\ufe0f","\ud83c\udff3\ufe0f\u200b\u26a7\ufe0f")?!1:!n(e,"\ud83c\uddfa\ud83c\uddf3","\ud83c\uddfa\u200b\ud83c\uddf3")&&!n(e,"\ud83c\udff4\udb40\udc67\udb40\udc62\udb40\udc65\udb40\udc6e\udb40\udc67\udb40\udc7f","\ud83c\udff4\u200b\udb40\udc67\u200b\udb40\udc62\u200b\udb40\udc65\u200b\udb40\udc6e\u200b\udb40\udc67\u200b\udb40\udc7f");case"emoji":return!n(e,"\ud83d\udc26\u200d\u2b1b","\ud83d\udc26\u200b\u2b1b")}return!1}function f(e,t,n){var r="undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope?new OffscreenCanvas(300,150):i.createElement("canvas"),a=r.getContext("2d",{willReadFrequently:!0}),o=(a.textBaseline="top",a.font="600 32px Arial",{});return e.forEach(function(e){o[e]=t(a,e,n)}),o}function t(e){var t=i.createElement("script");t.src=e,t.defer=!0,i.head.appendChild(t)}"undefined"!=typeof Promise&&(o="wpEmojiSettingsSupports",s=["flag","emoji"],n.supports={everything:!0,everythingExceptFlag:!0},e=new Promise(function(e){i.addEventListener("DOMContentLoaded",e,{once:!0})}),new Promise(function(t){var n=function(){try{var e=JSON.parse(sessionStorage.getItem(o));if("object"==typeof e&&"number"==typeof e.timestamp&&(new Date).valueOf()<e.timestamp+604800&&"object"==typeof e.supportTests)return e.supportTests}catch(e){}return null}();if(!n){if("undefined"!=typeof Worker&&"undefined"!=typeof OffscreenCanvas&&"undefined"!=typeof URL&&URL.createObjectURL&&"undefined"!=typeof Blob)try{var e="postMessage("+f.toString()+"("+[JSON.stringify(s),u.toString(),p.toString()].join(",")+"));",r=new Blob([e],{type:"text/javascript"}),a=new Worker(URL.createObjectURL(r),{name:"wpTestEmojiSupports"});return void(a.onmessage=function(e){c(n=e.data),a.terminate(),t(n)})}catch(e){}c(n=f(s,u,p))}t(n)}).then(function(e){for(var t in e)n.supports[t]=e[t],n.supports.everything=n.supports.everything&&n.supports[t],"flag"!==t&&(n.supports.everythingExceptFlag=n.supports.everythingExceptFlag&&n.supports[t]);n.supports.everythingExceptFlag=n.supports.everythingExceptFlag&&!n.supports.flag,n.DOMReady=!1,n.readyCallback=function(){n.DOMReady=!0}}).then(function(){return e}).then(function(){var e;n.supports.everything||(n.readyCallback(),(e=n.source||{}).concatemoji?t(e.concatemoji):e.wpemoji&&e.twemoji&&(t(e.twemoji),t(e.wpemoji)))}))}((window,document),window._wpemojiSettings); /* ]]> */ </script> <link rel='stylesheet' id='mci-footnotes-no-tooltips-pagelayout-none-css' href='https://www.dmitrymakarov.ru/wp-content/plugins/footnotes/css/footnotes-nottbrpl0.min.css?ver=2.7.3' type='text/css' media='all' /> <link rel='stylesheet' id='urvanov_syntax_highlighter-css' href='https://www.dmitrymakarov.ru/wp-content/plugins/urvanov-syntax-highlighter/css/min/urvanov_syntax_highlighter.min.css?ver=2.8.37' type='text/css' media='all' /> <link rel='stylesheet' id='crayon-theme-familiar-my-copy-css' href='https://www.dmitrymakarov.ru/wp-content/uploads/urvanov-syntax-highlighter/themes/familiar-my-copy/familiar-my-copy.css?ver=2.8.37' type='text/css' media='all' /> <link rel='stylesheet' id='crayon-font-consolas-css' href='https://www.dmitrymakarov.ru/wp-content/plugins/urvanov-syntax-highlighter/fonts/consolas.css?ver=2.8.37' type='text/css' media='all' /> <style id='wp-emoji-styles-inline-css' type='text/css'> img.wp-smiley, img.emoji { display: inline !important; border: none !important; box-shadow: none !important; height: 1em !important; width: 1em !important; margin: 0 0.07em !important; vertical-align: -0.1em !important; background: none !important; padding: 0 !important; } </style> <link rel='stylesheet' id='wp-block-library-css' href='https://www.dmitrymakarov.ru/wp-includes/css/dist/block-library/style.min.css?ver=6.6.2' type='text/css' media='all' /> <link rel='stylesheet' id='ultimate_blocks-cgb-style-css-css' href='https://www.dmitrymakarov.ru/wp-content/plugins/ultimate-blocks/dist/blocks.style.build.css?ver=672da50fcb061' type='text/css' media='all' /> <style id='classic-theme-styles-inline-css' type='text/css'> /*! This file is auto-generated */ .wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none} </style> <style id='global-styles-inline-css' type='text/css'> :root{--wp--preset--aspect-ratio--square: 1;--wp--preset--aspect-ratio--4-3: 4/3;--wp--preset--aspect-ratio--3-4: 3/4;--wp--preset--aspect-ratio--3-2: 3/2;--wp--preset--aspect-ratio--2-3: 2/3;--wp--preset--aspect-ratio--16-9: 16/9;--wp--preset--aspect-ratio--9-16: 9/16;--wp--preset--color--black: #121212;--wp--preset--color--cyan-bluish-gray: #abb8c3;--wp--preset--color--white: #fff;--wp--preset--color--pale-pink: #f78da7;--wp--preset--color--vivid-red: #cf2e2e;--wp--preset--color--luminous-vivid-orange: #ff6900;--wp--preset--color--luminous-vivid-amber: #fcb900;--wp--preset--color--light-green-cyan: #7bdcb5;--wp--preset--color--vivid-green-cyan: #00d084;--wp--preset--color--pale-cyan-blue: #8ed1fc;--wp--preset--color--vivid-cyan-blue: #0693e3;--wp--preset--color--vivid-purple: #9b51e0;--wp--preset--color--dark-gray: #333;--wp--preset--color--medium-gray: #555;--wp--preset--color--light-gray: #777;--wp--preset--gradient--vivid-cyan-blue-to-vivid-purple: linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%);--wp--preset--gradient--light-green-cyan-to-vivid-green-cyan: linear-gradient(135deg,rgb(122,220,180) 0%,rgb(0,208,130) 100%);--wp--preset--gradient--luminous-vivid-amber-to-luminous-vivid-orange: linear-gradient(135deg,rgba(252,185,0,1) 0%,rgba(255,105,0,1) 100%);--wp--preset--gradient--luminous-vivid-orange-to-vivid-red: linear-gradient(135deg,rgba(255,105,0,1) 0%,rgb(207,46,46) 100%);--wp--preset--gradient--very-light-gray-to-cyan-bluish-gray: linear-gradient(135deg,rgb(238,238,238) 0%,rgb(169,184,195) 100%);--wp--preset--gradient--cool-to-warm-spectrum: linear-gradient(135deg,rgb(74,234,220) 0%,rgb(151,120,209) 20%,rgb(207,42,186) 40%,rgb(238,44,130) 60%,rgb(251,105,98) 80%,rgb(254,248,76) 100%);--wp--preset--gradient--blush-light-purple: linear-gradient(135deg,rgb(255,206,236) 0%,rgb(152,150,240) 100%);--wp--preset--gradient--blush-bordeaux: linear-gradient(135deg,rgb(254,205,165) 0%,rgb(254,45,45) 50%,rgb(107,0,62) 100%);--wp--preset--gradient--luminous-dusk: linear-gradient(135deg,rgb(255,203,112) 0%,rgb(199,81,192) 50%,rgb(65,88,208) 100%);--wp--preset--gradient--pale-ocean: linear-gradient(135deg,rgb(255,245,203) 0%,rgb(182,227,212) 50%,rgb(51,167,181) 100%);--wp--preset--gradient--electric-grass: linear-gradient(135deg,rgb(202,248,128) 0%,rgb(113,206,126) 100%);--wp--preset--gradient--midnight: linear-gradient(135deg,rgb(2,3,129) 0%,rgb(40,116,252) 100%);--wp--preset--font-size--small: 16px;--wp--preset--font-size--medium: 20px;--wp--preset--font-size--large: 24px;--wp--preset--font-size--x-large: 42px;--wp--preset--font-size--normal: 18px;--wp--preset--font-size--larger: 28px;--wp--preset--spacing--20: 0.44rem;--wp--preset--spacing--30: 0.67rem;--wp--preset--spacing--40: 1rem;--wp--preset--spacing--50: 1.5rem;--wp--preset--spacing--60: 2.25rem;--wp--preset--spacing--70: 3.38rem;--wp--preset--spacing--80: 5.06rem;--wp--preset--shadow--natural: 6px 6px 9px rgba(0, 0, 0, 0.2);--wp--preset--shadow--deep: 12px 12px 50px rgba(0, 0, 0, 0.4);--wp--preset--shadow--sharp: 6px 6px 0px rgba(0, 0, 0, 0.2);--wp--preset--shadow--outlined: 6px 6px 0px -3px rgba(255, 255, 255, 1), 6px 6px rgba(0, 0, 0, 1);--wp--preset--shadow--crisp: 6px 6px 0px rgba(0, 0, 0, 1);}:where(.is-layout-flex){gap: 0.5em;}:where(.is-layout-grid){gap: 0.5em;}body .is-layout-flex{display: flex;}.is-layout-flex{flex-wrap: wrap;align-items: center;}.is-layout-flex > :is(*, div){margin: 0;}body .is-layout-grid{display: grid;}.is-layout-grid > :is(*, div){margin: 0;}:where(.wp-block-columns.is-layout-flex){gap: 2em;}:where(.wp-block-columns.is-layout-grid){gap: 2em;}:where(.wp-block-post-template.is-layout-flex){gap: 1.25em;}:where(.wp-block-post-template.is-layout-grid){gap: 1.25em;}.has-black-color{color: var(--wp--preset--color--black) !important;}.has-cyan-bluish-gray-color{color: var(--wp--preset--color--cyan-bluish-gray) !important;}.has-white-color{color: var(--wp--preset--color--white) !important;}.has-pale-pink-color{color: var(--wp--preset--color--pale-pink) !important;}.has-vivid-red-color{color: var(--wp--preset--color--vivid-red) !important;}.has-luminous-vivid-orange-color{color: var(--wp--preset--color--luminous-vivid-orange) !important;}.has-luminous-vivid-amber-color{color: var(--wp--preset--color--luminous-vivid-amber) !important;}.has-light-green-cyan-color{color: var(--wp--preset--color--light-green-cyan) !important;}.has-vivid-green-cyan-color{color: var(--wp--preset--color--vivid-green-cyan) !important;}.has-pale-cyan-blue-color{color: var(--wp--preset--color--pale-cyan-blue) !important;}.has-vivid-cyan-blue-color{color: var(--wp--preset--color--vivid-cyan-blue) !important;}.has-vivid-purple-color{color: var(--wp--preset--color--vivid-purple) !important;}.has-black-background-color{background-color: var(--wp--preset--color--black) !important;}.has-cyan-bluish-gray-background-color{background-color: var(--wp--preset--color--cyan-bluish-gray) !important;}.has-white-background-color{background-color: var(--wp--preset--color--white) !important;}.has-pale-pink-background-color{background-color: var(--wp--preset--color--pale-pink) !important;}.has-vivid-red-background-color{background-color: var(--wp--preset--color--vivid-red) !important;}.has-luminous-vivid-orange-background-color{background-color: var(--wp--preset--color--luminous-vivid-orange) !important;}.has-luminous-vivid-amber-background-color{background-color: var(--wp--preset--color--luminous-vivid-amber) !important;}.has-light-green-cyan-background-color{background-color: var(--wp--preset--color--light-green-cyan) !important;}.has-vivid-green-cyan-background-color{background-color: var(--wp--preset--color--vivid-green-cyan) !important;}.has-pale-cyan-blue-background-color{background-color: var(--wp--preset--color--pale-cyan-blue) !important;}.has-vivid-cyan-blue-background-color{background-color: var(--wp--preset--color--vivid-cyan-blue) !important;}.has-vivid-purple-background-color{background-color: var(--wp--preset--color--vivid-purple) !important;}.has-black-border-color{border-color: var(--wp--preset--color--black) !important;}.has-cyan-bluish-gray-border-color{border-color: var(--wp--preset--color--cyan-bluish-gray) !important;}.has-white-border-color{border-color: var(--wp--preset--color--white) !important;}.has-pale-pink-border-color{border-color: var(--wp--preset--color--pale-pink) !important;}.has-vivid-red-border-color{border-color: var(--wp--preset--color--vivid-red) !important;}.has-luminous-vivid-orange-border-color{border-color: var(--wp--preset--color--luminous-vivid-orange) !important;}.has-luminous-vivid-amber-border-color{border-color: var(--wp--preset--color--luminous-vivid-amber) !important;}.has-light-green-cyan-border-color{border-color: var(--wp--preset--color--light-green-cyan) !important;}.has-vivid-green-cyan-border-color{border-color: var(--wp--preset--color--vivid-green-cyan) !important;}.has-pale-cyan-blue-border-color{border-color: var(--wp--preset--color--pale-cyan-blue) !important;}.has-vivid-cyan-blue-border-color{border-color: var(--wp--preset--color--vivid-cyan-blue) !important;}.has-vivid-purple-border-color{border-color: var(--wp--preset--color--vivid-purple) !important;}.has-vivid-cyan-blue-to-vivid-purple-gradient-background{background: var(--wp--preset--gradient--vivid-cyan-blue-to-vivid-purple) !important;}.has-light-green-cyan-to-vivid-green-cyan-gradient-background{background: var(--wp--preset--gradient--light-green-cyan-to-vivid-green-cyan) !important;}.has-luminous-vivid-amber-to-luminous-vivid-orange-gradient-background{background: var(--wp--preset--gradient--luminous-vivid-amber-to-luminous-vivid-orange) !important;}.has-luminous-vivid-orange-to-vivid-red-gradient-background{background: var(--wp--preset--gradient--luminous-vivid-orange-to-vivid-red) !important;}.has-very-light-gray-to-cyan-bluish-gray-gradient-background{background: var(--wp--preset--gradient--very-light-gray-to-cyan-bluish-gray) !important;}.has-cool-to-warm-spectrum-gradient-background{background: var(--wp--preset--gradient--cool-to-warm-spectrum) !important;}.has-blush-light-purple-gradient-background{background: var(--wp--preset--gradient--blush-light-purple) !important;}.has-blush-bordeaux-gradient-background{background: var(--wp--preset--gradient--blush-bordeaux) !important;}.has-luminous-dusk-gradient-background{background: var(--wp--preset--gradient--luminous-dusk) !important;}.has-pale-ocean-gradient-background{background: var(--wp--preset--gradient--pale-ocean) !important;}.has-electric-grass-gradient-background{background: var(--wp--preset--gradient--electric-grass) !important;}.has-midnight-gradient-background{background: var(--wp--preset--gradient--midnight) !important;}.has-small-font-size{font-size: var(--wp--preset--font-size--small) !important;}.has-medium-font-size{font-size: var(--wp--preset--font-size--medium) !important;}.has-large-font-size{font-size: var(--wp--preset--font-size--large) !important;}.has-x-large-font-size{font-size: var(--wp--preset--font-size--x-large) !important;} :where(.wp-block-post-template.is-layout-flex){gap: 1.25em;}:where(.wp-block-post-template.is-layout-grid){gap: 1.25em;} :where(.wp-block-columns.is-layout-flex){gap: 2em;}:where(.wp-block-columns.is-layout-grid){gap: 2em;} :root :where(.wp-block-pullquote){font-size: 1.5em;line-height: 1.6;} </style> <link rel='stylesheet' id='ub-extension-style-css-css' href='https://www.dmitrymakarov.ru/wp-content/plugins/ultimate-blocks/src/extensions/style.css?ver=6.6.2' type='text/css' media='all' /> <link rel='stylesheet' id='mcluhan-fonts-css' href='https://www.dmitrymakarov.ru/wp-content/themes/mcluhan/assets/css/fonts.css?ver=6.6.2' type='text/css' media='all' /> <link rel='stylesheet' id='fontawesome-css' href='https://www.dmitrymakarov.ru/wp-content/themes/mcluhan/assets/css/font-awesome.css?ver=6.6.2' type='text/css' media='all' /> <link rel='stylesheet' id='mcluhan-style-css' href='https://www.dmitrymakarov.ru/wp-content/themes/mcluhan/style.css?ver=2.1.1' type='text/css' media='all' /> <script type="text/javascript" src="https://www.dmitrymakarov.ru/wp-includes/js/jquery/jquery.min.js?ver=3.7.1" id="jquery-core-js"></script> <script type="text/javascript" src="https://www.dmitrymakarov.ru/wp-includes/js/jquery/jquery-migrate.min.js?ver=3.4.1" id="jquery-migrate-js"></script> <script type="text/javascript" id="urvanov_syntax_highlighter_js-js-extra"> /* <![CDATA[ */ var UrvanovSyntaxHighlighterSyntaxSettings = {"version":"2.8.37","is_admin":"0","ajaxurl":"https:\/\/www.dmitrymakarov.ru\/wp-admin\/admin-ajax.php","prefix":"urvanov-syntax-highlighter-","setting":"urvanov-syntax-highlighter-setting","selected":"urvanov-syntax-highlighter-setting-selected","changed":"urvanov-syntax-highlighter-setting-changed","special":"urvanov-syntax-highlighter-setting-special","orig_value":"data-orig-value","debug":""}; var UrvanovSyntaxHighlighterSyntaxStrings = {"copy":"Copied to the clipboard","minimize":"Click To Expand Code"}; /* ]]> */ </script> <script type="text/javascript" src="https://www.dmitrymakarov.ru/wp-content/plugins/urvanov-syntax-highlighter/js/min/urvanov_syntax_highlighter.min.js?ver=2.8.37" id="urvanov_syntax_highlighter_js-js"></script> <style>#ub_table-of-contents-9d89a00d-4f15-42ac-9d1f-5197522d2e73{ }#ub_table-of-contents-9d89a00d-4f15-42ac-9d1f-5197522d2e73 .ub_table-of-contents-header{ text-align: left; } #ub-content-toggle-5adb461c-ef40-41be-ab28-dca2594c0395{ }#ub-content-toggle-5adb461c-ef40-41be-ab28-dca2594c0395 .wp-block-ub-content-toggle-accordion{ border-color: #f1f1f1; } #ub-content-toggle-5adb461c-ef40-41be-ab28-dca2594c0395 .wp-block-ub-content-toggle-accordion-title-wrap{ background-color: #f1f1f1; } #ub-content-toggle-5adb461c-ef40-41be-ab28-dca2594c0395 .wp-block-ub-content-toggle-accordion-title{ color: #000000; } #ub-content-toggle-5adb461c-ef40-41be-ab28-dca2594c0395 .wp-block-ub-content-toggle-accordion-toggle-wrap{ color: #000000; } .ub-content-toggle-title-2a8e7516-1153-4eee-a512-0d99db544a9b > a{ color: inherit; }#ub-content-toggle-f5a0548a-4eb2-478c-856c-7ef81ee85cb6{ }#ub-content-toggle-f5a0548a-4eb2-478c-856c-7ef81ee85cb6 .wp-block-ub-content-toggle-accordion{ border-color: #f1f1f1; } #ub-content-toggle-f5a0548a-4eb2-478c-856c-7ef81ee85cb6 .wp-block-ub-content-toggle-accordion-title-wrap{ background-color: #f1f1f1; } #ub-content-toggle-f5a0548a-4eb2-478c-856c-7ef81ee85cb6 .wp-block-ub-content-toggle-accordion-title{ color: #000000; } #ub-content-toggle-f5a0548a-4eb2-478c-856c-7ef81ee85cb6 .wp-block-ub-content-toggle-accordion-toggle-wrap{ color: #000000; } .ub-content-toggle-title-f0165add-deea-41b5-8c59-6f060393c8d5 > a{ color: inherit; }#ub-content-toggle-a242ec4d-135e-42a8-a357-129012abbcb7{ }#ub-content-toggle-a242ec4d-135e-42a8-a357-129012abbcb7 .wp-block-ub-content-toggle-accordion{ border-color: #f1f1f1; } #ub-content-toggle-a242ec4d-135e-42a8-a357-129012abbcb7 .wp-block-ub-content-toggle-accordion-title-wrap{ background-color: #f1f1f1; } #ub-content-toggle-a242ec4d-135e-42a8-a357-129012abbcb7 .wp-block-ub-content-toggle-accordion-title{ color: #000000; } #ub-content-toggle-a242ec4d-135e-42a8-a357-129012abbcb7 .wp-block-ub-content-toggle-accordion-toggle-wrap{ color: #000000; } .ub-content-toggle-title-4caf45ed-8fa8-49b3-9e08-b4b2594b4981 > a{ color: inherit; }#ub_divider_858ab9d1-310b-4809-ac50-0f0fc3480676{ }#ub_divider_858ab9d1-310b-4809-ac50-0f0fc3480676 .ub_divider{ border-top:2px solid #ccc; margin-top: 2px; margin-bottom: 2px; width: 60%; } </style> <!-- Customizer CSS --> <style type="text/css"> body .site-header { background-color:#004235; }.social-menu.desktop { background-color:#004235; }.social-menu a:hover { color:#004235; }.social-menu a.active { color:#004235; }.mobile-menu-wrapper { background-color:#004235; }.social-menu.mobile { background-color:#004235; }.mobile-search.active { background-color:#004235; } </style> <!-- /Customizer CSS --> <script>jQuery( 'html' ).removeClass( 'no-js' ).addClass( 'js' );</script> <style type="text/css">.broken_link, a.broken_link { text-decoration: line-through; }</style><style type="text/css" id="custom-background-css"> body.custom-background { background-color: #fff9ef; } </style> <link rel="icon" href="https://www.dmitrymakarov.ru/wp-content/uploads/2021/08/cropped-wp-favicon-512-32x32.png" sizes="32x32" /> <link rel="icon" href="https://www.dmitrymakarov.ru/wp-content/uploads/2021/08/cropped-wp-favicon-512-192x192.png" sizes="192x192" /> <link rel="apple-touch-icon" href="https://www.dmitrymakarov.ru/wp-content/uploads/2021/08/cropped-wp-favicon-512-180x180.png" /> <meta name="msapplication-TileImage" content="https://www.dmitrymakarov.ru/wp-content/uploads/2021/08/cropped-wp-favicon-512-270x270.png" /> <style type="text/css" media="all"> .footnotes_reference_container {margin-top: 24px !important; margin-bottom: 0px !important;} .footnote_container_prepare > p {border-bottom: 1px solid #aaaaaa !important;} </style> </head> <body class="page-template page-template-full-width-page-template page-template-full-width-page-template-php page page-id-6544 page-child parent-pageid-1374 custom-background full-width-template"> <a class="skip-link button" href="#site-content">Перейти к содержимому</a> <header class="site-header group"> <p class="site-title"><a href="https://www.dmitrymakarov.ru" class="site-name">Машинное обучение</a></p> <div class="nav-toggle"> <div class="bar"></div> <div class="bar"></div> </div> <div class="menu-wrapper"> <ul class="main-menu desktop"> </ul> </div><!-- .menu-wrapper --> <div class="social-menu desktop"> <ul class="social-menu-inner"> <li class="social-search-wrapper"><a href="https://www.dmitrymakarov.ru/?s="></a></li> </ul><!-- .social-menu-inner --> </div><!-- .social-menu --> </header><!-- header --> <div class="mobile-menu-wrapper"> <ul class="main-menu mobile"> <li class="toggle-mobile-search-wrapper"><a href="#" class="toggle-mobile-search">Поиск</a></li> </ul><!-- .main-menu.mobile --> </div><!-- .mobile-menu-wrapper --> <div class="mobile-search"> <div class="untoggle-mobile-search"></div> <form role="search" method="get" class="search-form" action="https://www.dmitrymakarov.ru/"> <label class="screen-reader-text" for="search-form-672da5100d144">Поиск:</label> <input type="search" id="search-form-672da5100d144" class="search-field" placeholder="Введите запрос" value="" name="s" autocomplete="off" /> </button> </form> <div class="mobile-results"> <div class="results-wrapper"></div> </div> </div><!-- .mobile-search --> <div class="search-overlay"> <form role="search" method="get" class="search-form" action="https://www.dmitrymakarov.ru/"> <label class="screen-reader-text" for="search-form-672da5100d1f1">Поиск:</label> <input type="search" id="search-form-672da5100d1f1" class="search-field" placeholder="Введите запрос" value="" name="s" autocomplete="off" /> </button> </form> </div><!-- .search-overlay --> <main class="site-content" id="site-content"> <article class="post-6544 page type-page status-publish hentry missing-thumbnail"> <header class="entry-header section-inner"> <h1 class="entry-title">Функции в Питоне</h1> </header><!-- .entry-header --> <div class="entry-content section-inner"> <p class="has-light-gray-color has-text-color"><a href="https://www.dmitrymakarov.ru/">Все курсы</a> > <a href="https://www.dmitrymakarov.ru/python/">Программирование на Питоне</a> > Занятие 6</p> <p>Функция в программировании не совсем то же самое, что функция в математике. В математике функция принимает одно значение и возвращает другое. В программировании — это программа в программе, отдельный участок кода, к которому можно обратиться по имени.</p> <div class="wp-block-image"> <figure class="aligncenter size-large"><img fetchpriority="high" decoding="async" width="1024" height="568" src="https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/f_math_prog-1024x568.jpg" alt="функции в математике и программировании" class="wp-image-6920" srcset="https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/f_math_prog-1024x568.jpg 1024w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/f_math_prog-300x166.jpg 300w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/f_math_prog-768x426.jpg 768w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/f_math_prog-1536x852.jpg 1536w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/f_math_prog-1200x666.jpg 1200w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/f_math_prog-600x333.jpg 600w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/f_math_prog.jpg 1948w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure></div> <p>Зачем это нужно? Если у вас есть код, который используется несколько раз в рамках программы, то функция позволяет написать код один раз, а потом просто <em>вызывать</em> его (call a function) при необходимости. Так мы не дублируем код и лучше его структурируем.</p> <div class="wp-block-ub-table-of-contents-block ub_table-of-contents" id="ub_table-of-contents-9d89a00d-4f15-42ac-9d1f-5197522d2e73" data-linktodivider="false" data-showtext="show" data-hidetext="hide" data-scrolltype="auto" data-initiallyhideonmobile="false" data-initiallyshow="true"><div class="ub_table-of-contents-header-container"><div class="ub_table-of-contents-header"> <div class="ub_table-of-contents-title"><strong>Содержание занятия</strong></div></div></div><div class="ub_table-of-contents-extra-container"><div class="ub_table-of-contents-container ub_table-of-contents-1-column "><ul><li><a href=https://www.dmitrymakarov.ru/python/functions-06/#0-vstroennye-funktsii>Встроенные функции</a><ul><li><a href=https://www.dmitrymakarov.ru/python/functions-06/#1-parametry-i-argumenty-funktsii>Параметры и аргументы функции</a></li><li><a href=https://www.dmitrymakarov.ru/python/functions-06/#2-funktsii-i-metody>Функции и методы</a></li></ul></li><li><a href=https://www.dmitrymakarov.ru/python/functions-06/#3-sobstvennye-funktsii-v-pitone>Собственные функции в Питоне</a><ul><li><a href=https://www.dmitrymakarov.ru/python/functions-06/#4-obyavlenie-i-vyzov-funktsii>Объявление и вызов функции</a></li><li><a href=https://www.dmitrymakarov.ru/python/functions-06/#5-pustoe-telo-funktsii>Пустое тело функции</a></li><li><a href=https://www.dmitrymakarov.ru/python/functions-06/#6-funktsiya-print-vmesto-return>Функция print() вместо return</a></li><li><a href=https://www.dmitrymakarov.ru/python/functions-06/#7-parametry-sobstvennykh-funktsiy>Параметры собственных функций</a></li><li><a href=https://www.dmitrymakarov.ru/python/functions-06/#8-annotatsiya-funktsii>Аннотация функции</a></li><li><a href=https://www.dmitrymakarov.ru/python/functions-06/#9-dopolnitelnye-vozmozhnosti-funktsiy>Дополнительные возможности функций</a></li><li><a href=https://www.dmitrymakarov.ru/python/functions-06/#10-rezultat-vyzova-funktsii>Результат вызова функции</a></li><li><a href=https://www.dmitrymakarov.ru/python/functions-06/#11-ispolzovanie-bibliotek>Использование библиотек</a></li><li><a href=https://www.dmitrymakarov.ru/python/functions-06/#12-globalnye-i-lokalnye-peremennye>Глобальные и локальные переменные</a></li></ul></li><li><a href=https://www.dmitrymakarov.ru/python/functions-06/#13-anonimnye-ili-lambda-funktsii>Анонимные или lambda-функции</a><ul><li><a href=https://www.dmitrymakarov.ru/python/functions-06/#14-lambda-funktsiya-vnutri-funktsii-filter>Lambda-функция внутри функции filter()</a></li><li><a href=https://www.dmitrymakarov.ru/python/functions-06/#15-lambda-funktsiya-vnutri-funktsii-sorted>Lambda-функция внутри функции sorted()</a></li><li><a href=https://www.dmitrymakarov.ru/python/functions-06/#16-nemedlenno-vyzyvaemye-funktsii>Немедленно вызываемые функции</a></li></ul></li><li><a href=https://www.dmitrymakarov.ru/python/functions-06/#17-args-i-kwargs>*args и **kwargs</a><ul><li><a href=https://www.dmitrymakarov.ru/python/functions-06/#18-args>*args</a></li><li><a href=https://www.dmitrymakarov.ru/python/functions-06/#19-kwargs>**kwargs</a></li></ul></li><li><a href=https://www.dmitrymakarov.ru/python/functions-06/#20-podvedem-itog>Подведем итог</a><ul><li><a href=https://www.dmitrymakarov.ru/python/functions-06/#21-voprosy-dlya-zakrepleniya>Вопросы для закрепления</a></li><li><a href=https://www.dmitrymakarov.ru/python/functions-06/#22-otvety-na-voprosy>Ответы на вопросы</a></li></ul></li></ul></div></div></div> <p class="has-text-align-center has-white-color has-text-color has-background" style="background-color:#004235"><a href="https://colab.research.google.com/drive/1sf87FVup1-g8WKI14JimRglUXefh1dJx?usp=sharing" target="_blank" rel="noreferrer noopener">Откроем блокнот к этому занятию</a>⧉</p> <h2 class="wp-block-heading" id="0-vstroennye-funktsii">Встроенные функции</h2> <p>В Питоне есть множество встроенных в (1) стандартный функционал (built-in functions) и (2) дополнительные библиотеки (library functions) функций, и мы много раз их использовали.</p> <p>Рассмотрим функцию для создания гистограммы <strong>plt.hist()</strong>. Вначале импортируем библиотеки.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d64333441965" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d64333441965-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d64333441965-2">2</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d64333441965-1"><span class="crayon-r">import</span><span class="crayon-h"> </span><span class="crayon-v">matplotlib</span><span class="crayon-sy">.</span><span class="crayon-e">pyplot </span><span class="crayon-st">as</span><span class="crayon-h"> </span><span class="crayon-e">plt</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d64333441965-2"><span class="crayon-r">import</span><span class="crayon-h"> </span><span class="crayon-e">numpy </span><span class="crayon-st">as</span><span class="crayon-h"> </span><span class="crayon-v">np</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <p>Сгенерируем данные, которые передадим этой функции (эти же данные мы создавали и использовали на <a href="https://www.dmitrymakarov.ru/intro/stats-08/">восьмом занятии</a> вводного курса).</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d6d308801911" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d6d308801911-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d6d308801911-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d6d308801911-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d6d308801911-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d6d308801911-5">5</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d6d308801911-1"><span class="crayon-c"># установим точку отсчета для воспроизведения такого же результата</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d6d308801911-2"><span class="crayon-v">np</span><span class="crayon-sy">.</span><span class="crayon-k ">random</span><span class="crayon-sy">.</span><span class="crayon-e">seed</span><span class="crayon-sy">(</span><span class="crayon-cn">42</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d6d308801911-3"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d6d308801911-4"><span class="crayon-c"># и сгенерируем данные о росте</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d6d308801911-5"><span class="crayon-v">height</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-k ">list</span><span class="crayon-sy">(</span><span class="crayon-v">np</span><span class="crayon-sy">.</span><span class="crayon-k ">round</span><span class="crayon-sy">(</span><span class="crayon-v">np</span><span class="crayon-sy">.</span><span class="crayon-k ">random</span><span class="crayon-sy">.</span><span class="crayon-e">normal</span><span class="crayon-sy">(</span><span class="crayon-cn">180</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">10</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">1000</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0003 seconds] --> </div> <p>Теперь построим гистограмму, передав ей в качестве параметров и аргументов наши данные и количество интервалов.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d6f418299974" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d6f418299974-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d6f418299974-2">2</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d6f418299974-1"><span class="crayon-v">plt</span><span class="crayon-sy">.</span><span class="crayon-e">hist</span><span class="crayon-sy">(</span><span class="crayon-v">height</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">bins</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-cn">10</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d6f418299974-2"><span class="crayon-v">plt</span><span class="crayon-sy">.</span><span class="crayon-e">show</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <figure class="wp-block-image size-full"><img decoding="async" width="375" height="248" src="https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/builtin_hist.png" alt="параметры функции plt.hist()" class="wp-image-6925" srcset="https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/builtin_hist.png 375w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/builtin_hist-300x198.png 300w" sizes="(max-width: 375px) 100vw, 375px" /></figure> <p>Как мы видим, достаточно обратиться к соответствующей библиотеке ( <span id="urvanov-syntax-highlighter-672da50fc2d70730052124" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline crayon-theme-familiar-my-copy crayon-theme-familiar-my-copy-inline urvanov-syntax-highlighter-font-consolas" style="font-size: 16px !important; line-height: 22px !important;font-size: 16px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 16px !important; line-height: 22px !important;font-size: 16px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">plt</span></span></span>), вызвать эту функцию по имени ( <span id="urvanov-syntax-highlighter-672da50fc2d71328949939" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline crayon-theme-familiar-my-copy crayon-theme-familiar-my-copy-inline urvanov-syntax-highlighter-font-consolas" style="font-size: 16px !important; line-height: 22px !important;font-size: 16px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 16px !important; line-height: 22px !important;font-size: 16px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">hist</span></span></span>) и задать параметры и их аргументы ( <span id="urvanov-syntax-highlighter-672da50fc2d72455702840" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline crayon-theme-familiar-my-copy crayon-theme-familiar-my-copy-inline urvanov-syntax-highlighter-font-consolas" style="font-size: 16px !important; line-height: 22px !important;font-size: 16px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 16px !important; line-height: 22px !important;font-size: 16px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">height</span></span></span>и <span id="urvanov-syntax-highlighter-672da50fc2d73338225839" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline crayon-theme-familiar-my-copy crayon-theme-familiar-my-copy-inline urvanov-syntax-highlighter-font-consolas" style="font-size: 16px !important; line-height: 22px !important;font-size: 16px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 16px !important; line-height: 22px !important;font-size: 16px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">bins</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-cn">10</span></span></span>), и будет исполнен тот код, который заложили в нее создатели библиотеки Matplotlib.</p> <p>Теперь несколько слов про параметры и аргументы функции.</p> <h3 class="wp-block-heading" id="1-parametry-i-argumenty-funktsii">Параметры и аргументы функции</h3> <p>Для начала определимся с терминами:</p> <ul class="wp-block-list"> <li><strong>параметр</strong> — это то, что запрашивает функция при вызове (например, <span id="urvanov-syntax-highlighter-672da50fc2d75206997320" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline crayon-theme-familiar-my-copy crayon-theme-familiar-my-copy-inline urvanov-syntax-highlighter-font-consolas" style="font-size: 16px !important; line-height: 22px !important;font-size: 16px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 16px !important; line-height: 22px !important;font-size: 16px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">bins</span></span></span>, количество интервалов) </li> <li><strong>аргумент</strong> — значение этого параметра (в нашем случае, <span id="urvanov-syntax-highlighter-672da50fc2d76485808423" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline crayon-theme-familiar-my-copy crayon-theme-familiar-my-copy-inline urvanov-syntax-highlighter-font-consolas" style="font-size: 16px !important; line-height: 22px !important;font-size: 16px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 16px !important; line-height: 22px !important;font-size: 16px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-cn">10</span></span></span>).</li> </ul> <p>Возникает вопрос, что же такое <span id="urvanov-syntax-highlighter-672da50fc2d77075063057" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline crayon-theme-familiar-my-copy crayon-theme-familiar-my-copy-inline urvanov-syntax-highlighter-font-consolas" style="font-size: 16px !important; line-height: 22px !important;font-size: 16px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 16px !important; line-height: 22px !important;font-size: 16px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">height</span></span></span>? Логично предположить, что это аргумент (ведь это наши данные). Но тогда как функция узнает, какому параметру он соответствует?</p> <p>Все дело в том, что параметры и их аргументы могут быть <strong>позиционными</strong> (positional) и <strong>именованными</strong> (keyword).</p> <div class="wp-block-image"> <figure class="aligncenter size-large is-resized"><img decoding="async" width="1024" height="350" src="https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/pos-key-1024x350.jpg" alt="позиционные и именованные аргументы функции" class="wp-image-7007" style="width:547px;height:187px" srcset="https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/pos-key-1024x350.jpg 1024w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/pos-key-300x103.jpg 300w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/pos-key-768x263.jpg 768w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/pos-key-1200x410.jpg 1200w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/pos-key-600x205.jpg 600w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/pos-key.jpg 1503w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure></div> <p>В первом случае, достаточно указать аргумент и поставить его в правильном порядке (позиции). Функция сама поймет, какой параметр ей передают. Во втором случае, нужно указать и название параметра, и аргумент.</p> <p>Позиционный параметр можно сделать именованным, и тогда порядок важен не будет.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d78466740937" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d78466740937-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d78466740937-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d78466740937-3">3</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d78466740937-1"><span class="crayon-c"># данные в этой функции обозначаются через x</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d78466740937-2"><span class="crayon-v">plt</span><span class="crayon-sy">.</span><span class="crayon-e">hist</span><span class="crayon-sy">(</span><span class="crayon-v">bins</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-cn">10</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">x</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">height</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d78466740937-3"><span class="crayon-v">plt</span><span class="crayon-sy">.</span><span class="crayon-e">show</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="375" height="248" src="https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/builtin_hist.png" alt="использование именованных параметров функции plt.hist()" class="wp-image-6925" srcset="https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/builtin_hist.png 375w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/builtin_hist-300x198.png 300w" sizes="(max-width: 375px) 100vw, 375px" /></figure> <p>Кроме того, функция может иметь параметры с <strong>аргументами по умолчанию</strong>. Это делает такой параметр <em>не обязательным</em>, а значит <em>упрощает</em> и <em>ускоряет</em> вызов функции.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d79964468658" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d79964468658-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d79964468658-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d79964468658-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d79964468658-4">4</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d79964468658-1"><span class="crayon-c"># у параметра bins есть аргумент по умолчанию (как раз 10 интервалов)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d79964468658-2"><span class="crayon-c"># а значит, этот параметр можно не указывать</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d79964468658-3"><span class="crayon-v">plt</span><span class="crayon-sy">.</span><span class="crayon-e">hist</span><span class="crayon-sy">(</span><span class="crayon-v">height</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d79964468658-4"><span class="crayon-v">plt</span><span class="crayon-sy">.</span><span class="crayon-e">show</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="375" height="248" src="https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/builtin_hist.png" alt="аргументы по умолчанию функции plt.hist()" class="wp-image-6925" srcset="https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/builtin_hist.png 375w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/builtin_hist-300x198.png 300w" sizes="(max-width: 375px) 100vw, 375px" /></figure> <p>Как вы видите, результат во всех трех случаях совершенно одинаковый.</p> <p class="has-background" style="background-color:#ffe8c4">Если вы сомневаетесь в том, какие параметры принимает функция и что является результатом ее работы, полезно обратиться к документации в Интернете. Например, по <strong>функции plt.hist()</strong> ее можно <a href="https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hist.html" target="_blank" rel="noreferrer noopener">найти вот здесь</a>⧉.</p> <p>Стоит отметить, что функция может как принимать один, два или несколько параметров, так и <strong>не принимать их вовсе</strong>. Например, <strong>функция print()</strong>, если не указывать параметры, выдает пустую строку.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d7a556755436" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d7a556755436-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d7a556755436-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d7a556755436-3">3</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d7a556755436-1"><span class="crayon-k ">print</span><span class="crayon-sy">(</span><span class="crayon-s">'Первая строка'</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d7a556755436-2"><span class="crayon-k ">print</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d7a556755436-3"><span class="crayon-k ">print</span><span class="crayon-sy">(</span><span class="crayon-s">'Третья строка'</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d7b015457067" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d7b015457067-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d7b015457067-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d7b015457067-3">3</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d7b015457067-1">Первая строка</div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d7b015457067-2"> </div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d7b015457067-3">Третья строка</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <h3 class="wp-block-heading" id="2-funktsii-i-metody">Функции и методы</h3> <p>Некоторые функции называются методами. Методы — это функции, которые можно применить только к конкретному объекту. Другими словами, если обычная функция будет выполнена «сама по себе», это просто участок кода, которому дали имя, то методу для исполнения нужен объект (например, строка, список или словарь). При этом, что важно, у каждого объекта свои методы.</p> <p>Предположим, у нас есть строка, и мы хотим сделать первую букву каждого слова заглавной. Для этого у строки есть <strong>метод .title()</strong>.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d7d704964766" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d7d704964766-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d7d704964766-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d7d704964766-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d7d704964766-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d7d704964766-5">5</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d7d704964766-1"><span class="crayon-c"># создаем строковый объект и</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d7d704964766-2"><span class="crayon-v">some_string</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-s">'machine learning'</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d7d704964766-3"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d7d704964766-4"><span class="crayon-c"># применяем к нему метод .title()</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d7d704964766-5"><span class="crayon-v">some_string</span><span class="crayon-sy">.</span><span class="crayon-e">title</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d7e247213311" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d7e247213311-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d7e247213311-1">'Machine Learning'</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>А теперь попробуем применить этот метод к списку, состоящему из тех же слов, что и строка.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d7f856221504" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d7f856221504-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d7f856221504-2">2</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d7f856221504-1"><span class="crayon-v">some_list</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-sy">[</span><span class="crayon-s">'machine'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-s">'learning'</span><span class="crayon-sy">]</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d7f856221504-2"><span class="crayon-v">some_list</span><span class="crayon-sy">.</span><span class="crayon-e">title</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" width="892" height="263" src="https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/m-error.jpg" alt="ошибка при применении строкового метода .title() к списку" class="wp-image-7026" style="width:708px;height:208px" srcset="https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/m-error.jpg 892w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/m-error-300x88.jpg 300w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/m-error-768x226.jpg 768w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/m-error-600x177.jpg 600w" sizes="(max-width: 892px) 100vw, 892px" /></figure> <p>Как мы видим, Питон выдал ошибку.</p> <h2 class="wp-block-heading" id="3-sobstvennye-funktsii-v-pitone">Собственные функции в Питоне</h2> <h3 class="wp-block-heading" id="4-obyavlenie-i-vyzov-funktsii">Объявление и вызов функции</h3> <p>Функции не обязательно должны быть встроены в базовый функционал или библиотеки. Мы вполне можем объявлять (т.е. создавать) собственные функции (user-defined functions). Рассмотрим пример.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d80746580076" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d80746580076-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d80746580076-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d80746580076-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d80746580076-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d80746580076-5">5</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d80746580076-1"><span class="crayon-c"># создадим функцию, которая удваивает </span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d80746580076-2"><span class="crayon-c"># любое передаваемое ей значение</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d80746580076-3"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">double</span><span class="crayon-sy">(</span><span class="crayon-v">x</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d80746580076-4"><span class="crayon-h"> </span><span class="crayon-v">res</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">x</span><span class="crayon-h"> </span><span class="crayon-o">*</span><span class="crayon-h"> </span><span class="crayon-cn">2</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d80746580076-5"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-v">res</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d81093513300" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d81093513300-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d81093513300-2">2</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d81093513300-1"><span class="crayon-c"># и вызовем ее, передав число 2</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d81093513300-2"><span class="crayon-e">double</span><span class="crayon-sy">(</span><span class="crayon-cn">2</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d82304408623" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d82304408623-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d82304408623-1">4</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Теперь давайте разберем каждый элемент этого кода. Вначале посмотрим как <strong>объявить функцию</strong> (declare a function). </p> <figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" width="1024" height="527" src="https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/create_f-1024x527.jpg" alt="синтаксис собственной функции" class="wp-image-7059" style="width:772px;height:397px" srcset="https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/create_f-1024x527.jpg 1024w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/create_f-300x154.jpg 300w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/create_f-768x395.jpg 768w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/create_f-1536x790.jpg 1536w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/create_f-1200x617.jpg 1200w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/create_f-600x309.jpg 600w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/create_f.jpg 1814w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure> <ul class="wp-block-list"> <li>ключевое слово <strong>def</strong> необходимо для объявления функции;</li> <li>далее идут <strong>название функции</strong>, которое вы сами определяете; и</li> <li><strong>параметры</strong>, которые может принимать ваша функция;</li> <li>после двоеточия на новой строке с отступом идет так называемое <strong>тело функции</strong>, то есть то, что будет исполняться при вызове функции;</li> <li>в конце ставится ключевое слово <strong>return</strong>, возвращающее результат работы функции.</li> </ul> <p>Затем, когда это необходимо, мы можем <strong>вызвать функцию</strong> (call a function), указав ее имя и передав, при их наличии, соответствующие аргументы внутри круглых скобок.</p> <figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" width="985" height="490" src="https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/call_f.jpg" alt="вызов собственной функции" class="wp-image-7067" style="width:404px;height:200px" srcset="https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/call_f.jpg 985w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/call_f-300x149.jpg 300w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/call_f-768x382.jpg 768w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/call_f-600x298.jpg 600w" sizes="(max-width: 985px) 100vw, 985px" /></figure> <h3 class="wp-block-heading" id="5-pustoe-telo-funktsii">Пустое тело функции</h3> <p>Оставлять тело функции совсем пустым нельзя. Нужно как минимум указать ключевое слово <strong>return</strong> или оператор <strong>pass</strong>.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d83023513945" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d83023513945-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d83023513945-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d83023513945-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d83023513945-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d83023513945-5">5</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d83023513945-1"><span class="crayon-c"># тело функции не может быть пустым</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d83023513945-2"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">only_return</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d83023513945-3"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d83023513945-4"><span class="crayon-h"> </span><span class="crayon-c"># нужно либо указать ключевое слово return</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d83023513945-5"><span class="crayon-h"> </span><span class="crayon-st">return</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d84107731464" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d84107731464-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d84107731464-1"><span class="crayon-e">only_return</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d86339031744" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d86339031744-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d86339031744-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d86339031744-3">3</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d86339031744-1"><span class="crayon-c"># либо оператор pass</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d86339031744-2"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">only_pass</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d86339031744-3"><span class="crayon-h"> </span><span class="crayon-r">pass</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d87518540962" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d87518540962-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d87518540962-1"><span class="crayon-e">only_pass</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <p>Хотя может показаться, что в этом случае функция ничего не выдает, на самом деле это не так. И в том, и в другом случае функция возвращает особый тип данных <strong>None</strong> (отсутствие значения).</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d88159212242" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d88159212242-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d88159212242-1"><span class="crayon-k ">print</span><span class="crayon-sy">(</span><span class="crayon-e">only_return</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d89224943634" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d89224943634-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d89224943634-1">None</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <h3 class="wp-block-heading" id="6-funktsiya-print-vmesto-return">Функция print() вместо return</h3> <p>Помимо ключевого слова <strong>return</strong>, результат работы функции можно вывести с помощью <strong>print()</strong>.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d8a066222439" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d8a066222439-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d8a066222439-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d8a066222439-3">3</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d8a066222439-1"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">double_print</span><span class="crayon-sy">(</span><span class="crayon-v">x</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d8a066222439-2"><span class="crayon-h"> </span><span class="crayon-v">res</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">x</span><span class="crayon-h"> </span><span class="crayon-o">*</span><span class="crayon-h"> </span><span class="crayon-cn">2</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d8a066222439-3"><span class="crayon-h"> </span><span class="crayon-k ">print</span><span class="crayon-sy">(</span><span class="crayon-v">res</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d8b948953248" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d8b948953248-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d8b948953248-1"><span class="crayon-e">double_print</span><span class="crayon-sy">(</span><span class="crayon-cn">5</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d8c344094581" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d8c344094581-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d8c344094581-1">10</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Хотя визуально вывод идентичен, отличие все-таки есть:</p> <ul class="wp-block-list"> <li>Использование <strong>return</strong> возвращает значение функции (в нашем случае значение переменной res) и прерывает ее работу;</li> <li>Функция <strong>print()</strong> просто выводит это значение пользователю и не влияет на дальнейшее исполнение кода, если он есть.</li> </ul> <h3 class="wp-block-heading" id="7-parametry-sobstvennykh-funktsiy">Параметры собственных функций</h3> <p>С точки зрения параметров, у собственных функций те же самые возможности, что и у встроенных функций. В частности, параметры могут быть позиционными и именованными.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d8d469180119" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d8d469180119-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d8d469180119-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d8d469180119-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d8d469180119-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d8d469180119-5">5</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d8d469180119-1"><span class="crayon-c"># объявим функцию с параметрами x и y,</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d8d469180119-2"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">calc_sum</span><span class="crayon-sy">(</span><span class="crayon-v">x</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">y</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d8d469180119-3"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d8d469180119-4"><span class="crayon-h"> </span><span class="crayon-c"># которая возвращает их сумму</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d8d469180119-5"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-v">x</span><span class="crayon-h"> </span><span class="crayon-o">+</span><span class="crayon-h"> </span><span class="crayon-v">y</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d8e070080440" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d8e070080440-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d8e070080440-2">2</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d8e070080440-1"><span class="crayon-c"># вызовем эту функцию с одним позиционным и одним именованным параметром</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d8e070080440-2"><span class="crayon-e">calc_sum</span><span class="crayon-sy">(</span><span class="crayon-cn">1</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">y</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-cn">2</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d8f417266924" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d8f417266924-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d8f417266924-1">3</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Параметры собственной функции также могут быть заданы по умолчанию. В этом случае при вызове функции их указывать не обязательно.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d90633275419" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d90633275419-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d90633275419-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d90633275419-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d90633275419-4">4</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d90633275419-1"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">calc_sum_default</span><span class="crayon-sy">(</span><span class="crayon-v">x</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-cn">1</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">y</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-cn">2</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d90633275419-2"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-v">x</span><span class="crayon-h"> </span><span class="crayon-o">+</span><span class="crayon-h"> </span><span class="crayon-i">y</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d90633275419-3"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d90633275419-4"><span class="crayon-e">calc_sum_default</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d91220293752" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d91220293752-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d91220293752-1">3</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>И конечно функция может изначально не иметь параметров.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d92938751949" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d92938751949-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d92938751949-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d92938751949-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d92938751949-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d92938751949-5">5</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d92938751949-1"><span class="crayon-c"># эта функция просто выводит текст 'Some string'</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d92938751949-2"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">print_string</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d92938751949-3"><span class="crayon-h"> </span><span class="crayon-k ">print</span><span class="crayon-sy">(</span><span class="crayon-s">'Some string'</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d92938751949-4"> </div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d92938751949-5"><span class="crayon-e">print_string</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d93343841408" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d93343841408-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d93343841408-1">Some string</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <h3 class="wp-block-heading" id="8-annotatsiya-funktsii">Аннотация функции</h3> <p>Аннотация функции (function annotation) позволяет явно прописать тип данных <em>параметров</em> (parameter annotation) и <em>возвращаемых значений</em> (return annotation).</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d94587060018" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d94587060018-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d94587060018-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d94587060018-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d94587060018-4">4</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d94587060018-1"><span class="crayon-c"># укажем, что на входе функция принимает тип float, а возвращает int</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d94587060018-2"><span class="crayon-c"># значение 3,5 - это значение параметра x по умолчанию</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d94587060018-3"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">f</span><span class="crayon-sy">(</span><span class="crayon-v">x</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-k ">float</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-cn">3.5</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-o">-></span><span class="crayon-h"> </span><span class="crayon-k ">int</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d94587060018-4"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-k ">int</span><span class="crayon-sy">(</span><span class="crayon-v">x</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d95436788242" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d95436788242-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d95436788242-2">2</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d95436788242-1"><span class="crayon-c"># желаемый тип данных можно посмотреть через атрибут __annotations__</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d95436788242-2"><span class="crayon-v">f</span><span class="crayon-sy">.</span><span class="crayon-v">__annotations__</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d96803770314" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d96803770314-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d96803770314-1">{'return': int, 'x': float}</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d97152736570" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d97152736570-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d97152736570-2">2</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d97152736570-1"><span class="crayon-c"># вызовем функцию без параметров</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d97152736570-2"><span class="crayon-e">f</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d98776609264" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d98776609264-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d98776609264-1">3</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Аннотация не является обязательной и никак не вляет на выполнение кода.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d99016756045" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d99016756045-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d99016756045-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d99016756045-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d99016756045-4">4</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d99016756045-1"><span class="crayon-c"># сохраним аннотации, но изменим суть функции</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d99016756045-2"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">f</span><span class="crayon-sy">(</span><span class="crayon-v">x</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-k ">float</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-o">-></span><span class="crayon-h"> </span><span class="crayon-k ">int</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d99016756045-3"><span class="crayon-h"> </span><span class="crayon-c"># теперь вместо int она будет возвращать float</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d99016756045-4"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-k ">float</span><span class="crayon-sy">(</span><span class="crayon-v">x</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d9a515952150" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d9a515952150-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d9a515952150-2">2</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d9a515952150-1"><span class="crayon-c"># вновь вызовем функцию, передав ей на входе int, и ожидая на выходе получить float</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d9a515952150-2"><span class="crayon-e">f</span><span class="crayon-sy">(</span><span class="crayon-cn">3</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d9b960444820" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d9b960444820-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d9b960444820-1">3.0</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <h3 class="wp-block-heading" id="9-dopolnitelnye-vozmozhnosti-funktsiy">Дополнительные возможности функций</h3> <p>Вызов функции можно совмещать с арифметическими операциями.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d9c699125243" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d9c699125243-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2d9c699125243-2">2</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d9c699125243-1"><span class="crayon-c"># вызовем объявленную выше функцию и умножим ее вывод на два</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2d9c699125243-2"><span class="crayon-e">calc_sum</span><span class="crayon-sy">(</span><span class="crayon-cn">1</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">2</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-o">*</span><span class="crayon-h"> </span><span class="crayon-cn">2</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d9d377263187" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d9d377263187-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d9d377263187-1">6</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Доступны и логические операции.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d9e451291676" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d9e451291676-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d9e451291676-1"><span class="crayon-e">calc_sum</span><span class="crayon-sy">(</span><span class="crayon-cn">1</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">2</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-cn">2</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2d9f048848391" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2d9f048848391-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2d9f048848391-1">True</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Если результатом вывода является строка, то у этой строки также есть индекс.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2da0887490835" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da0887490835-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2da0887490835-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da0887490835-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2da0887490835-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da0887490835-5">5</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da0887490835-1"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">first_letter</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2da0887490835-2"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-s">'Python'</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da0887490835-3"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2da0887490835-4"><span class="crayon-c"># обратимся к первой букве слова Python</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da0887490835-5"><span class="crayon-e">first_letter</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-sy">[</span><span class="crayon-cn">0</span><span class="crayon-sy">]</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2da1938820518" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da1938820518-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da1938820518-1">'P'</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Функция может не использовать параметры, но получать данные от пользователя через <strong>input()</strong>.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2da2328857514" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da2328857514-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2da2328857514-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da2328857514-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2da2328857514-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da2328857514-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2da2328857514-6">6</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da2328857514-7">7</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2da2328857514-8">8</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da2328857514-9">9</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2da2328857514-10">10</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da2328857514-11">11</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2da2328857514-12">12</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da2328857514-13">13</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da2328857514-1"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">use_input</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2da2328857514-2"> </div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da2328857514-3"><span class="crayon-h"> </span><span class="crayon-c"># запросим у пользователя число и переведем его в тип данных int</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2da2328857514-4"><span class="crayon-h"> </span><span class="crayon-v">user_inp</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-k ">int</span><span class="crayon-sy">(</span><span class="crayon-k ">input</span><span class="crayon-sy">(</span><span class="crayon-s">'Введите число: '</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da2328857514-5"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2da2328857514-6"><span class="crayon-h"> </span><span class="crayon-c"># возведем число в квадрат</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da2328857514-7"><span class="crayon-h"> </span><span class="crayon-v">result</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">user_inp</span><span class="crayon-h"> </span><span class="crayon-o">*</span><span class="crayon-o">*</span><span class="crayon-h"> </span><span class="crayon-cn">2</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2da2328857514-8"> </div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da2328857514-9"><span class="crayon-h"> </span><span class="crayon-c"># вернем результат</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2da2328857514-10"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-i">result</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da2328857514-11"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2da2328857514-12"><span class="crayon-c"># вызовем функцию</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da2328857514-13"><span class="crayon-e">use_input</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0003 seconds] --> </div> <p>Появится окно для ввода числа.</p> <figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" width="511" height="71" src="https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/inp_f.jpg" alt="функция может получать данные от пользователя" class="wp-image-7046" style="width:338px;height:47px" srcset="https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/inp_f.jpg 511w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/inp_f-300x42.jpg 300w" sizes="(max-width: 511px) 100vw, 511px" /></figure> <p>Введем число 5 и посмотрим на результат.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2da3823643820" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da3823643820-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2da3823643820-2">2</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da3823643820-1">Введите число: 5</div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2da3823643820-2">25</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <h3 class="wp-block-heading" id="10-rezultat-vyzova-funktsii">Результат вызова функции</h3> <p>Функция не обязательно возвращает только число или строку, это может быть, в частности, список, кортеж или словарь. Рассмотрим несколько примеров.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2da4028069694" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da4028069694-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2da4028069694-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da4028069694-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2da4028069694-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da4028069694-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2da4028069694-6">6</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da4028069694-7">7</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2da4028069694-8">8</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da4028069694-9">9</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2da4028069694-10">10</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da4028069694-11">11</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2da4028069694-12">12</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da4028069694-13">13</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2da4028069694-14">14</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da4028069694-15">15</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2da4028069694-16">16</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da4028069694-17">17</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2da4028069694-18">18</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da4028069694-1"><span class="crayon-c"># объявим функцию, которая на входе получает число,</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2da4028069694-2"><span class="crayon-c"># а на выходе формирует список чисел от 0 до числа, </span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da4028069694-3"><span class="crayon-c"># предшествующего заданному</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2da4028069694-4"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">create_list</span><span class="crayon-sy">(</span><span class="crayon-v">x</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da4028069694-5"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2da4028069694-6"><span class="crayon-h"> </span><span class="crayon-c"># создадим пустой список</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da4028069694-7"><span class="crayon-h"> </span><span class="crayon-v">l</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-sy">[</span><span class="crayon-sy">]</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2da4028069694-8"> </div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da4028069694-9"><span class="crayon-h"> </span><span class="crayon-c"># в цикле for создадим последовательность</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2da4028069694-10"><span class="crayon-h"> </span><span class="crayon-st">for</span><span class="crayon-h"> </span><span class="crayon-i">i</span><span class="crayon-h"> </span><span class="crayon-st">in</span><span class="crayon-h"> </span><span class="crayon-k ">range</span><span class="crayon-sy">(</span><span class="crayon-v">x</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da4028069694-11"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2da4028069694-12"><span class="crayon-h"> </span><span class="crayon-c"># и поместим ее в список</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da4028069694-13"><span class="crayon-h"> </span><span class="crayon-v">l</span><span class="crayon-sy">.</span><span class="crayon-e">append</span><span class="crayon-sy">(</span><span class="crayon-v">i</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2da4028069694-14"> </div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da4028069694-15"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-i">l</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2da4028069694-16"> </div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da4028069694-17"><span class="crayon-c"># результатом вызова этой функции будет список</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2da4028069694-18"><span class="crayon-e">create_list</span><span class="crayon-sy">(</span><span class="crayon-cn">5</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0003 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2da5899313232" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da5899313232-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da5899313232-1">[0, 1, 2, 3, 4]</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Функция может возвращать сразу несколько значений.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2da6598894313" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da6598894313-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2da6598894313-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da6598894313-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2da6598894313-4">4</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da6598894313-1"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">tuple_f</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2da6598894313-2"><span class="crayon-h"> </span><span class="crayon-k ">string</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-s">'Python'</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da6598894313-3"><span class="crayon-h"> </span><span class="crayon-v">x</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-cn">42</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2da6598894313-4"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-k ">string</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">x</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <p>Если результат вызова этой функции записать в две переменных, то ожидаемо в них окажется строка и число.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2da7863228911" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da7863228911-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2da7863228911-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da7863228911-3">3</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da7863228911-1"><span class="crayon-v">a</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">b</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">tuple_f</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2da7863228911-2"><span class="crayon-k ">print</span><span class="crayon-sy">(</span><span class="crayon-v">a</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">b</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da7863228911-3"><span class="crayon-k ">print</span><span class="crayon-sy">(</span><span class="crayon-k ">type</span><span class="crayon-sy">(</span><span class="crayon-v">a</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-k ">type</span><span class="crayon-sy">(</span><span class="crayon-v">b</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2da8603316297" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da8603316297-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2da8603316297-2">2</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da8603316297-1">Python 42</div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2da8603316297-2"><class 'str'> <class 'int'></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Если записать в одну, то получится кортеж.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2da9473145682" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da9473145682-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2da9473145682-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2da9473145682-3">3</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da9473145682-1"><span class="crayon-v">c</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-e">tuple_f</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2da9473145682-2"><span class="crayon-k ">print</span><span class="crayon-sy">(</span><span class="crayon-v">c</span><span class="crayon-sy">)</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2da9473145682-3"><span class="crayon-k ">print</span><span class="crayon-sy">(</span><span class="crayon-k ">type</span><span class="crayon-sy">(</span><span class="crayon-v">c</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2daa366189119" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2daa366189119-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2daa366189119-2">2</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2daa366189119-1">('Python', 42)</div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2daa366189119-2"><class 'tuple'></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Функция может возвращать и логическое значение. Давайте объявим функцию, которая проверяет четное ли ей передали число (и в этом случае вернет True) или нечетное (и тогда False).</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dab523233296" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dab523233296-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dab523233296-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dab523233296-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dab523233296-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dab523233296-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dab523233296-6">6</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dab523233296-7">7</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dab523233296-8">8</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dab523233296-9">9</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dab523233296-1"><span class="crayon-c"># проверим равен ли нулю остаток от деления на два</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dab523233296-2"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">if_divisible</span><span class="crayon-sy">(</span><span class="crayon-v">x</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dab523233296-3"><span class="crayon-h"> </span><span class="crayon-st">if</span><span class="crayon-h"> </span><span class="crayon-v">x</span><span class="crayon-h"> </span><span class="crayon-o">%</span><span class="crayon-h"> </span><span class="crayon-cn">2</span><span class="crayon-h"> </span><span class="crayon-o">==</span><span class="crayon-h"> </span><span class="crayon-cn">0</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dab523233296-4"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-t">True</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dab523233296-5"><span class="crayon-h"> </span><span class="crayon-st">else</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dab523233296-6"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-t">False</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dab523233296-7"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dab523233296-8"><span class="crayon-c"># попробуем с числом 10</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dab523233296-9"><span class="crayon-e">if_divisible</span><span class="crayon-sy">(</span><span class="crayon-cn">10</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0003 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dac374616857" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dac374616857-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dac374616857-1">True</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <h3 class="wp-block-heading" id="11-ispolzovanie-bibliotek">Использование библиотек</h3> <p>Внутри функций можно использовать дополнительные библиотеки Питона. Например, применим <strong>функцию mean()</strong> библиотеки Numpy для расчета среднего арифметического.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dad508266637" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dad508266637-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dad508266637-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dad508266637-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dad508266637-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dad508266637-5">5</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dad508266637-1"><span class="crayon-c"># на входе функция примет список или массив x,</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dad508266637-2"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">mean_f</span><span class="crayon-sy">(</span><span class="crayon-v">x</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dad508266637-3"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dad508266637-4"><span class="crayon-h"> </span><span class="crayon-c"># рассчитает среднее арифметическое и прибавит единицу</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dad508266637-5"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-v">np</span><span class="crayon-sy">.</span><span class="crayon-e">mean</span><span class="crayon-sy">(</span><span class="crayon-v">x</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-o">+</span><span class="crayon-h"> </span><span class="crayon-cn">1</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dae507132927" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dae507132927-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dae507132927-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dae507132927-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dae507132927-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dae507132927-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dae507132927-6">6</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dae507132927-7">7</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dae507132927-1"><span class="crayon-c"># перед вызовом функции нужно не забыть импортировать соответствующую библиотеку</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dae507132927-2"><span class="crayon-r">import</span><span class="crayon-h"> </span><span class="crayon-e">numpy </span><span class="crayon-st">as</span><span class="crayon-h"> </span><span class="crayon-i">np</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dae507132927-3"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dae507132927-4"><span class="crayon-c"># и подготовить данные</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dae507132927-5"><span class="crayon-v">x</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-sy">[</span><span class="crayon-cn">1</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">2</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">3</span><span class="crayon-sy">]</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dae507132927-6"> </div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dae507132927-7"><span class="crayon-e">mean_f</span><span class="crayon-sy">(</span><span class="crayon-v">x</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2db0246480550" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2db0246480550-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2db0246480550-1">3.0</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <h3 class="wp-block-heading" id="12-globalnye-i-lokalnye-peremennye">Глобальные и локальные переменные</h3> <p>Некоторые переменные существуют (или как еще говорят, видны) только внутри функции, другие во всей программе. В первом случае, говорят про локальные переменные (local variables), во втором — про глобальные (global variables). Сразу приведем несложный пример.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2db1597731731" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2db1597731731-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2db1597731731-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2db1597731731-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2db1597731731-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2db1597731731-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2db1597731731-6">6</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2db1597731731-1"><span class="crayon-c"># создадим глобальную переменную ВНЕ функции</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2db1597731731-2"><span class="crayon-v">global_name</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-s">'Петр'</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2db1597731731-3"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2db1597731731-4"><span class="crayon-c"># а затем используем ее внутри новой функции</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2db1597731731-5"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">show_name</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2db1597731731-6"><span class="crayon-h"> </span><span class="crayon-k ">print</span><span class="crayon-sy">(</span><span class="crayon-v">global_name</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2db2472805913" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2db2472805913-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2db2472805913-1"><span class="crayon-e">show_name</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2db3795616045" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2db3795616045-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2db3795616045-1">Петр</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Здесь никаких проблем не возникло. Мы создали глобальную переменную и потом легко использовали ее внутри очередной функции. Обратное не всегда возможно.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2db4352220479" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2db4352220479-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2db4352220479-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2db4352220479-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2db4352220479-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2db4352220479-5">5</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2db4352220479-1"><span class="crayon-c"># а теперь вначале создадим функцию,</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2db4352220479-2"><span class="crayon-c"># внутри которой объявим локальную переменную</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2db4352220479-3"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">show_local_name</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2db4352220479-4"><span class="crayon-h"> </span><span class="crayon-v">local_name</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-s">'Алена'</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2db4352220479-5"><span class="crayon-h"> </span><span class="crayon-k ">print</span><span class="crayon-sy">(</span><span class="crayon-v">local_name</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2db5537277631" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2db5537277631-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2db5537277631-1"><span class="crayon-e">show_local_name</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2db6450582219" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2db6450582219-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2db6450582219-1">Алена</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Функция была вызвана без проблем. При этом если мы попробуем обратиться к переменной local_name вне этой функции, Питон выдаст ошибку.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2db7831831296" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2db7831831296-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2db7831831296-1"><span class="crayon-v">local_name</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" width="918" height="242" src="https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/local_error.jpg" alt="ошибка при вызове локальной переменной вне своей функции" class="wp-image-7074" style="width:718px;height:189px" srcset="https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/local_error.jpg 918w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/local_error-300x79.jpg 300w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/local_error-768x202.jpg 768w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/11/local_error-600x158.jpg 600w" sizes="(max-width: 918px) 100vw, 918px" /></figure> <p>Все дело в том, что область видимости этой переменной ограничена функцией show_local_name(). Для того чтобы она была видна во всей программе, можно использовать ключевое слово <strong>global</strong>.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2db8696679005" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2db8696679005-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2db8696679005-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2db8696679005-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2db8696679005-4">4</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2db8696679005-1"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">make_global</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2db8696679005-2"><span class="crayon-h"> </span><span class="crayon-m">global</span><span class="crayon-h"> </span><span class="crayon-e">local_name</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2db8696679005-3"><span class="crayon-e"> </span><span class="crayon-v">local_name</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-s">'Алена'</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2db8696679005-4"><span class="crayon-h"> </span><span class="crayon-k ">print</span><span class="crayon-sy">(</span><span class="crayon-v">local_name</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2db9469216970" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2db9469216970-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2db9469216970-1"><span class="crayon-e">make_global</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dba236041322" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dba236041322-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dba236041322-1">Алена</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dbb206636773" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dbb206636773-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dbb206636773-2">2</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dbb206636773-1"><span class="crayon-c"># теперь ошибки быть не должно</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dbb206636773-2"><span class="crayon-v">local_name</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dbc294255936" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dbc294255936-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dbc294255936-1"><span class="crayon-s">'Алена'</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Давайте посмотрим, что будет, если есть две переменные с одинаковым названием, причем одна из них глобальная, а вторая — локальная.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dbd859714036" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dbd859714036-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dbd859714036-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dbd859714036-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dbd859714036-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dbd859714036-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dbd859714036-6">6</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dbd859714036-7">7</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dbd859714036-1"><span class="crayon-c"># объявим глобальную переменную</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dbd859714036-2"><span class="crayon-v">global_number</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-cn">5</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dbd859714036-3"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dbd859714036-4"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">print_number</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dbd859714036-5"><span class="crayon-h"> </span><span class="crayon-c"># затем объявим локальную переменную</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dbd859714036-6"><span class="crayon-h"> </span><span class="crayon-v">local_number</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-cn">10</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dbd859714036-7"><span class="crayon-h"> </span><span class="crayon-k ">print</span><span class="crayon-sy">(</span><span class="crayon-s">'Local number:'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">local_number</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <p>Функция всегда «предпочтет» содержащуюся в ней локальную переменную.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dbe563866936" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dbe563866936-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dbe563866936-1"><span class="crayon-e">print_number</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dbf557577251" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dbf557577251-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dbf557577251-1">Local number: 10</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>При этом значение глобальной переменной для остального кода не изменится.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dc0292589882" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dc0292589882-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dc0292589882-1"><span class="crayon-k ">print</span><span class="crayon-sy">(</span><span class="crayon-s">'Global number:'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">global_number</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dc1111448172" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dc1111448172-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dc1111448172-1">Global number: 5</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <h2 class="wp-block-heading" id="13-anonimnye-ili-lambda-funktsii">Анонимные или lambda-функции</h2> <p>Функции создают не только через ключевое слово <strong>def</strong> и название функции. Можно использовать слово <strong>lambda</strong> и вообще обойтись без названия (отсюда и второе название — <strong>анонимная</strong> (anonymous) функция). </p> <p>Приведем простой пример.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dc2810271616" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dc2810271616-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dc2810271616-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dc2810271616-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dc2810271616-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dc2810271616-5">5</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dc2810271616-1"><span class="crayon-c"># создадим функцию, которая принимает два числа и перемножает их</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dc2810271616-2"><span class="crayon-v">lf</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-r">lambda</span><span class="crayon-h"> </span><span class="crayon-v">a</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">b</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">a</span><span class="crayon-h"> </span><span class="crayon-o">*</span><span class="crayon-h"> </span><span class="crayon-i">b</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dc2810271616-3"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dc2810271616-4"><span class="crayon-c"># вызовем функцию и передадим ей числа 2 и 3</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dc2810271616-5"><span class="crayon-e">lf</span><span class="crayon-sy">(</span><span class="crayon-cn">2</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">3</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dc3089071209" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dc3089071209-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dc3089071209-1">6</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Теперь давайте в деталях рассмотрим, как мы получили такой результат.</p> <figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" width="1024" height="396" src="https://www.dmitrymakarov.ru/wp-content/uploads/2021/12/lambda_f-1024x396.jpg" alt="синтаксис (анонимной) lambda-функции" class="wp-image-7140" style="width:543px;height:209px" srcset="https://www.dmitrymakarov.ru/wp-content/uploads/2021/12/lambda_f-1024x396.jpg 1024w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/12/lambda_f-300x116.jpg 300w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/12/lambda_f-768x297.jpg 768w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/12/lambda_f-1200x465.jpg 1200w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/12/lambda_f-600x232.jpg 600w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/12/lambda_f.jpg 1395w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure> <ul class="wp-block-list"> <li>вначале ставится ключевое слово <strong>lambda</strong>;</li> <li>за ним идут передаваемые <strong>параметры</strong>;</li> <li>через двоеточие пишется <strong>исполняемое выражение</strong>.</li> </ul> <p>Разумеется, ничто не мешает поместить этот же функционал в обычную функцию.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dc4672332274" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dc4672332274-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dc4672332274-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dc4672332274-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dc4672332274-4">4</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dc4672332274-1"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">normal_f</span><span class="crayon-sy">(</span><span class="crayon-v">a</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">b</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dc4672332274-2"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-v">a</span><span class="crayon-h"> </span><span class="crayon-o">*</span><span class="crayon-h"> </span><span class="crayon-i">b</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dc4672332274-3"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dc4672332274-4"><span class="crayon-e">normal_f</span><span class="crayon-sy">(</span><span class="crayon-cn">2</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">3</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dc5965622943" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dc5965622943-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dc5965622943-1">6</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Анонимную функции при этом удобно использовать в тех случаях, когда применение обычной функции избыточно. Продемонстрирую на примере.</p> <h3 class="wp-block-heading" id="14-lambda-funktsiya-vnutri-funktsii-filter">Lambda-функция внутри функции filter()</h3> <p>Предположим, у нас есть список чисел, и мы хотим оставить в нем только те числа, которые больше 10. Давайте решим эту задачу с помощью функции <strong>filter()</strong> и <strong>lambda</strong>-функции.</p> <p><strong>Функция filter()</strong> принимает два параметра:</p> <ul class="wp-block-list"> <li>Во-первых, еще одну функцию, выполняющую роль критерия; она выдает <span id="urvanov-syntax-highlighter-672da50fc2dc6612371514" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline crayon-theme-familiar-my-copy crayon-theme-familiar-my-copy-inline urvanov-syntax-highlighter-font-consolas" style="font-size: 16px !important; line-height: 22px !important;font-size: 16px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 16px !important; line-height: 22px !important;font-size: 16px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-t">True</span></span></span>, если элемент нужно оставить, и <span id="urvanov-syntax-highlighter-672da50fc2dc7469271030" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline crayon-theme-familiar-my-copy crayon-theme-familiar-my-copy-inline urvanov-syntax-highlighter-font-consolas" style="font-size: 16px !important; line-height: 22px !important;font-size: 16px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 16px !important; line-height: 22px !important;font-size: 16px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-t">False</span></span></span> — если убрать;</li> <li>Во-вторых, набор элементов, которые нужно отфильтровать в виде списка, кортежа или множества.</li> </ul> <p>Получив их, <strong>filter()</strong> применяет критерий (функцию) к каждому из элементов набора.</p> <p>Посмотрим на реализацию на Питоне. Вначале создадим список.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dc8743068835" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dc8743068835-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dc8743068835-1"><span class="crayon-v">nums</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-sy">[</span><span class="crayon-cn">15</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">27</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">9</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">18</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">3</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">1</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">4</span><span class="crayon-sy">]</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <p>Теперь зададим фильтрующую <strong>lambda</strong>-функцию.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dc9732205541" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dc9732205541-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dc9732205541-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dc9732205541-3">3</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dc9732205541-1"><span class="crayon-c"># буквально мы пишем:</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dc9732205541-2"><span class="crayon-c"># "для каждого n, выдай True, если число больше 10, иначе - False"</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dc9732205541-3"><span class="crayon-v">criterion</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-r">lambda</span><span class="crayon-h"> </span><span class="crayon-v">n</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-t">True</span><span class="crayon-h"> </span><span class="crayon-st">if</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-v">n</span><span class="crayon-h"> </span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-cn">10</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-st">else</span><span class="crayon-h"> </span><span class="crayon-t">False</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <p>После этого поместим criterion и nums в функцию <strong>filter()</strong>. Так как сама функция <strong>filter()</strong> вернет нам не список, а специальный объект iterator, его в свою очередь нужно преобразовать обратно в список с помощью функции <strong>list()</strong>.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dca872143560" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dca872143560-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dca872143560-1"><span class="crayon-k ">list</span><span class="crayon-sy">(</span><span class="crayon-k ">filter</span><span class="crayon-sy">(</span><span class="crayon-v">criterion</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">nums</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dcb889613689" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dcb889613689-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dcb889613689-1">[15, 27, 18]</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Чаще такой функционал записывают в одну строку.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dcc556197277" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dcc556197277-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dcc556197277-1"><span class="crayon-k ">list</span><span class="crayon-sy">(</span><span class="crayon-k ">filter</span><span class="crayon-sy">(</span><span class="crayon-r">lambda</span><span class="crayon-h"> </span><span class="crayon-v">n</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-t">True</span><span class="crayon-h"> </span><span class="crayon-st">if</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-v">n</span><span class="crayon-h"> </span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-cn">10</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-st">else</span><span class="crayon-h"> </span><span class="crayon-t">False</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">nums</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dcd672246771" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dcd672246771-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dcd672246771-1">[15, 27, 18]</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>И в этом и заключается удобство lambda-функции, ее не надо объявлять заранее. Через обычную функцию код выглядел бы так.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dce081928268" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dce081928268-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dce081928268-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dce081928268-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dce081928268-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dce081928268-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dce081928268-6">6</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dce081928268-7">7</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dce081928268-1"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">criterion_2</span><span class="crayon-sy">(</span><span class="crayon-v">n</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dce081928268-2"><span class="crayon-h"> </span><span class="crayon-st">if</span><span class="crayon-h"> </span><span class="crayon-v">n</span><span class="crayon-h"> </span><span class="crayon-o">></span><span class="crayon-h"> </span><span class="crayon-cn">10</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dce081928268-3"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-t">True</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dce081928268-4"><span class="crayon-h"> </span><span class="crayon-st">else</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dce081928268-5"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-t">False</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dce081928268-6"> </div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dce081928268-7"><span class="crayon-k ">list</span><span class="crayon-sy">(</span><span class="crayon-k ">filter</span><span class="crayon-sy">(</span><span class="crayon-v">criterion_2</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">nums</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0003 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dcf055758500" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dcf055758500-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dcf055758500-1">[15, 27, 18]</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <h3 class="wp-block-heading" id="15-lambda-funktsiya-vnutri-funktsii-sorted">Lambda-функция внутри функции sorted()</h3> <p>Теперь рассмотрим lambda-функцию в коде, который мы использовали на занятии по <a href="https://www.dmitrymakarov.ru/intro/recommender-17/#ind_dist">рекомендательным системам</a>. Воспроизведем в миниатюре часть решения из того занятия.</p> <p>В какой-то момент мы получили список из кортежей. И в каждом кортеже был индекс фильма и расстояние до него.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dd1003589794" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dd1003589794-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dd1003589794-1"><span class="crayon-v">indices_distances</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-sy">[</span><span class="crayon-sy">(</span><span class="crayon-cn">901</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">0.0</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-cn">1002</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">0.22982440568634488</span><span class="crayon-sy">)</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-cn">442</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">0.25401128310081567</span><span class="crayon-sy">)</span><span class="crayon-sy">]</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <p>Затем мы захотели отсортировать этот список по расстоянию, то есть по второму элементу кортежа. </p> <p>Мы взяли <strong>функцию sorted()</strong> и в качестве параметра ключа, по которому сортировать список (key), передали ей lambda-функцию, принимающей кортеж из двух элементов на входе, и выдающей второй его элемент на выходе.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dd2230861954" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dd2230861954-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dd2230861954-1"><span class="crayon-k ">sorted</span><span class="crayon-sy">(</span><span class="crayon-v">indices_distances</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">key</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-r">lambda</span><span class="crayon-h"> </span><span class="crayon-v">x</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">x</span><span class="crayon-sy">[</span><span class="crayon-cn">1</span><span class="crayon-sy">]</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">reverse</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-t">False</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dd3403729441" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dd3403729441-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dd3403729441-1">[(901, 0.0), (1002, 0.22982440568634488), (442, 0.25401128310081567)]</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Параметр <span id="urvanov-syntax-highlighter-672da50fc2dd4705646725" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline crayon-theme-familiar-my-copy crayon-theme-familiar-my-copy-inline urvanov-syntax-highlighter-font-consolas" style="font-size: 16px !important; line-height: 22px !important;font-size: 16px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 16px !important; line-height: 22px !important;font-size: 16px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">reverse</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-t">False</span></span></span>, напомню, задает сортировку по возрастанию. В данном случае, от наименьшего расстояния до искомого фильма к наибольшему.</p> <h3 class="wp-block-heading" id="16-nemedlenno-vyzyvaemye-funktsii">Немедленно вызываемые функции</h3> <p>Lambda-функции относятся к так называемым немедленно вызываемым функциям или immediately invoked function expressions (IIFE). Это означает, что мы можем одновременно объявить и вызвать такую функцию.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dd5772684761" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dd5772684761-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dd5772684761-2">2</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dd5772684761-1"><span class="crayon-c"># обратите внимание на использование скобок</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dd5772684761-2"><span class="crayon-sy">(</span><span class="crayon-r">lambda</span><span class="crayon-h"> </span><span class="crayon-v">x</span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-v">x</span><span class="crayon-h"> </span><span class="crayon-o">*</span><span class="crayon-h"> </span><span class="crayon-v">x</span><span class="crayon-sy">)</span><span class="crayon-sy">(</span><span class="crayon-cn">10</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dd6055530269" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dd6055530269-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dd6055530269-1">100</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <h2 class="wp-block-heading" id="17-args-i-kwargs">*args и **kwargs</h2> <p>Прежде чем завершить, поговорим про еще одну важную тему, а именно про так называемые <strong>*args</strong> (сокращение от arguments) и <strong>**kwargs</strong> (keyword arguments).</p> <p class="has-background" style="background-color:#ffe8c4">Они позволяют передавать функции <em>различное</em> количество позиционных (*args) или именованных (**kwargs) аргументов.</p> <p>Рассмотрим на примере. Начнем с <strong>*args</strong>.</p> <h3 class="wp-block-heading" id="18-args">*args</h3> <p>Предположим, что у нас есть простая функция, которая принимает два числа и вычисляет их среднее арифметическое значение.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dd7103287875" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dd7103287875-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dd7103287875-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dd7103287875-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dd7103287875-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dd7103287875-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dd7103287875-6">6</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dd7103287875-1"><span class="crayon-c"># объявим функцию</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dd7103287875-2"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">mean</span><span class="crayon-sy">(</span><span class="crayon-v">a</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">b</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dd7103287875-3"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-sy">(</span><span class="crayon-v">a</span><span class="crayon-h"> </span><span class="crayon-o">+</span><span class="crayon-h"> </span><span class="crayon-v">b</span><span class="crayon-sy">)</span><span class="crayon-h"> </span><span class="crayon-o">/</span><span class="crayon-h"> </span><span class="crayon-cn">2</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dd7103287875-4"> </div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dd7103287875-5"><span class="crayon-c"># и передадим ей числа 1 и 2</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dd7103287875-6"><span class="crayon-e">mean</span><span class="crayon-sy">(</span><span class="crayon-cn">1</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">2</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dd8326435402" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dd8326435402-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dd8326435402-1">1.5</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Все отлично работает, но мы не можем передать этой функции больше двух чисел. Возможным решением станет функция, которая изначально принимает список в качестве аргумента.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dd9971302151" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dd9971302151-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dd9971302151-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dd9971302151-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dd9971302151-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dd9971302151-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dd9971302151-6">6</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dd9971302151-7">7</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dd9971302151-8">8</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dd9971302151-9">9</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dd9971302151-10">10</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dd9971302151-11">11</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dd9971302151-12">12</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dd9971302151-1"><span class="crayon-c"># объявим функцию, которой нужно передать список</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dd9971302151-2"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">mean</span><span class="crayon-sy">(</span><span class="crayon-v">list_</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dd9971302151-3"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dd9971302151-4"><span class="crayon-h"> </span><span class="crayon-c"># зададим переменную для суммы,</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dd9971302151-5"><span class="crayon-h"> </span><span class="crayon-v">total</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-cn">0</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dd9971302151-6"> </div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dd9971302151-7"><span class="crayon-h"> </span><span class="crayon-c"># в цикле сложим все числа из списка</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dd9971302151-8"><span class="crayon-h"> </span><span class="crayon-st">for</span><span class="crayon-h"> </span><span class="crayon-i">i</span><span class="crayon-h"> </span><span class="crayon-st">in</span><span class="crayon-h"> </span><span class="crayon-v">list_</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dd9971302151-9"><span class="crayon-h"> </span><span class="crayon-v">total</span><span class="crayon-h"> </span><span class="crayon-o">+=</span><span class="crayon-h"> </span><span class="crayon-i">i</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dd9971302151-10"> </div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dd9971302151-11"><span class="crayon-h"> </span><span class="crayon-c"># и разделим на количество элементов</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dd9971302151-12"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-v">total</span><span class="crayon-h"> </span><span class="crayon-o">/</span><span class="crayon-h"> </span><span class="crayon-k ">len</span><span class="crayon-sy">(</span><span class="crayon-v">list_</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0003 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dda635116076" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dda635116076-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dda635116076-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dda635116076-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dda635116076-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dda635116076-5">5</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dda635116076-1"><span class="crayon-c"># создадим список</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dda635116076-2"><span class="crayon-v">list_</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-sy">[</span><span class="crayon-cn">1</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">2</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">3</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">4</span><span class="crayon-sy">]</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dda635116076-3"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dda635116076-4"><span class="crayon-c"># и передадим его в новую функцию</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dda635116076-5"><span class="crayon-e">mean</span><span class="crayon-sy">(</span><span class="crayon-v">list_</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2ddb440031560" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2ddb440031560-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2ddb440031560-1">2.5</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Все опять же работает, но нам каждый раз нужно создавать список. При попытке передать отдельные числа функция выдаст ошибку.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2ddc821482672" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2ddc821482672-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2ddc821482672-1"><span class="crayon-e">mean</span><span class="crayon-sy">(</span><span class="crayon-cn">1</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">2</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" width="893" height="243" src="https://www.dmitrymakarov.ru/wp-content/uploads/2021/12/argserror.jpg" alt="ошибка при использовании *args" class="wp-image-8617" style="width:759px;height:206px" srcset="https://www.dmitrymakarov.ru/wp-content/uploads/2021/12/argserror.jpg 893w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/12/argserror-300x82.jpg 300w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/12/argserror-768x209.jpg 768w, https://www.dmitrymakarov.ru/wp-content/uploads/2021/12/argserror-600x163.jpg 600w" sizes="(max-width: 893px) 100vw, 893px" /></figure> <p>*args позволяет передавать функции произвольное количество отдельных чисел.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2ddd884216523" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2ddd884216523-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2ddd884216523-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2ddd884216523-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2ddd884216523-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2ddd884216523-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2ddd884216523-6">6</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2ddd884216523-1"><span class="crayon-c"># объявим функцию с *args</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2ddd884216523-2"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">mean</span><span class="crayon-sy">(</span><span class="crayon-o">*</span><span class="crayon-v">nums</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2ddd884216523-3"><span class="crayon-h"> </span><span class="crayon-v">total</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-cn">0</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2ddd884216523-4"><span class="crayon-h"> </span><span class="crayon-st">for</span><span class="crayon-h"> </span><span class="crayon-i">i</span><span class="crayon-h"> </span><span class="crayon-st">in</span><span class="crayon-h"> </span><span class="crayon-v">nums</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2ddd884216523-5"><span class="crayon-h"> </span><span class="crayon-v">total</span><span class="crayon-h"> </span><span class="crayon-o">+=</span><span class="crayon-h"> </span><span class="crayon-i">i</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2ddd884216523-6"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-v">total</span><span class="crayon-h"> </span><span class="crayon-o">/</span><span class="crayon-h"> </span><span class="crayon-k ">len</span><span class="crayon-sy">(</span><span class="crayon-v">nums</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0003 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dde765234469" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dde765234469-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dde765234469-1"><span class="crayon-e">mean</span><span class="crayon-sy">(</span><span class="crayon-cn">1</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">2</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">3</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">4</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2ddf977326208" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2ddf977326208-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2ddf977326208-1">2.5</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Как вы видите, главным элементом здесь является <strong>оператор распаковки *</strong> (unpacking operator). Он принимает все передаваемые в функцию числа и формирует из них кортеж.</p> <p>Затем мы проходимся по элементам этого кортежа, рассчитываем их сумму и делим на количество элементов. Использовать слово args не обязательно, мы назвали наш позиционный аргумент nums.</p> <p>Если мы по какой-то причине захотим передать функции список, мы можем это сделать.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2de0424836935" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2de0424836935-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2de0424836935-2">2</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2de0424836935-1"><span class="crayon-c"># передадим в функцию список</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2de0424836935-2"><span class="crayon-e">mean</span><span class="crayon-sy">(</span><span class="crayon-o">*</span><span class="crayon-v">list_</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2de1745452461" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2de1745452461-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2de1745452461-1">2.5</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>В этом случае мы передаем название списка со звездочкой *.</p> <p>Для того чтобы убедиться, что отдельные числа и списки преобразуются в кортеж, напишем вот такую несложную функцию.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2de2304461931" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2de2304461931-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2de2304461931-2">2</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2de2304461931-1"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">test_type</span><span class="crayon-sy">(</span><span class="crayon-o">*</span><span class="crayon-v">nums</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2de2304461931-2"><span class="crayon-h"> </span><span class="crayon-k ">print</span><span class="crayon-sy">(</span><span class="crayon-v">nums</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-k ">type</span><span class="crayon-sy">(</span><span class="crayon-v">nums</span><span class="crayon-sy">)</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0003 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2de3271608056" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2de3271608056-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2de3271608056-1"><span class="crayon-e">test_type</span><span class="crayon-sy">(</span><span class="crayon-cn">1</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">2</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">3</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">4</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2de4868844708" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2de4868844708-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2de4868844708-1">(1, 2, 3, 4) <class 'tuple'></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2de5541761379" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2de5541761379-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2de5541761379-1"><span class="crayon-e">test_type</span><span class="crayon-sy">(</span><span class="crayon-o">*</span><span class="crayon-v">list_</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2de6152146519" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2de6152146519-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2de6152146519-1">(1, 2, 3, 4) <class 'tuple'></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Для наглядности приведем еще один, не относящийся напрямую к работе функции, пример.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2de7577320494" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2de7577320494-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2de7577320494-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2de7577320494-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2de7577320494-4">4</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2de7577320494-1"><span class="crayon-v">a</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-sy">[</span><span class="crayon-cn">1</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">2</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">3</span><span class="crayon-sy">]</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2de7577320494-2"><span class="crayon-v">b</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-sy">[</span><span class="crayon-o">*</span><span class="crayon-v">a</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">4</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">5</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">6</span><span class="crayon-sy">]</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2de7577320494-3"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2de7577320494-4"><span class="crayon-k ">print</span><span class="crayon-sy">(</span><span class="crayon-v">b</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2de8076431869" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2de8076431869-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2de8076431869-1">[1, 2, 3, 4, 5, 6]</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Как мы видим, здесь распаковка произошла в список.</p> <h3 class="wp-block-heading" id="19-kwargs">**kwargs</h3> <p>При использовании <strong>**kwargs</strong> происходит почти то же самое за тем исключением, что мы распаковываем именованные, а не позиционные аргументы. И распаковываем их в словарь, а не в кортеж или список. Сразу посмотрим на примере.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2de9396345583" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2de9396345583-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2de9396345583-2">2</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2de9396345583-1"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">f</span><span class="crayon-sy">(</span><span class="crayon-o">*</span><span class="crayon-o">*</span><span class="crayon-v">kwargs</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2de9396345583-2"><span class="crayon-h"> </span><span class="crayon-st">return</span><span class="crayon-h"> </span><span class="crayon-v">kwargs</span><span class="crayon-sy">.</span><span class="crayon-e">items</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dea270248025" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dea270248025-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dea270248025-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dea270248025-3">3</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dea270248025-1"><span class="crayon-c"># оператор ** примет произвольное количество именованных аргументов</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dea270248025-2"><span class="crayon-c"># и внутри функции сформирует из них словарь</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dea270248025-3"><span class="crayon-e">f</span><span class="crayon-sy">(</span><span class="crayon-v">a</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-cn">1</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">b</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-cn">2</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0001 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2deb338345264" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2deb338345264-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2deb338345264-1">dict_items([('a', 1), ('b', 2)])</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Приведем более сложный пример. Напишем функцию, которая на вход примет произвольное количество чисел (позиционный аргумент), преобразует в кортеж (*args) и рассчитает <a href="https://www.dmitrymakarov.ru/intro/stats-08/#6-srednee-arifmeticheskoe">среднее арифметическое</a> (mean) и <a href="https://www.dmitrymakarov.ru/intro/stats-08/#10-razbros-ili-otklonenie-ot-srednego">среднее квадратическое отклонение</a> (standard deviation).</p> <p>Для каждой из метрик мы дополнительно создадим именованный параметр, который определит, выводить эту метрику или нет. Параметры мы передадим через **kwargs. Внутри функции из них будет сформирован словарь.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dec782773646" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dec782773646-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dec782773646-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dec782773646-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dec782773646-4">4</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dec782773646-5">5</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dec782773646-6">6</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dec782773646-7">7</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dec782773646-8">8</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dec782773646-9">9</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dec782773646-10">10</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dec782773646-11">11</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dec782773646-12">12</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dec782773646-13">13</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dec782773646-14">14</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dec782773646-15">15</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dec782773646-1"><span class="crayon-c"># nums функция преобразует в кортеж, params - в словарь</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dec782773646-2"><span class="crayon-r">def</span><span class="crayon-h"> </span><span class="crayon-e">simple_stats</span><span class="crayon-sy">(</span><span class="crayon-o">*</span><span class="crayon-v">nums</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-o">*</span><span class="crayon-o">*</span><span class="crayon-v">params</span><span class="crayon-sy">)</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dec782773646-3"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dec782773646-4"><span class="crayon-h"> </span><span class="crayon-c"># если ключ 'mean' есть в словаре params и его значение == True</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dec782773646-5"><span class="crayon-h"> </span><span class="crayon-st">if</span><span class="crayon-h"> </span><span class="crayon-s">'mean'</span><span class="crayon-h"> </span><span class="crayon-st">in</span><span class="crayon-h"> </span><span class="crayon-e">params </span><span class="crayon-st">and</span><span class="crayon-h"> </span><span class="crayon-v">params</span><span class="crayon-sy">[</span><span class="crayon-s">'mean'</span><span class="crayon-sy">]</span><span class="crayon-h"> </span><span class="crayon-o">==</span><span class="crayon-h"> </span><span class="crayon-t">True</span><span class="crayon-o">:</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dec782773646-6"> </div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dec782773646-7"><span class="crayon-h"> </span><span class="crayon-c"># рассчитаем среднее арифметическое кортежа nums и округлим</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dec782773646-8"><span class="crayon-h"> </span><span class="crayon-c"># \t - это символ табуляции</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dec782773646-9"><span class="crayon-h"> </span><span class="crayon-k ">print</span><span class="crayon-sy">(</span><span class="crayon-i">f</span><span class="crayon-s">'mean: \t{np.round(np.mean(nums), 3)}'</span><span class="crayon-sy">)</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dec782773646-10"> </div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dec782773646-11"><span class="crayon-h"> </span><span class="crayon-c"># если ключ 'std' есть в словаре params и его значение == True</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dec782773646-12"><span class="crayon-h"> </span><span class="crayon-st">if</span><span class="crayon-h"> </span><span class="crayon-s">'std'</span><span class="crayon-h"> </span><span class="crayon-st">in</span><span class="crayon-h"> </span><span class="crayon-e">params </span><span class="crayon-st">and</span><span class="crayon-h"> </span><span class="crayon-v">params</span><span class="crayon-sy">[</span><span class="crayon-s">'std'</span><span class="crayon-sy">]</span><span class="crayon-h"> </span><span class="crayon-o">==</span><span class="crayon-h"> </span><span class="crayon-t">True</span><span class="crayon-o">:</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dec782773646-13"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dec782773646-14"><span class="crayon-h"> </span><span class="crayon-c"># рассчитаем СКО кортежа nums и округлим</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dec782773646-15"><span class="crayon-h"> </span><span class="crayon-k ">print</span><span class="crayon-sy">(</span><span class="crayon-i">f</span><span class="crayon-s">'std: \t{np.round(np.std(nums), 3)}'</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0005 seconds] --> </div> <p>Вызовем функцию simple_stats() и передадим ей числа и именованные аргументы.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2ded275958187" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2ded275958187-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2ded275958187-1"><span class="crayon-e">simple_stats</span><span class="crayon-sy">(</span><span class="crayon-cn">5</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">10</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">15</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">20</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">mean</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-t">True</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">std</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-t">True</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2dee700386845" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2dee700386845-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2dee700386845-2">2</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2dee700386845-1">mean: 12.5</div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2dee700386845-2">std: 5.59</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Если для одного из параметров задать значение False, функция не выведет соответствующую метрику.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2def812257893" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2def812257893-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2def812257893-1"><span class="crayon-e">simple_stats</span><span class="crayon-sy">(</span><span class="crayon-cn">5</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">10</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">15</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">20</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">mean</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-t">True</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">std</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-t">False</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0002 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2df0659038240" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2df0659038240-1">1</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2df0659038240-1">mean: 12.5</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Для того чтобы передать параметры списком и словарем, нам нужно использовать операторы распаковки <span id="urvanov-syntax-highlighter-672da50fc2df1561766490" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline crayon-theme-familiar-my-copy crayon-theme-familiar-my-copy-inline urvanov-syntax-highlighter-font-consolas" style="font-size: 16px !important; line-height: 22px !important;font-size: 16px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 16px !important; line-height: 22px !important;font-size: 16px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-o">*</span></span></span> и <span id="urvanov-syntax-highlighter-672da50fc2df2283883314" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline crayon-theme-familiar-my-copy crayon-theme-familiar-my-copy-inline urvanov-syntax-highlighter-font-consolas" style="font-size: 16px !important; line-height: 22px !important;font-size: 16px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 16px !important; line-height: 22px !important;font-size: 16px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-o">*</span><span class="crayon-o">*</span></span></span> соответственно.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2df3572760104" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2df3572760104-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2df3572760104-2">2</div><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2df3572760104-3">3</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2df3572760104-4">4</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2df3572760104-1"><span class="crayon-v">list_</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-sy">[</span><span class="crayon-cn">5</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">10</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">15</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">20</span><span class="crayon-sy">]</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2df3572760104-2"><span class="crayon-v">settings</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-sy">{</span><span class="crayon-s">'mean'</span><span class="crayon-h"> </span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-t">True</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-s">'std'</span><span class="crayon-h"> </span><span class="crayon-o">:</span><span class="crayon-h"> </span><span class="crayon-t">True</span><span class="crayon-sy">}</span></div><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2df3572760104-3"> </div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2df3572760104-4"><span class="crayon-e">simple_stats</span><span class="crayon-sy">(</span><span class="crayon-o">*</span><span class="crayon-v">list_</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-o">*</span><span class="crayon-o">*</span><span class="crayon-v">settings</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0005 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2df4960806388" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2df4960806388-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2df4960806388-2">2</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2df4960806388-1">mean: 12.5</div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2df4960806388-2">std: 5.59</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Количество именованных аргументов в **kwargs может быть любым. Ничто не мешает нам добавить еще один параметр.</p> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2df5367452973" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2df5367452973-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2df5367452973-2">2</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2df5367452973-1"><span class="crayon-c"># добавим параметр median</span></div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2df5367452973-2"><span class="crayon-e">simple_stats</span><span class="crayon-sy">(</span><span class="crayon-cn">5</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">10</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">15</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-cn">20</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">mean</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-t">True</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">std</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-t">True</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-v">median</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-t">True</span><span class="crayon-sy">)</span></div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0004 seconds] --> </div> <div class="wp-block-urvanov-syntax-highlighter-code-block"><!-- Urvanov Syntax Highlighter v2.8.37 --> <div id="urvanov-syntax-highlighter-672da50fc2df6994538411" class="urvanov-syntax-highlighter-syntax crayon-theme-familiar-my-copy urvanov-syntax-highlighter-font-consolas urvanov-syntax-highlighter-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 15px; margin-bottom: 15px; margin-left: 15px; margin-right: 15px; font-size: 16px !important; line-height: 22px !important;"> <div class="urvanov-syntax-highlighter-plain-wrap"></div> <div class="urvanov-syntax-highlighter-main" style=""> <table class="crayon-table"> <tr class="urvanov-syntax-highlighter-row"> <td class="crayon-nums " data-settings="show"> <div class="urvanov-syntax-highlighter-nums-content" style="font-size: 16px !important; line-height: 22px !important;"><div class="crayon-num" data-line="urvanov-syntax-highlighter-672da50fc2df6994538411-1">1</div><div class="crayon-num crayon-striped-num" data-line="urvanov-syntax-highlighter-672da50fc2df6994538411-2">2</div></div> </td> <td class="urvanov-syntax-highlighter-code"><div class="crayon-pre" style="font-size: 16px !important; line-height: 22px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><div class="crayon-line" id="urvanov-syntax-highlighter-672da50fc2df6994538411-1">mean: 12.5</div><div class="crayon-line crayon-striped-line" id="urvanov-syntax-highlighter-672da50fc2df6994538411-2">std: 5.59</div></div></td> </tr> </table> </div> </div> <!-- [Format Time: 0.0000 seconds] --> </div> <p>Впрочем, для того чтобы это имело смысл, такой параметр должен быть прописан внутри функции.</p> <p>В заключение скажу, что все приведенные выше примеры являются учебными и без <strong>*args</strong> и <strong>**kwargs</strong> здесь конечно можно обойтись. На практике, они применяются в более сложных конструкциях, например, в так называемых декораторах, однако эта тема выходит за рамки сегодняшнего занятия.</p> <h2 class="wp-block-heading" id="20-podvedem-itog">Подведем итог</h2> <p>Сегодня мы впервые поговорили про функции в программировании и выяснили, чем они отличаются от функций в математике. Кроме того мы узнали, что в Питоне можно:</p> <ul class="wp-block-list"> <li>использовать <strong>готовые функции</strong>, которые уже встроены либо в базовый функционал, либо в дополнительную библиотеку;</li> <li>объявлять <strong>собственные функции</strong> через ключевое слово <strong>def</strong> и название функции; а также</li> <li>создавать <strong>анонимные</strong> или <strong>lambda-функции</strong>, которые очень удобно применять там, где в полноценных собственных функциях нет необходимости.</li> </ul> <p>Помимо этого, мы выяснили, что любой функции можно передать <strong>параметры</strong> и <strong>аргументы</strong>, которые в зависимости от способа передачи могут быть <em>позиционными</em> или <em>именованными</em>.</p> <p>Мы узнали, что у переменной может быть <em>локальная</em> и <em>глобальная</em> области видимости. Наконец, мы поговорили про возможность передачи различного количества позиционных и именованных аргументов через <strong>*args</strong> и <strong>**kwargs</strong>.</p> <h3 class="wp-block-heading" id="21-voprosy-dlya-zakrepleniya">Вопросы для закрепления</h3> <p class="has-black-color has-text-color has-background" style="background-color:#ffe8c4">Какие три вида функций мы изучили?</p> <div class="wp-block-ub-content-toggle" id="ub-content-toggle-5adb461c-ef40-41be-ab28-dca2594c0395" data-mobilecollapse="true" data-desktopcollapse="true"> <div class="wp-block-ub-content-toggle-accordion" id="ub-content-toggle-panel-block-2a8e7516-1153-4eee-a512-0d99db544a9b"> <div class="wp-block-ub-content-toggle-accordion-title-wrap"" aria-controls="ub-content-toggle-panel-0-5adb461c-ef40-41be-ab28-dca2594c0395" tabindex="0"> <p class="wp-block-ub-content-toggle-accordion-title ub-content-toggle-title-5adb461c-ef40-41be-ab28-dca2594c0395">Посмотреть правильный ответ</p><div class="wp-block-ub-content-toggle-accordion-toggle-wrap left"><span class="wp-block-ub-content-toggle-accordion-state-indicator wp-block-ub-math-plus"></span> </div></div><div role="region" aria-expanded="false" class="wp-block-ub-content-toggle-accordion-content-wrap ub-hide" id="ub-content-toggle-panel-0-5adb461c-ef40-41be-ab28-dca2594c0395"> <p>Ответ: встроенные, собственные, а также анонимные или lambda-функции.</p> </div></div> </div> <p class="has-black-color has-text-color has-background" style="background-color:#ffe8c4">Какие бывают параметры и аргументы функции?</p> <div class="wp-block-ub-content-toggle" id="ub-content-toggle-f5a0548a-4eb2-478c-856c-7ef81ee85cb6" data-mobilecollapse="true" data-desktopcollapse="true"> <div class="wp-block-ub-content-toggle-accordion" id="ub-content-toggle-panel-block-f0165add-deea-41b5-8c59-6f060393c8d5"> <div class="wp-block-ub-content-toggle-accordion-title-wrap"" aria-controls="ub-content-toggle-panel-0-f5a0548a-4eb2-478c-856c-7ef81ee85cb6" tabindex="0"> <p class="wp-block-ub-content-toggle-accordion-title ub-content-toggle-title-f5a0548a-4eb2-478c-856c-7ef81ee85cb6">Посмотреть правильный ответ</p><div class="wp-block-ub-content-toggle-accordion-toggle-wrap left"><span class="wp-block-ub-content-toggle-accordion-state-indicator wp-block-ub-math-plus"></span> </div></div><div role="region" aria-expanded="false" class="wp-block-ub-content-toggle-accordion-content-wrap ub-hide" id="ub-content-toggle-panel-0-f5a0548a-4eb2-478c-856c-7ef81ee85cb6"> <p>Ответ: позиционные (в этом случае мы указываем только аргумент, но ставим его в определенном порядке) и именованные (указываем и параметр, и аргумент, но порядок не важен).</p> </div></div> </div> <p class="has-black-color has-text-color has-background" style="background-color:#ffe8c4">Какова область видимости локальной переменной?</p> <div class="wp-block-ub-content-toggle" id="ub-content-toggle-a242ec4d-135e-42a8-a357-129012abbcb7" data-mobilecollapse="true" data-desktopcollapse="true"> <div class="wp-block-ub-content-toggle-accordion" id="ub-content-toggle-panel-block-4caf45ed-8fa8-49b3-9e08-b4b2594b4981"> <div class="wp-block-ub-content-toggle-accordion-title-wrap"" aria-controls="ub-content-toggle-panel-0-a242ec4d-135e-42a8-a357-129012abbcb7" tabindex="0"> <p class="wp-block-ub-content-toggle-accordion-title ub-content-toggle-title-a242ec4d-135e-42a8-a357-129012abbcb7">Посмотреть правильный ответ</p><div class="wp-block-ub-content-toggle-accordion-toggle-wrap left"><span class="wp-block-ub-content-toggle-accordion-state-indicator wp-block-ub-math-plus"></span> </div></div><div role="region" aria-expanded="false" class="wp-block-ub-content-toggle-accordion-content-wrap ub-hide" id="ub-content-toggle-panel-0-a242ec4d-135e-42a8-a357-129012abbcb7"> <p>Ответ: область видимости локальной переменной ограничена той функцией, в которой эта переменная была объявлена.</p> </div></div> </div> <p>В следующий раз мы подробнее рассмотрим <a href="https://www.dmitrymakarov.ru/python/list-tuple-set-07/">списки, кортежи и множества</a>.</p> <div id="ub_divider_858ab9d1-310b-4809-ac50-0f0fc3480676" class="wp-block-ub-divider ub-divider-orientation-horizontal"><hr class="ub_divider" ></hr></div> <h3 class="wp-block-heading" id="22-otvety-na-voprosy">Ответы на вопросы</h3> <p><strong>Вопрос</strong>. Скажите, а почему используется символ нижнего подчеркивания в названии переменной list_?</p> <p><strong>Ответ</strong>. Не стоит использовать просто list для <a href="https://www.dmitrymakarov.ru/python/variables-01/">именования переменных</a>, потому что это слово зарезервировано для названия функции <strong>list()</strong>. Аналогично не стоит использовать dict, tuple, set и т.д. При этом некоторые слова использовать в качестве названия переменных просто не получится. Питон выдаст ошибку. Это, например, if, for, True, False, import и т.п.</p> <p>Символ подчеркивания является одним из вариантов решения.</p> </div> <!-- .content --> </div> <!-- .post --> <footer class="site-footer section-inner"> <!--<p class="copyright">© 2024 <a href="https://www.dmitrymakarov.ru" class="site-name">Машинное обучение</a></p>--> </footer> <!-- footer --> </main> <script type="text/javascript" src="//cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js?ver=6.6.2" id="mathjax-js"></script> <script type="text/javascript" src="https://www.dmitrymakarov.ru/wp-content/plugins/ultimate-blocks/src/blocks/content-toggle/front.build.js?ver=3.2.2" id="ultimate_blocks-content-toggle-front-script-js"></script> <script type="text/javascript" src="https://www.dmitrymakarov.ru/wp-content/plugins/ultimate-blocks/src/blocks/table-of-contents/front.build.js?ver=3.2.2" id="ultimate_blocks-table-of-contents-front-script-js"></script> <script type="text/javascript" src="https://www.dmitrymakarov.ru/wp-includes/js/imagesloaded.min.js?ver=5.0.0" id="imagesloaded-js"></script> <script type="text/javascript" src="https://www.dmitrymakarov.ru/wp-includes/js/masonry.min.js?ver=4.2.2" id="masonry-js"></script> <script type="text/javascript" id="mcluhan_global-js-extra"> /* <![CDATA[ */ var mcluhan_ajaxpagination = {"ajaxurl":"https:\/\/www.dmitrymakarov.ru\/wp-admin\/admin-ajax.php","query_vars":"{\"page\":\"\",\"pagename\":\"python\\\/functions-06\"}"}; /* ]]> */ </script> <script type="text/javascript" src="https://www.dmitrymakarov.ru/wp-content/themes/mcluhan/assets/js/global.js?ver=2.1.1" id="mcluhan_global-js"></script> </body> </html>