CINXE.COM

פייתון/פייתון גרסה 2/לולאות – ויקיספר

<!DOCTYPE html> <html class="client-nojs vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-sticky-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-feature-night-mode-disabled skin-theme-clientpref-day vector-toc-available" lang="he" dir="rtl"> <head> <meta charset="UTF-8"> <title>פייתון/פייתון גרסה 2/לולאות – ויקיספר</title> <script>(function(){var className="client-js vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-sticky-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-feature-night-mode-disabled skin-theme-clientpref-day vector-toc-available";var cookie=document.cookie.match(/(?:^|; )hewikibooksmwclientpreferences=([^;]+)/);if(cookie){cookie[1].split('%2C').forEach(function(pref){className=className.replace(new RegExp('(^| )'+pref.replace(/-clientpref-\w+$|[^\w-]+/g,'')+'-clientpref-\\w+( |$)'),'$1'+pref+'$2');});}document.documentElement.className=className;}());RLCONF={"wgBreakFrames":false,"wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat": "dmy","wgMonthNames":["","ינואר","פברואר","מרץ","אפריל","מאי","יוני","יולי","אוגוסט","ספטמבר","אוקטובר","נובמבר","דצמבר"],"wgRequestId":"1b032d59-15bf-43f4-93ce-30bd888a3731","wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"פייתון/פייתון_גרסה_2/לולאות","wgTitle":"פייתון/פייתון גרסה 2/לולאות","wgCurRevisionId":148886,"wgRevisionId":148886,"wgArticleId":11589,"wgIsArticle":true,"wgIsRedirect":false,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":["דפים שמשתמשים בתג source","ויקיספר: דפים לאיחוד","פייתון גרסה 2"],"wgPageViewLanguage":"he","wgPageContentLanguage":"he","wgPageContentModel":"wikitext","wgRelevantPageName":"פייתון/פייתון_גרסה_2/לולאות","wgRelevantArticleId":11589,"wgIsProbablyEditable":true,"wgRelevantPageIsProbablyEditable":true, "wgRestrictionEdit":[],"wgRestrictionMove":[],"wgNoticeProject":"wikibooks","wgCiteReferencePreviewsActive":true,"wgMediaViewerOnClick":true,"wgMediaViewerEnabledByDefault":true,"wgVisualEditor":{"pageLanguageCode":"he","pageLanguageDir":"rtl","pageVariantFallbacks":"he"},"wgMFDisplayWikibaseDescriptions":{"search":true,"watchlist":true,"tagline":false,"nearby":true},"wgWMESchemaEditAttemptStepOversample":false,"wgWMEPageLength":10000,"wgCentralAuthMobileDomain":false,"wgEditSubmitButtonLabelPublish":true,"wgULSPosition":"interlanguage","wgULSisCompactLinksEnabled":false,"wgVector2022LanguageInHeader":true,"wgULSisLanguageSelectorEmpty":false,"wgCheckUserClientHintsHeadersJsApi":["brands","architecture","bitness","fullVersionList","mobile","model","platform","platformVersion"]};RLSTATE={"ext.gadget.HideExternalLinkIcon":"ready","ext.globalCssJs.user.styles":"ready","site.styles":"ready","user.styles":"ready","ext.globalCssJs.user":"ready","user":"ready","user.options":"loading", "ext.pygments":"ready","skins.vector.search.codex.styles":"ready","skins.vector.styles":"ready","skins.vector.icons":"ready","jquery.makeCollapsible.styles":"ready","ext.wikimediamessages.styles":"ready","ext.visualEditor.desktopArticleTarget.noscript":"ready","ext.uls.interlanguage":"ready","wikibase.client.init":"ready","ext.wikimediaBadges":"ready"};RLPAGEMODULES=["ext.pygments.view","site","mediawiki.page.ready","jquery.makeCollapsible","mediawiki.toc","skins.vector.js","ext.centralNotice.geoIP","ext.centralNotice.startUp","ext.gadget.EditToolbar","ext.gadget.SummariesList","ext.urlShortener.toolbar","ext.centralauth.centralautologin","mmv.bootstrap","ext.visualEditor.desktopArticleTarget.init","ext.visualEditor.targetLoader","ext.echo.centralauth","ext.eventLogging","ext.wikimediaEvents","ext.navigationTiming","ext.uls.interface","ext.checkUser.clientHints"];</script> <script>(RLQ=window.RLQ||[]).push(function(){mw.loader.impl(function(){return["user.options@12s5i",function($,jQuery,require,module){mw.user.tokens.set({"patrolToken":"+\\","watchToken":"+\\","csrfToken":"+\\"}); }];});});</script> <link rel="stylesheet" href="/w/load.php?lang=he&amp;modules=ext.pygments%2CwikimediaBadges%7Cext.uls.interlanguage%7Cext.visualEditor.desktopArticleTarget.noscript%7Cext.wikimediamessages.styles%7Cjquery.makeCollapsible.styles%7Cskins.vector.icons%2Cstyles%7Cskins.vector.search.codex.styles%7Cwikibase.client.init&amp;only=styles&amp;skin=vector-2022"> <script async="" src="/w/load.php?lang=he&amp;modules=startup&amp;only=scripts&amp;raw=1&amp;skin=vector-2022"></script> <meta name="ResourceLoaderDynamicStyles" content=""> <link rel="stylesheet" href="/w/load.php?lang=he&amp;modules=ext.gadget.HideExternalLinkIcon&amp;only=styles&amp;skin=vector-2022"> <link rel="stylesheet" href="/w/load.php?lang=he&amp;modules=site.styles&amp;only=styles&amp;skin=vector-2022"> <meta name="generator" content="MediaWiki 1.44.0-wmf.4"> <meta name="referrer" content="origin"> <meta name="referrer" content="origin-when-cross-origin"> <meta name="robots" content="max-image-preview:standard"> <meta name="format-detection" content="telephone=no"> <meta name="viewport" content="width=1120"> <meta property="og:title" content="פייתון/פייתון גרסה 2/לולאות – ויקיספר"> <meta property="og:type" content="website"> <link rel="preconnect" href="//upload.wikimedia.org"> <link rel="alternate" media="only screen and (max-width: 640px)" href="//he.m.wikibooks.org/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA"> <link rel="alternate" type="application/x-wiki" title="עריכה" href="/w/index.php?title=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA&amp;action=edit"> <link rel="icon" href="/static/favicon/wikibooks-rtl.ico"> <link rel="search" type="application/opensearchdescription+xml" href="/w/rest.php/v1/search" title="ויקיספר (he)"> <link rel="EditURI" type="application/rsd+xml" href="//he.wikibooks.org/w/api.php?action=rsd"> <link rel="canonical" href="https://he.wikibooks.org/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA"> <link rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/deed.he"> <link rel="alternate" type="application/atom+xml" title="הזנת Atom של ויקיספר" href="/w/index.php?title=%D7%9E%D7%99%D7%95%D7%97%D7%93:%D7%A9%D7%99%D7%A0%D7%95%D7%99%D7%99%D7%9D_%D7%90%D7%97%D7%A8%D7%95%D7%A0%D7%99%D7%9D&amp;feed=atom"> <link rel="dns-prefetch" href="//meta.wikimedia.org" /> <link rel="dns-prefetch" href="//login.wikimedia.org"> </head> <body class="skin--responsive skin-vector skin-vector-search-vue mediawiki rtl sitedir-rtl mw-hide-empty-elt ns-0 ns-subject mw-editable page-פייתון_פייתון_גרסה_2_לולאות rootpage-פייתון skin-vector-2022 action-view"><a class="mw-jump-link" href="#bodyContent">לדלג לתוכן</a> <div class="vector-header-container"> <header class="vector-header mw-header"> <div class="vector-header-start"> <nav class="vector-main-menu-landmark" aria-label="אתר"> <div id="vector-main-menu-dropdown" class="vector-dropdown vector-main-menu-dropdown vector-button-flush-left vector-button-flush-right" > <input type="checkbox" id="vector-main-menu-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-main-menu-dropdown" class="vector-dropdown-checkbox " aria-label="תפריט ראשי" > <label id="vector-main-menu-dropdown-label" for="vector-main-menu-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only " aria-hidden="true" ><span class="vector-icon mw-ui-icon-menu mw-ui-icon-wikimedia-menu"></span> <span class="vector-dropdown-label-text">תפריט ראשי</span> </label> <div class="vector-dropdown-content"> <div id="vector-main-menu-unpinned-container" class="vector-unpinned-container"> <div id="vector-main-menu" class="vector-main-menu vector-pinnable-element"> <div class="vector-pinnable-header vector-main-menu-pinnable-header vector-pinnable-header-unpinned" data-feature-name="main-menu-pinned" data-pinnable-element-id="vector-main-menu" data-pinned-container-id="vector-main-menu-pinned-container" data-unpinned-container-id="vector-main-menu-unpinned-container" > <div class="vector-pinnable-header-label">תפריט ראשי</div> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-pin-button" data-event-name="pinnable-header.vector-main-menu.pin">העברה לסרגל הצד</button> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-unpin-button" data-event-name="pinnable-header.vector-main-menu.unpin">הסתרה</button> </div> <div id="p-navigation" class="vector-menu mw-portlet mw-portlet-navigation" > <div class="vector-menu-heading"> ניווט </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="n-mainpage-description" class="mw-list-item"><a href="/wiki/%D7%A2%D7%9E%D7%95%D7%93_%D7%A8%D7%90%D7%A9%D7%99" title="ביקור בעמוד הראשי [z]" accesskey="z"><span>עמוד ראשי</span></a></li><li id="n-welcome" class="mw-list-item"><a href="/wiki/%D7%95%D7%99%D7%A7%D7%99%D7%A1%D7%A4%D7%A8:%D7%91%D7%A8%D7%95%D7%9B%D7%99%D7%9D_%D7%94%D7%91%D7%90%D7%99%D7%9D" title="מבוא לקוראים ולעורכים חדשים באתר ויקיספר"><span>ברוכים הבאים</span></a></li><li id="n-recentchanges" class="mw-list-item"><a href="/wiki/%D7%9E%D7%99%D7%95%D7%97%D7%93:%D7%A9%D7%99%D7%A0%D7%95%D7%99%D7%99%D7%9D_%D7%90%D7%97%D7%A8%D7%95%D7%A0%D7%99%D7%9D" title="רשימת השינויים האחרונים באתר [r]" accesskey="r"><span>שינויים אחרונים</span></a></li><li id="n-randompage" class="mw-list-item"><a href="/wiki/%D7%9E%D7%99%D7%95%D7%97%D7%93:%D7%90%D7%A7%D7%A8%D7%90%D7%99" title="טעינת דף אקראי [x]" accesskey="x"><span>דף אקראי</span></a></li> </ul> </div> </div> <div id="p-community" class="vector-menu mw-portlet mw-portlet-community" > <div class="vector-menu-heading"> קהילה </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="n-portal" class="mw-list-item"><a href="/wiki/%D7%95%D7%99%D7%A7%D7%99%D7%A1%D7%A4%D7%A8:%D7%A9%D7%A2%D7%A8_%D7%94%D7%A7%D7%94%D7%99%D7%9C%D7%94" title="אודות המיזם, איך אפשר לעזור, איפה למצוא דברים"><span>שער הקהילה</span></a></li><li id="n-help" class="mw-list-item"><a href="/wiki/%D7%A2%D7%96%D7%A8%D7%94:%D7%AA%D7%A4%D7%A8%D7%99%D7%98_%D7%A8%D7%90%D7%A9%D7%99" title="המקום למצוא מידע"><span>עזרה</span></a></li><li id="n-villagepump" class="mw-list-item"><a href="/wiki/%D7%95%D7%99%D7%A7%D7%99%D7%A1%D7%A4%D7%A8:%D7%9E%D7%96%D7%A0%D7%95%D7%9F" title="המזנון הוא המקום שבו מתקיימים דיונים הקשורים לאתר ויקיספר"><span>מזנון</span></a></li><li id="n-helpdesk" class="mw-list-item"><a href="/wiki/%D7%95%D7%99%D7%A7%D7%99%D7%A1%D7%A4%D7%A8:%D7%93%D7%9C%D7%A4%D7%A7_%D7%99%D7%99%D7%A2%D7%95%D7%A5" title="בדלפק הייעוץ אפשר לבקש סיוע בנושאים הקשורים לאתר ויקיספר"><span>דלפק ייעוץ</span></a></li> </ul> </div> </div> </div> </div> </div> </div> </nav> <a href="/wiki/%D7%A2%D7%9E%D7%95%D7%93_%D7%A8%D7%90%D7%A9%D7%99" class="mw-logo"> <img class="mw-logo-icon" src="/static/images/icons/wikibooks.svg" alt="" aria-hidden="true" height="50" width="50"> <span class="mw-logo-container skin-invert"> <img class="mw-logo-wordmark" alt="ויקיספר" src="/static/images/mobile/copyright/wikibooks-wordmark-he.svg" style="width: 7.5em; height: 2.3125em;"> <img class="mw-logo-tagline" alt="אוסף הספרים והמדריכים החופשי" src="/static/images/mobile/copyright/wikibooks-tagline-he.svg" width="120" height="11" style="width: 7.5em; height: 0.6875em;"> </span> </a> </div> <div class="vector-header-end"> <div id="p-search" role="search" class="vector-search-box-vue vector-search-box-collapses vector-search-box-show-thumbnail vector-search-box-auto-expand-width vector-search-box"> <a href="/wiki/%D7%9E%D7%99%D7%95%D7%97%D7%93:%D7%97%D7%99%D7%A4%D7%95%D7%A9" class="cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only search-toggle" title="‏חיפוש בוויקיספר‏ [f]" accesskey="f"><span class="vector-icon mw-ui-icon-search mw-ui-icon-wikimedia-search"></span> <span>חיפוש</span> </a> <div class="vector-typeahead-search-container"> <div class="cdx-typeahead-search cdx-typeahead-search--show-thumbnail cdx-typeahead-search--auto-expand-width"> <form action="/w/index.php" id="searchform" class="cdx-search-input cdx-search-input--has-end-button"> <div id="simpleSearch" class="cdx-search-input__input-wrapper" data-search-loc="header-moved"> <div class="cdx-text-input cdx-text-input--has-start-icon"> <input class="cdx-text-input__input" type="search" name="search" placeholder="חיפוש בוויקיספר" aria-label="חיפוש בוויקיספר" autocapitalize="sentences" title="‏חיפוש בוויקיספר‏ [f]" accesskey="f" id="searchInput" > <span class="cdx-text-input__icon cdx-text-input__start-icon"></span> </div> <input type="hidden" name="title" value="מיוחד:חיפוש"> </div> <button class="cdx-button cdx-search-input__end-button">חיפוש</button> </form> </div> </div> </div> <nav class="vector-user-links vector-user-links-wide" aria-label="כלים אישיים"> <div class="vector-user-links-main"> <div id="p-vector-user-menu-preferences" class="vector-menu mw-portlet emptyPortlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> </ul> </div> </div> <div id="p-vector-user-menu-userpage" class="vector-menu mw-portlet emptyPortlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> </ul> </div> </div> <nav class="vector-appearance-landmark" aria-label="מראה"> <div id="vector-appearance-dropdown" class="vector-dropdown " title="שינוי המראה של גודל הגופן, הרוחב והצבע של הדף" > <input type="checkbox" id="vector-appearance-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-appearance-dropdown" class="vector-dropdown-checkbox " aria-label="מראה" > <label id="vector-appearance-dropdown-label" for="vector-appearance-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only " aria-hidden="true" ><span class="vector-icon mw-ui-icon-appearance mw-ui-icon-wikimedia-appearance"></span> <span class="vector-dropdown-label-text">מראה</span> </label> <div class="vector-dropdown-content"> <div id="vector-appearance-unpinned-container" class="vector-unpinned-container"> </div> </div> </div> </nav> <div id="p-vector-user-menu-notifications" class="vector-menu mw-portlet emptyPortlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> </ul> </div> </div> <div id="p-vector-user-menu-overflow" class="vector-menu mw-portlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="pt-sitesupport-2" class="user-links-collapsible-item mw-list-item user-links-collapsible-item"><a data-mw="interface" href="https://donate.wikimedia.org/?utm_source=donate&amp;utm_medium=sidebar&amp;utm_campaign=spontaneous&amp;uselang=he" class=""><span>תרומה לוויקיספר</span></a> </li> <li id="pt-createaccount-2" class="user-links-collapsible-item mw-list-item user-links-collapsible-item"><a data-mw="interface" href="/w/index.php?title=%D7%9E%D7%99%D7%95%D7%97%D7%93:%D7%94%D7%A8%D7%A9%D7%9E%D7%94_%D7%9C%D7%97%D7%A9%D7%91%D7%95%D7%9F&amp;returnto=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F%2F%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F+%D7%92%D7%A8%D7%A1%D7%94+2%2F%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA" title="מומלץ ליצור חשבון ולהיכנס אליו, אך אין חובה לעשות זאת" class=""><span>יצירת חשבון</span></a> </li> <li id="pt-login-2" class="user-links-collapsible-item mw-list-item user-links-collapsible-item"><a data-mw="interface" href="/w/index.php?title=%D7%9E%D7%99%D7%95%D7%97%D7%93:%D7%9B%D7%A0%D7%99%D7%A1%D7%94_%D7%9C%D7%97%D7%A9%D7%91%D7%95%D7%9F&amp;returnto=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F%2F%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F+%D7%92%D7%A8%D7%A1%D7%94+2%2F%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA" title="מומלץ להיכנס לחשבון, אך אין חובה לעשות זאת [o]" accesskey="o" class=""><span>כניסה לחשבון</span></a> </li> </ul> </div> </div> </div> <div id="vector-user-links-dropdown" class="vector-dropdown vector-user-menu vector-button-flush-right vector-user-menu-logged-out" title="אפשרויות נוספות" > <input type="checkbox" id="vector-user-links-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-user-links-dropdown" class="vector-dropdown-checkbox " aria-label="כלים אישיים" > <label id="vector-user-links-dropdown-label" for="vector-user-links-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only " aria-hidden="true" ><span class="vector-icon mw-ui-icon-ellipsis mw-ui-icon-wikimedia-ellipsis"></span> <span class="vector-dropdown-label-text">כלים אישיים</span> </label> <div class="vector-dropdown-content"> <div id="p-personal" class="vector-menu mw-portlet mw-portlet-personal user-links-collapsible-item" title="תפריט משתמש" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="pt-sitesupport" class="user-links-collapsible-item mw-list-item"><a href="https://donate.wikimedia.org/?utm_source=donate&amp;utm_medium=sidebar&amp;utm_campaign=spontaneous&amp;uselang=he"><span>תרומה לוויקיספר</span></a></li><li id="pt-createaccount" class="user-links-collapsible-item mw-list-item"><a href="/w/index.php?title=%D7%9E%D7%99%D7%95%D7%97%D7%93:%D7%94%D7%A8%D7%A9%D7%9E%D7%94_%D7%9C%D7%97%D7%A9%D7%91%D7%95%D7%9F&amp;returnto=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F%2F%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F+%D7%92%D7%A8%D7%A1%D7%94+2%2F%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA" title="מומלץ ליצור חשבון ולהיכנס אליו, אך אין חובה לעשות זאת"><span class="vector-icon mw-ui-icon-userAdd mw-ui-icon-wikimedia-userAdd"></span> <span>יצירת חשבון</span></a></li><li id="pt-login" class="user-links-collapsible-item mw-list-item"><a href="/w/index.php?title=%D7%9E%D7%99%D7%95%D7%97%D7%93:%D7%9B%D7%A0%D7%99%D7%A1%D7%94_%D7%9C%D7%97%D7%A9%D7%91%D7%95%D7%9F&amp;returnto=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F%2F%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F+%D7%92%D7%A8%D7%A1%D7%94+2%2F%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA" title="מומלץ להיכנס לחשבון, אך אין חובה לעשות זאת [o]" accesskey="o"><span class="vector-icon mw-ui-icon-logIn mw-ui-icon-wikimedia-logIn"></span> <span>כניסה לחשבון</span></a></li> </ul> </div> </div> <div id="p-user-menu-anon-editor" class="vector-menu mw-portlet mw-portlet-user-menu-anon-editor" > <div class="vector-menu-heading"> דפים לעורכים שלא נכנסו לחשבון <a href="/wiki/%D7%A2%D7%96%D7%A8%D7%94:%D7%91%D7%A8%D7%95%D7%9B%D7%99%D7%9D_%D7%94%D7%91%D7%90%D7%99%D7%9D" aria-label="מידע נוסף על עריכה"><span>מידע נוסף</span></a> </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="pt-anoncontribs" class="mw-list-item"><a href="/wiki/%D7%9E%D7%99%D7%95%D7%97%D7%93:%D7%94%D7%AA%D7%A8%D7%95%D7%9E%D7%95%D7%AA_%D7%A9%D7%9C%D7%99" title="רשימת העריכות שנעשו מכתובת IP זו [y]" accesskey="y"><span>תרומות</span></a></li><li id="pt-anontalk" class="mw-list-item"><a href="/wiki/%D7%9E%D7%99%D7%95%D7%97%D7%93:%D7%94%D7%A9%D7%99%D7%97%D7%94_%D7%A9%D7%9C%D7%99" title="דיון על העריכות שנעשו מכתובת IP זו [n]" accesskey="n"><span>שיחה</span></a></li> </ul> </div> </div> </div> </div> </nav> </div> </header> </div> <div class="mw-page-container"> <div class="mw-page-container-inner"> <div class="vector-sitenotice-container"> <div id="siteNotice"><!-- CentralNotice --></div> </div> <div class="vector-column-start"> <div class="vector-main-menu-container"> <div id="mw-navigation"> <nav id="mw-panel" class="vector-main-menu-landmark" aria-label="אתר"> <div id="vector-main-menu-pinned-container" class="vector-pinned-container"> </div> </nav> </div> </div> <div class="vector-sticky-pinned-container"> <nav id="mw-panel-toc" aria-label="תוכן עניינים" data-event-name="ui.sidebar-toc" class="mw-table-of-contents-container vector-toc-landmark"> <div id="vector-toc-pinned-container" class="vector-pinned-container"> <div id="vector-toc" class="vector-toc vector-pinnable-element"> <div class="vector-pinnable-header vector-toc-pinnable-header vector-pinnable-header-pinned" data-feature-name="toc-pinned" data-pinnable-element-id="vector-toc" > <h2 class="vector-pinnable-header-label">תוכן עניינים</h2> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-pin-button" data-event-name="pinnable-header.vector-toc.pin">העברה לסרגל הצד</button> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-unpin-button" data-event-name="pinnable-header.vector-toc.unpin">הסתרה</button> </div> <ul class="vector-toc-contents" id="mw-panel-toc-list"> <li id="toc-mw-content-text" class="vector-toc-list-item vector-toc-level-1"> <a href="#" class="vector-toc-link"> <div class="vector-toc-text">התחלה</div> </a> </li> <li id="toc-הצורך_בלולאות" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#הצורך_בלולאות"> <div class="vector-toc-text"> <span class="vector-toc-numb">1</span> <span>הצורך בלולאות</span> </div> </a> <ul id="toc-הצורך_בלולאות-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-לולאת_while" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#לולאת_while"> <div class="vector-toc-text"> <span class="vector-toc-numb">2</span> <span>לולאת while</span> </div> </a> <ul id="toc-לולאת_while-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-לולאת_for" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#לולאת_for"> <div class="vector-toc-text"> <span class="vector-toc-numb">3</span> <span>לולאת for</span> </div> </a> <button aria-controls="toc-לולאת_for-sublist" class="cdx-button cdx-button--weight-quiet cdx-button--icon-only vector-toc-toggle"> <span class="vector-icon mw-ui-icon-wikimedia-expand"></span> <span>שינוי מצב התת־פרק לולאת for</span> </button> <ul id="toc-לולאת_for-sublist" class="vector-toc-list"> <li id="toc-מעבר_על_רשימה" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#מעבר_על_רשימה"> <div class="vector-toc-text"> <span class="vector-toc-numb">3.1</span> <span>מעבר על רשימה</span> </div> </a> <ul id="toc-מעבר_על_רשימה-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-השימוש_ב-range" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#השימוש_ב-range"> <div class="vector-toc-text"> <span class="vector-toc-numb">3.2</span> <span>השימוש ב-range</span> </div> </a> <ul id="toc-השימוש_ב-range-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-הערה_לבעלי_נסיון_בשפות_אחרות" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#הערה_לבעלי_נסיון_בשפות_אחרות"> <div class="vector-toc-text"> <span class="vector-toc-numb">3.3</span> <span>הערה לבעלי נסיון בשפות אחרות</span> </div> </a> <ul id="toc-הערה_לבעלי_נסיון_בשפות_אחרות-sublist" class="vector-toc-list"> </ul> </li> </ul> </li> <li id="toc-סכנות_בתנאי_העצירה" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#סכנות_בתנאי_העצירה"> <div class="vector-toc-text"> <span class="vector-toc-numb">4</span> <span>סכנות בתנאי העצירה</span> </div> </a> <ul id="toc-סכנות_בתנאי_העצירה-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-דוגמה_לשילוב_פלט/קלט,_תנאים,_ולולאות" class="vector-toc-list-item vector-toc-level-1 vector-toc-list-item-expanded"> <a class="vector-toc-link" href="#דוגמה_לשילוב_פלט/קלט,_תנאים,_ולולאות"> <div class="vector-toc-text"> <span class="vector-toc-numb">5</span> <span>דוגמה לשילוב פלט/קלט, תנאים, ולולאות</span> </div> </a> <ul id="toc-דוגמה_לשילוב_פלט/קלט,_תנאים,_ולולאות-sublist" class="vector-toc-list"> </ul> </li> </ul> </div> </div> </nav> </div> </div> <div class="mw-content-container"> <main id="content" class="mw-body"> <header class="mw-body-header vector-page-titlebar"> <nav aria-label="תוכן עניינים" class="vector-toc-landmark"> <div id="vector-page-titlebar-toc" class="vector-dropdown vector-page-titlebar-toc vector-button-flush-left" > <input type="checkbox" id="vector-page-titlebar-toc-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-page-titlebar-toc" class="vector-dropdown-checkbox " aria-label="מצב תוכן העניינים" > <label id="vector-page-titlebar-toc-label" for="vector-page-titlebar-toc-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only " aria-hidden="true" ><span class="vector-icon mw-ui-icon-listBullet mw-ui-icon-wikimedia-listBullet"></span> <span class="vector-dropdown-label-text">מצב תוכן העניינים</span> </label> <div class="vector-dropdown-content"> <div id="vector-page-titlebar-toc-unpinned-container" class="vector-unpinned-container"> </div> </div> </div> </nav> <h1 id="firstHeading" class="firstHeading mw-first-heading"><span class="mw-page-title-main">פייתון/פייתון גרסה 2/לולאות</span></h1> <div id="p-lang-btn" class="vector-dropdown mw-portlet mw-portlet-lang" > <input type="checkbox" id="p-lang-btn-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-p-lang-btn" class="vector-dropdown-checkbox mw-interlanguage-selector" aria-label="הערך הזה קיים רק בשפה הזאת. מוזמנים להוסיף את הערך לשפות אחרות" > <label id="p-lang-btn-label" for="p-lang-btn-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--action-progressive mw-portlet-lang-heading-0" aria-hidden="true" ><span class="vector-icon mw-ui-icon-language-progressive mw-ui-icon-wikimedia-language-progressive"></span> <span class="vector-dropdown-label-text">הוספת שפות</span> </label> <div class="vector-dropdown-content"> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> </ul> <div class="after-portlet after-portlet-lang"><span class="uls-after-portlet-link"></span><span class="wb-langlinks-add wb-langlinks-link"><a href="https://www.wikidata.org/wiki/Special:NewItem?site=hewikibooks&amp;page=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F%2F%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F+%D7%92%D7%A8%D7%A1%D7%94+2%2F%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA" title="הוספת קישורים בין־לשוניים" class="wbc-editpage">הוספת קישורים</a></span></div> </div> </div> </div> </header> <div class="vector-page-toolbar"> <div class="vector-page-toolbar-container"> <div id="left-navigation"> <nav aria-label="מרחבי שם"> <div id="p-associated-pages" class="vector-menu vector-menu-tabs mw-portlet mw-portlet-associated-pages" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="ca-nstab-main" class="selected vector-tab-noicon mw-list-item"><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA" title="צפייה בדף התוכן [c]" accesskey="c"><span>דף</span></a></li><li id="ca-talk" class="vector-tab-noicon mw-list-item"><a href="/wiki/%D7%A9%D7%99%D7%97%D7%94:%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA" rel="discussion" title="שיחה על דף התוכן [t]" accesskey="t"><span>שיחה</span></a></li> </ul> </div> </div> <div id="vector-variants-dropdown" class="vector-dropdown emptyPortlet" > <input type="checkbox" id="vector-variants-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-variants-dropdown" class="vector-dropdown-checkbox " aria-label="שינוי הגוון השפה" > <label id="vector-variants-dropdown-label" for="vector-variants-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet" aria-hidden="true" ><span class="vector-dropdown-label-text">עברית</span> </label> <div class="vector-dropdown-content"> <div id="p-variants" class="vector-menu mw-portlet mw-portlet-variants emptyPortlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> </ul> </div> </div> </div> </div> </nav> </div> <div id="right-navigation" class="vector-collapsible"> <nav aria-label="צפיות"> <div id="p-views" class="vector-menu vector-menu-tabs mw-portlet mw-portlet-views" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="ca-view" class="selected vector-tab-noicon mw-list-item"><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA"><span>קריאה</span></a></li><li id="ca-edit" class="vector-tab-noicon mw-list-item"><a href="/w/index.php?title=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA&amp;action=edit" title="עריכת הדף הזה [e]" accesskey="e"><span>עריכה</span></a></li><li id="ca-history" class="vector-tab-noicon mw-list-item"><a href="/w/index.php?title=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA&amp;action=history" title="גרסאות קודמות של דף זה [h]" accesskey="h"><span>גרסאות קודמות</span></a></li> </ul> </div> </div> </nav> <nav class="vector-page-tools-landmark" aria-label="כלי דף"> <div id="vector-page-tools-dropdown" class="vector-dropdown vector-page-tools-dropdown" > <input type="checkbox" id="vector-page-tools-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-page-tools-dropdown" class="vector-dropdown-checkbox " aria-label="כלים" > <label id="vector-page-tools-dropdown-label" for="vector-page-tools-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet" aria-hidden="true" ><span class="vector-dropdown-label-text">כלים</span> </label> <div class="vector-dropdown-content"> <div id="vector-page-tools-unpinned-container" class="vector-unpinned-container"> <div id="vector-page-tools" class="vector-page-tools vector-pinnable-element"> <div class="vector-pinnable-header vector-page-tools-pinnable-header vector-pinnable-header-unpinned" data-feature-name="page-tools-pinned" data-pinnable-element-id="vector-page-tools" data-pinned-container-id="vector-page-tools-pinned-container" data-unpinned-container-id="vector-page-tools-unpinned-container" > <div class="vector-pinnable-header-label">כלים</div> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-pin-button" data-event-name="pinnable-header.vector-page-tools.pin">העברה לסרגל הצד</button> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-unpin-button" data-event-name="pinnable-header.vector-page-tools.unpin">הסתרה</button> </div> <div id="p-cactions" class="vector-menu mw-portlet mw-portlet-cactions emptyPortlet vector-has-collapsible-items" title="אפשרויות נוספות" > <div class="vector-menu-heading"> פעולות </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="ca-more-view" class="selected vector-more-collapsible-item mw-list-item"><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA"><span>קריאה</span></a></li><li id="ca-more-edit" class="vector-more-collapsible-item mw-list-item"><a href="/w/index.php?title=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA&amp;action=edit" title="עריכת הדף הזה [e]" accesskey="e"><span>עריכה</span></a></li><li id="ca-more-history" class="vector-more-collapsible-item mw-list-item"><a href="/w/index.php?title=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA&amp;action=history"><span>גרסאות קודמות</span></a></li> </ul> </div> </div> <div id="p-tb" class="vector-menu mw-portlet mw-portlet-tb" > <div class="vector-menu-heading"> כללי </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="t-whatlinkshere" class="mw-list-item"><a href="/wiki/%D7%9E%D7%99%D7%95%D7%97%D7%93:%D7%93%D7%A4%D7%99%D7%9D_%D7%94%D7%9E%D7%A7%D7%95%D7%A9%D7%A8%D7%99%D7%9D_%D7%9C%D7%9B%D7%90%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA" title="רשימה של כל דפי הוויקי שמקשרים לדף הזה [j]" accesskey="j"><span>דפים המקושרים לכאן</span></a></li><li id="t-recentchangeslinked" class="mw-list-item"><a href="/wiki/%D7%9E%D7%99%D7%95%D7%97%D7%93:%D7%A9%D7%99%D7%A0%D7%95%D7%99%D7%99%D7%9D_%D7%91%D7%93%D7%A4%D7%99%D7%9D_%D7%94%D7%9E%D7%A7%D7%95%D7%A9%D7%A8%D7%99%D7%9D/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA" rel="nofollow" title="השינויים האחרונים בדפים המקושרים מהדף הזה [k]" accesskey="k"><span>שינויים בדפים המקושרים</span></a></li><li id="t-specialpages" class="mw-list-item"><a href="/wiki/%D7%9E%D7%99%D7%95%D7%97%D7%93:%D7%93%D7%A4%D7%99%D7%9D_%D7%9E%D7%99%D7%95%D7%97%D7%93%D7%99%D7%9D" title="רשימה של כל הדפים המיוחדים [q]" accesskey="q"><span>דפים מיוחדים</span></a></li><li id="t-permalink" class="mw-list-item"><a href="/w/index.php?title=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA&amp;oldid=148886" title="קישור קבוע לגרסה הזאת של הדף הזה"><span>קישור קבוע</span></a></li><li id="t-info" class="mw-list-item"><a href="/w/index.php?title=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA&amp;action=info" title="מידע נוסף על הדף הזה"><span>מידע על הדף</span></a></li><li id="t-cite" class="mw-list-item"><a href="/w/index.php?title=%D7%9E%D7%99%D7%95%D7%97%D7%93:%D7%A6%D7%99%D7%98%D7%95%D7%98_%D7%93%D7%A3_%D7%96%D7%94&amp;page=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F%2F%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2%2F%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA&amp;id=148886&amp;wpFormIdentifier=titleform" title="מידע איך לצטט את הדף הזה"><span>ציטוט הדף הזה</span></a></li><li id="t-urlshortener" class="mw-list-item"><a href="/w/index.php?title=%D7%9E%D7%99%D7%95%D7%97%D7%93:%D7%9E%D7%A7%D7%A6%D7%A8_%D7%9B%D7%AA%D7%95%D7%91%D7%95%D7%AA&amp;url=https%3A%2F%2Fhe.wikibooks.org%2Fwiki%2F%25D7%25A4%25D7%2599%25D7%2599%25D7%25AA%25D7%2595%25D7%259F%2F%25D7%25A4%25D7%2599%25D7%2599%25D7%25AA%25D7%2595%25D7%259F_%25D7%2592%25D7%25A8%25D7%25A1%25D7%2594_2%2F%25D7%259C%25D7%2595%25D7%259C%25D7%2590%25D7%2595%25D7%25AA"><span>קבלת כתובת מקוצרת</span></a></li><li id="t-urlshortener-qrcode" class="mw-list-item"><a href="/w/index.php?title=%D7%9E%D7%99%D7%95%D7%97%D7%93:QrCode&amp;url=https%3A%2F%2Fhe.wikibooks.org%2Fwiki%2F%25D7%25A4%25D7%2599%25D7%2599%25D7%25AA%25D7%2595%25D7%259F%2F%25D7%25A4%25D7%2599%25D7%2599%25D7%25AA%25D7%2595%25D7%259F_%25D7%2592%25D7%25A8%25D7%25A1%25D7%2594_2%2F%25D7%259C%25D7%2595%25D7%259C%25D7%2590%25D7%2595%25D7%25AA"><span>הורדת קוד QR</span></a></li> </ul> </div> </div> <div id="p-coll-print_export" class="vector-menu mw-portlet mw-portlet-coll-print_export" > <div class="vector-menu-heading"> הדפסה/יצוא </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="coll-create_a_book" class="mw-list-item"><a href="/w/index.php?title=%D7%9E%D7%99%D7%95%D7%97%D7%93:%D7%A1%D7%A4%D7%A8&amp;bookcmd=book_creator&amp;referer=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F%2F%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F+%D7%92%D7%A8%D7%A1%D7%94+2%2F%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA"><span>יצירת ספר</span></a></li><li id="coll-download-as-rl" class="mw-list-item"><a href="/w/index.php?title=%D7%9E%D7%99%D7%95%D7%97%D7%93:DownloadAsPdf&amp;page=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F%2F%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2%2F%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA&amp;action=show-download-screen"><span>הורדה כ־PDF</span></a></li><li id="t-print" class="mw-list-item"><a href="/w/index.php?title=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA&amp;printable=yes" title="גרסה להדפסה של הדף הזה [p]" accesskey="p"><span>גרסה להדפסה</span></a></li> </ul> </div> </div> <div id="p-wikibase-otherprojects" class="vector-menu mw-portlet mw-portlet-wikibase-otherprojects emptyPortlet" > <div class="vector-menu-heading"> במיזמים אחרים </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> </ul> </div> </div> </div> </div> </div> </div> </nav> </div> </div> </div> <div class="vector-column-end"> <div class="vector-sticky-pinned-container"> <nav class="vector-page-tools-landmark" aria-label="כלי דף"> <div id="vector-page-tools-pinned-container" class="vector-pinned-container"> </div> </nav> <nav class="vector-appearance-landmark" aria-label="מראה"> <div id="vector-appearance-pinned-container" class="vector-pinned-container"> <div id="vector-appearance" class="vector-appearance vector-pinnable-element"> <div class="vector-pinnable-header vector-appearance-pinnable-header vector-pinnable-header-pinned" data-feature-name="appearance-pinned" data-pinnable-element-id="vector-appearance" data-pinned-container-id="vector-appearance-pinned-container" data-unpinned-container-id="vector-appearance-unpinned-container" > <div class="vector-pinnable-header-label">מראה</div> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-pin-button" data-event-name="pinnable-header.vector-appearance.pin">העברה לסרגל הצד</button> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-unpin-button" data-event-name="pinnable-header.vector-appearance.unpin">הסתרה</button> </div> </div> </div> </nav> </div> </div> <div id="bodyContent" class="vector-body" aria-labelledby="firstHeading" data-mw-ve-target-container> <div class="vector-body-before-content"> <div class="mw-indicators"> </div> <div id="siteSub" class="noprint">מתוך ויקיספר, אוסף הספרים והמדריכים החופשי</div> </div> <div id="contentSub"><div id="mw-content-subtitle"><div class="subpages">&lt; <bdi dir="rtl"><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F" title="פייתון">פייתון</a></bdi> | <bdi dir="rtl"><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2" title="פייתון/פייתון גרסה 2">פייתון גרסה 2</a></bdi></div></div></div> <div id="mw-content-text" class="mw-body-content"><div class="mw-content-rtl mw-parser-output" lang="he" dir="rtl"><table class="ambox ambox-content plainlinks noprint" align="center" style="border: 1px solid #CCCCCC; border-radius:1em; background: #FFFCFC; clear: both;"> <tbody><tr> <td style="text-align: right;" class="mbox-text"><div class="mbox-text-div"><b><a href="/wiki/%D7%A7%D7%98%D7%92%D7%95%D7%A8%D7%99%D7%94:%D7%95%D7%99%D7%A7%D7%99%D7%A1%D7%A4%D7%A8:_%D7%93%D7%A4%D7%99%D7%9D_%D7%94%D7%9E%D7%95%D7%A2%D7%9E%D7%93%D7%99%D7%9D_%D7%9C%D7%90%D7%99%D7%97%D7%95%D7%93" title="קטגוריה:ויקיספר: דפים המועמדים לאיחוד">דף זה מועמד לאיחוד</a></b></div><div class="hide-when-compact">דף זה דן בנושא של הדף <b><a href="/wiki/%D7%9C%D7%99%D7%9E%D7%95%D7%93_%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%9C%D7%90%D7%99%D7%A9_%D7%94%D7%A4%D7%A9%D7%95%D7%98/%D7%9C%D7%A1%D7%A4%D7%95%D7%A8_%D7%A2%D7%93_10" title="לימוד פייתון לאיש הפשוט/לספור עד 10">לימוד פייתון לאיש הפשוט/לספור עד 10</a></b> וככל הנראה מוסיף עליו מידע. על כן כנראה שיש לאחד את שני הדפים. (<b><a href="/wiki/%D7%A9%D7%99%D7%97%D7%94:%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA" title="שיחה:פייתון/פייתון גרסה 2/לולאות">דיון</a></b>)</div> </td></tr></tbody></table> <p><br /> </p> <div class="NavFrame noprint" style="clear: left; float: left; margin: 5px; width: 250px;"> <div style="background-color: #CCCCFF;"> <p><b><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2" title="פייתון/פייתון גרסה 2">פייתון/פייתון גרסה 2</a></b> </p> </div> <div style="padding: 0.5em 1em 0.5em 1em; text-align: right; background-color: #f9f9f9;"> <ul><li><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%94%D7%9B%D7%A0%D7%94" title="פייתון/פייתון גרסה 2/הכנה">הכנה</a></li> <li><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/IDLE" title="פייתון/פייתון גרסה 2/IDLE">IDLE</a></li> <li><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%A9%D7%9C%D7%95%D7%9D_%D7%A2%D7%95%D7%9C%D7%9D!" title="פייתון/פייתון גרסה 2/שלום עולם!">שלום עולם!</a></li> <li><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%94%D7%A2%D7%A8%D7%95%D7%AA" title="פייתון/פייתון גרסה 2/הערות">הערות</a></li> <li><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9E%D7%97%D7%A8%D7%95%D7%96%D7%95%D7%AA" title="פייתון/פייתון גרסה 2/מחרוזות">מחרוזות</a></li> <li><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9E%D7%A1%D7%A4%D7%A8%D7%99%D7%9D_%D7%95%D7%A4%D7%A2%D7%95%D7%9C%D7%95%D7%AA_%D7%97%D7%A9%D7%91%D7%95%D7%A0%D7%99%D7%95%D7%AA" title="פייתון/פייתון גרסה 2/מספרים ופעולות חשבוניות">מספרים ופעולות חשבוניות</a></li> <li><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%94%D7%9E%D7%A8%D7%95%D7%AA_%D7%91%D7%99%D7%9F_%D7%9E%D7%A1%D7%A4%D7%A8%D7%99%D7%9D_%D7%9C%D7%A2%D7%A6%D7%9E%D7%9D_%D7%95%D7%9E%D7%97%D7%A8%D7%95%D7%96%D7%95%D7%AA" title="פייתון/פייתון גרסה 2/המרות בין מספרים לעצמם ומחרוזות">המרות בין מספרים לעצמם ומחרוזות</a></li> <li><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%91%D7%9E%D7%A7%D7%95%D7%9D_%D7%9E%D7%A9%D7%AA%D7%A0%D7%99%D7%9D_-_%D7%A2%D7%A6%D7%9E%D7%99%D7%9D_%D7%95%D7%A9%D7%9E%D7%95%D7%AA" title="פייתון/פייתון גרסה 2/במקום משתנים - עצמים ושמות">במקום משתנים - עצמים ושמות</a></li> <li><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%A7%D7%9C%D7%98_%D7%95%D7%A4%D7%9C%D7%98" title="פייתון/פייתון גרסה 2/קלט ופלט">קלט ופלט</a></li> <li><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%A8%D7%A9%D7%99%D7%9E%D7%95%D7%AA" title="פייתון/פייתון גרסה 2/רשימות">רשימות</a></li> <li><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%91%D7%99%D7%98%D7%95%D7%99%D7%99%D7%9D_%D7%91%D7%95%D7%9C%D7%99%D7%90%D7%A0%D7%99%D7%99%D7%9D_%D7%95%D7%AA%D7%A0%D7%90%D7%99%D7%9D" title="פייתון/פייתון גרסה 2/ביטויים בוליאניים ותנאים">ביטויים בוליאניים ותנאים</a></li> <li><a class="mw-selflink selflink">לולאות</a></li> <li><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%98%D7%90%D7%A4%D7%9C" title="פייתון/פייתון גרסה 2/טאפל">טאפל</a></li> <li><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9E%D7%99%D7%9C%D7%95%D7%A0%D7%99%D7%9D" title="פייתון/פייתון גרסה 2/מילונים">מילונים</a></li> <li><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%A4%D7%95%D7%A0%D7%A7%D7%A6%D7%99%D7%95%D7%AA" title="פייתון/פייתון גרסה 2/פונקציות">פונקציות</a></li> <li><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%A7%D7%9C%D7%98_%D7%95%D7%A4%D7%9C%D7%98_%D7%91%D7%A7%D7%91%D7%A6%D7%99%D7%9D" title="פייתון/פייתון גרסה 2/קלט ופלט בקבצים">קלט ופלט בקבצים</a></li> <li><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9E%D7%97%D7%9C%D7%A7%D7%95%D7%AA" title="פייתון/פייתון גרסה 2/מחלקות">מחלקות</a></li> <li><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%97%D7%A8%D7%99%D7%92%D7%99%D7%9D" title="פייתון/פייתון גרסה 2/חריגים">חריגים</a></li> <li><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9E%D7%95%D7%93%D7%95%D7%9C%D7%99%D7%9D" title="פייתון/פייתון גרסה 2/מודולים">מודולים</a></li> <li><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%A7%D7%91%D7%A6%D7%99_%D7%90%D7%A6%D7%95%D7%95%D7%94" title="פייתון/פייתון גרסה 2/קבצי אצווה">קבצי אצווה</a></li> <li><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%94%D7%A1%D7%A4%D7%A8%D7%99%D7%94_%D7%94%D7%A1%D7%98%D7%A0%D7%93%D7%A8%D7%98%D7%99%D7%AA" title="פייתון/פייתון גרסה 2/הספריה הסטנדרטית">הספריה הסטנדרטית</a></li> <li><a href="/w/index.php?title=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%94%D7%A8%D7%97%D7%91%D7%95%D7%AA_%D7%91%D7%A2%D7%96%D7%A8%D7%AA_%D7%A9%D7%A4%D7%95%D7%AA_%D7%A0%D7%9E%D7%95%D7%9B%D7%95%D7%AA_%D7%99%D7%95%D7%AA%D7%A8&amp;action=edit&amp;redlink=1" class="new" title="פייתון/פייתון גרסה 2/הרחבות בעזרת שפות נמוכות יותר (הדף אינו קיים)">הרחבות בעזרת שפות נמוכות יותר</a></li> <li><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%AA%D7%9B%D7%A0%D7%95%D7%AA_%D7%9E%D7%A7%D7%91%D7%99%D7%9C%D7%99" title="פייתון/פייתון גרסה 2/תכנות מקבילי">תכנות מקבילי</a></li> <li><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%A9%D7%99%D7%9E%D7%95%D7%A9_%D7%91%D7%A9%D7%A7%D7%A2%D7%99%D7%9D" title="פייתון/פייתון גרסה 2/שימוש בשקעים">שימוש בשקעים</a></li> <li><a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%A0%D7%A1%D7%A4%D7%97%D7%99%D7%9D" title="פייתון/פייתון גרסה 2/נספחים">נספחים</a></li></ul> <div style="text-align: center;"><a class="external text" href="https://he.wikibooks.org/w/index.php?title=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%92%D7%A8%D7%A1%D7%94_%D7%9C%D7%94%D7%93%D7%A4%D7%A1%D7%94&amp;printable=yes">גרסה להדפסה</a> </div> </div> </div> <p><i>לולאות</i> משמשות לחזרה על קטע קוד מספר פעמים. לולאה חוסכת בזמן כתיבת התוכנה ומסדרת את הקוד. </p> <meta property="mw:PageProp/toc" /> <div class="mw-heading mw-heading2"><h2 id="הצורך_בלולאות"><span id=".D7.94.D7.A6.D7.95.D7.A8.D7.9A_.D7.91.D7.9C.D7.95.D7.9C.D7.90.D7.95.D7.AA"></span>הצורך בלולאות</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA&amp;action=edit&amp;section=1" title="עריכת פסקה: &quot;הצורך בלולאות&quot;"><span>עריכה</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>ב<a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9E%D7%A1%D7%A4%D7%A8%D7%99%D7%9D_%D7%95%D7%A4%D7%A2%D7%95%D7%9C%D7%95%D7%AA_%D7%97%D7%A9%D7%91%D7%95%D7%A0%D7%99%D7%95%D7%AA" title="פייתון/פייתון גרסה 2/מספרים ופעולות חשבוניות">מספרים ופעולות חשבוניות</a>, ראינו <a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9E%D7%A1%D7%A4%D7%A8%D7%99%D7%9D_%D7%95%D7%A4%D7%A2%D7%95%D7%9C%D7%95%D7%AA_%D7%97%D7%A9%D7%91%D7%95%D7%A0%D7%99%D7%95%D7%AA#דוגמה:_המרה_בין_סוגי_מעלות_שונים" title="פייתון/פייתון גרסה 2/מספרים ופעולות חשבוניות">דוגמה להמרה בין סוגי מעלות שונים</a> כיצד להמיר ממעלות בCelsius למעלות בFahrenheit. נניח שאנו רוצים להדפיס את התרגום למעלות Fahrenheit של מעלות הCelsius בערכים <span dir="ltr">0, 2, 4, ..., 20</span>. ננסה לעשות זאת כך: </p> <div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span></span><span class="n">c</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">f</span> <span class="o">=</span> <span class="mf">1.8</span> <span class="o">*</span> <span class="n">c</span> <span class="o">+</span> <span class="mi">32</span><span class="p">;</span> <span class="nb">print</span> <span class="nb">str</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; in Celsius is &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; in Fahrenheit&#39;</span> <span class="n">c</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span> <span class="n">f</span> <span class="o">=</span> <span class="mf">1.8</span> <span class="o">*</span> <span class="n">c</span> <span class="o">+</span> <span class="mi">32</span><span class="p">;</span> <span class="nb">print</span> <span class="nb">str</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; in Celsius is &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; in Fahrenheit&#39;</span> <span class="n">c</span> <span class="o">=</span> <span class="mi">4</span><span class="p">;</span> <span class="n">f</span> <span class="o">=</span> <span class="mf">1.8</span> <span class="o">*</span> <span class="n">c</span> <span class="o">+</span> <span class="mi">32</span><span class="p">;</span> <span class="nb">print</span> <span class="nb">str</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; in Celsius is &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; in Fahrenheit&#39;</span> <span class="n">c</span> <span class="o">=</span> <span class="mi">6</span><span class="p">;</span> <span class="n">f</span> <span class="o">=</span> <span class="mf">1.8</span> <span class="o">*</span> <span class="n">c</span> <span class="o">+</span> <span class="mi">32</span><span class="p">;</span> <span class="nb">print</span> <span class="nb">str</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; in Celsius is &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; in Fahrenheit&#39;</span> <span class="n">c</span> <span class="o">=</span> <span class="mi">8</span><span class="p">;</span> <span class="n">f</span> <span class="o">=</span> <span class="mf">1.8</span> <span class="o">*</span> <span class="n">c</span> <span class="o">+</span> <span class="mi">32</span><span class="p">;</span> <span class="nb">print</span> <span class="nb">str</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; in Celsius is &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; in Fahrenheit&#39;</span> <span class="n">c</span> <span class="o">=</span> <span class="mi">10</span><span class="p">;</span> <span class="n">f</span> <span class="o">=</span> <span class="mf">1.8</span> <span class="o">*</span> <span class="n">c</span> <span class="o">+</span> <span class="mi">32</span><span class="p">;</span> <span class="nb">print</span> <span class="nb">str</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; in Celsius is &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; in Fahrenheit&#39;</span> <span class="n">c</span> <span class="o">=</span> <span class="mi">12</span><span class="p">;</span> <span class="n">f</span> <span class="o">=</span> <span class="mf">1.8</span> <span class="o">*</span> <span class="n">c</span> <span class="o">+</span> <span class="mi">32</span><span class="p">;</span> <span class="nb">print</span> <span class="nb">str</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; in Celsius is &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; in Fahrenheit&#39;</span> <span class="n">c</span> <span class="o">=</span> <span class="mi">14</span><span class="p">;</span> <span class="n">f</span> <span class="o">=</span> <span class="mf">1.8</span> <span class="o">*</span> <span class="n">c</span> <span class="o">+</span> <span class="mi">32</span><span class="p">;</span> <span class="nb">print</span> <span class="nb">str</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; in Celsius is &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; in Fahrenheit&#39;</span> <span class="n">c</span> <span class="o">=</span> <span class="mi">16</span><span class="p">;</span> <span class="n">f</span> <span class="o">=</span> <span class="mf">1.8</span> <span class="o">*</span> <span class="n">c</span> <span class="o">+</span> <span class="mi">32</span><span class="p">;</span> <span class="nb">print</span> <span class="nb">str</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; in Celsius is &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; in Fahrenheit&#39;</span> <span class="n">c</span> <span class="o">=</span> <span class="mi">18</span><span class="p">;</span> <span class="n">f</span> <span class="o">=</span> <span class="mf">1.8</span> <span class="o">*</span> <span class="n">c</span> <span class="o">+</span> <span class="mi">32</span><span class="p">;</span> <span class="nb">print</span> <span class="nb">str</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; in Celsius is &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; in Fahrenheit&#39;</span> <span class="n">c</span> <span class="o">=</span> <span class="mi">20</span><span class="p">;</span> <span class="n">f</span> <span class="o">=</span> <span class="mf">1.8</span> <span class="o">*</span> <span class="n">c</span> <span class="o">+</span> <span class="mi">32</span><span class="p">;</span> <span class="nb">print</span> <span class="nb">str</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; in Celsius is &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; in Fahrenheit&#39;</span> </pre></div> <p>קל לראות שמשהו בעייתי בקוד, והבעייתיות היתה גוברת לו היינו פועלים בתחום גדול יותר, לדוגמה <span dir="ltr">0, 2, 4, ..., 100</span>. בין היתר: </p> <ol><li>הקוד ארוך ומסורבל מאד.</li> <li>תמיד ייתכן שהקוד כולל שגיאה כלשהי: ייתכן שטעינו בהעתקת הנוסחה הממירה, לדוגמה. כאן נצטרך לתקן את הקוד ב-11 מקומות.</li></ol> <p>בפרק זה נלמד להשתמש בלולאות, המאפשרות לתרגם את הקוד הקודם לקוד תמציתי יותר: </p> <div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span></span><span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">2</span><span class="p">):</span> <span class="n">f</span> <span class="o">=</span> <span class="mf">1.8</span> <span class="o">*</span> <span class="n">c</span> <span class="o">+</span> <span class="mi">32</span><span class="p">;</span> <span class="nb">print</span> <span class="nb">str</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; in Celsius is &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; in Fahrenheit&#39;</span> </pre></div> <p>שתוצאתו דומה. </p> <div class="mw-heading mw-heading2"><h2 id="לולאת_while"><span id=".D7.9C.D7.95.D7.9C.D7.90.D7.AA_while"></span>לולאת while</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA&amp;action=edit&amp;section=2" title="עריכת פסקה: &quot;לולאת while&quot;"><span>עריכה</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>לולאת while היא לולאה הפועלת כל עוד תנאי מוגדר מתקיים. אופן כתיבת הלולאה הוא: </p> <div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span></span><span class="k">while</span> <span class="o">&lt;</span><span class="n">condition</span><span class="o">&gt;</span><span class="p">:</span> <span class="o">&lt;</span><span class="n">action</span><span class="o">&gt;</span> </pre></div> <p>כאשר condition הוא <a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%91%D7%99%D7%98%D7%95%D7%99%D7%99%D7%9D_%D7%91%D7%95%D7%9C%D7%99%D7%90%D7%A0%D7%99%D7%99%D7%9D_%D7%95%D7%AA%D7%A0%D7%90%D7%99%D7%9D#ערכים_בוליאניים" title="פייתון/פייתון גרסה 2/ביטויים בוליאניים ותנאים">תנאי בוליאני</a>, ו-action הוא ביטוי (או <a href="/w/index.php?title=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9E%D7%A9%D7%AA%D7%A0%D7%99%D7%9D&amp;action=edit&amp;redlink=1" class="new" title="פייתון/פייתון גרסה 2/משתנים (הדף אינו קיים)">בלוק</a>) המתבצע כל עוד התנאי הבוליאני מתקיים. </p><p>לדוגמה, קטע הקוד הבא מדפיס את המספרים 1-20 (כל אחד בשורה): </p> <div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span></span><span class="n">i</span> <span class="o">=</span> <span class="mi">1</span> <span class="k">while</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="mi">20</span><span class="p">:</span> <span class="nb">print</span> <span class="n">i</span> <span class="n">i</span> <span class="o">=</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span> </pre></div> <p><br /> </p> <table style="background-color: #F0FFFF; border: solid 1px Gray; margin: 0.5em 2em; cellspacing: 1; cellpadding: 1: -webkit-border-radius: 8px; border-radius: 8px;"> <tbody><tr> <td valign="top"><span typeof="mw:File"><span><img alt="" src="//upload.wikimedia.org/wikipedia/commons/thumb/c/c7/Document-restricted.svg/50px-Document-restricted.svg.png" decoding="async" width="50" height="53" class="mw-file-element" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/c/c7/Document-restricted.svg/75px-Document-restricted.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/c/c7/Document-restricted.svg/100px-Document-restricted.svg.png 2x" data-file-width="714" data-file-height="762" /></span></span> </td> <td style="padding: 5px;"><p><b>עכשיו תורכם:</b></p>כתבו תוכנית שמדפיסה את כל המספרים האי-זוגיים מ-1 עד 20, השתמשו בלולאת while. </td></tr></tbody></table> <p><br /> </p> <div style="width:-moz-fit-content; block-size:-moz-fit-content;width:-webkit-fit-content; block-size:-webkit-fit-content;width:-ms-fit-content; block-size:-ms-fit-content;width:-o-fit-content; block-size:-o-fit-content;width:fit-content; block-size:fit-content;" class="mw-collapsible mw-collapsed"> <div style="background-color: transparent;">פתרון</div> <div style="text-align:right; background-color: transparent;" class="mw-collapsible-content"> <div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span></span><span class="n">i</span> <span class="o">=</span> <span class="mi">1</span> <span class="k">while</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="mi">20</span><span class="p">:</span> <span class="nb">print</span> <span class="n">i</span> <span class="n">i</span> <span class="o">=</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">2</span> </pre></div> </div></div> <p><br /> </p> <div class="mw-heading mw-heading2"><h2 id="לולאת_for"><span id=".D7.9C.D7.95.D7.9C.D7.90.D7.AA_for"></span>לולאת for</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA&amp;action=edit&amp;section=3" title="עריכת פסקה: &quot;לולאת for&quot;"><span>עריכה</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>לולאת for הנה לרוב תמציתית יותר מלולאת while. לולאת while דורשת פקודה שתעצור אותה שתופסת מקום רב בזכרון, לכן עדיף להשתמש בלולאת for. </p> <div class="mw-heading mw-heading3"><h3 id="מעבר_על_רשימה"><span id=".D7.9E.D7.A2.D7.91.D7.A8_.D7.A2.D7.9C_.D7.A8.D7.A9.D7.99.D7.9E.D7.94"></span>מעבר על רשימה</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA&amp;action=edit&amp;section=4" title="עריכת פסקה: &quot;מעבר על רשימה&quot;"><span>עריכה</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>לולאת for עוברת על כל אחד מאיברי <a href="/wiki/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%A8%D7%A9%D7%99%D7%9E%D7%95%D7%AA" title="פייתון/פייתון גרסה 2/רשימות">רשימה</a>. אופן כתיבת הלולאה הוא: </p> <div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span></span><span class="k">for</span> <span class="o">&lt;</span><span class="n">name</span><span class="o">&gt;</span> <span class="ow">in</span> <span class="o">&lt;</span><span class="n">lst</span><span class="o">&gt;</span><span class="p">:</span> <span class="o">&lt;</span><span class="n">action</span><span class="o">&gt;</span> </pre></div> <p>כאשר name הוא שם, lst היא רשימה, ו-action היא פעולה לביצוע על כל איבר. הלולאה תעבור איבר איבר, תשייך את השם name לאיבר, ותבצע את action. </p><p>לדוגמה, הלולאה הבא תדפיס שבע מחרוזות, כל יום בשורה נפרדת: </p> <div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span></span><span class="k">for</span> <span class="n">day</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;Sunday&#39;</span><span class="p">,</span> <span class="s1">&#39;Monday&#39;</span><span class="p">,</span> <span class="s1">&#39;Tuesday&#39;</span><span class="p">,</span> <span class="s1">&#39;Wednesday&#39;</span><span class="p">,</span> <span class="s1">&#39;Thursday&#39;</span><span class="p">,</span> <span class="s1">&#39;Friday&#39;</span><span class="p">,</span> <span class="s1">&#39;Saturday&#39;</span><span class="p">]:</span> <span class="nb">print</span> <span class="n">day</span> </pre></div> <p><br /> </p> <table style="background-color: #F0FFFF; border: solid 1px Gray; margin: 0.5em 2em; cellspacing: 1; cellpadding: 1: -webkit-border-radius: 8px; border-radius: 8px;"> <tbody><tr> <td valign="top"><span typeof="mw:File"><span><img alt="" src="//upload.wikimedia.org/wikipedia/commons/thumb/c/c7/Document-restricted.svg/50px-Document-restricted.svg.png" decoding="async" width="50" height="53" class="mw-file-element" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/c/c7/Document-restricted.svg/75px-Document-restricted.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/c/c7/Document-restricted.svg/100px-Document-restricted.svg.png 2x" data-file-width="714" data-file-height="762" /></span></span> </td> <td style="padding: 5px;"><p><b>עכשיו תורכם:</b></p>מה תדפיס הלולאה? </td></tr></tbody></table> <div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span></span><span class="k">for</span> <span class="n">day</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;Sunday&#39;</span><span class="p">,</span> <span class="s1">&#39;Monday&#39;</span><span class="p">,</span> <span class="s1">&#39;Tuesday&#39;</span><span class="p">,</span> <span class="s1">&#39;Wednesday&#39;</span><span class="p">,</span> <span class="s1">&#39;Thursday&#39;</span><span class="p">,</span> <span class="s1">&#39;Friday&#39;</span><span class="p">,</span> <span class="s1">&#39;Saturday&#39;</span><span class="p">]:</span> <span class="nb">print</span> <span class="s1">&#39;‎Today is &#39;</span> <span class="o">+</span> <span class="n">day</span> </pre></div> <p><br /> </p> <div style="width:-moz-fit-content; block-size:-moz-fit-content;width:-webkit-fit-content; block-size:-webkit-fit-content;width:-ms-fit-content; block-size:-ms-fit-content;width:-o-fit-content; block-size:-o-fit-content;width:fit-content; block-size:fit-content;" class="mw-collapsible mw-collapsed"> <div style="background-color: transparent;">פתרון</div> <div style="text-align:right; background-color: transparent;" class="mw-collapsible-content"> <pre>Today is Sunday Today is Monday Today is Tuesday Today is Wednesday Today is Thursday Today is Friday Today is Saturday </pre> </div></div> <p><br /> </p> <table style="background-color: #F0FFFF; border: solid 1px Gray; margin: 0.5em 2em; cellspacing: 1; cellpadding: 1: -webkit-border-radius: 8px; border-radius: 8px;"> <tbody><tr> <td valign="top"><span typeof="mw:File"><span><img alt="" src="//upload.wikimedia.org/wikipedia/commons/thumb/c/c7/Document-restricted.svg/50px-Document-restricted.svg.png" decoding="async" width="50" height="53" class="mw-file-element" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/c/c7/Document-restricted.svg/75px-Document-restricted.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/c/c7/Document-restricted.svg/100px-Document-restricted.svg.png 2x" data-file-width="714" data-file-height="762" /></span></span> </td> <td style="padding: 5px;"><p><b>עכשיו תורכם:</b></p>כתבו תוכנית שמדפיסה את כל המספרים האי-זוגיים מ-1 עד 20, השתמשו בלולאת for. </td></tr></tbody></table> <p><br /> </p> <div style="width:-moz-fit-content; block-size:-moz-fit-content;width:-webkit-fit-content; block-size:-webkit-fit-content;width:-ms-fit-content; block-size:-ms-fit-content;width:-o-fit-content; block-size:-o-fit-content;width:fit-content; block-size:fit-content;" class="mw-collapsible mw-collapsed"> <div style="background-color: transparent;">פתרון</div> <div style="text-align:right; background-color: transparent;" class="mw-collapsible-content"> <div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span></span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">17</span><span class="p">,</span> <span class="mi">19</span><span class="p">]:</span> <span class="nb">print</span> <span class="n">i</span> </pre></div> </div></div> <p><br /> </p> <div class="mw-heading mw-heading3"><h3 id="השימוש_ב-range"><span id=".D7.94.D7.A9.D7.99.D7.9E.D7.95.D7.A9_.D7.91-range"></span>השימוש ב-range</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA&amp;action=edit&amp;section=5" title="עריכת פסקה: &quot;השימוש ב-range&quot;"><span>עריכה</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>לולאת for הנה נוחה במיוחד כאשר משתמשים בה בצירוף <a href="/w/index.php?title=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%94%D7%A8%D7%A9%D7%99%D7%9E%D7%94_range&amp;action=edit&amp;redlink=1" class="new" title="פייתון/פייתון גרסה 2/הרשימה range (הדף אינו קיים)">הרשימה range</a>. </p><p>לדוגמה, קטע הקוד הבא מדפיס את המספרים 0-19 (כל אחד בשורה): </p> <div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span></span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">20</span><span class="p">):</span> <span class="nb">print</span> <span class="n">i</span> </pre></div> <p><br /> </p> <table style="background-color: #F0FFFF; border: solid 1px Gray; margin: 0.5em 2em; cellspacing: 1; cellpadding: 1: -webkit-border-radius: 8px; border-radius: 8px;"> <tbody><tr> <td valign="top"><span typeof="mw:File"><span><img alt="" src="//upload.wikimedia.org/wikipedia/commons/thumb/c/c7/Document-restricted.svg/50px-Document-restricted.svg.png" decoding="async" width="50" height="53" class="mw-file-element" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/c/c7/Document-restricted.svg/75px-Document-restricted.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/c/c7/Document-restricted.svg/100px-Document-restricted.svg.png 2x" data-file-width="714" data-file-height="762" /></span></span> </td> <td style="padding: 5px;"><p><b>עכשיו תורכם:</b></p>כתבו תוכנית שמדפיסה את כל המספרים האי-זוגיים מ-1 עד 20, השתמשו בלולאת for. </td></tr></tbody></table> <p><br /> </p> <div style="width:-moz-fit-content; block-size:-moz-fit-content;width:-webkit-fit-content; block-size:-webkit-fit-content;width:-ms-fit-content; block-size:-ms-fit-content;width:-o-fit-content; block-size:-o-fit-content;width:fit-content; block-size:fit-content;" class="mw-collapsible mw-collapsed"> <div style="background-color: transparent;">פתרון</div> <div style="text-align:right; background-color: transparent;" class="mw-collapsible-content"> <div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span></span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">2</span><span class="p">):</span> <span class="nb">print</span> <span class="n">i</span> </pre></div> <div style="text-align: right;"> <p>ואפשר גם כך: </p> </div> <div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span></span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">):</span> <span class="nb">print</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span> </pre></div> </div></div> <p><br /> </p> <div class="mw-heading mw-heading3"><h3 id="הערה_לבעלי_נסיון_בשפות_אחרות"><span id=".D7.94.D7.A2.D7.A8.D7.94_.D7.9C.D7.91.D7.A2.D7.9C.D7.99_.D7.A0.D7.A1.D7.99.D7.95.D7.9F_.D7.91.D7.A9.D7.A4.D7.95.D7.AA_.D7.90.D7.97.D7.A8.D7.95.D7.AA"></span>הערה לבעלי נסיון בשפות אחרות</h3><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA&amp;action=edit&amp;section=6" title="עריכת פסקה: &quot;הערה לבעלי נסיון בשפות אחרות&quot;"><span>עריכה</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>ברוב שפות התכנות, לולאת for מקדמת אינדקסים מספריים. לכן, קוד פייתון של בעלי נסיון בשפות אחרות נראה לפעמים כך: </p> <div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;Shalom&#39;</span><span class="p">,</span> <span class="s1">&#39;olam&#39;</span><span class="p">,</span> <span class="s1">&#39;ma&#39;</span><span class="p">,</span> <span class="s1">&#39;shlomha&#39;</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">a</span><span class="p">)):</span> <span class="nb">print</span> <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> </pre></div> <p>חשוב להבין שזה <i><a href="https://he.wikipedia.org/wiki/en:python_programming_language#Neologisms" class="extiw" title="w:en:python programming language">לא פיתוני</a></i>, ואפשר לכתוב זאת בצורה פשוטה יותר, כך: </p> <div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;Shalom&#39;</span><span class="p">,</span> <span class="s1">&#39;olam&#39;</span><span class="p">,</span> <span class="s1">&#39;ma&#39;</span><span class="p">,</span> <span class="s1">&#39;shlomha&#39;</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">a</span><span class="p">:</span> <span class="nb">print</span> <span class="n">i</span> </pre></div> <div class="mw-heading mw-heading2"><h2 id="סכנות_בתנאי_העצירה"><span id=".D7.A1.D7.9B.D7.A0.D7.95.D7.AA_.D7.91.D7.AA.D7.A0.D7.90.D7.99_.D7.94.D7.A2.D7.A6.D7.99.D7.A8.D7.94"></span>סכנות בתנאי העצירה</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA&amp;action=edit&amp;section=7" title="עריכת פסקה: &quot;סכנות בתנאי העצירה&quot;"><span>עריכה</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>כשכותבים לולאות, יש לוודא שתנאי העצירה אכן יתקיים בהכרח בוודאות - המהדר לא יעשה זאת בשבילנו. נתבונן, לדוגמה, בקטע הקוד הבא: </p> <div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span></span><span class="n">i</span> <span class="o">=</span> <span class="mi">2</span> <span class="k">while</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">20</span> <span class="ow">or</span> <span class="n">i</span> <span class="o">&gt;</span> <span class="mi">20</span><span class="p">:</span> <span class="nb">print</span> <span class="n">i</span> </pre></div> <p>לולאה זו לא תעצר לעולם, שכן התנאי להמשך הלולאה תמיד יתקיים. כאשר קטע קוד זה יופעל, התוכנית תראה כאילו ש"קפאה". </p> <div class="mw-heading mw-heading2"><h2 id="דוגמה_לשילוב_פלט/קלט,_תנאים,_ולולאות"><span id=".D7.93.D7.95.D7.92.D7.9E.D7.94_.D7.9C.D7.A9.D7.99.D7.9C.D7.95.D7.91_.D7.A4.D7.9C.D7.98.2F.D7.A7.D7.9C.D7.98.2C_.D7.AA.D7.A0.D7.90.D7.99.D7.9D.2C_.D7.95.D7.9C.D7.95.D7.9C.D7.90.D7.95.D7.AA"></span>דוגמה לשילוב פלט/קלט, תנאים, ולולאות</h2><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA&amp;action=edit&amp;section=8" title="עריכת פסקה: &quot;דוגמה לשילוב פלט/קלט, תנאים, ולולאות&quot;"><span>עריכה</span></a><span class="mw-editsection-bracket">]</span></span></div> <p>נסיים בתוכנית קטנה המדגימה את השימוש בתנאים ולולאות. התוכנית קולטת מהמשתמש שני מספרים, ומדפיסה הודעה האומרת מה היחס ביניהם. אחר כך היא שואלת את המשתמש אם הוא רוצה להמשיך ולתת שני מספרים נוספים, וכך הלאה, עד שהמשתמש בוחר להפסיק. </p> <div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span></span><span class="n">asked_to_quit</span> <span class="o">=</span> <span class="kc">False</span> <span class="k">while</span> <span class="ow">not</span> <span class="n">asked_to_quit</span><span class="p">:</span> <span class="nb">print</span> <span class="s1">&#39;Please enter two numbers with a space between them:&#39;</span> <span class="n">a</span> <span class="o">=</span> <span class="n">raw_input</span><span class="p">()</span> <span class="n">b</span> <span class="o">=</span> <span class="n">raw_input</span><span class="p">()</span> <span class="k">if</span> <span class="n">a</span> <span class="o">&gt;</span> <span class="n">b</span><span class="p">:</span> <span class="nb">print</span> <span class="nb">str</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; is bigger than &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="k">elif</span> <span class="n">a</span> <span class="o">&lt;</span> <span class="n">b</span><span class="p">:</span> <span class="nb">print</span> <span class="nb">str</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; is smaller than &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="nb">print</span> <span class="s1">&#39;the numbers are equal&#39;</span> <span class="nb">print</span> <span class="s1">&#39;Please enter 1 to repeat, any other number to quit.&#39;</span> <span class="n">response</span><span class="o">=</span><span class="nb">int</span><span class="p">(</span><span class="n">raw_input</span><span class="p">())</span> <span class="n">asked_to_quit</span> <span class="o">=</span> <span class="n">response</span> <span class="o">!=</span> <span class="mi">1</span> </pre></div> <p>להלן הסבר לתוכנית. </p><p>נתבונן ראשית במבנה של הקוד בתוך main. הקוד הוא למעשה כמעט כולו לולאת while: </p> <div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span></span><span class="n">asked_to_quit</span> <span class="o">=</span> <span class="kc">False</span> <span class="k">while</span> <span class="ow">not</span> <span class="n">asked_to_quit</span><span class="p">:</span> <span class="o">...</span> </pre></div> <p>כלומר, עושים פעולה כלשהי כל עוד ערך asked_to_quit הוא False. מתי נקבע ערכו של asked_to_quit? בתוך הלולאה, נוכל לראות את רצף השורות הבאות: </p> <div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span></span> <span class="nb">print</span> <span class="s1">&#39;Please enter 1 to repeat, any other number to quit.&#39;</span> <span class="n">response</span> <span class="o">=</span> <span class="n">raw_input</span><span class="p">()</span> <span class="n">asked_to_quit</span> <span class="o">=</span> <span class="n">respons</span> <span class="o">==</span> <span class="mi">1</span> </pre></div> <p>השורות מבקשות מהמשתמש להכניס ערך (הקובע האם להמשיך בתוכנית), וקולטות את הערך למשתנה response. אם ערך זה הוא 1, אז קובעים את ערכו של asked_to_quit ל-True. </p><p>חוץ מכך, הלולאה מתחילה בשורות: </p> <div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span></span> <span class="nb">print</span> <span class="s1">&#39;Please enter two numbers with a space between them:&#39;</span> <span class="n">a</span> <span class="o">=</span> <span class="n">raw_input</span><span class="p">()</span> <span class="n">b</span> <span class="o">=</span> <span class="n">raw_input</span><span class="p">()</span> </pre></div> <p>המבקשות מהמשתמש להכניס שני ערכים. לאחר שהוכנסו שני הערכים, השורות הבאות מדפיסות את היחס ביניהם: </p> <div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span></span> <span class="k">if</span> <span class="n">a</span> <span class="o">&gt;</span> <span class="n">b</span><span class="p">:</span> <span class="nb">print</span> <span class="nb">str</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; is bigger than &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span> <span class="n">a</span> <span class="o">&lt;</span> <span class="n">b</span> <span class="p">)</span> <span class="nb">print</span> <span class="nb">str</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39; is bigger than &#39;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="k">else</span> <span class="nb">print</span> <span class="s1">&#39;the numbers are equal&#39;</span> </pre></div> <table class="toccolours" id="colophon" frame="hsides" style="clear: both; margin-left: auto; margin-right: auto; text-align: center; width: 75%;"> <tbody><tr><td style="width: 30%;">- </td> <td style="width: 40%;"><b>לולאות</b></td> <td style="width: 30%;">-</td> </tr></tbody></table> <!-- NewPP limit report Parsed by mw‐web.eqiad.main‐7c55db6c78‐jwgwr Cached time: 20241027053658 Cache expiry: 2592000 Reduced expiry: false Complications: [show‐toc] CPU time usage: 0.142 seconds Real time usage: 2.562 seconds Preprocessor visited node count: 1265/1000000 Post‐expand include size: 48935/2097152 bytes Template argument size: 8385/2097152 bytes Highest expansion depth: 32/100 Expensive parser function count: 48/500 Unstrip recursion depth: 0/20 Unstrip post‐expand size: 21990/5000000 bytes Lua time usage: 0.004/10.000 seconds Lua memory usage: 625406/52428800 bytes Number of Wikibase entities loaded: 0/400 --> <!-- Transclusion expansion time report (%,ms,calls,template) 100.00% 2512.314 1 -total 18.16% 456.257 4 תבנית:מוסתר 3.06% 76.757 2 תבנית:פייתון/פייתון_גרסה_2 1.75% 43.935 1 תבנית:פרקים 1.16% 29.186 1 תבנית:לאחד 0.80% 20.132 1 תבנית:מבנה_תבנית_עבודה 0.52% 13.025 1 תבנית:פרקים_מוגבל 0.50% 12.612 4 תבנית:משימה 0.37% 9.355 4 תבנית:בלון 0.30% 7.538 25 תבנית:פרקים/פרק --> <!-- Saved in parser cache with key hewikibooks:pcache:idhash:11589-0!canonical and timestamp 20241027053658 and revision id 148886. Rendering was triggered because: page-view --> </div><!--esi <esi:include src="/esitest-fa8a495983347898/content" /> --><noscript><img src="https://login.wikimedia.org/wiki/Special:CentralAutoLogin/start?type=1x1" alt="" width="1" height="1" style="border: none; position: absolute;"></noscript> <div class="printfooter" data-nosnippet="">אוחזר מתוך "<a dir="ltr" href="https://he.wikibooks.org/w/index.php?title=פייתון/פייתון_גרסה_2/לולאות&amp;oldid=148886">https://he.wikibooks.org/w/index.php?title=פייתון/פייתון_גרסה_2/לולאות&amp;oldid=148886</a>"</div></div> <div id="catlinks" class="catlinks" data-mw="interface"><div id="mw-normal-catlinks" class="mw-normal-catlinks"><a href="/wiki/%D7%9E%D7%99%D7%95%D7%97%D7%93:%D7%A7%D7%98%D7%92%D7%95%D7%A8%D7%99%D7%95%D7%AA" title="מיוחד:קטגוריות">קטגוריה</a>: <ul><li><a href="/wiki/%D7%A7%D7%98%D7%92%D7%95%D7%A8%D7%99%D7%94:%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2" title="קטגוריה:פייתון גרסה 2">פייתון גרסה 2</a></li></ul></div><div id="mw-hidden-catlinks" class="mw-hidden-catlinks mw-hidden-cats-hidden">קטגוריות מוסתרות: <ul><li><a href="/wiki/%D7%A7%D7%98%D7%92%D7%95%D7%A8%D7%99%D7%94:%D7%93%D7%A4%D7%99%D7%9D_%D7%A9%D7%9E%D7%A9%D7%AA%D7%9E%D7%A9%D7%99%D7%9D_%D7%91%D7%AA%D7%92_source" title="קטגוריה:דפים שמשתמשים בתג source">דפים שמשתמשים בתג source</a></li><li><a href="/wiki/%D7%A7%D7%98%D7%92%D7%95%D7%A8%D7%99%D7%94:%D7%95%D7%99%D7%A7%D7%99%D7%A1%D7%A4%D7%A8:_%D7%93%D7%A4%D7%99%D7%9D_%D7%9C%D7%90%D7%99%D7%97%D7%95%D7%93" title="קטגוריה:ויקיספר: דפים לאיחוד">ויקיספר: דפים לאיחוד</a></li></ul></div></div> </div> </main> </div> <div class="mw-footer-container"> <footer id="footer" class="mw-footer" > <ul id="footer-info"> <li id="footer-info-lastmod"> דף זה נערך לאחרונה ב־7 באוקטובר 2017, בשעה 12:22.</li> <li id="footer-info-copyright">הטקסט מוגש בכפוף לרישיון <a rel="nofollow" class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.he">Creative Commons ייחוס-שיתוף זהה 4.0</a>; ייתכן שישנם תנאים נוספים. ר׳ את <a class="external text" href="https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Terms_of_Use">תנאי השימוש</a> לפרטים.</li> </ul> <ul id="footer-places"> <li id="footer-places-privacy"><a href="https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Privacy_policy">מדיניות פרטיות</a></li> <li id="footer-places-about"><a href="/wiki/%D7%95%D7%99%D7%A7%D7%99%D7%A1%D7%A4%D7%A8:%D7%90%D7%95%D7%93%D7%95%D7%AA">אודות ויקיספר</a></li> <li id="footer-places-disclaimers"><a href="/wiki/%D7%95%D7%99%D7%A7%D7%99%D7%A1%D7%A4%D7%A8:%D7%94%D7%91%D7%94%D7%A8%D7%95%D7%AA_%D7%9E%D7%A9%D7%A4%D7%98%D7%99%D7%95%D7%AA">הבהרות משפטיות</a></li> <li id="footer-places-wm-codeofconduct"><a href="https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Universal_Code_of_Conduct">קוד התנהגות</a></li> <li id="footer-places-developers"><a href="https://developer.wikimedia.org">מפתחים</a></li> <li id="footer-places-statslink"><a href="https://stats.wikimedia.org/#/he.wikibooks.org">סטטיסטיקות</a></li> <li id="footer-places-cookiestatement"><a href="https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Cookie_statement">הצהרה על עוגיות</a></li> <li id="footer-places-mobileview"><a href="//he.m.wikibooks.org/w/index.php?title=%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F/%D7%A4%D7%99%D7%99%D7%AA%D7%95%D7%9F_%D7%92%D7%A8%D7%A1%D7%94_2/%D7%9C%D7%95%D7%9C%D7%90%D7%95%D7%AA&amp;mobileaction=toggle_view_mobile" class="noprint stopMobileRedirectToggle">תצוגת מכשירים ניידים</a></li> </ul> <ul id="footer-icons" class="noprint"> <li id="footer-copyrightico"><a href="https://wikimediafoundation.org/" class="cdx-button cdx-button--fake-button cdx-button--size-large cdx-button--fake-button--enabled"><img src="/static/images/footer/wikimedia-button.svg" width="84" height="29" alt="Wikimedia Foundation" loading="lazy"></a></li> <li id="footer-poweredbyico"><a href="https://www.mediawiki.org/" class="cdx-button cdx-button--fake-button cdx-button--size-large cdx-button--fake-button--enabled"><img src="/w/resources/assets/poweredby_mediawiki.svg" alt="Powered by MediaWiki" width="88" height="31" loading="lazy"></a></li> </ul> </footer> </div> </div> </div> <div class="vector-settings" id="p-dock-bottom"> <ul></ul> </div><script>(RLQ=window.RLQ||[]).push(function(){mw.config.set({"wgHostname":"mw-web.codfw.main-f69cdc8f6-rlhbk","wgBackendResponseTime":137,"wgPageParseReport":{"limitreport":{"cputime":"0.142","walltime":"2.562","ppvisitednodes":{"value":1265,"limit":1000000},"postexpandincludesize":{"value":48935,"limit":2097152},"templateargumentsize":{"value":8385,"limit":2097152},"expansiondepth":{"value":32,"limit":100},"expensivefunctioncount":{"value":48,"limit":500},"unstrip-depth":{"value":0,"limit":20},"unstrip-size":{"value":21990,"limit":5000000},"entityaccesscount":{"value":0,"limit":400},"timingprofile":["100.00% 2512.314 1 -total"," 18.16% 456.257 4 תבנית:מוסתר"," 3.06% 76.757 2 תבנית:פייתון/פייתון_גרסה_2"," 1.75% 43.935 1 תבנית:פרקים"," 1.16% 29.186 1 תבנית:לאחד"," 0.80% 20.132 1 תבנית:מבנה_תבנית_עבודה"," 0.52% 13.025 1 תבנית:פרקים_מוגבל"," 0.50% 12.612 4 תבנית:משימה"," 0.37% 9.355 4 תבנית:בלון"," 0.30% 7.538 25 תבנית:פרקים/פרק"]},"scribunto":{"limitreport-timeusage":{"value":"0.004","limit":"10.000"},"limitreport-memusage":{"value":625406,"limit":52428800}},"cachereport":{"origin":"mw-web.eqiad.main-7c55db6c78-jwgwr","timestamp":"20241027053658","ttl":2592000,"transientcontent":false}}});});</script> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10