Book2 3 of 5
Book2 3 of 5
Book2 3 of 5
doctype html>
<html
lang="en"
dir="ltr">
<head>
<meta name="google-signin-client-id" content="721724668570-
nbkv1cfusk7kk4eni4pjvepaus73b13t.apps.googleusercontent.com">
<meta name="google-signin-scope"
content="profile email https://www.googleapis.com/auth/developerprofiles
https://www.googleapis.com/auth/developerprofiles.award">
<meta property="og:site_name" content="Google Developers">
<meta property="og:type" content="website"><meta name="theme-color"
content="#12b5cb"><meta charset="utf-8">
<meta content="IE=Edge" http-equiv="X-UA-Compatible">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="manifest" href="/_pwa/developers/manifest.json"
crossorigin="use-credentials">
<link rel="preconnect" href="//www.gstatic.com" crossorigin>
<link rel="preconnect" href="//fonts.gstatic.com" crossorigin>
<link rel="preconnect" href="//fonts.googleapis.com" crossorigin>
<link rel="preconnect" href="//apis.google.com" crossorigin>
<link rel="preconnect" href="//www.google-analytics.com" crossorigin><link
rel="stylesheet" href="//fonts.googleapis.com/css?family=Google+Sans:400,500|
Roboto:400,400italic,500,500italic,700,700italic|Roboto+Mono:400,500,700|
Material+Icons&display=swap"><link rel="stylesheet"
href="https://www.gstatic.com/devrel-
devsite/prod/v1674d466be3b1154327dd11cf186e748303b1e92ae31ff35df0f5192fbd777ea/deve
lopers/css/app.css"><link rel="shortcut icon" href="https://www.gstatic.com/devrel-
devsite/prod/v1674d466be3b1154327dd11cf186e748303b1e92ae31ff35df0f5192fbd777ea/deve
lopers/images/favicon.png">
<link rel="apple-touch-icon" href="https://www.gstatic.com/devrel-
devsite/prod/v1674d466be3b1154327dd11cf186e748303b1e92ae31ff35df0f5192fbd777ea/deve
lopers/images/touchicon-180.png"><link rel="canonical"
href="https://developers.google.com/edu/python/introduction"><link rel="search"
type="application/opensearchdescription+xml"
title="Google Developers"
href="https://developers.google.com/s/opensearch.xml">
</head>
<body class=""
template="page"
theme="cyan"
type="article"
layout="docs"
pending>
<devsite-progress type="indeterminate" id="app-progress"></devsite-progress>
<section class="devsite-wrapper"><devsite-header>
<div class="devsite-header--inner nocontent">
<div class="devsite-top-logo-row-wrapper-wrapper">
<div class="devsite-top-logo-row-wrapper">
<div class="devsite-top-logo-row">
<button type="button" id="devsite-hamburger-menu"
class="devsite-header-icon-button button-flat material-icons gc-
analytics-event"
data-category="Site-Wide Custom Events"
data-label="Navigation menu button"
visually-hidden
aria-label="Open menu">
</button>
<div class="devsite-product-name-wrapper">
<span class="devsite-product-name">
<ul class="devsite-breadcrumb-list"
aria-label="Product breadcrumb">
<li class="devsite-breadcrumb-item
devsite-has-google-wordmark">
<a href="https://developers.google.com/edu"
class="devsite-breadcrumb-link gc-analytics-event"
data-label="Upper Header"
data-value="1"
track-type="globalNav"
track-name="breadcrumb"
track-metadata-position="1"
track-metadata-eventdetail="Google for Education"
>
<svg class="devsite-google-wordmark"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 148 48">
<title>Google</title>
<path class="devsite-google-wordmark-svg-path" d="M19.58,37.65c-9.87,0-18.17-
8.04-18.17-17.91c0-9.87,8.3-17.91,18.17-17.91c5.46,0,9.35,2.14,12.27,4.94l-
3.45,3.45c-2.1-1.97-4.93-3.49-8.82-3.49c-7.21,0-12.84,5.81-
12.84,13.02c0,7.21,5.64,13.02,12.84,13.02c4.67,0,7.34-1.88,9.04-3.58c1.4-1.4,2.32-
3.41,2.66-6.16H19.58v-4.89h16.47c0.18,0.87,0.26,1.92,0.26,3.06c0,3.67-1.01,8.21-
4.24,11.44C28.93,35.9,24.91,37.65,19.58,37.65z M61.78,26.12c0,6.64-5.1,11.53-
11.36,11.53s-11.36-4.89-11.36-11.53c0-6.68,5.1-11.53,11.36-
11.53S61.78,19.43,61.78,26.12z M56.8,26.12c0-4.15-2.96-6.99-6.39-6.99c-3.43,0-
6.39,2.84-6.39,6.99c0,4.11,2.96,6.99,6.39,6.99C53.84,33.11,56.8,30.22,56.8,26.12z
M87.25,26.12c0,6.64-5.1,11.53-11.36,11.53c-6.26,0-11.36-4.89-11.36-11.53c0-
6.68,5.1-11.53,11.36-11.53C82.15,14.59,87.25,19.43,87.25,26.12zM82.28,26.12c0-4.15-
2.96-6.99-6.39-6.99c-3.43,0-6.39,2.84-
6.39,6.99c0,4.11,2.96,6.99,6.39,6.99C79.32,33.11,82.28,30.22,82.28,26.12z
M112.09,15.29v20.7c0,8.52-5.02,12.01-10.96,12.01c-5.59,0-8.95-3.76-10.22-6.81l4.41-
1.83c0.79,1.88,2.71,4.1,5.81,4.1c3.8,0,6.16-2.36,6.16-6.77v-1.66h-0.18c-1.14,1.4-
3.32,2.62-6.07,2.62c-5.76,0-11.05-5.02-11.05-11.49c0-6.51,5.28-11.57,11.05-
11.57c2.75,0,4.93,1.22,6.07,2.58h0.18v-1.88H112.09z M107.64,26.16c0-4.06-2.71-7.03-
6.16-7.03c-3.49,0-6.42,2.97-
6.42,7.03c0,4.02,2.93,6.94,6.42,6.94C104.93,33.11,107.64,30.18,107.64,26.16z
M120.97,3.06v33.89h-5.07V3.06H120.97z M140.89,29.92l3.93,2.62c-1.27,1.88-4.32,5.11-
9.61,5.11c-6.55,0-11.28-5.07-11.28-11.53c0-6.86,4.77-11.53,10.71-
11.53c5.98,0,8.91,4.76,9.87,7.34l0.52,1.31l-
15.42,6.38c1.18,2.31,3.01,3.49,5.59,3.49C137.79,33.11,139.58,31.84,140.89,29.92zM12
8.79,25.77l10.31-4.28c-0.57-1.44-2.27-2.45-4.28-
2.45C132.24,19.04,128.66,21.31,128.79,25.77z"/>
</svg>for Education
</a>
</li>
<li class="devsite-breadcrumb-item
">
<a href="https://developers.google.com/edu/python"
class="devsite-breadcrumb-link gc-analytics-event"
data-label="Upper Header"
data-value="2"
track-type="globalNav"
track-name="breadcrumb"
track-metadata-position="2"
>
Python
</a>
</li>
</ul>
</span>
</div>
<div class="devsite-top-logo-row-middle">
<div class="devsite-header-upper-tabs">
</div>
<devsite-search
aria-expanded="false"
aria-haspopup="listbox"
enable-signin
enable-search
enable-suggestions
enable-query-completion
project-name="Python Education"
tenant-name="Google Developers"
project-scope="/edu/python"
url-scoped="https://developers.google.com/s/results/edu/python"
role="combobox">
<form class="devsite-search-form"
action="https://developers.google.com/s/results" method="GET">
<div class="devsite-search-container">
<div class="devsite-searchbox">
<input
aria-activedescendant=""
aria-autocomplete="list"
aria-label="Search"
aria-haspopup="false"
aria-multiline="false"
aria-label="Search box"
autocomplete="off"
class="devsite-search-field devsite-search-query"
name="q"
placeholder="Search"
role="combobox"
type="text"
value=""
>
<div class="devsite-search-image material-icons" aria-hidden="true"></div>
</div>
<button type="button"
search-open
class="devsite-search-button devsite-header-icon-button button-flat
material-icons"
aria-label="Open search"></button>
</div>
</form>
<button type="button"
search-close
class="devsite-search-button devsite-header-icon-button button-flat
material-icons"
aria-label="Close search"></button>
</devsite-search>
</div>
<devsite-language-selector>
<devsite-select class="devsite-language-selector-menu">
<option>Language</option>
<option value="en"
lang="en"
track-metadata-original-language="en"
track-metadata-selected-language="en"
track-name="changed"
track-type="languageSelector"
>
English
</option>
<option value="id"
lang="id"
track-metadata-original-language="en"
track-metadata-selected-language="id"
track-name="changed"
track-type="languageSelector"
>
Bahasa Indonesia
</option>
<option value="de"
lang="de"
track-metadata-original-language="en"
track-metadata-selected-language="de"
track-name="changed"
track-type="languageSelector"
>
Deutsch
</option>
<option value="es"
lang="es"
track-metadata-original-language="en"
track-metadata-selected-language="es"
track-name="changed"
track-type="languageSelector"
>
Español
</option>
<option value="fr"
lang="fr"
track-metadata-original-language="en"
track-metadata-selected-language="fr"
track-name="changed"
track-type="languageSelector"
>
Français
</option>
<option value="pt_br"
lang="pt_br"
track-metadata-original-language="en"
track-metadata-selected-language="pt_br"
track-name="changed"
track-type="languageSelector"
>
Português – Brasil
</option>
<option value="ru"
lang="ru"
track-metadata-original-language="en"
track-metadata-selected-language="ru"
track-name="changed"
track-type="languageSelector"
>
Русский
</option>
<option value="zh_cn"
lang="zh_cn"
track-metadata-original-language="en"
track-metadata-selected-language="zh_cn"
track-name="changed"
track-type="languageSelector"
>
中文 – 简体
</option>
<option value="ja"
lang="ja"
track-metadata-original-language="en"
track-metadata-selected-language="ja"
track-name="changed"
track-type="languageSelector"
>
日本語
</option>
<option value="ko"
lang="ko"
track-metadata-original-language="en"
track-metadata-selected-language="ko"
track-name="changed"
track-type="languageSelector"
>
한국어
</option>
</select>
</devsite-select>
</devsite-language-selector>
<devsite-user signed-in
enable-profiles
fp-auth
id="devsite-user"></devsite-user>
</div>
</div>
</div>
<div class="devsite-collapsible-section
devsite-header-no-lower-tabs
">
<div class="devsite-header-background">
</div>
</div>
</div>
</devsite-header>
<devsite-book-nav scrollbars >
<ul class="devsite-breadcrumb-list"
<li class="devsite-breadcrumb-item
devsite-has-google-wordmark">
<a href="https://developers.google.com/edu"
class="devsite-breadcrumb-link gc-analytics-event"
data-label="Upper Header"
data-value="1"
track-type="globalNav"
track-name="breadcrumb"
track-metadata-position="1"
>
<svg class="devsite-google-wordmark"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 148 48">
<title>Google</title>
<path class="devsite-google-wordmark-svg-path" d="M19.58,37.65c-9.87,0-18.17-
8.04-18.17-17.91c0-9.87,8.3-17.91,18.17-17.91c5.46,0,9.35,2.14,12.27,4.94l-
3.45,3.45c-2.1-1.97-4.93-3.49-8.82-3.49c-7.21,0-12.84,5.81-
12.84,13.02c0,7.21,5.64,13.02,12.84,13.02c4.67,0,7.34-1.88,9.04-3.58c1.4-1.4,2.32-
3.41,2.66-6.16H19.58v-4.89h16.47c0.18,0.87,0.26,1.92,0.26,3.06c0,3.67-1.01,8.21-
4.24,11.44C28.93,35.9,24.91,37.65,19.58,37.65z M61.78,26.12c0,6.64-5.1,11.53-
11.36,11.53s-11.36-4.89-11.36-11.53c0-6.68,5.1-11.53,11.36-
11.53S61.78,19.43,61.78,26.12z M56.8,26.12c0-4.15-2.96-6.99-6.39-6.99c-3.43,0-
6.39,2.84-6.39,6.99c0,4.11,2.96,6.99,6.39,6.99C53.84,33.11,56.8,30.22,56.8,26.12z
M87.25,26.12c0,6.64-5.1,11.53-11.36,11.53c-6.26,0-11.36-4.89-11.36-11.53c0-
6.68,5.1-11.53,11.36-11.53C82.15,14.59,87.25,19.43,87.25,26.12zM82.28,26.12c0-4.15-
2.96-6.99-6.39-6.99c-3.43,0-6.39,2.84-
6.39,6.99c0,4.11,2.96,6.99,6.39,6.99C79.32,33.11,82.28,30.22,82.28,26.12z
M112.09,15.29v20.7c0,8.52-5.02,12.01-10.96,12.01c-5.59,0-8.95-3.76-10.22-6.81l4.41-
1.83c0.79,1.88,2.71,4.1,5.81,4.1c3.8,0,6.16-2.36,6.16-6.77v-1.66h-0.18c-1.14,1.4-
3.32,2.62-6.07,2.62c-5.76,0-11.05-5.02-11.05-11.49c0-6.51,5.28-11.57,11.05-
11.57c2.75,0,4.93,1.22,6.07,2.58h0.18v-1.88H112.09z M107.64,26.16c0-4.06-2.71-7.03-
6.16-7.03c-3.49,0-6.42,2.97-
6.42,7.03c0,4.02,2.93,6.94,6.42,6.94C104.93,33.11,107.64,30.18,107.64,26.16z
M120.97,3.06v33.89h-5.07V3.06H120.97z M140.89,29.92l3.93,2.62c-1.27,1.88-4.32,5.11-
9.61,5.11c-6.55,0-11.28-5.07-11.28-11.53c0-6.86,4.77-11.53,10.71-
11.53c5.98,0,8.91,4.76,9.87,7.34l0.52,1.31l-
15.42,6.38c1.18,2.31,3.01,3.49,5.59,3.49C137.79,33.11,139.58,31.84,140.89,29.92zM12
8.79,25.77l10.31-4.28c-0.57-1.44-2.27-2.45-4.28-
2.45C132.24,19.04,128.66,21.31,128.79,25.77z"/>
</svg>for Education
</a>
</li>
<li class="devsite-breadcrumb-item
">
<a href="https://developers.google.com/edu/python"
class="devsite-breadcrumb-link gc-analytics-event"
data-label="Upper Header"
data-value="2"
track-type="globalNav"
track-name="breadcrumb"
track-metadata-position="2"
>
Python
</a>
</li>
</ul>
</span>
</div>
</div>
<div class="devsite-book-nav-wrapper">
<div class="devsite-mobile-nav-top">
<ul class="devsite-nav-list">
<li class="devsite-nav-item">
<a href="/edu/python"
class="devsite-nav-title gc-analytics-event
devsite-nav-has-children
devsite-nav-active"
</a>
</li>
</ul>
</div>
<li class="devsite-nav-item
devsite-nav-heading"><div class="devsite-nav-title devsite-nav-title-no-
path"><span class="devsite-nav-text" tooltip>Lecture Videos</span></div><ul
class="devsite-nav-section"><li class="devsite-nav-item
devsite-nav-external"><a href="http://www.youtube.com/watch?
v=tKTZoB2Vjuk"
class="devsite-nav-title"
><span class="devsite-nav-text" tooltip>1.1 Introduction, strings</span><span
class="devsite-nav-icon material-icons"
data-icon="external"
data-title="External"
aria-hidden="true"></span></a></li><li class="devsite-nav-item
devsite-nav-external"><a href="http://www.youtube.com/watch?
v=EPYupizJYQI"
class="devsite-nav-title"
><span class="devsite-nav-text" tooltip>1.2 Lists and sorting</span><span
class="devsite-nav-icon material-icons"
data-icon="external"
data-title="External"
aria-hidden="true"></span></a></li><li class="devsite-nav-item
devsite-nav-external"><a href="http://www.youtube.com/watch?
v=haycL41dAhg"
class="devsite-nav-title"
><span class="devsite-nav-text" tooltip>1.3 Dicts and files</span><span
class="devsite-nav-icon material-icons"
data-icon="external"
data-title="External"
aria-hidden="true"></span></a></li><li class="devsite-nav-item
devsite-nav-external"><a href="http://www.youtube.com/watch?
v=kWyoYtvJpe4"
class="devsite-nav-title"
><span class="devsite-nav-text" tooltip>2.1 Regular expr</span><span
class="devsite-nav-icon material-icons"
data-icon="external"
data-title="External"
aria-hidden="true"></span></a></li><li class="devsite-nav-item
devsite-nav-external"><a href="http://www.youtube.com/watch?
v=uKZ8GBKmeDM"
class="devsite-nav-title"
><span class="devsite-nav-text" tooltip>2.2 Utilities</span><span
class="devsite-nav-icon material-icons"
data-icon="external"
data-title="External"
aria-hidden="true"></span></a></li><li class="devsite-nav-item
devsite-nav-external"><a href="http://www.youtube.com/watch?
v=Nn2KQmVF5Og"
class="devsite-nav-title"
><span class="devsite-nav-text" tooltip>2.3 Utilities urllib</span><span
class="devsite-nav-icon material-icons"
data-icon="external"
data-title="External"
aria-hidden="true"></span></a></li><li class="devsite-nav-item
devsite-nav-external"><a href="http://www.youtube.com/watch?
v=IcteAbMC1Ok"
class="devsite-nav-title"
><span class="devsite-nav-text" tooltip>2.4 Conclusions</span><span
class="devsite-nav-icon material-icons"
data-icon="external"
data-title="External"
aria-hidden="true"></span></a></li></ul></li>
<li class="devsite-nav-item
devsite-nav-heading"><div class="devsite-nav-title devsite-nav-title-no-
path"><span class="devsite-nav-text" tooltip>Python Exercises</span></div><ul
class="devsite-nav-section"><li class="devsite-nav-item"><a
href="/edu/python/exercises/basic"
class="devsite-nav-title"
><span class="devsite-nav-text" tooltip>Basic Exercises</span></a></li><li
class="devsite-nav-item"><a href="/edu/python/exercises/baby-names"
class="devsite-nav-title"
><span class="devsite-nav-text" tooltip>Baby Names Exercise</span></a></li><li
class="devsite-nav-item"><a href="/edu/python/exercises/copy-special"
class="devsite-nav-title"
><span class="devsite-nav-text" tooltip>Copy Special Exercise</span></a></li><li
class="devsite-nav-item"><a href="/edu/python/exercises/log-puzzle"
class="devsite-nav-title"
><span class="devsite-nav-text" tooltip>Log Puzzle
Exercise</span></a></li></ul></li>
</ul>
</div>
</div>
</nav>
</devsite-book-nav>
<section id="gc-wrapper">
<main role="main" class="devsite-main-content"
has-book-nav
has-toc>
<devsite-toc class="devsite-nav"
></devsite-toc>
<devsite-content>
<article class="devsite-article">
<ul class="devsite-breadcrumb-list"
aria-label="Breadcrumb">
<li class="devsite-breadcrumb-item
">
<a href="https://developers.google.com/"
class="devsite-breadcrumb-link gc-analytics-event"
data-label="Breadcrumbs"
data-value="1"
track-type="globalNav"
track-name="breadcrumb"
track-metadata-position="1"
track-metadata-eventdetail=""
>
Home
</a>
</li>
<li class="devsite-breadcrumb-item
">
<a href="https://developers.google.com/products"
class="devsite-breadcrumb-link gc-analytics-event"
data-label="Breadcrumbs"
data-value="2"
track-type="globalNav"
track-name="breadcrumb"
track-metadata-position="2"
track-metadata-eventdetail=""
>
Products
</a>
</li>
<li class="devsite-breadcrumb-item
">
<a href="https://developers.google.com/edu"
class="devsite-breadcrumb-link gc-analytics-event"
data-label="Breadcrumbs"
data-value="3"
track-type="globalNav"
track-name="breadcrumb"
track-metadata-position="3"
>
</a>
</li>
<li class="devsite-breadcrumb-item
">
<a href="https://developers.google.com/edu/python"
class="devsite-breadcrumb-link gc-analytics-event"
data-label="Breadcrumbs"
data-value="4"
track-type="globalNav"
track-name="breadcrumb"
track-metadata-position="4"
>
Python
</a>
</li>
</ul>
<devsite-thumb-rating position="header">
<template class="thumb-down-categories">
[{
"type": "thumb-down",
"id": "missingTheInformationINeed",
"label":"Missing the information I need"
},{
"type": "thumb-down",
"id": "tooComplicatedTooManySteps",
"label":"Too complicated / too many steps"
},{
"type": "thumb-down",
"id": "outOfDate",
"label":"Out of date"
},{
"type": "thumb-down",
"id": "samplesCodeIssue",
"label":"Samples/Code issue"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"Other"
}]
</template>
<template class="thumb-up-categories">
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"Easy to understand"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"Solved my problem"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"Other"
}]
</template>
</devsite-thumb-rating>
</div>
<p>We strongly recommend you follow along with the companion videos throughout the
course, starting with <a href="http://youtu.be/tKTZoB2Vjuk">the first one</a>. If
you're seeking a companion MOOC course, try the ones from <a
href="http://udacity.com/course/cs101">Udacity</a> and Coursera (<a
href="http://coursera.org/course/programming1">intro to programming</a> [beginners]
or <a href="http://coursera.org/course/interactivepython">intro to Python</a>), and
if you're looking for a companion book to your learning, regardless of your Python
skill level, check out <a href="http://goo.gl/i4u0R">these reading lists</a>.
Finally, if you're seeking self-paced online learning <i>without</i> watching
videos, try the ones listed towards the end of <a
href="http://stackoverflow.com/a/3226704/305689">this post</a> — each feature
learning content as well as a Python interactive interpreter you can practice with.
What's this "interpreter" we mention? You'll find out in the next section!</p>
<p>An excellent way to see how Python code works is to run the Python interpreter
and type code right into it. If you ever have a question like, "What happens if I
add an <code translate="no" dir="ltr">int</code> to a <code translate="no"
dir="ltr">list</code>?" Just typing it into the Python interpreter is a fast and
likely the best way to see what happens. (See below to see what really
happens!)</p>
<p>As you can see above, it's easy to experiment with variables and operators.
Also, the interpreter throws, or "raises" in Python parlance, a runtime error if
the code tries to read a variable that has not been assigned a value. Like C++ and
Java, Python is case sensitive so "a" and "A" are different variables. The end of a
line marks the end of a statement, so unlike C++ and Java, Python does not require
a semicolon at the end of each statement. Comments begin with a '#' and extend to
the end of the line.</p>
<p>Python source files use the ".py" extension and are called "modules." With a
Python module <code translate="no" dir="ltr">hello.py</code>, the easiest way to
run it is with the shell command "python hello.py Alice" which calls the Python
interpreter to execute the code in <code translate="no" dir="ltr">hello.py</code>,
passing it the command line argument "Alice".
See the <a href="http://docs.python.org/using/cmdline">official docs page</a> on
all the different options you have when running Python from the command-line.
</p>
<p>Notice also how the lines that make up the function or if-statement are grouped
by all having the same level of indentation. We also presented 2 different ways to
repeat strings, using the + operator which is more user-friendly, but * also works
because it's Python's "repeat" operator, meaning that <code translate="no"
dir="ltr">'-' * 10</code> gives <code translate="no" dir="ltr">'----------'</code>,
a neat way to create an onscreen "line." In the code comment, we hinted that *
works faster than +, the reason being that * calculates the size of the resulting
object once whereas with +, that calculation is made each time + is called. Both +
and * are called "overloaded" operators because they mean different things for
numbers vs. for strings (and other data types).</p>
<p>Here is code that calls the above repeat() function, printing what it
returns:</p>
<p>At run time, functions must be defined by the execution of a "def" before they
are called. It's typical to def a main() function towards the bottom of the file
with the functions it calls above it.</p>
<p>One unusual Python feature is that the whitespace indentation of a piece of code
affects its meaning. A logical block of statements such as the ones that make up a
function should all have the same indentation, set in from the indentation of their
parent function or "if" or whatever. If one of the lines in a group has a different
indentation, it is flagged as a syntax error.</p>
<p>Python's use of whitespace feels a little strange at first, but it's logical and
I found I got used to it very quickly. Avoid using TABs as they greatly complicate
the indentation scheme (not to mention TABs may mean different things on different
platforms). Set your editor to insert spaces instead of TABs for Python code.</p>
<p>A common question beginners ask is, "How many spaces should I indent?" According
to <a href="http://python.org/dev/peps/pep-0008/#indentation">the official Python
style guide (PEP 8)</a>, you should indent with 4 spaces. (Fun fact: Google's
internal style guideline dictates indenting by 2 spaces!)</p>
<p>Python does very little checking at compile time, deferring almost all type,
name, etc. checks on each line until that line runs. Suppose the above main() calls
repeat() like this:</p>
<p>Since Python variables don't have any type spelled out in the source code, it's
extra helpful to give meaningful names to your variables to remind yourself of
what's going on. So use "name" if it's a single name, and "names" if it's a list of
names, and "tuples" if it's a list of tuples. <!-- may link to list/tuple docs here
--> Many basic Python errors result from forgetting what type of value is in each
variable, so use your variable names (all you have really) to help keep things
straight.</p>
<p>As far as actual naming goes, some languages prefer underscored_parts for
variable names made up of "more than one word," but other languages prefer
camelCasing. In general, Python <a href="http://python.org/dev/peps/pep-
0008/#function-names">prefers</a> the underscore method but guides developers to
defer to camelCasing if integrating into existing Python code that already uses
that style. Readability counts. Read more in <a
href="https://www.python.org/dev/peps/pep-0008/#naming-conventions">the section on
naming conventions in PEP 8</a>.</p>
<p>As you can guess, keywords like 'print' and 'while' cannot be used as variable
names — you'll get a syntax error if you do. However, be careful not to use
built-ins as variable names. For example, while 'str' and 'list' may seem like good
names, you'd be overriding those system variables. Built-ins are not keywords and
thus, are susceptible to inadvertent use by new Python developers.</p>
<p>Suppose you've got a module "binky.py" which contains a "def foo()". The fully
qualified name of that foo function is "binky.foo". In this way, various Python
modules can name their functions and variables whatever they want, and the variable
names won't conflict — module1.foo is different from module2.foo. In the
Python vocabulary, we'd say that binky, module1, and module2 each have their own
"namespaces," which as you can guess are variable name-to-object bindings.</p>
<p>For example, we have the standard "sys" module that contains some standard
system facilities, like the argv list, and exit() function. With the statement
"import sys" you can then access the definitions in the sys module and make them
available by their fully-qualified name, e.g. sys.exit(). (Yes, 'sys' has a
namespace too!)</p>
<p>There is another import form that looks like this: "from sys import argv, exit".
That makes argv and exit() available by their short names; however, we recommend
the original form with the fully-qualified names because it's a lot easier to
determine where a function or attribute came from.</p>
<p>There are many modules and packages which are bundled with a standard
installation of the Python interpreter, so you don't have to do anything extra to
use them. These are collectively known as the "Python Standard Library." Commonly
used modules/packages include:</p>
<ul>
<li> sys — access to exit(), argv, stdin, stdout, ...
<p>You can find the documentation of all the Standard Library modules and packages
at <a href="http://docs.python.org/library">http://docs.python.org/library</a>.</p>
<ul>
<li>Do a Google search, starting with the word "python", like "python list" or
"python string lowercase". The first hit is often the answer. This technique seems
to work better for Python than it does for other languages for some reason.</li>
<ul>
<li><code translate="no" dir="ltr">help(len)</code> — help string for the
built-in <code translate="no" dir="ltr">len()</code> function; note that it's "len"
not "len()", which is a <strong>call</strong> to the function, which we don't
want</li>
</div>
<devsite-thumb-rating position="footer">
<template class="thumb-down-categories">
[{
"type": "thumb-down",
"id": "missingTheInformationINeed",
"label":"Missing the information I need"
},{
"type": "thumb-down",
"id": "tooComplicatedTooManySteps",
"label":"Too complicated / too many steps"
},{
"type": "thumb-down",
"id": "outOfDate",
"label":"Out of date"
},{
"type": "thumb-down",
"id": "samplesCodeIssue",
"label":"Samples/Code issue"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"Other"
}]
</template>
<template class="thumb-up-categories">
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"Easy to understand"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"Solved my problem"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"Other"
}]
</template>
</devsite-thumb-rating>
</article>
<devsite-content-footer class="nocontent">
<p>Except as otherwise noted, the content of this page is licensed under the <a
href="https://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution
4.0 License</a>, and code samples are licensed under the <a
href="https://www.apache.org/licenses/LICENSE-2.0">Apache 2.0 License</a>. For
details, see the <a href="https://developers.google.com/site-policies">Google
Developers Site Policies</a>. Java is a registered trademark of Oracle and/or its
affiliates.</p>
<p>Last updated 2018-08-20 UTC.</p>
</devsite-content-footer>
</devsite-content>
</main>
<devsite-footer-promos class="devsite-footer">
</devsite-footer-promos>
<devsite-footer-linkboxes class="devsite-footer">
<ul class="devsite-footer-linkboxes-list">
<li class="devsite-footer-linkbox-item">
<a href="//googledevelopers.blogspot.com"
class="devsite-footer-linkbox-link gc-analytics-event"
data-category="Site-Wide Custom Events"
Blog
</a>
</li>
<li class="devsite-footer-linkbox-item">
<a href="//www.facebook.com/Google-Developers-967415219957038"
class="devsite-footer-linkbox-link gc-analytics-event"
data-category="Site-Wide Custom Events"
</a>
</li>
<li class="devsite-footer-linkbox-item">
<a href="//medium.com/google-developers"
class="devsite-footer-linkbox-link gc-analytics-event"
data-category="Site-Wide Custom Events"
Medium
</a>
</li>
<li class="devsite-footer-linkbox-item">
<a href="//twitter.com/googledevs"
class="devsite-footer-linkbox-link gc-analytics-event"
data-category="Site-Wide Custom Events"
</a>
</li>
<li class="devsite-footer-linkbox-item">
<a href="//www.youtube.com/user/GoogleDevelopers"
class="devsite-footer-linkbox-link gc-analytics-event"
data-category="Site-Wide Custom Events"
YouTube
</a>
</li>
</ul>
</li>
<li class="devsite-footer-linkbox-item">
<a href="//www.womentechmakers.com"
class="devsite-footer-linkbox-link gc-analytics-event"
data-category="Site-Wide Custom Events"
Women Techmakers
</a>
</li>
<li class="devsite-footer-linkbox-item">
<a href="/community/gdg"
class="devsite-footer-linkbox-link gc-analytics-event"
data-category="Site-Wide Custom Events"
</a>
</li>
<li class="devsite-footer-linkbox-item">
<a href="/community/experts"
class="devsite-footer-linkbox-link gc-analytics-event"
data-category="Site-Wide Custom Events"
</a>
</li>
<li class="devsite-footer-linkbox-item">
<a href="/community/accelerators"
class="devsite-footer-linkbox-link gc-analytics-event"
data-category="Site-Wide Custom Events"
Accelerators
</a>
</li>
<li class="devsite-footer-linkbox-item">
<a href="/community/dsc"
class="devsite-footer-linkbox-link gc-analytics-event"
data-category="Site-Wide Custom Events"
</a>
</li>
</ul>
</li>
<a href="//console.developers.google.com"
class="devsite-footer-linkbox-link gc-analytics-event"
data-category="Site-Wide Custom Events"
</a>
</li>
<li class="devsite-footer-linkbox-item">
<a href="//console.cloud.google.com"
class="devsite-footer-linkbox-link gc-analytics-event"
data-category="Site-Wide Custom Events"
</a>
</li>
<li class="devsite-footer-linkbox-item">
<a href="//play.google.com/apps/publish"
class="devsite-footer-linkbox-link gc-analytics-event"
data-category="Site-Wide Custom Events"
</a>
</li>
<li class="devsite-footer-linkbox-item">
<a href="//console.firebase.google.com"
class="devsite-footer-linkbox-link gc-analytics-event"
data-category="Site-Wide Custom Events"
Firebase Console
</a>
</li>
<li class="devsite-footer-linkbox-item">
<a href="//console.actions.google.com"
class="devsite-footer-linkbox-link gc-analytics-event"
data-category="Site-Wide Custom Events"
</a>
</li>
<li class="devsite-footer-linkbox-item">
<a href="//cast.google.com/publish"
class="devsite-footer-linkbox-link gc-analytics-event"
data-category="Site-Wide Custom Events"
</a>
</li>
<li class="devsite-footer-linkbox-item">
<a href="//chrome.google.com/webstore/developer/dashboard"
class="devsite-footer-linkbox-link gc-analytics-event"
data-category="Site-Wide Custom Events"
</a>
</li>
</ul>
</li>
</ul>
</nav>
</devsite-footer-linkboxes>
<devsite-footer-utility class="devsite-footer">
<li class="devsite-footer-sites-item">
<a href="//developer.android.com"
class="devsite-footer-sites-link
gc-analytics-event"
data-category="Site-Wide Custom Events"
>
Android
</a>
</li>
<li class="devsite-footer-sites-item">
<a href="//developer.chrome.com/home"
class="devsite-footer-sites-link
gc-analytics-event"
data-category="Site-Wide Custom Events"
>
Chrome
</a>
</li>
<li class="devsite-footer-sites-item">
<a href="//firebase.google.com"
class="devsite-footer-sites-link
gc-analytics-event"
data-category="Site-Wide Custom Events"
>
Firebase
</a>
</li>
<li class="devsite-footer-sites-item">
<a href="//cloud.google.com"
class="devsite-footer-sites-link
gc-analytics-event"
data-category="Site-Wide Custom Events"
>
Google Cloud Platform
</a>
</li>
<li class="devsite-footer-sites-item">
<a href="/products"
class="devsite-footer-sites-link
gc-analytics-event"
data-category="Site-Wide Custom Events"
>
All products
</a>
</li>
</ul>
</nav>
<ul class="devsite-footer-utility-list">
<li class="devsite-footer-utility-item
">
</li>
<li class="devsite-footer-utility-item
">
</li>
<li class="devsite-footer-utility-item
devsite-footer-utility-button">
</li>
</ul>
<devsite-language-selector>
<devsite-select class="devsite-language-selector-menu">
<option>Language</option>
<option value="en"
lang="en"
track-metadata-original-language="en"
track-metadata-selected-language="en"
track-name="changed"
track-type="languageSelector"
>
English
</option>
<option value="id"
lang="id"
track-metadata-original-language="en"
track-metadata-selected-language="id"
track-name="changed"
track-type="languageSelector"
>
Bahasa Indonesia
</option>
<option value="de"
lang="de"
track-metadata-original-language="en"
track-metadata-selected-language="de"
track-name="changed"
track-type="languageSelector"
>
Deutsch
</option>
<option value="es"
lang="es"
track-metadata-original-language="en"
track-metadata-selected-language="es"
track-name="changed"
track-type="languageSelector"
>
Español
</option>
<option value="fr"
lang="fr"
track-metadata-original-language="en"
track-metadata-selected-language="fr"
track-name="changed"
track-type="languageSelector"
>
Français
</option>
<option value="pt_br"
lang="pt_br"
track-metadata-original-language="en"
track-metadata-selected-language="pt_br"
track-name="changed"
track-type="languageSelector"
>
Português – Brasil
</option>
<option value="ru"
lang="ru"
track-metadata-original-language="en"
track-metadata-selected-language="ru"
track-name="changed"
track-type="languageSelector"
>
Русский
</option>
<option value="zh_cn"
lang="zh_cn"
track-metadata-original-language="en"
track-metadata-selected-language="zh_cn"
track-name="changed"
track-type="languageSelector"
>
中文 – 简体
</option>
<option value="ja"
lang="ja"
track-metadata-original-language="en"
track-metadata-selected-language="ja"
track-name="changed"
track-type="languageSelector"
>
日本語
</option>
<option value="ko"
lang="ko"
track-metadata-original-language="en"
track-metadata-selected-language="ko"
track-name="changed"
track-type="languageSelector"
>
한국어
</option>
</select>
</devsite-select>
</devsite-language-selector>
</nav>
</div>
</devsite-footer-utility>
</section></section>
<devsite-sitemask></devsite-sitemask>
<devsite-snackbar
></devsite-snackbar>
<devsite-tooltip ></devsite-tooltip>
<devsite-heading-link></devsite-heading-link>
<devsite-analytics enable-analytics-iframe>
</devsite-analytics>
<devsite-badger></devsite-badger>
<script nonce="2sT2Cm2iGxerSJH6xHcQwoc9HCHsa1">
(function(d,e,v,s,i,t,E){d['GoogleDevelopersObject']=i;
t=e.createElement(v);t.async=1;t.src=s;E=e.getElementsByTagName(v)[0];
E.parentNode.insertBefore(t,E);})(window, document, 'script',
'https://www.gstatic.com/devrel-
devsite/prod/v1674d466be3b1154327dd11cf186e748303b1e92ae31ff35df0f5192fbd777ea/deve
lopers/js/app_loader.js',
'[1,"en",null,"/js/devsite_app_module.js","https://www.gstatic.com/devrel-
devsite/prod/v1674d466be3b1154327dd11cf186e748303b1e92ae31ff35df0f5192fbd777ea","ht
tps://www.gstatic.com/devrel-
devsite/prod/v1674d466be3b1154327dd11cf186e748303b1e92ae31ff35df0f5192fbd777ea/deve
lopers","https://developers-dot-devsite-v2-prod.appspot.com",null,null,
["/_pwa/developers/manifest.json","https://www.gstatic.com/devrel-
devsite/prod/v1674d466be3b1154327dd11cf186e748303b1e92ae31ff35df0f5192fbd777ea/imag
es/video-placeholder.svg","https://www.gstatic.com/devrel-
devsite/prod/v1674d466be3b1154327dd11cf186e748303b1e92ae31ff35df0f5192fbd777ea/deve
lopers/images/favicon.png","https://fonts.googleapis.com/css?
family=Google+Sans:400,500|Roboto:400,400italic,500,500italic,700,700italic|
Roboto+Mono:400,500,700|Material+Icons"],1,null,
[1,6,8,12,14,17,21,25,40,45,50,63,70,75,76,80,87,88,91,92,93,97,98,100,101,102,103,
105,107,108,111,112,113,115,116,117,118,120,122,123,124,125,127,129,131,132,133,134
,135,136],"AIzaSyAP-jjEJBzmIyKR4F-
3XITp8yM9T1gEEI8","AIzaSyB6xiKGDR5O3Ak2okS4rLkauxGUG7XP0hg"]')
</script>
<devsite-a11y-announce></devsite-a11y-announce>
</body>
</html>