{"appState":{"pageLoadApiCallsStatus":true},"categoryState":{"relatedCategories":{"headers":{"timestamp":"2022-05-24T18:31:12+00:00"},"categoryId":33606,"data":{"title":"Python","slug":"python","image":{"src":null,"width":0,"height":0},"breadcrumbs":[{"name":"Technology","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33512"},"slug":"technology","categoryId":33512},{"name":"Programming & Web Design","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33592"},"slug":"programming-web-design","categoryId":33592},{"name":"Python","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33606"},"slug":"python","categoryId":33606}],"parentCategory":{"categoryId":33592,"title":"Programming & Web Design","slug":"programming-web-design","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33592"}},"childCategories":[],"description":"Don't be scared, it's not poisonous. Python is one of the easiest languages you can learn. Check out our articles on Python here.","relatedArticles":{"self":"https://dummies-api.dummies.com/v2/articles?category=33606&offset=0&size=5"}},"_links":{"self":"https://dummies-api.dummies.com/v2/categories/33606"}},"relatedCategoriesLoadedStatus":"success"},"listState":{"list":{"count":10,"total":84,"items":[{"headers":{"creationTime":"2016-03-27T16:47:50+00:00","modifiedTime":"2022-04-26T15:00:23+00:00","timestamp":"2022-04-26T18:01:03+00:00"},"data":{"breadcrumbs":[{"name":"Technology","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33512"},"slug":"technology","categoryId":33512},{"name":"Programming & Web Design","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33592"},"slug":"programming-web-design","categoryId":33592},{"name":"Python","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33606"},"slug":"python","categoryId":33606}],"title":"Beginning Programming with Python For Dummies Cheat Sheet","strippedTitle":"beginning programming with python for dummies cheat sheet","slug":"beginning-programming-python-dummies-cheat-sheet","canonicalUrl":"","seo":{"metaDescription":"Ensure your Python applications have the best chance possible of working exactly as planned, wherever you run them.","noIndex":0,"noFollow":0},"content":"Python is an incredibly flexible language that has significant third-party support and is used in a broad range of applications. The applications you build will run on any platform that Python supports without any modification as long as you create a pure Python solution. Of course, you want to ensure that your applications have the best chance possible of working exactly as you anticipated everywhere they're run, which is why you need the information in this cheat sheet.","description":"Python is an incredibly flexible language that has significant third-party support and is used in a broad range of applications. The applications you build will run on any platform that Python supports without any modification as long as you create a pure Python solution. Of course, you want to ensure that your applications have the best chance possible of working exactly as you anticipated everywhere they're run, which is why you need the information in this cheat sheet.","blurb":"","authors":[{"authorId":9109,"name":"John Paul Mueller","slug":"john-paul-mueller","description":"John Paul Mueller has written more than 100 books and more than 600 articles on topics ranging from functional programming techniques to application development using C++. ","_links":{"self":"https://dummies-api.dummies.com/v2/authors/9109"}}],"primaryCategoryTaxonomy":{"categoryId":33606,"title":"Python","slug":"python","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33606"}},"secondaryCategoryTaxonomy":{"categoryId":0,"title":null,"slug":null,"_links":null},"tertiaryCategoryTaxonomy":{"categoryId":0,"title":null,"slug":null,"_links":null},"trendingArticles":null,"inThisArticle":[],"relatedArticles":{"fromBook":[{"articleId":250588,"title":"How to Get Additional Python Libraries","slug":"get-additional-python-libraries","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/250588"}},{"articleId":250582,"title":"Printing Lists Using Python","slug":"printing-lists-using-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/250582"}},{"articleId":250578,"title":"Understanding How Permanent Storage Works for Python Programming","slug":"understanding-permanent-storage-works-python-programming","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/250578"}},{"articleId":250575,"title":"Extending Python Classes to Make New Classes","slug":"extending-python-classes-make-new-classes","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/250575"}},{"articleId":250571,"title":"Understanding the Python Class as a Packaging Method","slug":"understanding-python-class-packaging-method","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/250571"}}],"fromCategory":[{"articleId":264919,"title":"How to Define and Use Python Lists","slug":"how-to-define-and-use-python-lists","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264919"}},{"articleId":264911,"title":"How to Use Lambda Functions in Python","slug":"how-to-use-lambda-functions-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264911"}},{"articleId":264906,"title":"Your Guide to the Python Standard Library","slug":"your-guide-to-the-python-standard-library","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264906"}},{"articleId":264894,"title":"A Beginner’s Guide to Python Versions","slug":"a-beginners-guide-to-python-versions","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264894"}},{"articleId":264888,"title":"How to Build a Simple Neural Network in Python","slug":"how-to-build-a-simple-neural-network-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264888"}}]},"hasRelatedBookFromSearch":false,"relatedBook":{"bookId":281830,"slug":"beginning-programming-with-python-for-dummies-2nd-edition","isbn":"9781119457893","categoryList":["technology","programming-web-design","python"],"amazon":{"default":"https://www.amazon.com/gp/product/1119457890/ref=as_li_tl?ie=UTF8&tag=wiley01-20","ca":"https://www.amazon.ca/gp/product/1119457890/ref=as_li_tl?ie=UTF8&tag=wiley01-20","indigo_ca":"http://www.tkqlhce.com/click-9208661-13710633?url=https://www.chapters.indigo.ca/en-ca/books/product/1119457890-item.html&cjsku=978111945484","gb":"https://www.amazon.co.uk/gp/product/1119457890/ref=as_li_tl?ie=UTF8&tag=wiley01-20","de":"https://www.amazon.de/gp/product/1119457890/ref=as_li_tl?ie=UTF8&tag=wiley01-20"},"image":{"src":"https://www.dummies.com/wp-content/uploads/beginning-programming-with-python-for-dummies-2nd-edition-cover-9781119457893-203x255.jpg","width":203,"height":255},"title":"Beginning Programming with Python For Dummies, 2nd Edition","testBankPinActivationLink":"","bookOutOfPrint":false,"authorsInfo":"\n <p><b data-author-id=\"9109\">John Paul Mueller</b> is a freelance author and technical editor with more than 107 books and 600 articles to his credit. His subjects range from networking and artificial intelligence to database management and heads-down programming. He also consults and writes certification exams. Visit his website at http://www.johnmuellerbooks.com/. </p>","authors":[{"authorId":9109,"name":"John Paul Mueller","slug":"john-paul-mueller","description":"John Paul Mueller has written more than 100 books and more than 600 articles on topics ranging from functional programming techniques to application development using C++. ","_links":{"self":"https://dummies-api.dummies.com/v2/authors/9109"}}],"_links":{"self":"https://dummies-api.dummies.com/v2/books/"}},"collections":[],"articleAds":{"footerAd":"<div class=\"du-ad-region row\" id=\"article_page_adhesion_ad\"><div class=\"du-ad-unit col-md-12\" data-slot-id=\"article_page_adhesion_ad\" data-refreshed=\"false\" \r\n data-target = \"[{&quot;key&quot;:&quot;cat&quot;,&quot;values&quot;:[&quot;technology&quot;,&quot;programming-web-design&quot;,&quot;python&quot;]},{&quot;key&quot;:&quot;isbn&quot;,&quot;values&quot;:[&quot;9781119457893&quot;]}]\" id=\"du-slot-6268335f618b7\"></div></div>","rightAd":"<div class=\"du-ad-region row\" id=\"article_page_right_ad\"><div class=\"du-ad-unit col-md-12\" data-slot-id=\"article_page_right_ad\" data-refreshed=\"false\" \r\n data-target = \"[{&quot;key&quot;:&quot;cat&quot;,&quot;values&quot;:[&quot;technology&quot;,&quot;programming-web-design&quot;,&quot;python&quot;]},{&quot;key&quot;:&quot;isbn&quot;,&quot;values&quot;:[&quot;9781119457893&quot;]}]\" id=\"du-slot-6268335f62247\"></div></div>"},"articleType":{"articleType":"Cheat Sheet","articleList":[{"articleId":248967,"title":"The 4 Common Python Programming Styles","slug":"4-common-python-programming-styles","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/248967"}},{"articleId":248973,"title":"Common Python Operators","slug":"common-python-operators","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/248973"}},{"articleId":248976,"title":"Python Operator Precedence","slug":"python-operator-precedence-2","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/248976"}},{"articleId":248970,"title":"Common Jupyter Notebook Magic Functions","slug":"common-jupyter-notebook-magic-functions","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/248970"}}],"content":[{"title":"The 4 common Python programming styles","thumb":null,"image":null,"content":"<p>You might be a nonprogrammer or a developer who isn&#8217;t interested in learning some new arcane method of writing code. If so, Python is the a great language for you. Most programming languages use just one coding style, which reduces flexibility for the programmer. Python is different, though. You can use a number of coding styles with it to achieve differing effects. Among the various Python coding styles, here are the four commonly used:</p>\n<ul>\n<li><strong>Functional:</strong> Every statement is a kind of math equation. This style lends itself well to use in parallel processing activities. Academics and data scientists tend to use this coding style regularly. However, nothing stops you from using it even if you don&#8217;t fall into one of these groups.</li>\n<li><strong>Imperative:</strong> Computations occur as changes to program state. This style is most used for manipulating data structures. Scientists of all sorts rely on this programming style because it demonstrates processes so clearly.</li>\n<li><strong>Object-oriented:</strong> This is the style commonly used with other languages to simplify the coding environment by using objects to model the real world. Python doesn&#8217;t fully implement this coding style because it doesn&#8217;t support features like data hiding, but you can still use this approach to a significant degree. This is the style that most developers use, but other groups can use it when creating more complicated applications.</li>\n<li><strong>Procedural:</strong> Most people begin learning a language by using procedural code, where tasks proceed a step at a time. This style is most used for iteration, sequencing, selection, and modularization. It&#8217;s the simplest form of coding you can use. Nonprogrammers love this style because it&#8217;s the least complicated way to achieve smaller, experimental tasks.</li>\n</ul>\n"},{"title":"Common Python operators","thumb":null,"image":null,"content":"<p><em>Operators</em> help determine the interactions between elements in a Python statement. For example, when you write <code>2 + 3</code>, it means to add (using the <code>+</code> operator) the value <code>2</code> to the value <code>3</code> for a sum of <code>5</code>. It&#8217;s important to know which operators Python supports, and remembering them all is not always easy. The following table provides a quick summary.</p>\n<table>\n<tbody>\n<tr>\n<td width=\"122\"><strong>Operator</strong></td>\n<td width=\"120\"><strong>Type</strong></td>\n<td width=\"255\"><strong>Description</strong></td>\n<td width=\"168\"><strong>Example</strong></td>\n</tr>\n<tr>\n<td width=\"122\">−</td>\n<td width=\"120\">Arithmetic</td>\n<td width=\"255\">Subtracts the right operand from left hand operand.</td>\n<td width=\"168\">5 − 2 = 3</td>\n</tr>\n<tr>\n<td width=\"122\">−</td>\n<td width=\"120\">Unary</td>\n<td width=\"255\">Negates the original value so that positive becomes negative and vice versa.</td>\n<td width=\"168\">−(−4) results in 4 while −4 results in −4</td>\n</tr>\n<tr>\n<td width=\"122\">−=</td>\n<td width=\"120\">Assignment</td>\n<td width=\"255\">Subtracts the value found in the right operand from the value found in the left operand and places the result in the left operand.</td>\n<td width=\"168\">MyVar -= 2 results in MyVar containing 3</td>\n</tr>\n<tr>\n<td width=\"122\">!=</td>\n<td width=\"120\">Relational</td>\n<td width=\"255\">Determines whether two values are not equal. Some older versions of Python would allow you to use the &lt;&gt; operator in place of the != operator. Using the &lt;&gt; operator results in an error in current versions of Python.</td>\n<td width=\"168\">1 != 2 is True</td>\n</tr>\n<tr>\n<td width=\"122\">%</td>\n<td width=\"120\">Arithmetic</td>\n<td width=\"255\">Divides the left operand by the right operand and returns the remainder.</td>\n<td width=\"168\">5 % 2 = 1</td>\n</tr>\n<tr>\n<td width=\"122\">%=</td>\n<td width=\"120\">Assignment</td>\n<td width=\"255\">Divides the value found in the left operand by the value found in the right operand and places the remainder in the left operand.</td>\n<td width=\"168\">MyVar %= 2 results in MyVar containing 1</td>\n</tr>\n<tr>\n<td width=\"122\">&amp; (And)</td>\n<td width=\"120\">Bitwise</td>\n<td width=\"255\">Determines whether both individual bits within two operators are true and sets the resulting bit to true when they are.</td>\n<td width=\"168\">0b1100 &amp; 0b0110 = 0b0100</td>\n</tr>\n<tr>\n<td width=\"122\">*</td>\n<td width=\"120\">Arithmetic</td>\n<td width=\"255\">Multiplies the right operand by the left operand.</td>\n<td width=\"168\">5 * 2 = 10</td>\n</tr>\n<tr>\n<td width=\"122\">**</td>\n<td width=\"120\">Arithmetic</td>\n<td width=\"255\">Calculates the exponential value of the right operand by the left operand.</td>\n<td width=\"168\">5 ** 2 = 25</td>\n</tr>\n<tr>\n<td width=\"122\">**=</td>\n<td width=\"120\">Assignment</td>\n<td width=\"255\">Determines the exponential value found in the left operand when raised to the power of the value found in the right operand and places the result in the left operand.</td>\n<td width=\"168\">MyVar ** 2 results in MyVar containing 25</td>\n</tr>\n<tr>\n<td width=\"122\">*=</td>\n<td width=\"120\">Assignment</td>\n<td width=\"255\">Multiplies the value found in the right operand by the value found in the left operand and places the result in the left operand.</td>\n<td width=\"168\">MyVar *= 2 results in MyVar containing 10</td>\n</tr>\n<tr>\n<td width=\"122\">/</td>\n<td width=\"120\">Arithmetic</td>\n<td width=\"255\">Divides the left operand by the right operand.</td>\n<td width=\"168\">5 / 2 = 2.5</td>\n</tr>\n<tr>\n<td width=\"122\">//</td>\n<td width=\"120\">Arithmetic</td>\n<td width=\"255\">Performs integer division, where the left operand is divided by the right operand and only the whole number is returned (also called floor division).</td>\n<td width=\"168\">5 // 2 = 2</td>\n</tr>\n<tr>\n<td width=\"122\">//=</td>\n<td width=\"120\">Assignment</td>\n<td width=\"255\">Divides the value found in the left operand by the value found in the right operand and places the integer (whole number) result in the left operand.</td>\n<td width=\"168\">MyVar //= 2 results in MyVar containing 2</td>\n</tr>\n<tr>\n<td width=\"122\">/=</td>\n<td width=\"120\">Assignment</td>\n<td width=\"255\">Divides the value found in the left operand by the value found in the right operand and places the result in the left operand.</td>\n<td width=\"168\">MyVar /= 2 results in MyVar containing 2.5</td>\n</tr>\n<tr>\n<td width=\"122\">^ (Exclusive or)</td>\n<td width=\"120\">Bitwise</td>\n<td width=\"255\">Determines whether just one of the individual bits within two operators are true and sets the resulting bit to true when they are. When both bits are true or both bits are false, the result is false.</td>\n<td width=\"168\">0b1100 ^ 0b0110 = 0b1010</td>\n</tr>\n<tr>\n<td width=\"122\">| (Or)</td>\n<td width=\"120\">Bitwise</td>\n<td width=\"255\">Determines whether either of the individual bits within two operators are true and sets the resulting bit to true when they are.</td>\n<td width=\"168\">0b1100 | 0b0110 = 0b1110</td>\n</tr>\n<tr>\n<td width=\"122\">~</td>\n<td width=\"120\">Unary</td>\n<td width=\"255\">Inverts the bits in a number so that all the 0 bits become 1 bits and vice versa.</td>\n<td width=\"168\">~4 results in a value of −5</td>\n</tr>\n<tr>\n<td width=\"122\">~ (One&#8217;s complement)</td>\n<td width=\"120\">Bitwise</td>\n<td width=\"255\">Calculates the one&#8217;s complement value a number.</p>\n<p><strong><em> </em></strong></td>\n<td width=\"168\">~0b1100 = −0b1101</p>\n<p>~0b0110 = −0b0111</td>\n</tr>\n<tr>\n<td width=\"122\">+</td>\n<td width=\"120\">Arithmetic</td>\n<td width=\"255\">Adds two values together.</td>\n<td width=\"168\">5 + 2 = 7</td>\n</tr>\n<tr>\n<td width=\"122\">+</td>\n<td width=\"120\">Unary</td>\n<td width=\"255\">Provided purely for the sake of completeness. This operator returns the same value that you provide as input.</td>\n<td width=\"168\">+4 results in a value of 4</td>\n</tr>\n<tr>\n<td width=\"122\">+=</td>\n<td width=\"120\">Assignment</td>\n<td width=\"255\">Adds the value found in the right operand to the value found in the left operand and places the result in the left operand.</td>\n<td width=\"168\">MyVar += 2 results in MyVar containing 7</td>\n</tr>\n<tr>\n<td width=\"122\">&lt;</td>\n<td width=\"120\">Relational</td>\n<td width=\"255\">Verifies that the left operand value is less than the right operand value.</td>\n<td width=\"168\">1 &lt; 2 is True</td>\n</tr>\n<tr>\n<td width=\"122\">&lt;&lt; (Left shift)</td>\n<td width=\"120\">Bitwise</td>\n<td width=\"255\">Shifts the bits in the left operand left by the value of the right operand. All new bits are set to 0 and all bits that flow off the end are lost.</td>\n<td width=\"168\">0b00110011 &lt;&lt; 2 = 0b11001100</td>\n</tr>\n<tr>\n<td width=\"122\">&lt;=</td>\n<td width=\"120\">Relational</td>\n<td width=\"255\">Verifies that the left operand value is less than or equal to the right operand value.</td>\n<td width=\"168\">1 &lt;= 2 is True</td>\n</tr>\n<tr>\n<td width=\"122\">=</td>\n<td width=\"120\">Assignment</td>\n<td width=\"255\">Assigns the value found in the right operand to the left operand.</td>\n<td width=\"168\">MyVar = 2 results in MyVar containing 2</td>\n</tr>\n<tr>\n<td width=\"122\">==</td>\n<td width=\"120\">Relational</td>\n<td width=\"255\">Determines whether two values are equal. Notice that the relational operator uses two equals signs. A mistake many developers make is using just one equals sign, which results in one value being assigned to another.</td>\n<td width=\"168\">1 == 2 is False</td>\n</tr>\n<tr>\n<td width=\"122\">&gt;</td>\n<td width=\"120\">Relational</td>\n<td width=\"255\">Verifies that the left operand value is greater than the right operand value.</td>\n<td width=\"168\">1 &gt; 2 is False</td>\n</tr>\n<tr>\n<td width=\"122\">&gt;=</td>\n<td width=\"120\">Relational</td>\n<td width=\"255\">Verifies that the left operand value is greater than or equal to the right operand value.</td>\n<td width=\"168\">1 &gt;= 2 is False</td>\n</tr>\n<tr>\n<td width=\"122\">&gt;&gt; (Right shift)</td>\n<td width=\"120\">Bitwise</td>\n<td width=\"255\">Shifts the bits in the left operand right by the value of the right operand. All new bits are set to 0 and all bits that flow off the end are lost.</td>\n<td width=\"168\">0b00110011 &gt;&gt; 2 = 0b00001100</td>\n</tr>\n<tr>\n<td width=\"122\">and</td>\n<td width=\"120\">Logical</td>\n<td width=\"255\">Determines whether both operands are true.</td>\n<td width=\"168\">True and True is True</p>\n<p>True and False is False</p>\n<p>False and True is False</p>\n<p>False and False is False</td>\n</tr>\n<tr>\n<td width=\"122\">in</td>\n<td width=\"120\">Membership</td>\n<td width=\"255\">Determines whether the value in the left operand appears in the sequence found in the right operand.</td>\n<td width=\"168\">&#8220;Hello&#8221; in &#8220;Hello Goodbye&#8221; is True</td>\n</tr>\n<tr>\n<td width=\"122\">is</td>\n<td width=\"120\">Identity</td>\n<td width=\"255\">Evaluates to true when the type of the value or expression in the right operand points to the same type in the left operand.</td>\n<td width=\"168\">type(2) is int is True</td>\n</tr>\n<tr>\n<td width=\"122\">is not</td>\n<td width=\"120\">Identity</td>\n<td width=\"255\">Evaluates to true when the type of the value or expression in the right operand points to a different type than the value or expression in the left operand.</td>\n<td width=\"168\">type(2) is not int is False</td>\n</tr>\n<tr>\n<td width=\"122\">not</td>\n<td width=\"120\">Logical</td>\n<td width=\"255\">Negates the truth value of a single operand. A true value becomes false and a false value becomes true.</td>\n<td width=\"168\">not True is False</p>\n<p>not False is True</td>\n</tr>\n<tr>\n<td width=\"122\">not in</td>\n<td width=\"120\">Membership</td>\n<td width=\"255\">Determines whether the value in the left operand is missing from the sequence found in the right operand.</td>\n<td width=\"168\">&#8220;Hello&#8221; not in &#8220;Hello Goodbye&#8221; is False</td>\n</tr>\n<tr>\n<td width=\"122\">or</td>\n<td width=\"120\">Logical</td>\n<td width=\"255\">Determines when one of two operands are true.</td>\n<td width=\"168\">True or True is True</p>\n<p>True or False is True</p>\n<p>False or True is True</p>\n<p>False or False is False</td>\n</tr>\n</tbody>\n</table>\n"},{"title":"Python operator precedence","thumb":null,"image":null,"content":"<table>\n<tbody>\n<tr>\n<td width=\"92\"><strong>Operator</strong></td>\n<td width=\"573\"><strong>Description</strong></td>\n</tr>\n<tr>\n<td width=\"92\">()</td>\n<td width=\"573\">Parentheses are used to group expressions and to override the default precedence so that you can force an operation of lower precedence (such as addition) to take precedence over an operation of higher precedence (such as multiplication).</td>\n</tr>\n<tr>\n<td width=\"92\">**</td>\n<td width=\"573\">Exponentiation raises the value of the left operand to the power of the right operand.</td>\n</tr>\n<tr>\n<td width=\"92\">~ + &#8211;</td>\n<td width=\"573\">Unary operators interact with a single variable or expression.</td>\n</tr>\n<tr>\n<td width=\"92\">* / % //</td>\n<td width=\"573\">Multiply, divide, modulo and floor division.</td>\n</tr>\n<tr>\n<td width=\"92\">+ &#8211;</td>\n<td width=\"573\">Addition and subtraction.</td>\n</tr>\n<tr>\n<td width=\"92\">&gt;&gt; &lt;&lt;</td>\n<td width=\"573\">Right and left bitwise shift.</td>\n</tr>\n<tr>\n<td width=\"92\">&amp;</td>\n<td width=\"573\">Bitwise AND.</td>\n</tr>\n<tr>\n<td width=\"92\">^ |</td>\n<td width=\"573\">Bitwise exclusive OR and standard OR.</td>\n</tr>\n<tr>\n<td width=\"92\">&lt;= &lt; &gt; &gt;=</td>\n<td width=\"573\">Comparison operators.</td>\n</tr>\n<tr>\n<td width=\"92\">== !=</td>\n<td width=\"573\">Equality operators.</td>\n</tr>\n<tr>\n<td width=\"92\">= %= /= //= -= += *= **=</td>\n<td width=\"573\">Assignment operators.</td>\n</tr>\n<tr>\n<td width=\"92\">is is not</td>\n<td width=\"573\">Identity operators.</td>\n</tr>\n<tr>\n<td width=\"92\">in not in</td>\n<td width=\"573\">Membership operators.</td>\n</tr>\n<tr>\n<td width=\"92\">not or and</td>\n<td width=\"573\">Logical operators.</td>\n</tr>\n</tbody>\n</table>\n"},{"title":"Common Jupyter Notebook magic functions","thumb":null,"image":null,"content":"<p>Jupyter Notebook relies on the literate programming approach originally advanced by Donald Knuth. This means that the Integrated Development Environment (IDE) produces output that looks more like a report than the complex coding environments that most people rely on. The magic functions add to this capability by creating an environment in which you can choose something other than the expected result. The following table highlights the most important magic functions that Jupyter Notebook provides.</p>\n<table>\n<tbody>\n<tr>\n<td width=\"122\"><strong>Magic Function</strong></td>\n<td width=\"105\"><strong>Type Alone Provides Status?</strong></td>\n<td width=\"438\"><strong>Description</strong></td>\n</tr>\n<tr>\n<td width=\"122\">%alias</td>\n<td width=\"105\">Yes</td>\n<td width=\"438\">Assigns or displays an alias for a system command.</td>\n</tr>\n<tr>\n<td width=\"122\">%autocall</td>\n<td width=\"105\">Yes</td>\n<td width=\"438\">Enables you to call functions without including the parentheses. The settings are Off, Smart (default), and Full. The Smart setting applies the parentheses only if you include an argument with the call.</td>\n</tr>\n<tr>\n<td width=\"122\">%automagic</td>\n<td width=\"105\">Yes</td>\n<td width=\"438\">Enables you to call the line magic functions without including the % sign. The settings are False (default) and True.</td>\n</tr>\n<tr>\n<td width=\"122\">%autosave</td>\n<td width=\"105\">Yes</td>\n<td width=\"438\">Displays or modifies the intervals between automatic Notebook saves. The default setting is every 120 seconds.</td>\n</tr>\n<tr>\n<td width=\"122\">%cd</td>\n<td width=\"105\">Yes</td>\n<td width=\"438\">Changes directory to a new storage location. You can also use this command to move through the directory history or to change directories to a bookmark.</td>\n</tr>\n<tr>\n<td width=\"122\">%cls</td>\n<td width=\"105\">No</td>\n<td width=\"438\">Clears the screen.</td>\n</tr>\n<tr>\n<td width=\"122\">%colors</td>\n<td width=\"105\">No</td>\n<td width=\"438\">Specifies the colors used to display text associated with prompts, information system, and exception handlers. You can choose between NoColor (black and white), Linux (default), and LightBG.</td>\n</tr>\n<tr>\n<td width=\"122\">%config</td>\n<td width=\"105\">Yes</td>\n<td width=\"438\">Enables you to configure IPython.</td>\n</tr>\n<tr>\n<td width=\"122\">%dhist</td>\n<td width=\"105\">Yes</td>\n<td width=\"438\">Displays a list of directories visited during the current session.</td>\n</tr>\n<tr>\n<td width=\"122\">%file</td>\n<td width=\"105\">No</td>\n<td width=\"438\">Outputs the name of the file that contains the source code for the object.</td>\n</tr>\n<tr>\n<td width=\"122\">%hist</td>\n<td width=\"105\">Yes</td>\n<td width=\"438\">Displays a list of magic function commands issued during the current session.</td>\n</tr>\n<tr>\n<td width=\"122\">%install_ext</td>\n<td width=\"105\">No</td>\n<td width=\"438\">Installs the specified extension.</td>\n</tr>\n<tr>\n<td width=\"122\">%load</td>\n<td width=\"105\">No</td>\n<td width=\"438\">Loads application code from another source, such as an online example.</td>\n</tr>\n<tr>\n<td width=\"122\">%load_ext</td>\n<td width=\"105\">No</td>\n<td width=\"438\">Loads a Python extension using its module name.</td>\n</tr>\n<tr>\n<td width=\"122\">%lsmagic</td>\n<td width=\"105\">Yes</td>\n<td width=\"438\">Displays a list of the currently available magic functions.</td>\n</tr>\n<tr>\n<td width=\"122\">%magic</td>\n<td width=\"105\">Yes</td>\n<td width=\"438\">Displays a help screen showing information about the magic functions.</td>\n</tr>\n<tr>\n<td width=\"122\">%matplotlib</td>\n<td width=\"105\">Yes</td>\n<td width=\"438\">Sets the backend processor used for plots. Using the inline value displays the plot within the cell for an IPython Notebook file. The possible values are gtk&#8217;; &#8216;gtk3&#8217;; &#8216;inline&#8217;; &#8216;nbagg&#8217;; &#8216;osx&#8217;; &#8216;qt&#8217;; &#8216;qt4&#8217;; &#8216;qt5&#8217;; &#8216;tk&#8217;; and &#8216;wx&#8217;.</td>\n</tr>\n<tr>\n<td width=\"122\">%paste</td>\n<td width=\"105\">No</td>\n<td width=\"438\">Pastes the content of the Clipboard into the IPython environment.</td>\n</tr>\n<tr>\n<td width=\"122\">%pdef</td>\n<td width=\"105\">No</td>\n<td width=\"438\">Shows how to call the object (assuming that the object is callable).</td>\n</tr>\n<tr>\n<td width=\"122\">%pdoc</td>\n<td width=\"105\">No</td>\n<td width=\"438\">Displays the docstring for an object.</td>\n</tr>\n<tr>\n<td width=\"122\">%pinfo</td>\n<td width=\"105\">No</td>\n<td width=\"438\">Displays detailed information about the object (often more than is provided by help alone).</td>\n</tr>\n<tr>\n<td width=\"122\">%pinfo2</td>\n<td width=\"105\">No</td>\n<td width=\"438\">Displays extra detailed information about the object (when available).</td>\n</tr>\n<tr>\n<td width=\"122\">%reload_ext</td>\n<td width=\"105\">No</td>\n<td width=\"438\">Reloads a previously installed extension.</td>\n</tr>\n<tr>\n<td width=\"122\">%source</td>\n<td width=\"105\">No</td>\n<td width=\"438\">Displays the source code for the object (assuming that the source is available).</td>\n</tr>\n<tr>\n<td width=\"122\">%timeit</td>\n<td width=\"105\">No</td>\n<td width=\"438\">Calculates the best performance time for an instruction.</td>\n</tr>\n<tr>\n<td width=\"122\">%%timeit</td>\n<td width=\"105\">No</td>\n<td width=\"438\">Calculates the best time performance for all the instructions in a cell, apart from the one placed on the same cell line as the cell magic (which could therefore be an initialization instruction).</td>\n</tr>\n<tr>\n<td width=\"122\">%unalias</td>\n<td width=\"105\">No</td>\n<td width=\"438\">Removes a previously created alias from the list.</td>\n</tr>\n<tr>\n<td width=\"122\">%unload_ext</td>\n<td width=\"105\">No</td>\n<td width=\"438\">Unloads the specified extension.</td>\n</tr>\n<tr>\n<td width=\"122\">%%writefile</td>\n<td width=\"105\">No</td>\n<td width=\"438\">Writes the contents of a cell to the specified file.</td>\n</tr>\n</tbody>\n</table>\n"}],"videoInfo":{"videoId":null,"name":null,"accountId":null,"playerId":null,"thumbnailUrl":null,"description":null,"uploadDate":null}},"sponsorship":{"sponsorshipPage":false,"backgroundImage":{"src":null,"width":0,"height":0},"brandingLine":"","brandingLink":"","brandingLogo":{"src":null,"width":0,"height":0}},"primaryLearningPath":"Advance","lifeExpectancy":"One year","lifeExpectancySetFrom":"2022-02-25T00:00:00+00:00","dummiesForKids":"no","sponsoredContent":"no","adInfo":"","adPairKey":[]},"status":"publish","visibility":"public","articleId":207635},{"headers":{"creationTime":"2016-03-27T16:47:06+00:00","modifiedTime":"2022-02-24T19:52:55+00:00","timestamp":"2022-02-25T00:01:04+00:00"},"data":{"breadcrumbs":[{"name":"Technology","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33512"},"slug":"technology","categoryId":33512},{"name":"Programming & Web Design","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33592"},"slug":"programming-web-design","categoryId":33592},{"name":"Python","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33606"},"slug":"python","categoryId":33606}],"title":"Python for Data Science For Dummies Cheat Sheet","strippedTitle":"python for data science for dummies cheat sheet","slug":"python-for-data-science-for-dummies-cheat-sheet","canonicalUrl":"","seo":{"metaDescription":"Perform data science tasks with minimum effort by using Python. Learn line plot styles, common programming errors, and more.","noIndex":0,"noFollow":0},"content":"Python is an incredible programming language that you can use to perform data science tasks with a minimum of effort. The huge number of available libraries means that the low-level code you normally need to write is likely already available from some other source. All you need to focus on is getting the job done. With that in mind, this cheat sheet helps you access the most commonly needed reminders for making your programming experience fast and easy.","description":"Python is an incredible programming language that you can use to perform data science tasks with a minimum of effort. The huge number of available libraries means that the low-level code you normally need to write is likely already available from some other source. All you need to focus on is getting the job done. With that in mind, this cheat sheet helps you access the most commonly needed reminders for making your programming experience fast and easy.","blurb":"","authors":[{"authorId":9109,"name":"John Paul Mueller","slug":"john-paul-mueller","description":"John Paul Mueller has written more than 100 books and more than 600 articles on topics ranging from functional programming techniques to application development using C++. ","_links":{"self":"https://dummies-api.dummies.com/v2/authors/9109"}},{"authorId":9110,"name":"Luca Massaron","slug":"luca-massaron","description":"Luca Massaron is a Google developer expert in machine learning. Massaron is a data scientist and marketing research director specializing in multivariate statistical analysis, machine learning, and customer insight.","_links":{"self":"https://dummies-api.dummies.com/v2/authors/9110"}}],"primaryCategoryTaxonomy":{"categoryId":33606,"title":"Python","slug":"python","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33606"}},"secondaryCategoryTaxonomy":{"categoryId":33580,"title":"General (Data Science)","slug":"general-data-science","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33580"}},"tertiaryCategoryTaxonomy":{"categoryId":0,"title":null,"slug":null,"_links":null},"trendingArticles":null,"inThisArticle":[],"relatedArticles":{"fromBook":[{"articleId":262687,"title":"Working with Google Colaboratory Notebooks","slug":"working-with-google-colaboratory-notebooks","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/262687"}},{"articleId":262680,"title":"Python Programming: Making Machine Learning Accessible with the Random Forest Algorithm","slug":"python-programming-making-machine-learning-accessible-with-the-random-forest-algorithm","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/262680"}},{"articleId":262675,"title":"What is Google Colaboratory?","slug":"what-is-google-colaboratory","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/262675"}},{"articleId":262651,"title":"Playing with Scikit-Learn and Neural Networks","slug":"playing-with-scikit-learn-and-neural-networks","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/262651"}},{"articleId":262643,"title":"Tips for Using Jupyter Notebook for Python Programming","slug":"tips-for-using-jupyter-notebook-for-python-programming","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/262643"}}],"fromCategory":[{"articleId":264919,"title":"How to Define and Use Python Lists","slug":"how-to-define-and-use-python-lists","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264919"}},{"articleId":264911,"title":"How to Use Lambda Functions in Python","slug":"how-to-use-lambda-functions-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264911"}},{"articleId":264906,"title":"Your Guide to the Python Standard Library","slug":"your-guide-to-the-python-standard-library","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264906"}},{"articleId":264894,"title":"A Beginner’s Guide to Python Versions","slug":"a-beginners-guide-to-python-versions","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264894"}},{"articleId":264888,"title":"How to Build a Simple Neural Network in Python","slug":"how-to-build-a-simple-neural-network-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264888"}}]},"hasRelatedBookFromSearch":false,"relatedBook":{"bookId":281834,"slug":"python-for-data-science-for-dummies-2nd-edition","isbn":"9781119547624","categoryList":["technology","programming-web-design","python"],"amazon":{"default":"https://www.amazon.com/gp/product/1119547628/ref=as_li_tl?ie=UTF8&tag=wiley01-20","ca":"https://www.amazon.ca/gp/product/1119547628/ref=as_li_tl?ie=UTF8&tag=wiley01-20","indigo_ca":"http://www.tkqlhce.com/click-9208661-13710633?url=https://www.chapters.indigo.ca/en-ca/books/product/1119547628-item.html&cjsku=978111945484","gb":"https://www.amazon.co.uk/gp/product/1119547628/ref=as_li_tl?ie=UTF8&tag=wiley01-20","de":"https://www.amazon.de/gp/product/1119547628/ref=as_li_tl?ie=UTF8&tag=wiley01-20"},"image":{"src":"https://www.dummies.com/wp-content/uploads/python-for-data-science-for-dummies-2nd-edition-cover-9781119547624-203x255.jpg","width":203,"height":255},"title":"Python for Data Science For Dummies, 2nd Edition","testBankPinActivationLink":"","bookOutOfPrint":false,"authorsInfo":"\n <p><b data-author-id=\"9109\">John Paul Mueller</b> is a tech editor and the author of over 100 books on topics from networking and home security to database management and heads-down programming. Follow John's blog at http://blog.johnmuellerbooks.com/. Luca Massaron is a data scientist who specializes in organizing and interpreting big data and transforming it into smart data. He is a Google Developer Expert (GDE) in machine learning. </p>","authors":[{"authorId":9109,"name":"John Paul Mueller","slug":"john-paul-mueller","description":"John Paul Mueller has written more than 100 books and more than 600 articles on topics ranging from functional programming techniques to application development using C++. ","_links":{"self":"https://dummies-api.dummies.com/v2/authors/9109"}},{"authorId":9110,"name":"Luca Massaron","slug":"luca-massaron","description":"Luca Massaron is a Google developer expert in machine learning. Massaron is a data scientist and marketing research director specializing in multivariate statistical analysis, machine learning, and customer insight.","_links":{"self":"https://dummies-api.dummies.com/v2/authors/9110"}}],"_links":{"self":"https://dummies-api.dummies.com/v2/books/"}},"collections":[],"articleAds":{"footerAd":"<div class=\"du-ad-region row\" id=\"article_page_adhesion_ad\"><div class=\"du-ad-unit col-md-12\" data-slot-id=\"article_page_adhesion_ad\" data-refreshed=\"false\" \r\n data-target = \"[{&quot;key&quot;:&quot;cat&quot;,&quot;values&quot;:[&quot;technology&quot;,&quot;programming-web-design&quot;,&quot;python&quot;]},{&quot;key&quot;:&quot;isbn&quot;,&quot;values&quot;:[&quot;9781119547624&quot;]}]\" id=\"du-slot-62181c404fd40\"></div></div>","rightAd":"<div class=\"du-ad-region row\" id=\"article_page_right_ad\"><div class=\"du-ad-unit col-md-12\" data-slot-id=\"article_page_right_ad\" data-refreshed=\"false\" \r\n data-target = \"[{&quot;key&quot;:&quot;cat&quot;,&quot;values&quot;:[&quot;technology&quot;,&quot;programming-web-design&quot;,&quot;python&quot;]},{&quot;key&quot;:&quot;isbn&quot;,&quot;values&quot;:[&quot;9781119547624&quot;]}]\" id=\"du-slot-62181c40506f8\"></div></div>"},"articleType":{"articleType":"Cheat Sheet","articleList":[{"articleId":142841,"title":"The 8 Most Common Python Programming Errors","slug":"the-8-most-common-python-programming-errors","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/142841"}},{"articleId":142839,"title":"Line Plot Styles","slug":"line-plot-styles","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/142839"}},{"articleId":142840,"title":"Common IPython Magic Functions","slug":"common-ipython-magic-functions","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/142840"}},{"articleId":142686,"title":"Scikit-Learn Method Summary","slug":"scikit-learn-method-summary","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/142686"}}],"content":[{"title":"The 8 most common Python programming errors","thumb":null,"image":null,"content":"<p>Every developer on the planet makes mistakes. However, knowing about common mistakes will save you time and effort later. The following list tells you about the most common errors that developers experience when working with Python:</p>\n<ul class=\"level-one\">\n<li>\n<p class=\"first-para\"><strong>Using the incorrect indentation:</strong> Many Python features rely on indentation. For example, when you create a new class, everything in that class is indented under the class declaration. The same is true for decision, loop, and other structural statements. If you find that your code is executing a task when it really shouldn’t be, start reviewing the indentation you’re using.</p>\n</li>\n<li>\n<p class=\"first-para\"><strong>Relying on the assignment operator instead of the equality operator:</strong> When performing a comparison between two objects or value, you just use the equality operator (==), not the assignment operator (=). The assignment operator places an object or value within a variable and doesn’t compare anything.</p>\n</li>\n<li>\n<p class=\"first-para\"><strong>Placing function calls in the wrong order when creating complex statements:</strong> Python always executes functions from left to right. So the statement <code>MyString.strip().center(21, \"*\")</code> produces a different result than <code>MyString.center(21, \"*\").strip()</code>. When you encounter a situation in which the output of a series of concatenated functions is different from what you expected, you need to check function order to ensure that each function is in the correct place.</p>\n</li>\n<li>\n<p class=\"first-para\"><strong>Misplacing punctuation:</strong> You can put punctuation in the wrong place and create an entirely different result. Remember that you must include a colon at the end of each structural statement. In addition, the placement of parentheses is critical. For example, <code>(1 + 2) * (3 + 4), 1 + ((2 * 3) + 4)</code>, and <code>1 + (2 * (3 + 4))</code> all produce different results.</p>\n</li>\n<li>\n<p class=\"first-para\"><strong>Using the incorrect logical operator:</strong> Most of the operators don’t present developers with problems, but the logical operators do. Remember to use <code>and </code>to determine when both operands must be <code>True </code>and <code>or </code>when either of the operands can be <code>True</code>.</p>\n</li>\n<li>\n<p class=\"first-para\"><strong>Creating count-by-one errors on loops:</strong> Remember that a loop doesn’t count the last number you specify in a range. So, if you specify the range <code>[1:11]</code>, you actually get output for values between 1 and 10.</p>\n</li>\n<li>\n<p class=\"first-para\"><strong>Using the wrong capitalization:</strong> Python is case sensitive, so MyVar is different from myvar and MYVAR. Always check capitalization when you find that you can’t access a value you expected to access.</p>\n</li>\n<li>\n<p class=\"first-para\"><strong>Making a spelling mistake:</strong> Even seasoned developers suffer from spelling errors at times. Ensuring that you use a common approach to naming variables, classes, and functions does help. However, even a consistent naming scheme won’t always prevent you from typing MyVer when you meant to type MyVar.</p>\n</li>\n</ul>\n"},{"title":"Line plot styles","thumb":null,"image":null,"content":"<p>Whenever you create a plot, you need to identify the sources of information using more than just the lines. Creating a plot that uses differing line types and data point symbols makes the plot much easier for other people to use. The following table lists the line plot styles.</p>\n<table>\n<tbody>\n<tr>\n<td colspan=\"2\"><strong>Color</strong></td>\n<td colspan=\"2\"><strong>Marker</strong></td>\n<td colspan=\"2\"><strong>Style</strong></td>\n</tr>\n<tr>\n<td><strong>Code</strong></td>\n<td><strong>Line Color</strong></td>\n<td><strong>Code</strong></td>\n<td><strong>Marker Style</strong></td>\n<td><strong>Code</strong></td>\n<td><strong>Line Style</strong></td>\n</tr>\n<tr>\n<td>b</td>\n<td>blue</td>\n<td>.</td>\n<td>point</td>\n<td>&#8211;</td>\n<td>Solid</td>\n</tr>\n<tr>\n<td>g</td>\n<td>green</td>\n<td>o</td>\n<td>circle</td>\n<td>:</td>\n<td>Dotted</td>\n</tr>\n<tr>\n<td>r</td>\n<td>red</td>\n<td>x</td>\n<td>x-mark</td>\n<td>-.</td>\n<td>dash dot</td>\n</tr>\n<tr>\n<td>c</td>\n<td>cyan</td>\n<td>+</td>\n<td>plus</td>\n<td>&#8212;</td>\n<td>Dashed</td>\n</tr>\n<tr>\n<td>m</td>\n<td>magenta</td>\n<td>*</td>\n<td>star</td>\n<td>(none)</td>\n<td>no line</td>\n</tr>\n<tr>\n<td>y</td>\n<td>yellow</td>\n<td>s</td>\n<td>square</td>\n<td></td>\n<td></td>\n</tr>\n<tr>\n<td>k</td>\n<td>black</td>\n<td>d</td>\n<td>diamond</td>\n<td></td>\n<td></td>\n</tr>\n<tr>\n<td>w</td>\n<td>white</td>\n<td>v</td>\n<td>down triangle</td>\n<td></td>\n<td></td>\n</tr>\n<tr>\n<td></td>\n<td></td>\n<td>^</td>\n<td>up triangle</td>\n<td></td>\n<td></td>\n</tr>\n<tr>\n<td></td>\n<td></td>\n<td>&lt;</td>\n<td>left triangle</td>\n<td></td>\n<td></td>\n</tr>\n<tr>\n<td></td>\n<td></td>\n<td>&gt;</td>\n<td>right triangle</td>\n<td></td>\n<td></td>\n</tr>\n<tr>\n<td></td>\n<td></td>\n<td>p</td>\n<td>5-point star</td>\n<td></td>\n<td></td>\n</tr>\n<tr>\n<td></td>\n<td></td>\n<td>h</td>\n<td>6-point star</td>\n<td></td>\n<td></td>\n</tr>\n</tbody>\n</table>\n<p class=\"Tip\">Remember that you can also use these styles with other kinds of plots. For example, a scatter plot can use these styles to define each of the data points. When in doubt, try the styles to see whether they’ll work with your particular plot.</p>\n"},{"title":"Common IPython Magic Functions","thumb":null,"image":null,"content":"<p>It’s kind of amazing to think that IPython provides you with magic, but that’s precisely what you get with the magic functions. A magic function begins with either a % or %% sign. Those with a % sign work within the environment, and those with a %% sign work at the cell level.</p>\n<p>Note that the magic functions work best with Jupyter Notebook. People using alternatives, such as Google Colab, may find that some magic functions fail to provide the desired result.</p>\n<p>The following list gives you a few of the most common magic functions and their purpose. To obtain a full list, type <strong>%quickref</strong> and press Enter in the IPython console or check out the <a href=\"https://damontallen.github.io/IPython-quick-ref-sheets/\" target=\"_blank\" rel=\"noopener\">full list</a>.</p>\n<table>\n<tbody>\n<tr>\n<td width=\"109\"><strong>Magic Function</strong></td>\n<td width=\"96\"><strong>Type Alone Provides Status?</strong></td>\n<td width=\"327\"><strong>Description</strong></td>\n</tr>\n<tr>\n<td width=\"109\">%%timeit</td>\n<td width=\"96\">No</td>\n<td width=\"327\">Calculates the best time performance for all the instructions in a cell, apart from the one placed on the same cell line as the cell magic (which could therefore be an initialization instruction).</td>\n</tr>\n<tr>\n<td width=\"109\">%%writefile</td>\n<td width=\"96\">No</td>\n<td width=\"327\">Writes the contents of a cell to the specified file.</td>\n</tr>\n<tr>\n<td width=\"109\">%alias</td>\n<td width=\"96\">Yes</td>\n<td width=\"327\">Assigns or displays an alias for a system command.</td>\n</tr>\n<tr>\n<td width=\"109\">%autocall</td>\n<td width=\"96\">Yes</td>\n<td width=\"327\">Enables you to call functions without including the parentheses. The settings are Off, Smart (default), and Full. The Smart setting applies the parentheses only if you include an argument with the call.</td>\n</tr>\n<tr>\n<td width=\"109\">%automagic</td>\n<td width=\"96\">Yes</td>\n<td width=\"327\">Enables you to call the line magic functions without including the % sign. The settings are False (default) and True.</td>\n</tr>\n<tr>\n<td width=\"109\">%cd</td>\n<td width=\"96\">Yes</td>\n<td width=\"327\">Changes directory to a new storage location. You can also use this command to move through the directory history or to change directories to a bookmark.</td>\n</tr>\n<tr>\n<td width=\"109\">%cls</td>\n<td width=\"96\">No</td>\n<td width=\"327\">Clears the screen.</td>\n</tr>\n<tr>\n<td width=\"109\">%colors</td>\n<td width=\"96\">No</td>\n<td width=\"327\">Specifies the colors used to display text associated with prompts, the information system, and exception handlers. You can choose between NoColor (black and white), Linux (default), and LightBG.</td>\n</tr>\n<tr>\n<td width=\"109\">%config</td>\n<td width=\"96\">Yes</td>\n<td width=\"327\">Enables you to configure IPython.</td>\n</tr>\n<tr>\n<td width=\"109\">%dhist</td>\n<td width=\"96\">Yes</td>\n<td width=\"327\">Displays a list of directories visited during the current session.</td>\n</tr>\n<tr>\n<td width=\"109\">%file</td>\n<td width=\"96\">No</td>\n<td width=\"327\">Outputs the name of the file that contains the source code for the object.</td>\n</tr>\n<tr>\n<td width=\"109\">%hist</td>\n<td width=\"96\">Yes</td>\n<td width=\"327\">Displays a list of magic function commands issued during the current session.</td>\n</tr>\n<tr>\n<td width=\"109\">%install_ext</td>\n<td width=\"96\">No</td>\n<td width=\"327\">Installs the specified extension.</td>\n</tr>\n<tr>\n<td width=\"109\">%load</td>\n<td width=\"96\">No</td>\n<td width=\"327\">Loads application code from another source, such as an online example.</td>\n</tr>\n<tr>\n<td width=\"109\">%load_ext</td>\n<td width=\"96\">No</td>\n<td width=\"327\">Loads a Python extension using its module name.</td>\n</tr>\n<tr>\n<td width=\"109\">%lsmagic</td>\n<td width=\"96\">Yes</td>\n<td width=\"327\">Displays a list of the currently available magic functions.</td>\n</tr>\n<tr>\n<td width=\"109\">%matplotlib</td>\n<td width=\"96\">Yes</td>\n<td width=\"327\">Sets the backend processor used for plots. Using the inline value displays the plot within the cell for an IPython Notebook file. The possible values are ‘gtk’, ‘gtk3’, ‘inline’, ‘nbagg’, ‘osx’, ‘qt’, ‘qt4’, ‘qt5’, ‘tk’, and ‘wx’.</td>\n</tr>\n<tr>\n<td width=\"109\">%paste</td>\n<td width=\"96\">No</td>\n<td width=\"327\">Pastes the content of the clipboard into the IPython environment.</td>\n</tr>\n<tr>\n<td width=\"109\">%pdef</td>\n<td width=\"96\">No</td>\n<td width=\"327\">Shows how to call the object (assuming that the object is callable).</td>\n</tr>\n<tr>\n<td width=\"109\">%pdoc</td>\n<td width=\"96\">No</td>\n<td width=\"327\">Displays the docstring for an object.</td>\n</tr>\n<tr>\n<td width=\"109\">%pinfo</td>\n<td width=\"96\">No</td>\n<td width=\"327\">Displays detailed information about the object (often more than provided by help alone).</td>\n</tr>\n<tr>\n<td width=\"109\">%pinfo2</td>\n<td width=\"96\">No</td>\n<td width=\"327\">Displays extra detailed information about the object (when available).</td>\n</tr>\n<tr>\n<td width=\"109\">%reload_ext</td>\n<td width=\"96\">No</td>\n<td width=\"327\">Reloads a previously installed extension.</td>\n</tr>\n<tr>\n<td width=\"109\">%source</td>\n<td width=\"96\">No</td>\n<td width=\"327\">Displays the source code for the object (assuming that the source is available).</td>\n</tr>\n<tr>\n<td width=\"109\">%timeit</td>\n<td width=\"96\">No</td>\n<td width=\"327\">Calculates the best performance time for an instruction.</td>\n</tr>\n<tr>\n<td width=\"109\">%unalias</td>\n<td width=\"96\">No</td>\n<td width=\"327\">Removes a previously created alias from the list.</td>\n</tr>\n<tr>\n<td width=\"109\">%unload_ext</td>\n<td width=\"96\">No</td>\n<td width=\"327\">Unloads the specified extension.</td>\n</tr>\n</tbody>\n</table>\n"},{"title":"Scikit-Learn method summary","thumb":null,"image":null,"content":"<p>Scikit-learn is a focal point for data science work with Python, so it pays to know which methods you need most. The following table provides a brief overview of the most important methods used for data analysis.</p>\n<table>\n<tbody>\n<tr>\n<td width=\"145\"><strong>Syntax</strong></td>\n<td width=\"132\"><strong>Usage</strong></td>\n<td width=\"255\"><strong>Description</strong></td>\n</tr>\n<tr>\n<td width=\"145\"><code>model_selection.cross_val_score</code></td>\n<td width=\"132\">Cross-validation phase</td>\n<td width=\"255\">Estimate the cross-validation score</td>\n</tr>\n<tr>\n<td width=\"145\"><code>model_selection.KFold</code></td>\n<td width=\"132\">Cross-validation phase</td>\n<td width=\"255\">Divide the dataset into k folds for cross validation</td>\n</tr>\n<tr>\n<td width=\"145\"><code>model_selection.StratifiedKFold</code></td>\n<td width=\"132\">Cross-validation phase</td>\n<td width=\"255\">Stratified validation that takes into account the distribution of the classes you predict</td>\n</tr>\n<tr>\n<td width=\"145\"><code>model_selection.train_test_split</code></td>\n<td width=\"132\">Cross-validation phase</td>\n<td width=\"255\">Split your data into training and test sets</td>\n</tr>\n<tr>\n<td width=\"145\"><code>decomposition.PCA</code></td>\n<td width=\"132\">Dimensionality reduction</td>\n<td width=\"255\">Principal component analysis (PCA)</td>\n</tr>\n<tr>\n<td width=\"145\"><code>decomposition.RandomizedPCA</code></td>\n<td width=\"132\">Dimensionality reduction</td>\n<td width=\"255\">Principal component analysis (PCA) using randomized SVD</td>\n</tr>\n<tr>\n<td width=\"145\"><code>feature_extraction.FeatureHasher</code></td>\n<td width=\"132\">Preparing your data</td>\n<td width=\"255\">The hashing trick, allowing you to accommodate a large number of features in your dataset</td>\n</tr>\n<tr>\n<td width=\"145\"><code>feature_extraction.text.CountVectorizer</code></td>\n<td width=\"132\">Preparing your data</td>\n<td width=\"255\">Convert text documents into a matrix of count data</td>\n</tr>\n<tr>\n<td width=\"145\"><code>feature_extraction.text.HashingVectorizer</code></td>\n<td width=\"132\">Preparing your data</td>\n<td width=\"255\">Directly convert your text using the hashing trick</td>\n</tr>\n<tr>\n<td width=\"145\"><code>feature_extraction.text.TfidfVectorizer</code></td>\n<td width=\"132\">Preparing your data</td>\n<td width=\"255\">Creates a dataset of TF-IDF features</td>\n</tr>\n<tr>\n<td width=\"145\"><code>feature_selection.RFECV</code></td>\n<td width=\"132\">Feature selection</td>\n<td width=\"255\">Automatic feature selection</td>\n</tr>\n<tr>\n<td width=\"145\"><code>model_selection.GridSearchCV</code></td>\n<td width=\"132\">Optimization</td>\n<td width=\"255\">Exhaustive search in order to maximize a machine learning algorithm</td>\n</tr>\n<tr>\n<td width=\"145\"><code>linear_model.LinearRegression</code></td>\n<td width=\"132\">Prediction</td>\n<td width=\"255\">Linear regression</td>\n</tr>\n<tr>\n<td width=\"145\"><code>linear_model.LogisticRegression</code></td>\n<td width=\"132\">Prediction</td>\n<td width=\"255\">Linear logistic regression</td>\n</tr>\n<tr>\n<td width=\"145\"><code>metrics.accuracy_score</code></td>\n<td width=\"132\">Solution evaluation</td>\n<td width=\"255\">Accuracy classification score</td>\n</tr>\n<tr>\n<td width=\"145\"><code>metrics.f1_score</code></td>\n<td width=\"132\">Solution evaluation</td>\n<td width=\"255\">Compute the F1 score, balancing accuracy and recall</td>\n</tr>\n<tr>\n<td width=\"145\"><code>metrics.mean_absolute_error</code></td>\n<td width=\"132\">Solution evaluation</td>\n<td width=\"255\">Mean absolute error regression error</td>\n</tr>\n<tr>\n<td width=\"145\"><code>metrics.mean_squared_error</code></td>\n<td width=\"132\">Solution evaluation</td>\n<td width=\"255\">Mean squared error regression error</td>\n</tr>\n<tr>\n<td width=\"145\"><code>metrics.roc_auc_score</code></td>\n<td width=\"132\">Solution evaluation</td>\n<td width=\"255\">Compute Area Under the Curve (AUC) from prediction scores</td>\n</tr>\n<tr>\n<td width=\"145\"><code>naive_bayes.MultinomialNB</code></td>\n<td width=\"132\">Prediction</td>\n<td width=\"255\">Multinomial Naïve Bayes</td>\n</tr>\n<tr>\n<td width=\"145\"><code>neighbors.KNeighborsClassifier</code></td>\n<td width=\"132\">Prediction</td>\n<td width=\"255\">K-Neighbors classification</td>\n</tr>\n<tr>\n<td width=\"145\"><code>preprocessing.Binarizer</code></td>\n<td width=\"132\">Preparing your data</td>\n<td width=\"255\">Create binary variables (feature values to 0 or 1)</td>\n</tr>\n<tr>\n<td width=\"145\"><code>preprocessing.Imputer</code></td>\n<td width=\"132\">Preparing your data</td>\n<td width=\"255\">Missing values imputation</td>\n</tr>\n<tr>\n<td width=\"145\"><code>preprocessing.MinMaxScaler</code></td>\n<td width=\"132\">Preparing your data</td>\n<td width=\"255\">Create variables bound by a minimum and maximum value</td>\n</tr>\n<tr>\n<td width=\"145\"><code>preprocessing.OneHotEncoder</code></td>\n<td width=\"132\">Preparing your data</td>\n<td width=\"255\">Transform categorical integer features into binary ones</td>\n</tr>\n<tr>\n<td width=\"145\"><code>preprocessing.StandardScaler</code></td>\n<td width=\"132\">Preparing your data</td>\n<td width=\"255\">Variable standardization by removing the mean and scaling to unit variance</td>\n</tr>\n</tbody>\n</table>\n"}],"videoInfo":{"videoId":null,"name":null,"accountId":null,"playerId":null,"thumbnailUrl":null,"description":null,"uploadDate":null}},"sponsorship":{"sponsorshipPage":false,"backgroundImage":{"src":null,"width":0,"height":0},"brandingLine":"","brandingLink":"","brandingLogo":{"src":null,"width":0,"height":0}},"primaryLearningPath":"Advance","lifeExpectancy":"One year","lifeExpectancySetFrom":"2022-02-24T00:00:00+00:00","dummiesForKids":"no","sponsoredContent":"no","adInfo":"","adPairKey":[]},"status":"publish","visibility":"public","articleId":207489},{"headers":{"creationTime":"2016-03-27T16:46:44+00:00","modifiedTime":"2022-02-24T15:08:04+00:00","timestamp":"2022-02-24T17:07:38+00:00"},"data":{"breadcrumbs":[{"name":"Technology","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33512"},"slug":"technology","categoryId":33512},{"name":"Programming & Web Design","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33592"},"slug":"programming-web-design","categoryId":33592},{"name":"Python","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33606"},"slug":"python","categoryId":33606}],"title":"Python For Kids For Dummies Cheat Sheet","strippedTitle":"python for kids for dummies cheat sheet","slug":"python-for-kids-for-dummies-cheat-sheet","canonicalUrl":"","seo":{"metaDescription":"Keep this Cheat Sheet handy as you're learning Python. It includes keyword use, Tkinter widgets, and how to get help with your math homework.","noIndex":0,"noFollow":0},"content":"Python coding helps you with things you do every day, like math homework. Python programming can also help with things like making web pages: Thank goodness for widgets and keywords!","description":"Python coding helps you with things you do every day, like math homework. Python programming can also help with things like making web pages: Thank goodness for widgets and keywords!","blurb":"","authors":[{"authorId":9026,"name":"Brendan Scott","slug":"brendan-scott","description":"Brendan Scott is a dad who loves Python and wants kids to experience its magic, too. He started python4kids.brendanscott.com to teach his oldest child to code. He maintains it to help other young people learn Python. ","_links":{"self":"https://dummies-api.dummies.com/v2/authors/9026"}}],"primaryCategoryTaxonomy":{"categoryId":33606,"title":"Python","slug":"python","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33606"}},"secondaryCategoryTaxonomy":{"categoryId":0,"title":null,"slug":null,"_links":null},"tertiaryCategoryTaxonomy":{"categoryId":0,"title":null,"slug":null,"_links":null},"trendingArticles":null,"inThisArticle":[],"relatedArticles":{"fromBook":[{"articleId":141581,"title":"Use Python to Help with Your Math Homework","slug":"use-python-to-help-with-your-math-homework","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/141581"}},{"articleId":141474,"title":"Python 2.7 Keyword Subset and Examples","slug":"python-2-7-keyword-subset-and-examples","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/141474"}},{"articleId":141443,"title":"Using Tkinter Widgets in Python","slug":"using-tkinter-widgets-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/141443"}},{"articleId":139551,"title":"How to Interrupt a Program in Python","slug":"how-to-interrupt-a-program-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/139551"}},{"articleId":139550,"title":"How to Name Functions in Python","slug":"how-to-name-functions-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/139550"}}],"fromCategory":[{"articleId":264919,"title":"How to Define and Use Python Lists","slug":"how-to-define-and-use-python-lists","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264919"}},{"articleId":264911,"title":"How to Use Lambda Functions in Python","slug":"how-to-use-lambda-functions-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264911"}},{"articleId":264906,"title":"Your Guide to the Python Standard Library","slug":"your-guide-to-the-python-standard-library","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264906"}},{"articleId":264894,"title":"A Beginner’s Guide to Python Versions","slug":"a-beginners-guide-to-python-versions","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264894"}},{"articleId":264888,"title":"How to Build a Simple Neural Network in Python","slug":"how-to-build-a-simple-neural-network-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264888"}}]},"hasRelatedBookFromSearch":false,"relatedBook":{"bookId":281835,"slug":"python-for-kids-for-dummies","isbn":"9781119093107","categoryList":["technology","programming-web-design","python"],"amazon":{"default":"https://www.amazon.com/gp/product/1119093104/ref=as_li_tl?ie=UTF8&tag=wiley01-20","ca":"https://www.amazon.ca/gp/product/1119093104/ref=as_li_tl?ie=UTF8&tag=wiley01-20","indigo_ca":"http://www.tkqlhce.com/click-9208661-13710633?url=https://www.chapters.indigo.ca/en-ca/books/product/1119093104-item.html&cjsku=978111945484","gb":"https://www.amazon.co.uk/gp/product/1119093104/ref=as_li_tl?ie=UTF8&tag=wiley01-20","de":"https://www.amazon.de/gp/product/1119093104/ref=as_li_tl?ie=UTF8&tag=wiley01-20"},"image":{"src":"https://www.dummies.com/wp-content/uploads/python-for-kids-for-dummies-cover-9781119093107-203x255.jpg","width":203,"height":255},"title":"Python For Kids For Dummies","testBankPinActivationLink":"","bookOutOfPrint":false,"authorsInfo":"\n <p>Brendan Scott is a dad who loves Python and wants kids to get some of its magic too. He started pythonforkids.brendanscott.com to help teach his oldest child to code. He maintains it to help other young people learn Python.</p> ","authors":[{"authorId":9026,"name":"Brendan Scott","slug":"brendan-scott","description":"Brendan Scott is a dad who loves Python and wants kids to experience its magic, too. He started python4kids.brendanscott.com to teach his oldest child to code. He maintains it to help other young people learn Python. ","_links":{"self":"https://dummies-api.dummies.com/v2/authors/9026"}}],"_links":{"self":"https://dummies-api.dummies.com/v2/books/"}},"collections":[],"articleAds":{"footerAd":"<div class=\"du-ad-region row\" id=\"article_page_adhesion_ad\"><div class=\"du-ad-unit col-md-12\" data-slot-id=\"article_page_adhesion_ad\" data-refreshed=\"false\" \r\n data-target = \"[{&quot;key&quot;:&quot;cat&quot;,&quot;values&quot;:[&quot;technology&quot;,&quot;programming-web-design&quot;,&quot;python&quot;]},{&quot;key&quot;:&quot;isbn&quot;,&quot;values&quot;:[&quot;9781119093107&quot;]}]\" id=\"du-slot-6217bb5a78bd7\"></div></div>","rightAd":"<div class=\"du-ad-region row\" id=\"article_page_right_ad\"><div class=\"du-ad-unit col-md-12\" data-slot-id=\"article_page_right_ad\" data-refreshed=\"false\" \r\n data-target = \"[{&quot;key&quot;:&quot;cat&quot;,&quot;values&quot;:[&quot;technology&quot;,&quot;programming-web-design&quot;,&quot;python&quot;]},{&quot;key&quot;:&quot;isbn&quot;,&quot;values&quot;:[&quot;9781119093107&quot;]}]\" id=\"du-slot-6217bb5a7955f\"></div></div>"},"articleType":{"articleType":"Cheat Sheet","articleList":[{"articleId":141474,"title":"Python 2.7 Keyword Subset and Examples","slug":"python-2-7-keyword-subset-and-examples","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/141474"}},{"articleId":141581,"title":"Use Python to Help with Your Math Homework","slug":"use-python-to-help-with-your-math-homework","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/141581"}},{"articleId":141443,"title":"Using Tkinter Widgets in Python","slug":"using-tkinter-widgets-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/141443"}}],"content":[{"title":"Python 2.7 keyword subset and examples","thumb":null,"image":null,"content":"<p>Programming is an important skill. Python will serve you well for years to come. The tables here give you the core words, built-ins, standard library functions, and operators that you&#8217;ll use most when you&#8217;re coding with Python.</p>\n<table border=\"0\">\n<caption>Python Core Words</caption>\n<tbody>\n<tr>\n<th>Keyword</th>\n<th>Summary</th>\n<th>Example</th>\n</tr>\n<tr>\n<td>and</td>\n<td>Logical operator to test whether two things are both <span class=\"code\">True</span>.</td>\n<td><span class=\"code\"><em>&lt;conditional expression&gt; </em><span style=\"font-family: Verdana;\">and</span></span><br />\n<span class=\"code\"><em>&lt;conditional expression&gt;</em></span><br />\n<span class=\"code\">x&gt;2 and x&lt;10</span></td>\n</tr>\n<tr>\n<td>as</td>\n<td>Assign a file object to a variable. Used with <span class=\"code\">with</span>.<br />\nLet your code refer to a module under a different name (also called an <em>alias</em>). Used with <span class=\"code\">import</span>.</td>\n<td><span class=\"code\">with open(&lt;</span><span class=\"code\"><em>name of file</em></span><span class=\"code\">&gt;,&lt;</span><span class=\"code\"><em>file mode</em></span><span class=\"code\">&gt;) as &lt;</span><span class=\"code\"><em>object name</em></span><span class=\"code\">&gt;:</span><span class=\"code\"><br />\n</span><span class=\"code\">import cPickle as pickle</span></td>\n</tr>\n<tr>\n<td>break</td>\n<td>Stop execution of a loop.</td>\n<td><span class=\"code\">for i in range(10):</span><br />\n<span class=\"code\">    if i%2 ==0:</span><br />\n<span class=\"code\">        break</span></td>\n</tr>\n<tr>\n<td>class</td>\n<td>Define a custom object.</td>\n<td><span class=\"code\">class &lt;</span><span class=\"code\"><em>name of class</em></span><span class=\"code\">&gt;(object):</span><span class=\"code\"><br />\n</span><span class=\"code\">    &#8220;&#8221;<em>Your docstring</em>&#8220;&#8221;</span><br />\n<span class=\"code\">class MyClass(object):</span><span class=\"code\"><br />\n</span><span class=\"code\">    &#8220;&#8221;A cool function.&#8221;&#8221;</span></td>\n</tr>\n<tr>\n<td>continue</td>\n<td>Skip balance of loop and begin a new iteration.</td>\n<td><span class=\"code\">for i in range(10):</span><br />\n<span class=\"code\">    if i%2 ==0:</span><br />\n<span class=\"code\">        continue</span></td>\n</tr>\n<tr>\n<td>def</td>\n<td>Define a function.</td>\n<td><span class=\"code\">def &lt;</span><span class=\"code\"><em>name of function</em></span><span class=\"code\">&gt;(&lt;argument list&gt;):</span><span class=\"code\"><br />\n</span><span class=\"code\">    &#8220;&#8221;<em>Your docstring</em>&#8220;&#8221;</span> <span class=\"code\"><br />\n</span><span class=\"code\">def my_function():</span><span class=\"code\"><br />\n</span><span class=\"code\">    &#8220;&#8221;This does&#8230; &#8220;&#8221;</span></td>\n</tr>\n<tr>\n<td>elif</td>\n<td>Add conditional test to an <span class=\"code\">if</span> clause.</td>\n<td>See <span class=\"code\">if</span>.</td>\n</tr>\n<tr>\n<td>else</td>\n<td>Add an alternative code block.</td>\n<td>See <span class=\"code\">if</span>.</td>\n</tr>\n<tr>\n<td>for</td>\n<td>Create a loop which iterates through elements of a list (or other iterable).</td>\n<td><span class=\"code\">for &lt;</span><span class=\"code\"><em>dummy variable name</em></span><span class=\"code\">&gt; in &lt;</span><span class=\"code\"><em>sequence</em></span><span class=\"code\">&gt;:</span><span class=\"code\"><br />\n</span><span class=\"code\">for i in range(10):</span></td>\n</tr>\n<tr>\n<td>from</td>\n<td>Import specific functions from a module without importing the whole module.</td>\n<td><span class=\"code\">from &lt;</span><span class=\"code\"><em>module name</em></span><span class=\"code\">&gt; import &lt;</span><span class=\"code\"><em>name of function or object</em></span><span class=\"code\">&gt;</span><span class=\"code\"><br />\n</span><span class=\"code\">from random import randint</span></td>\n</tr>\n<tr>\n<td>global</td>\n<td>Make a variable global in scope. (If a variable is defined in the main section, you can change its value within a function.)</td>\n<td><span class=\"code\">global x</span></td>\n</tr>\n<tr>\n<td>if</td>\n<td>Create a condition. If the condition is <span class=\"code\">True</span>, the associated code block is executed. Otherwise, any <span class=\"code\">elif</span> commands are processed. If there are none, or none are satisfied, execute the <span class=\"code\">else</span> block if there is one.</td>\n<td><span class=\"code\">if <em>&lt;conditional expression&gt;</em>:</span><br />\n<span class=\"code\"><em>   &lt;code block&gt;</em></span><br />\n<span class=\"code\">[elif &lt;conditional expression&gt;:</span><br />\n<span class=\"code\">    &lt;code block&gt;, &#8230;]</span><br />\n<span class=\"code\">[else:</span><br />\n<span class=\"code\">    &lt;code block&gt;]</span><br />\n<span class=\"code\">if x == 1:</span><br />\n<span class=\"code\">    print(&#8220;x is 1&#8221;)</span><br />\n<span class=\"code\">elif x == 2:</span><br />\n<span class=\"code\">    print(&#8220;x is 2&#8221;)</span><br />\n<span class=\"code\">elif x &gt; 3:</span><br />\n<span class=\"code\">    print(&#8220;x is greater than 3&#8221;)</span><br />\n<span class=\"code\">else</span><br />\n<span class=\"code\">    print(&#8220;x is not greater than 3, nor is it 1 one or 2&#8221;)</span></td>\n</tr>\n<tr>\n<td>import</td>\n<td>Use code defined in another file without retyping it.</td>\n<td><span class=\"code\">import &lt;</span><span class=\"code\"><em>name of module</em></span><span class=\"code\">&gt;</span><span class=\"code\"><br />\n</span><span class=\"code\">import random</span></td>\n</tr>\n<tr>\n<td>in</td>\n<td>Used to test whether a given value is one of the elements of an object.</td>\n<td><span class=\"code\">1 in range(10)</span></td>\n</tr>\n<tr>\n<td>is</td>\n<td>Used to test whether names reference the same object.</td>\n<td><span class=\"code\">x = None</span><br />\n<span class=\"code\">x is None # faster than</span><br />\n<span class=\"code\">x == None</span></td>\n</tr>\n<tr>\n<td>lambda</td>\n<td>Shorthand function definition. Usually used where a function needs to be passed as an argument to another function.</td>\n<td><span class=\"code\">lamda <em>&lt;dummy variables&gt;</em>:</span><br />\n<span class=\"code\"><em>&lt;expression using dummy variables&gt;</em></span><br />\n<span class=\"code\">times = lambda x, y: x*y</span><br />\n<span class=\"code\">command=lambda x: self.draw_line(self.control_points)</span></td>\n</tr>\n<tr>\n<td>not</td>\n<td>Logical negation, used to negate a logical condition. Don&#8217;t use for testing greater than, less than, or equal.</td>\n<td><span class=\"code\">10 not in range(10)</span></td>\n</tr>\n<tr>\n<td>or</td>\n<td>Logical operator to test whether at least one of two things is <span class=\"code\">True</span>.</td>\n<td><span class=\"code\"><em>&lt;conditional expression&gt;</em> <span style=\"font-family: Verdana;\">or</span></span><br />\n<span class=\"code\"><em>&lt;conditional expression&gt;</em></span><br />\n<span class=\"code\">x&lt;2 or x&gt;10</span></td>\n</tr>\n<tr>\n<td>pass</td>\n<td>Placeholder keyword. Does nothing but stop Python complaining that a code block is empty.</td>\n<td><span class=\"code\">for i in range (10):</span><span class=\"code\"><br />\n</span><span class=\"code\">    pass</span></td>\n</tr>\n<tr>\n<td>print</td>\n<td>Output text to a terminal.</td>\n<td><span class=\"code\">print(</span><span class=\"code\">&#8220;</span><span class=\"code\">Hello World!</span><span class=\"code\">&#8220;</span><span class=\"code\">)</span></td>\n</tr>\n<tr>\n<td>return</td>\n<td>Return from the execution of a function. If a value is specified, return that value, otherwise return <span class=\"code\">None</span>.</td>\n<td><span class=\"code\">return &lt;value or expression&gt;</span><span class=\"code\"><br />\n</span><span class=\"code\">return x+2</span></td>\n</tr>\n<tr>\n<td>while</td>\n<td>Execute a code block while the associated condition is <span class=\"code\">True</span>.</td>\n<td><span class=\"code\">while &lt;conditional expression&gt;:</span><br />\n<span class=\"code\">while True:</span><br />\n<span class=\"code\">    pass</span></td>\n</tr>\n<tr>\n<td>with</td>\n<td>Get Python to manage a resource (like a file) for you.</td>\n<td><span class=\"code\">with open(&lt;name of file&gt;,&lt;file mode&gt;) as &lt;object name&gt;:</span></td>\n</tr>\n</tbody>\n</table>\n<p>Extend Python&#8217;s core functionality with these built-ins.</p>\n<table border=\"0\">\n<caption>Python Built-ins</caption>\n<tbody>\n<tr>\n<th>Built-in</th>\n<th>Notes</th>\n<th>Example</th>\n</tr>\n<tr>\n<td>False</td>\n<td>Value, returned by a logical operation or directly assigned.</td>\n<td><span class=\"code\">ok_to_continue = False</span><br />\n<span class=\"code\">age = 16</span><br />\n<span class=\"code\">old_enough = age &gt;=21</span><br />\n(evaluates comparison <span class=\"code\">age&gt;=21</span><br />\nand assigns the result to <span class=\"code\">old_enough)</span></td>\n</tr>\n<tr>\n<td>None</td>\n<td>Value used when representing the absence of a value or to initialise a variable which will be changed later. Returned by functions which do not explicitly return a value.</td>\n<td><span class=\"code\">x = None</span></td>\n</tr>\n<tr>\n<td>True</td>\n<td>Value, returned by a logical operation.</td>\n<td><span class=\"code\">ok_to_continue = True</span><br />\n<span class=\"code\">age = 16</span><br />\n<span class=\"code\">old_enough = age &gt;=21</span><br />\n(evaluates comparison <span class=\"code\">age&gt;=21</span><br />\nand assigns the result to <span class=\"code\">old_enough)</span></td>\n</tr>\n<tr>\n<td>__name__</td>\n<td>Constant, shows module name. If it&#8217;s not <span class=\"code\">&#8220;</span><span class=\"code\">__</span><span class=\"code\">main</span><span class=\"code\">__</span><span class=\"code\">&#8220;</span>, the code is being used in an import.</td>\n<td><span class=\"code\">if __name__==</span><span class=\"code\">&#8220;</span><span class=\"code\">__main__</span><span class=\"code\">&#8220;</span><span class=\"code\">:</span></td>\n</tr>\n<tr>\n<td>dir</td>\n<td>List attributes of an item.</td>\n<td><span class=\"code\">dir(&lt;</span><span class=\"code\"><em>object name</em></span><span class=\"code\">&gt;)</span></td>\n</tr>\n<tr>\n<td>enumerate</td>\n<td>Iterate through a sequence and number each item.</td>\n<td><span class=\"code\">enumerate(</span><span class=\"code\">&#8216;</span><span class=\"code\">Hello</span><span class=\"code\">&#8216;</span><span class=\"code\">)</span></td>\n</tr>\n<tr>\n<td>exit</td>\n<td>Exit Python (Command Line) interpreter.</td>\n<td><span class=\"code\">exit()</span></td>\n</tr>\n<tr>\n<td>float</td>\n<td>Convert a number into a decimal, usually so that division works properly.</td>\n<td><span class=\"code\">1/float(2)</span></td>\n</tr>\n<tr>\n<td>getattr</td>\n<td>Get an attribute of an object by a name. Useful for introspection.</td>\n<td><span class=\"code\">getattr(&lt;</span><span class=\"code\"><em>name of object</em></span><span class=\"code\">&gt;, &lt;</span><span class=\"code\"><em>name of attribute</em></span><span class=\"code\">&gt;)</span></td>\n</tr>\n<tr>\n<td>help</td>\n<td>Get Python docstring on object.</td>\n<td><span class=\"code\">help(&lt;</span><span class=\"code\"><em>name of object</em></span><span class=\"code\">&gt;)</span><span class=\"code\"><br />\n</span><span class=\"code\">help(getattr)</span></td>\n</tr>\n<tr>\n<td>id</td>\n<td>Show the location in the computer&#8217;s RAM where an object is stored.</td>\n<td><span class=\"code\">id(&lt;</span><span class=\"code\"><em>name of object</em></span><span class=\"code\">&gt;)</span><span class=\"code\"><br />\n</span><span class=\"code\">id(help)</span></td>\n</tr>\n<tr>\n<td>int</td>\n<td>Convert a string into an integer number.</td>\n<td><span class=\"code\">int(</span><span class=\"code\">&#8216;</span><span class=\"code\">0</span><span class=\"code\">&#8216;</span><span class=\"code\">)</span></td>\n</tr>\n<tr>\n<td>len</td>\n<td>Get the number of elements in a sequence.</td>\n<td><span class=\"code\">len([0,1])</span></td>\n</tr>\n<tr>\n<td>object</td>\n<td>A base on which other classes can inherit from.</td>\n<td><span class=\"code\">class CustomObject(object):</span></td>\n</tr>\n<tr>\n<td>open</td>\n<td>Open a file on disk, return a file object.</td>\n<td><span class=\"code\">open(&lt;path to file&gt;, &lt;mode&gt;)</span><br />\n<span class=\"code\">open(&#8216;mydatafile.txt&#8217;, &#8216;r&#8217;) # read</span><br />\n(opens a file to read data from)<br />\n<span class=\"code\">open(&#8216;mydatafile.txt&#8217;, &#8216;w&#8217;) # write</span><br />\n(creates a new file to write to, destroys any existing file with the same name)<br />\n<span class=\"code\">open(&#8216;mydatafile.txt&#8217;, &#8216;a&#8217;) # append</span><br />\n(adds to an existing file if any, or creates<br />\na new one if none existing already)</td>\n</tr>\n<tr>\n<td>print</td>\n<td>Reimplementation of <span class=\"code\">print</span> keyword, but as a function.<br />\nNeed to import from the future to use it (srsly!)</td>\n<td><span class=\"code\">from future import print_function</span><br />\n<span class=\"code\">print (&#8216;Hello World!</span><span class=\"code\">&#8216;</span><span class=\"code\">)</span></td>\n</tr>\n<tr>\n<td>range</td>\n<td>Gives numbers between the lower and upper limits specified (including the lower, but excluding the upper limit). A step may be specified.</td>\n<td><span class=\"code\">range(10)</span><span class=\"code\"><br />\n</span><span class=\"code\">range(5,10)</span><span class=\"code\"><br />\n</span><span class=\"code\">range(1,10,2)</span></td>\n</tr>\n<tr>\n<td>raw_input</td>\n<td>Get some text as a string from the user, with an optional prompt.</td>\n<td><span class=\"code\">prompt =</span> <span class=\"code\">&#8216;</span><span class=\"code\">What is your guess?</span> <span class=\"code\">&#8216;</span><span class=\"code\"><br />\nplayers_guess = raw_input(prompt)</span></td>\n</tr>\n<tr>\n<td>str</td>\n<td>Convert an object (usually a number) into a string (usually for printing).</td>\n<td><span class=\"code\">str(0)</span></td>\n</tr>\n<tr>\n<td>type</td>\n<td>Give the type of the specified object.</td>\n<td><span class=\"code\">type(0)<br />\ntype(</span><span class=\"code\">&#8216;</span><span class=\"code\">0</span><span class=\"code\">&#8216;</span><span class=\"code\">)<br />\ntype([])<br />\ntype({})<br />\ntype(())</span></td>\n</tr>\n</tbody>\n</table>\n<p>Use the work that others have already done. Try these modules from the Python standard library.</p>\n<table border=\"0\">\n<caption>Selected Functions from the Standard Library</caption>\n<tbody>\n<tr>\n<th>Module</th>\n<th>What It Does</th>\n<th>Sample Functions/Objects</th>\n</tr>\n<tr>\n<td>os.path</td>\n<td>Functions relating to files and file paths.</td>\n<td><span class=\"code\">os.path.exists(&lt;path to file&gt;)</span></td>\n</tr>\n<tr>\n<td>pickle, cPickle</td>\n<td>Save and load objects to/from a file.</td>\n<td><span class=\"code\">pickle.load(&lt;file object to load from&gt;),</span> <span class=\"code\">pickle.dump(&lt;object to dump&gt;, &lt;file object to save to&gt;)</span></td>\n</tr>\n<tr>\n<td>random</td>\n<td>Various functions relating to random numbers.</td>\n<td><span class=\"code\">random.choice(&lt;sequence to choose from&gt;), random.randint(&lt;lower limit&gt;, &lt;upper limit&gt;), random.shuffle(&lt;name of list to shuffle&gt;)</span></td>\n</tr>\n<tr>\n<td>String</td>\n<td>Stuff relating to strings.</td>\n<td><span class=\"code\">string.printable</span></td>\n</tr>\n<tr>\n<td>sys</td>\n<td>Various functions related to your computer system.</td>\n<td><span class=\"code\">sys.exit()</span></td>\n</tr>\n<tr>\n<td>Time</td>\n<td>Time-related functions.</td>\n<td><span class=\"code\">time.time()</span></td>\n</tr>\n<tr>\n<td>Tkinter</td>\n<td>User interface widgets and associated constants.</td>\n<td><span class=\"code\">Tkinter.ALL</span><br />\n<span class=\"code\">Tkinter.BOTH</span><br />\n<span class=\"code\">Tkinter.CENTER</span><br />\n<span class=\"code\">Tkinter.END</span><br />\n<span class=\"code\">Tkinter.HORIZONTAL</span><br />\n<span class=\"code\">Tkinter.LEFT</span><br />\n<span class=\"code\">Tkinter.NW</span><br />\n<span class=\"code\">Tkinter.RIGHT</span><br />\n<span class=\"code\">Tkinter.TOP</span><br />\n<span class=\"code\">Tkinter.Y</span><br />\n<span class=\"code\">Tkinter.Button(&lt;parent widget&gt;,</span><br />\n<span class=\"code\">text=&lt;button text&gt;)</span><br />\n<span class=\"code\">Tkinter.Canvas(&lt;parent widget&gt;,</span><br />\n<span class=\"code\">    width=&lt;width&gt;, height=&lt;height&gt;)</span><br />\n<span class=\"code\">Tkinter.Checkbutton(&lt;parent widget&gt;,</span><br />\n<span class=\"code\">    text=&lt;checkbutton text&gt;)</span><br />\n<span class=\"code\">Tkinter.Entry(&lt;parent widget&gt;,</span><br />\n<span class=\"code\">    width=&lt;number of characters wide&gt;),</span><br />\n<span class=\"code\">Tkinter.Frame(&lt;parent widget&gt;)</span><br />\n<span class=\"code\">Tkinter.IntVar()</span><br />\n<span class=\"code\">Tkinter.Label(&lt;parent widget&gt;,</span><br />\n<span class=\"code\">    text = &lt;label text&gt;)</span><br />\n<span class=\"code\">Tkinter.mainloop()</span><br />\n<span class=\"code\">Tkinter.Menu(&lt;parent widget&gt;)</span><br />\n<span class=\"code\">Tkinter.OptionMenu(&lt;parent widget&gt;,</span><br />\n<span class=\"code\">    None, None)</span><br />\n<span class=\"code\">Tkinter.Scale(&lt;parent widget&gt;,</span><br />\n<span class=\"code\">    from_=&lt;lower limit&gt;,</span><br />\n<span class=\"code\">    to=&lt;upper limit&gt;)</span><br />\n<span class=\"code\">Tkinter.Scrollbar(&lt;parent widget&gt;)</span><br />\n<span class=\"code\">Tkinter.StringVar()</span><br />\n<span class=\"code\">Tkinter.Tk()</span></td>\n</tr>\n</tbody>\n</table>\n<p>Add, subtract, divide, multiply, and more using these operators.</p>\n<table border=\"0\">\n<caption>Python Operators</caption>\n<tbody>\n<tr>\n<th>Operator</th>\n<th>Name</th>\n<th>Effect</th>\n<th>Examples</th>\n</tr>\n<tr>\n<td>+</td>\n<td>Plus</td>\n<td>Add two numbers.<br />\nJoin two strings together.</td>\n<td>Add: <span class=\"code\">&gt;&gt;&gt; 1+1</span><span class=\"code\"><br />\n</span><span class=\"code\">2</span><span class=\"code\"><br />\n</span>Join: <span class=\"code\">&gt;&gt;&gt;</span> <span class=\"code\">&#8216;</span><span class=\"code\">a</span><span class=\"code\">&#8216;</span><span class=\"code\">+</span><span class=\"code\">&#8216;</span><span class=\"code\">b</span><span class=\"code\">&#8216;</span><span class=\"code\"><br />\n</span><span class=\"code\">&#8216;</span><span class=\"code\">ab</span><span class=\"code\">&#8216;</span></td>\n</tr>\n<tr>\n<td>–</td>\n<td>Minus</td>\n<td>Subtract a number from another.<br />\nCan&#8217;t use for strings.</td>\n<td><span class=\"code\">&gt;&gt;&gt; 1-1</span><span class=\"code\"><br />\n</span><span class=\"code\">0</span></td>\n</tr>\n<tr>\n<td>*</td>\n<td>Times</td>\n<td>Multiply two numbers.<br />\nMake copies of a string.</td>\n<td>Multiply: <span class=\"code\">&gt;&gt;&gt; 2*2</span><span class=\"code\"><br />\n</span><span class=\"code\">4</span><span class=\"code\"><br />\n</span>Copy: <span class=\"code\">&gt;&gt;&gt;</span> <span class=\"code\">&#8216;</span><span class=\"code\">a</span><span class=\"code\">&#8216;</span><span class=\"code\">*2</span><span class=\"code\"><br />\n</span><span class=\"code\">&#8216;</span><span class=\"code\">aa</span><span class=\"code\">&#8216;</span></td>\n</tr>\n<tr>\n<td>/</td>\n<td>Divide</td>\n<td>Divide one number by another.<br />\nCan&#8217;t use for strings.</td>\n<td>1/2 # integer division:<br />\nAnswer will be rounded down.<br />\n1/2.0 # decimal division<br />\n1/float(2) # decimal division</td>\n</tr>\n<tr>\n<td>%</td>\n<td>Remainder (Modulo)</td>\n<td>Give the remainder when dividing the left number by the right number.<br />\nFormatting operator for strings.</td>\n<td><span class=\"code\">&gt;&gt;&gt; 10%3</span><br />\n<span class=\"code\">1</span></td>\n</tr>\n<tr>\n<td>**</td>\n<td>Power</td>\n<td><span class=\"code\">x**y</span> means raise <span class=\"code\">x</span> to the power of <span class=\"code\">y</span>.<br />\nCan&#8217;t use for strings.</td>\n<td><span class=\"code\">&gt;&gt;&gt; 3**2</span><span class=\"code\"><br />\n</span><span class=\"code\">9</span></td>\n</tr>\n<tr>\n<td>=</td>\n<td>Assignment</td>\n<td>Assign the value on the right to the variable on the left.</td>\n<td><span class=\"code\">&gt;&gt;&gt; a = 1</span></td>\n</tr>\n<tr>\n<td>==</td>\n<td>Equality</td>\n<td>Is the left side equal to the right side? Is <span class=\"code\">True</span> if so; is <span class=\"code\">False</span> otherwise.</td>\n<td><span class=\"code\">&gt;&gt;&gt; 1 == 1</span><br />\n<span class=\"code\">True</span><br />\n<span class=\"code\">&gt;&gt;&gt; &#8216;a&#8217; == &#8216;a&#8217;</span><br />\n<span class=\"code\">True</span></td>\n</tr>\n<tr>\n<td>!=</td>\n<td>Not equal</td>\n<td>Is the left side <em>not</em> equal to the right side? Is <span class=\"code\">True</span> if so; is False otherwise.</td>\n<td><span class=\"code\">&gt;&gt;&gt; 1 != 1</span><br />\n<span class=\"code\">False</span><br />\n<span class=\"code\">&gt;&gt;&gt; 1 != 2</span><br />\n<span class=\"code\">True</span><br />\n<span class=\"code\">&gt;&gt;&gt; &#8216;a&#8217; != &#8216;a&#8217;</span><br />\n<span class=\"code\">True</span></td>\n</tr>\n<tr>\n<td>&gt;</td>\n<td>Greater than</td>\n<td>Is the left side greater than the right side?<br />\n<span class=\"code\">&gt;=</span> means greater than or equal to</td>\n<td><span class=\"code\">&gt;&gt;&gt; 2 &gt; 1</span><span class=\"code\"><br />\n</span><span class=\"code\">True</span></td>\n</tr>\n<tr>\n<td>&lt;</td>\n<td>Less than</td>\n<td>Is the left side less than the right side?<br />\n<span class=\"code\">&lt;=</span> means less than or equal to</td>\n<td><span class=\"code\">&gt;&gt;&gt; 1 &lt; 2</span><span class=\"code\"><br />\n</span><span class=\"code\">True</span></td>\n</tr>\n<tr>\n<td>&amp; (or and)</td>\n<td>And</td>\n<td>Are both left and right <span class=\"code\">True</span>?<br />\nTypically used for complex conditions where you want to do something if everything is <span class=\"code\">True</span>:<br />\n<span class=\"code\">while im_hungry and you_have_food:</span></td>\n<td><span class=\"code\">&gt;&gt;&gt; True &amp; True</span><br />\n<span class=\"code\">True</span><br />\n<span class=\"code\">&gt;&gt;&gt; True and False</span><br />\n<span class=\"code\">False   </span><br />\n<span class=\"code\">&gt;&gt;&gt; True &amp; (1 == 2)</span><br />\n<span class=\"code\">False</span></td>\n</tr>\n<tr>\n<td>| (or or)</td>\n<td>Or</td>\n<td>Is either left or right <span class=\"code\">True</span>?<br />\nTypically used for complex conditions where you want at least one thing to be <span class=\"code\">True</span>:<br />\n<span class=\"code\">while im_bored or youre_bored:</span></td>\n<td><span class=\"code\">&gt;&gt;&gt; True | False</span><br />\n<span class=\"code\">True</span><br />\n<span class=\"code\">&gt;&gt;&gt; True or False</span><br />\n<span class=\"code\">True</span><br />\n<span class=\"code\">&gt;&gt;&gt; False | False</span><br />\n<span class=\"code\">False</span><br />\n<span class=\"code\">&gt;&gt;&gt; (1 == 1) | False</span><br />\n<span class=\"code\">True</span></td>\n</tr>\n</tbody>\n</table>\n"},{"title":"Use Python to help with your math homework","thumb":null,"image":null,"content":"<p>Python can do fractions so you can check your homework answers. Use the <span class=\"code\">fractions</span> module, and its <span class=\"code\">Fraction</span> object, specifying the numerator and denominator. To get one-half, type <span class=\"code\">fractions.Fraction(1, 2)</span>. For four-fifths, type <span class=\"code\">fractions.Fraction(4, 5)</span>:</p>\n<pre class=\"code\">&gt;&gt;&gt; import fractions\r\n&gt;&gt;&gt; one_half = fractions.Fraction(1, 2)\r\n&gt;&gt;&gt; one_fifth = fractions.Fraction(1, 5)</pre>\n<p><span class=\"code\">Fractions</span> can do normal fraction calculations (including multiplication and division) for you:</p>\n<pre class=\"code\">&gt;&gt;&gt; one_half+one_fifth\r\nFraction(7, 10)\r\n&gt;&gt;&gt; one_half-one_fifth\r\nFraction(3, 10)\r\n&gt;&gt;&gt; one_half*one_fifth\r\nFraction(1, 10)\r\n&gt;&gt;&gt; one_half/one_fifth\r\nFraction(5, 2)</pre>\n"},{"title":"Using Tkinter widgets in Python","thumb":null,"image":null,"content":"<p><span class=\"code\">Tkinter</span> in Python comes with a lot of good widgets. <em>Widgets</em> are standard graphical user interface (GUI) elements, like different kinds of buttons and menus. Most of the <span class=\"code\">Tkinter</span> widgets are given here.</p>\n<h2>Label Widget</h2>\n<p><img loading=\"lazy\" src=\"https://www.dummies.com/wp-content/uploads/489135.image0.jpg\" alt=\"image0.jpg\" width=\"124\" height=\"57\" /></p>\n<p>A <span class=\"code\">Label</span> widget shows text to the user. You can update the widget programmatically to, for example, provide a readout or status bar.</p>\n<pre class=\"code\">import Tkinter\r\nparent_widget = Tkinter.Tk()\r\nlabel_widget = Tkinter.Label(parent_widget, text=\"A Label\")\r\nlabel_widget.pack()\r\nTkinter.mainloop()</pre>\n<h2>Button Widget</h2>\n<p><img loading=\"lazy\" src=\"https://www.dummies.com/wp-content/uploads/489136.image1.jpg\" alt=\"image1.jpg\" width=\"124\" height=\"63\" /></p>\n<p>A <span class=\"code\">Button</span> can be on and off. When a user clicks it, the button emits an event. Images can be displayed on buttons.</p>\n<pre class=\"code\">import Tkinter\r\nparent_widget = Tkinter.Tk()\r\nbutton_widget = Tkinter.Button(parent_widget,\r\n text=\"A Button\")\r\nbutton_widget.pack()\r\nTkinter.mainloop()</pre>\n<h2>Entry Widget</h2>\n<p><img loading=\"lazy\" src=\"https://www.dummies.com/wp-content/uploads/489137.image2.jpg\" alt=\"image2.jpg\" width=\"160\" height=\"55\" /></p>\n<p>An <span class=\"code\">Entry</span> widget gets text input from the user.</p>\n<pre class=\"code\">import Tkinter\r\nparent_widget = Tkinter.Tk()\r\nentry_widget = Tkinter.Entry(parent_widget)\r\nentry_widget.insert(0, \"Type your text here\")\r\nentry_widget.pack()\r\nTkinter.mainloop()</pre>\n<h2>Radiobutton Widget</h2>\n<p><img loading=\"lazy\" src=\"https://www.dummies.com/wp-content/uploads/489138.image3.jpg\" alt=\"image3.jpg\" width=\"125\" height=\"87\" /></p>\n<p>A <span class=\"code\">Radiobutton</span> lets you put buttons together, so that only one of them can be clicked. If one button is <span class=\"code\">on</span> and the user clicks another, the first is set to <span class=\"code\">off</span>. Use <span class=\"code\">Tkinter</span> variables (mainly <span class=\"code\">Tkinter.IntVar</span> and <span class=\"code\">Tkinter.StringVar</span>) to access its state.</p>\n<pre class=\"code\">import Tkinter\r\nparent_widget = Tkinter.Tk()\r\nv = Tkinter.IntVar()\r\nv.set(1) # need to use v.set and v.get to\r\n# set and get the value of this variable\r\nradiobutton_widget1 = Tkinter.Radiobutton(parent_widget,\r\n text=\"Radiobutton 1\",\r\n variable=v, value=1)\r\nradiobutton_widget2 = Tkinter.Radiobutton(parent_widget,\r\n text=\"Radiobutton 2\",\r\n variable=v, value=2)\r\nradiobutton_widget1.pack()\r\nradiobutton_widget2.pack()\r\nTkinter.mainloop()</pre>\n<h2>Radiobutton Widget (Alternate)</h2>\n<p><img loading=\"lazy\" src=\"https://www.dummies.com/wp-content/uploads/489139.image4.jpg\" alt=\"image4.jpg\" width=\"124\" height=\"87\" /></p>\n<p>You can display a <span class=\"code\">Radiobutton</span> without the dot indicator. In that case it displays its state by being sunken or raised.</p>\n<pre class=\"code\">import Tkinter\r\nparent_widget = Tkinter.Tk()\r\nv = Tkinter.IntVar()\r\nv.set(1)\r\nradiobutton_widget1 = Tkinter.Radiobutton(parent_widget,\r\n text=\"Radiobutton 1\",\r\n variable=v, value=1,\r\n indicatoron=False)\r\nradiobutton_widget2 = Tkinter.Radiobutton(parent_widget,\r\n text=\"Radiobutton 2\",\r\n variable=v, value=2,\r\n indicatoron=False)\r\nradiobutton_widget1.pack()\r\nradiobutton_widget2.pack()\r\nTkinter.mainloop()</pre>\n<h2>Checkbutton Widget</h2>\n<p><img loading=\"lazy\" src=\"https://www.dummies.com/wp-content/uploads/489140.image5.jpg\" alt=\"image5.jpg\" width=\"124\" height=\"61\" /></p>\n<p>A <span class=\"code\">Checkbutton</span> records on/off or true/false status. Like a <span class=\"code\">Radiobutton</span>, a <span class=\"code\">Checkbutton</span> widget can be displayed without its check mark, and you need to use a <span class=\"code\">Tkinter</span> variable to access its state.</p>\n<pre class=\"code\">import Tkinter\r\nparent_widget = Tkinter.Tk()\r\ncheckbutton_widget = Tkinter.Checkbutton(parent_widget,\r\n text=\"Checkbutton\")\r\ncheckbutton_widget.select()\r\ncheckbutton_widget.pack()\r\nTkinter.mainloop()</pre>\n<h2>Scale Widget: Horizontal</h2>\n<p><img loading=\"lazy\" src=\"https://www.dummies.com/wp-content/uploads/489141.image6.jpg\" alt=\"image6.jpg\" width=\"124\" height=\"78\" /></p>\n<p>Use a <span class=\"code\">Scale</span> widget when you want a slider that goes from one value to another. You can set the start and end values, as well as the step. For example, you can have a slider that has only the even values between <span class=\"code\">2</span> and <span class=\"code\">100</span>. Access its current value by its <span class=\"code\">get</span> method; set its current value by its <span class=\"code\">set</span> method.</p>\n<pre class=\"code\">import Tkinter\r\nparent_widget = Tkinter.Tk()\r\nscale_widget = Tkinter.Scale(parent_widget, from_=0, to=100,\r\n orient=Tkinter.HORIZONTAL)\r\nscale_widget.set(25)\r\nscale_widget.pack()\r\nTkinter.mainloop()</pre>\n<h2>Scale Widget: Vertical</h2>\n<p><img loading=\"lazy\" src=\"https://www.dummies.com/wp-content/uploads/489142.image7.jpg\" alt=\"image7.jpg\" width=\"124\" height=\"141\" /></p>\n<p>A <span class=\"code\">Scale</span> widget can be <em>vertical</em> (up and down).</p>\n<pre class=\"code\">import Tkinter\r\nparent_widget = Tkinter.Tk()\r\nscale_widget = Tkinter.Scale(parent_widget, from_=0, to=100,\r\n orient=Tkinter.VERTICAL)\r\nscale_widget.set(25)\r\nscale_widget.pack()\r\nTkinter.mainloop()</pre>\n<h2>Text Widget</h2>\n<p><img loading=\"lazy\" src=\"https://www.dummies.com/wp-content/uploads/489143.image8.jpg\" alt=\"image8.jpg\" width=\"200\" height=\"90\" /></p>\n<p>Use a <span class=\"code\">Text</span> widget to show large areas of text. The <span class=\"code\">Text</span> widget lets the user edit and search.</p>\n<pre class=\"code\">import Tkinter\r\nparent_widget = Tkinter.Tk()\r\ntext_widget = Tkinter.Text(parent_widget,\r\n width=20, height=3)\r\ntext_widget.insert(Tkinter.END,\r\n \"Text Widgetn20 characters widen3 lines high\")\r\ntext_widget.pack()\r\nTkinter.mainloop()</pre>\n<h2>LabelFrame Widget</h2>\n<p><img loading=\"lazy\" src=\"https://www.dummies.com/wp-content/uploads/489144.image9.jpg\" alt=\"image9.jpg\" width=\"224\" height=\"97\" /></p>\n<p>The <span class=\"code\">LabelFrame</span> acts as a parent widget for other widgets, displaying them with a title and an outline. <span class=\"code\">LabelFrame</span> has to have a child widget before you can see it.</p>\n<pre class=\"code\">import Tkinter\r\nparent_widget = Tkinter.Tk()\r\nlabelframe_widget = Tkinter.LabelFrame(parent_widget,\r\n text=\"LabelFrame\")\r\nlabel_widget=Tkinter.Label(labelframe_widget,\r\n text=\"Child widget of the LabelFrame\")\r\nlabelframe_widget.pack(padx=10, pady=10)\r\nlabel_widget.pack()\r\nTkinter.mainloop()</pre>\n<h2>Canvas Widget</h2>\n<p><img loading=\"lazy\" src=\"https://www.dummies.com/wp-content/uploads/489145.image10.jpg\" alt=\"image10.jpg\" width=\"124\" height=\"89\" /></p>\n<p>You use a <span class=\"code\">Canvas</span> widget to draw on. It supports different drawing methods.</p>\n<pre class=\"code\">import Tkinter\r\nparent_widget = Tkinter.Tk()\r\ncanvas_widget = Tkinter.Canvas(parent_widget\r\n bg=\"blue\",\r\n width=100,\r\n height= 50)\r\ncanvas_widget.pack()\r\nTkinter.mainloop()</pre>\n<h2>Listbox Widget</h2>\n<p><img loading=\"lazy\" src=\"https://www.dummies.com/wp-content/uploads/489146.image11.jpg\" alt=\"image11.jpg\" width=\"160\" height=\"209\" /></p>\n<p><span class=\"code\">Listbox</span> lets the user choose from one set of options or displays a list of items.</p>\n<pre class=\"code\">import Tkinter\r\nparent_widget = Tkinter.Tk()\r\nlistbox_entries = [\"Entry 1\", \"Entry 2\",\r\n \"Entry 3\", \"Entry 4\"]\r\nlistbox_widget = Tkinter.Listbox(parent_widget)\r\nfor entry in listbox_entries:\r\n listbox_widget.insert(Tkinter.END, entry)\r\nlistbox_widget.pack()\r\nTkinter.mainloop()</pre>\n<h2>Menu Widget</h2>\n<p><img loading=\"lazy\" src=\"https://www.dummies.com/wp-content/uploads/489147.image12.jpg\" alt=\"image12.jpg\" width=\"216\" height=\"254\" /></p>\n<p>The <span class=\"code\">Menu</span> widget can create a menu bar. Creating menus can be hard, especially if you want drop-down menus. To do that, you use a separate <span class=\"code\">Menu</span> widget for each drop-down menu you&#8217;re creating.</p>\n<pre class=\"code\">import Tkinter\r\nparent_widget = Tkinter.Tk()\r\ndef menu_callback():\r\n print(\"I'm in the menu callback!\")\r\ndef submenu_callback():\r\n print(\"I'm in the submenu callback!\")\r\nmenu_widget = Tkinter.Menu(parent_widget)\r\nsubmenu_widget = Tkinter.Menu(menu_widget, tearoff=False)\r\nsubmenu_widget.add_command(label=\"Submenu Item1\",\r\n command=submenu_callback)\r\nsubmenu_widget.add_command(label=\"Submenu Item2\",\r\n command=submenu_callback)\r\nmenu_widget.add_cascade(label=\"Item1\", menu=submenu_widget)\r\nmenu_widget.add_command(label=\"Item2\",\r\n command=menu_callback)\r\nmenu_widget.add_command(label=\"Item3\",\r\n command=menu_callback)\r\nparent_widget.config(menu=menu_widget)\r\nTkinter.mainloop()</pre>\n<h2>OptionMenu Widget</h2>\n<p><img loading=\"lazy\" src=\"https://www.dummies.com/wp-content/uploads/489148.image13.jpg\" alt=\"image13.jpg\" width=\"129\" height=\"122\" /></p>\n<p>The <span class=\"code\">OptionMenu</span> widget lets the user choose from a list of options. To use the <span class=\"code\">OptionMenu</span> the right way, you&#8217;ll probably need to bind it to a separate callback that updates other information based on the user&#8217;s selection. Get the currently selected value with its <span class=\"code\">get</span> method.</p>\n<pre class=\"code\">import Tkinter\r\nparent_widget = Tkinter.Tk()\r\ncontrol_variable = Tkinter.StringVar(parent_widget)\r\nOPTION_TUPLE = (\"Option 1\", \"Option 2\", \"Option 3\") \r\noptionmenu_widget = Tkinter.OptionMenu(parent_widget,\r\n control_variable, *OPTION_TUPLE)\r\noptionmenu_widget.pack()\r\nTkinter.mainloop()</pre>\n"}],"videoInfo":{"videoId":null,"name":null,"accountId":null,"playerId":null,"thumbnailUrl":null,"description":null,"uploadDate":null}},"sponsorship":{"sponsorshipPage":false,"backgroundImage":{"src":null,"width":0,"height":0},"brandingLine":"","brandingLink":"","brandingLogo":{"src":null,"width":0,"height":0}},"primaryLearningPath":"Explore","lifeExpectancy":"Two years","lifeExpectancySetFrom":"2022-02-24T00:00:00+00:00","dummiesForKids":"no","sponsoredContent":"no","adInfo":"","adPairKey":[]},"status":"publish","visibility":"public","articleId":207407},{"headers":{"creationTime":"2019-09-25T13:13:49+00:00","modifiedTime":"2022-01-25T15:07:11+00:00","timestamp":"2022-02-24T17:07:26+00:00"},"data":{"breadcrumbs":[{"name":"Technology","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33512"},"slug":"technology","categoryId":33512},{"name":"Programming & Web Design","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33592"},"slug":"programming-web-design","categoryId":33592},{"name":"Python","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33606"},"slug":"python","categoryId":33606}],"title":"What is a Python Module?","strippedTitle":"what is a python module?","slug":"what-is-a-python-module","canonicalUrl":"","seo":{"metaDescription":"Curious about Python modules? Discover what they area and use this guide for basic directions on creating your own modules, from Dummies.com.","noIndex":0,"noFollow":0},"content":"For all the hoopla about <a href=\"https://www.dummies.com/programming/python/python-all-in-one-for-dummies-cheat-sheet/\">Python</a> modules, a module is actually a pretty simple thing. In fact, a Python module is just a file with a .py extension that contains Python code. That’s it. So any time you write Python code and save it in a .py file, you’ve basically created a module.\r\n\r\nThat’s not to say you always have to use that code as a module. It can certainly be treated as a standalone app. But if you <em>wanted</em> to create your own Python module, with just code that you need often in your own work, you could certainly do so.\r\n\r\n[caption id=\"attachment_264474\" align=\"aligncenter\" width=\"535\"]<img class=\"wp-image-264474 size-full\" src=\"https://www.dummies.com/wp-content/uploads/python-module.jpg\" alt=\"programming Python modules\" width=\"535\" height=\"300\" /> ©Shutterstock/dTosh[/caption]\r\n\r\nA <a href=\"https://www.dummies.com/programming/python/how-to-view-module-content-in-python/\">Python module</a> is also just a <a href=\"https://www.dummies.com/article/technology/programming-web-design/python/how-to-delete-a-file-in-python-148235\" target=\"_blank\" rel=\"noopener\">file</a> with a .py filename extension. The name of the module is the same as the filename (without the .py). Like any .py file, the module contains Python code. As a working example, let’s suppose you want to have three functions to simplify formatting dates and currency values. You can make up any name you like for each function. For our working example, we’ll use these three names:\r\n<ul>\r\n \t<li><strong><code>to_date(<em>any_str</em>)</code>:</strong> Lets you pass in any string (<em><code>any_str</code></em>) date in <em>mm/dd/yy</em> or <em>mm/dd/yyyy</em> format and sends back a Python <code>datetime.date </code> that you can use for date calculations.</li>\r\n \t<li><strong><code>mdy(<em>any_date</em>)</code>:</strong> Lets you pass in any Python date or datetime, and returns a string date formatted in <em>mm/dd/yyyy</em> format for display on the screen.</li>\r\n \t<li><strong><code>to_curr(<em>any_num, len</em>)</code>:</strong> Lets you pass in any Python float or integer number and returns a string with a leading dollar sign, commas in thousands places, and two digits for the pennies. The <em><code>len</code></em> is an optional number for length. If provided, the return value will be padded on the left with spaces to match the length specified</li>\r\n</ul>\r\nSo here is all the code for that:\r\n<pre class=\"code\"># Contains custom functions for dates and currency values.\r\nimport datetime as dt\r\ndef to_date(any_str):\r\n\"\"\" Convert mm/dd/yy or mm/dd/yyyy string to datetime.date, or None if invalid date. \"\"\"\r\ntry:\r\nif len(any_str) == 10:\r\nthe_date = dt.datetime.strptime(any_str,'%m/%d/%Y').date()\r\nelse:\r\nthe_date = dt.datetime.strptime(any_str,'%m/%d/%y').date()\r\nexcept (ValueError, TypeError):\r\nthe_date = None\r\nreturn the_date\r\ndef mdy(any_date):\r\n\"\"\" Returns a string date in mm/dd/yyyy format. Pass in Python date or string date in mm/dd/yyyy format \"\"\"\r\nif type(any_date) == str:\r\nany_date = to_date(anydate)\r\n# Make sure its a dateime being forwarded\r\nif isinstance(any_date,dt.date):\r\ns_date = f\"{any_date:'%m/%d/%Y'}\"\r\nelse:\r\ns_date = \"Invalid date\"\r\nreturn s_date\r\ndef to_curr(anynum, len=0):\r\n\"\"\" Returns a number as a string with $ and commas. Length is optional \"\"\"\r\ns = \"Invalid amount\"\r\ntry:\r\nx = float(anynum)\r\nexcept ValueError:\r\nx= None\r\nif isinstance(x,float):\r\ns = '$' + f\"{x:,.2f}\"\r\nif len > 0:\r\ns=s.rjust(len)\r\nreturn s</pre>\r\nYou can create the same file yourself and name it <code>myfunctions.py</code> if you want to follow along. Notice that the file contains only functions. So if you run it, it won't do anything on the screen because there is no code in there that calls any of those functions.\r\n\r\nTo use those functions in any Python app or program you write, first make sure you copy that <code>myfunc.py</code> file to the same folder as the rest of the Python code that you’re writing. Then, when you create a new page, you can import <code>myfunc</code> as a module just as you would any other module created by somebody else. Just use\r\n<pre class=\"code\">import myfunc</pre>\r\nYou will have to use the module name in front of any of the functions that you call from that module. So if you want to make the code a little more readable, you can use this instead:\r\n<pre class=\"code\">import myfunc as my</pre>\r\nWith that as your opening line, you can refer to any <a href=\"https://www.dummies.com/programming/python/how-to-define-a-function-in-python/\">function</a> in your custom Python module with <code>my.</code> as the prefix. For example,<code> my.to_date()</code> to call the <code>to_date</code> function. Here is a page that imports the module and then tests out all three functions using that my syntax:\r\n<pre class=\"code\"># Import all the code from myfunc.py as my.\r\nimport myfunc as my\r\n# Need dates in this code\r\nfrom datetime import datetime as dt\r\n# Some simple test data.\r\nstring_date=\"12/31/2019\"\r\n# Convert string date to datetime.date\r\nprint(my.to_date(string_date))\r\ntoday = dt.today()\r\n# Show today's date in mm/dd/yyyy format.\r\nprint(my.mdy(today))\r\ndollar_amt=12345.678\r\n# Show this big number in currency format.\r\nprint(my.to_curr(dollar_amt))</pre>\r\nYou can also skip using the prefix if you import items by name. In this case, that means you could call <code>to_date()</code> and <code>mdy()</code> and to_<code>curr()</code> without using the <code>my.</code> prefix. The first line of code would need to be\r\n<pre class=\"code\">from myfunc import to_date, mdy, to_curr</pre>\r\nThe rest of the code would be the same as in the previous example, except you can leave off the <code>my.</code> prefixes as in the following code:\r\n<pre class=\"code\"># Import all the code from myfunc.py by name.\r\nfrom myfunc import to_date, mdy, to_curr\r\n# Need dates in this code\r\nfrom datetime import datetime as dt\r\n# Some simple test data.\r\nstring_date=\"12/31/2019\"\r\n# Convert string date to datetime.date\r\nprint(to_date(string_date))\r\ntoday = dt.today()\r\n# Show today's date in mm/dd/yyyy format.\r\nprint(mdy(today))\r\ndollar_amt=12345.678\r\n# Show this big number in currency format.\r\nprint(to_curr(dollar_amt))</pre>\r\n<p class=\"article-tips tip\">Check out these <a href=\"https://www.dummies.com/programming/python/10-amazing-python-programming-resources/\">10 amazing Python programming resources</a> for more information.</p>","description":"For all the hoopla about <a href=\"https://www.dummies.com/programming/python/python-all-in-one-for-dummies-cheat-sheet/\">Python</a> modules, a module is actually a pretty simple thing. In fact, a Python module is just a file with a .py extension that contains Python code. That’s it. So any time you write Python code and save it in a .py file, you’ve basically created a module.\r\n\r\nThat’s not to say you always have to use that code as a module. It can certainly be treated as a standalone app. But if you <em>wanted</em> to create your own Python module, with just code that you need often in your own work, you could certainly do so.\r\n\r\n[caption id=\"attachment_264474\" align=\"aligncenter\" width=\"535\"]<img class=\"wp-image-264474 size-full\" src=\"https://www.dummies.com/wp-content/uploads/python-module.jpg\" alt=\"programming Python modules\" width=\"535\" height=\"300\" /> ©Shutterstock/dTosh[/caption]\r\n\r\nA <a href=\"https://www.dummies.com/programming/python/how-to-view-module-content-in-python/\">Python module</a> is also just a <a href=\"https://www.dummies.com/article/technology/programming-web-design/python/how-to-delete-a-file-in-python-148235\" target=\"_blank\" rel=\"noopener\">file</a> with a .py filename extension. The name of the module is the same as the filename (without the .py). Like any .py file, the module contains Python code. As a working example, let’s suppose you want to have three functions to simplify formatting dates and currency values. You can make up any name you like for each function. For our working example, we’ll use these three names:\r\n<ul>\r\n \t<li><strong><code>to_date(<em>any_str</em>)</code>:</strong> Lets you pass in any string (<em><code>any_str</code></em>) date in <em>mm/dd/yy</em> or <em>mm/dd/yyyy</em> format and sends back a Python <code>datetime.date </code> that you can use for date calculations.</li>\r\n \t<li><strong><code>mdy(<em>any_date</em>)</code>:</strong> Lets you pass in any Python date or datetime, and returns a string date formatted in <em>mm/dd/yyyy</em> format for display on the screen.</li>\r\n \t<li><strong><code>to_curr(<em>any_num, len</em>)</code>:</strong> Lets you pass in any Python float or integer number and returns a string with a leading dollar sign, commas in thousands places, and two digits for the pennies. The <em><code>len</code></em> is an optional number for length. If provided, the return value will be padded on the left with spaces to match the length specified</li>\r\n</ul>\r\nSo here is all the code for that:\r\n<pre class=\"code\"># Contains custom functions for dates and currency values.\r\nimport datetime as dt\r\ndef to_date(any_str):\r\n\"\"\" Convert mm/dd/yy or mm/dd/yyyy string to datetime.date, or None if invalid date. \"\"\"\r\ntry:\r\nif len(any_str) == 10:\r\nthe_date = dt.datetime.strptime(any_str,'%m/%d/%Y').date()\r\nelse:\r\nthe_date = dt.datetime.strptime(any_str,'%m/%d/%y').date()\r\nexcept (ValueError, TypeError):\r\nthe_date = None\r\nreturn the_date\r\ndef mdy(any_date):\r\n\"\"\" Returns a string date in mm/dd/yyyy format. Pass in Python date or string date in mm/dd/yyyy format \"\"\"\r\nif type(any_date) == str:\r\nany_date = to_date(anydate)\r\n# Make sure its a dateime being forwarded\r\nif isinstance(any_date,dt.date):\r\ns_date = f\"{any_date:'%m/%d/%Y'}\"\r\nelse:\r\ns_date = \"Invalid date\"\r\nreturn s_date\r\ndef to_curr(anynum, len=0):\r\n\"\"\" Returns a number as a string with $ and commas. Length is optional \"\"\"\r\ns = \"Invalid amount\"\r\ntry:\r\nx = float(anynum)\r\nexcept ValueError:\r\nx= None\r\nif isinstance(x,float):\r\ns = '$' + f\"{x:,.2f}\"\r\nif len > 0:\r\ns=s.rjust(len)\r\nreturn s</pre>\r\nYou can create the same file yourself and name it <code>myfunctions.py</code> if you want to follow along. Notice that the file contains only functions. So if you run it, it won't do anything on the screen because there is no code in there that calls any of those functions.\r\n\r\nTo use those functions in any Python app or program you write, first make sure you copy that <code>myfunc.py</code> file to the same folder as the rest of the Python code that you’re writing. Then, when you create a new page, you can import <code>myfunc</code> as a module just as you would any other module created by somebody else. Just use\r\n<pre class=\"code\">import myfunc</pre>\r\nYou will have to use the module name in front of any of the functions that you call from that module. So if you want to make the code a little more readable, you can use this instead:\r\n<pre class=\"code\">import myfunc as my</pre>\r\nWith that as your opening line, you can refer to any <a href=\"https://www.dummies.com/programming/python/how-to-define-a-function-in-python/\">function</a> in your custom Python module with <code>my.</code> as the prefix. For example,<code> my.to_date()</code> to call the <code>to_date</code> function. Here is a page that imports the module and then tests out all three functions using that my syntax:\r\n<pre class=\"code\"># Import all the code from myfunc.py as my.\r\nimport myfunc as my\r\n# Need dates in this code\r\nfrom datetime import datetime as dt\r\n# Some simple test data.\r\nstring_date=\"12/31/2019\"\r\n# Convert string date to datetime.date\r\nprint(my.to_date(string_date))\r\ntoday = dt.today()\r\n# Show today's date in mm/dd/yyyy format.\r\nprint(my.mdy(today))\r\ndollar_amt=12345.678\r\n# Show this big number in currency format.\r\nprint(my.to_curr(dollar_amt))</pre>\r\nYou can also skip using the prefix if you import items by name. In this case, that means you could call <code>to_date()</code> and <code>mdy()</code> and to_<code>curr()</code> without using the <code>my.</code> prefix. The first line of code would need to be\r\n<pre class=\"code\">from myfunc import to_date, mdy, to_curr</pre>\r\nThe rest of the code would be the same as in the previous example, except you can leave off the <code>my.</code> prefixes as in the following code:\r\n<pre class=\"code\"># Import all the code from myfunc.py by name.\r\nfrom myfunc import to_date, mdy, to_curr\r\n# Need dates in this code\r\nfrom datetime import datetime as dt\r\n# Some simple test data.\r\nstring_date=\"12/31/2019\"\r\n# Convert string date to datetime.date\r\nprint(to_date(string_date))\r\ntoday = dt.today()\r\n# Show today's date in mm/dd/yyyy format.\r\nprint(mdy(today))\r\ndollar_amt=12345.678\r\n# Show this big number in currency format.\r\nprint(to_curr(dollar_amt))</pre>\r\n<p class=\"article-tips tip\">Check out these <a href=\"https://www.dummies.com/programming/python/10-amazing-python-programming-resources/\">10 amazing Python programming resources</a> for more information.</p>","blurb":"","authors":[{"authorId":26709,"name":"Alan Shovic","slug":"alan-shovic","description":"","_links":{"self":"https://dummies-api.dummies.com/v2/authors/26709"}},{"authorId":26710,"name":"John Shovic","slug":"john-shovic","description":"John Shovic, PhD, is a computer science faculty member at the University of Idaho specializing in robotics and artificial intelligence.","_links":{"self":"https://dummies-api.dummies.com/v2/authors/26710"}}],"primaryCategoryTaxonomy":{"categoryId":33606,"title":"Python","slug":"python","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33606"}},"secondaryCategoryTaxonomy":{"categoryId":0,"title":null,"slug":null,"_links":null},"tertiaryCategoryTaxonomy":{"categoryId":0,"title":null,"slug":null,"_links":null},"trendingArticles":null,"inThisArticle":[],"relatedArticles":{"fromBook":[{"articleId":264919,"title":"How to Define and Use Python Lists","slug":"how-to-define-and-use-python-lists","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264919"}},{"articleId":264911,"title":"How to Use Lambda Functions in Python","slug":"how-to-use-lambda-functions-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264911"}},{"articleId":264906,"title":"Your Guide to the Python Standard Library","slug":"your-guide-to-the-python-standard-library","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264906"}},{"articleId":264894,"title":"A Beginner’s Guide to Python Versions","slug":"a-beginners-guide-to-python-versions","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264894"}},{"articleId":264888,"title":"How to Build a Simple Neural Network in Python","slug":"how-to-build-a-simple-neural-network-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264888"}}],"fromCategory":[{"articleId":264919,"title":"How to Define and Use Python Lists","slug":"how-to-define-and-use-python-lists","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264919"}},{"articleId":264911,"title":"How to Use Lambda Functions in Python","slug":"how-to-use-lambda-functions-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264911"}},{"articleId":264906,"title":"Your Guide to the Python Standard Library","slug":"your-guide-to-the-python-standard-library","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264906"}},{"articleId":264894,"title":"A Beginner’s Guide to Python Versions","slug":"a-beginners-guide-to-python-versions","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264894"}},{"articleId":264888,"title":"How to Build a Simple Neural Network in Python","slug":"how-to-build-a-simple-neural-network-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264888"}}]},"hasRelatedBookFromSearch":false,"relatedBook":{"bookId":281833,"slug":"python-all-in-one-for-dummies","isbn":"9781119787600","categoryList":["technology","programming-web-design","python"],"amazon":{"default":"https://www.amazon.com/gp/product/1119787602/ref=as_li_tl?ie=UTF8&tag=wiley01-20","ca":"https://www.amazon.ca/gp/product/1119787602/ref=as_li_tl?ie=UTF8&tag=wiley01-20","indigo_ca":"http://www.tkqlhce.com/click-9208661-13710633?url=https://www.chapters.indigo.ca/en-ca/books/product/1119787602-item.html&cjsku=978111945484","gb":"https://www.amazon.co.uk/gp/product/1119787602/ref=as_li_tl?ie=UTF8&tag=wiley01-20","de":"https://www.amazon.de/gp/product/1119787602/ref=as_li_tl?ie=UTF8&tag=wiley01-20"},"image":{"src":"https://www.dummies.com/wp-content/uploads/python-all-in-one-for-dummies-2e-cover-9781119787600-203x255.jpg","width":203,"height":255},"title":"Python All-in-One For Dummies, 2nd Edition","testBankPinActivationLink":"","bookOutOfPrint":true,"authorsInfo":"\n <p><b data-author-id=\"26710\">John Shovic</b>, PhD, is a computer science faculty member at the University of Idaho specializing in robotics and artificial intelligence. <b data-author-id=\"10199\">Alan Simpson</b> is a web development professional who has published more than 100 articles and books on technology.</p>","authors":[{"authorId":26710,"name":"John Shovic","slug":"john-shovic","description":"John Shovic, PhD, is a computer science faculty member at the University of Idaho specializing in robotics and artificial intelligence.","_links":{"self":"https://dummies-api.dummies.com/v2/authors/26710"}},{"authorId":10199,"name":"Alan Simpson","slug":"alan-simpson","description":"Alan Simpson is a web development professional who has published more than 100 articles and books on technology.","_links":{"self":"https://dummies-api.dummies.com/v2/authors/10199"}}],"_links":{"self":"https://dummies-api.dummies.com/v2/books/"}},"collections":[],"articleAds":{"footerAd":"<div class=\"du-ad-region row\" id=\"article_page_adhesion_ad\"><div class=\"du-ad-unit col-md-12\" data-slot-id=\"article_page_adhesion_ad\" data-refreshed=\"false\" \r\n data-target = \"[{&quot;key&quot;:&quot;cat&quot;,&quot;values&quot;:[&quot;technology&quot;,&quot;programming-web-design&quot;,&quot;python&quot;]},{&quot;key&quot;:&quot;isbn&quot;,&quot;values&quot;:[&quot;9781119787600&quot;]}]\" id=\"du-slot-6217bb4e406ff\"></div></div>","rightAd":"<div class=\"du-ad-region row\" id=\"article_page_right_ad\"><div class=\"du-ad-unit col-md-12\" data-slot-id=\"article_page_right_ad\" data-refreshed=\"false\" \r\n data-target = \"[{&quot;key&quot;:&quot;cat&quot;,&quot;values&quot;:[&quot;technology&quot;,&quot;programming-web-design&quot;,&quot;python&quot;]},{&quot;key&quot;:&quot;isbn&quot;,&quot;values&quot;:[&quot;9781119787600&quot;]}]\" id=\"du-slot-6217bb4e4108c\"></div></div>"},"articleType":{"articleType":"Articles","articleList":null,"content":null,"videoInfo":{"videoId":null,"name":null,"accountId":null,"playerId":null,"thumbnailUrl":null,"description":null,"uploadDate":null}},"sponsorship":{"sponsorshipPage":false,"backgroundImage":{"src":null,"width":0,"height":0},"brandingLine":"","brandingLink":"","brandingLogo":{"src":null,"width":0,"height":0}},"primaryLearningPath":"Advance","lifeExpectancy":"One year","lifeExpectancySetFrom":"2022-01-25T00:00:00+00:00","dummiesForKids":"no","sponsoredContent":"no","adInfo":"","adPairKey":[]},"status":"publish","visibility":"public","articleId":264471},{"headers":{"creationTime":"2016-03-26T10:48:29+00:00","modifiedTime":"2022-01-25T15:06:38+00:00","timestamp":"2022-02-24T17:07:26+00:00"},"data":{"breadcrumbs":[{"name":"Technology","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33512"},"slug":"technology","categoryId":33512},{"name":"Programming & Web Design","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33592"},"slug":"programming-web-design","categoryId":33592},{"name":"Python","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33606"},"slug":"python","categoryId":33606}],"title":"How to Delete a File in Python","strippedTitle":"how to delete a file in python","slug":"how-to-delete-a-file-in-python","canonicalUrl":"","seo":{"metaDescription":"While you can use Python to delete information from files, you may find you no longer need the file at all. Here's how to delete it.","noIndex":0,"noFollow":0},"content":"While you can use Python to delete information from files, you may find you no longer need the file at all. The following steps describe how to delete files that you no longer need.\r\n<ol class=\"level-one\">\r\n \t<li>\r\n<p class=\"first-para\">Open a Python File window.</p>\r\n<p class=\"child-para\">You see an editor in which you can type the example code.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">Type the following code into the window — pressing Enter after each line:</p>\r\n</li>\r\n \t<li>Choose Run→Run <a href=\"https://www.dummies.com/article/technology/programming-web-design/python/what-is-a-python-module-264471\" target=\"_blank\" rel=\"noopener\">Module</a>\r\n\r\nThe application displays the File Removed! message. When you look in the directory that originally contained the ChangedFile.csv file, you see that the file is gone.\r\n<p class=\"child-para\">The task looks simple in this case, and it is. All you need to do to remove a file is call <span class=\"code\">os.remove()</span> with the appropriate filename and path (Python defaults to the current directory, so you don’t need to specify a path if the file you want to remove is in the default directory). The ease with which you can perform this task is almost scary because it’s too easy.</p>\r\n<p class=\"child-para\">Putting safeguards in place is always a good idea. You may want to remove other items, so here are other functions you should know about:</p>\r\n\r\n<ul class=\"level-two\">\r\n \t<li>\r\n<p class=\"first-para\"><b>os.rmdir():</b> Removes the specified directory. The directory must be empty or Python will display an exception message.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>shutil.rmtree():</b> Removes the specified directory, all subdirectories, and all files. This function is especially dangerous because it removes everything without checking (Python assumes that you know what you’re doing). As a result, you can easily lose data using this function.</p>\r\n</li>\r\n</ul>\r\n</li>\r\n</ol>","description":"While you can use Python to delete information from files, you may find you no longer need the file at all. The following steps describe how to delete files that you no longer need.\r\n<ol class=\"level-one\">\r\n \t<li>\r\n<p class=\"first-para\">Open a Python File window.</p>\r\n<p class=\"child-para\">You see an editor in which you can type the example code.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">Type the following code into the window — pressing Enter after each line:</p>\r\n</li>\r\n \t<li>Choose Run→Run <a href=\"https://www.dummies.com/article/technology/programming-web-design/python/what-is-a-python-module-264471\" target=\"_blank\" rel=\"noopener\">Module</a>\r\n\r\nThe application displays the File Removed! message. When you look in the directory that originally contained the ChangedFile.csv file, you see that the file is gone.\r\n<p class=\"child-para\">The task looks simple in this case, and it is. All you need to do to remove a file is call <span class=\"code\">os.remove()</span> with the appropriate filename and path (Python defaults to the current directory, so you don’t need to specify a path if the file you want to remove is in the default directory). The ease with which you can perform this task is almost scary because it’s too easy.</p>\r\n<p class=\"child-para\">Putting safeguards in place is always a good idea. You may want to remove other items, so here are other functions you should know about:</p>\r\n\r\n<ul class=\"level-two\">\r\n \t<li>\r\n<p class=\"first-para\"><b>os.rmdir():</b> Removes the specified directory. The directory must be empty or Python will display an exception message.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>shutil.rmtree():</b> Removes the specified directory, all subdirectories, and all files. This function is especially dangerous because it removes everything without checking (Python assumes that you know what you’re doing). As a result, you can easily lose data using this function.</p>\r\n</li>\r\n</ul>\r\n</li>\r\n</ol>","blurb":"","authors":[{"authorId":9109,"name":"John Paul Mueller","slug":"john-paul-mueller","description":"John Paul Mueller has written more than 100 books and more than 600 articles on topics ranging from functional programming techniques to application development using C++. ","_links":{"self":"https://dummies-api.dummies.com/v2/authors/9109"}}],"primaryCategoryTaxonomy":{"categoryId":33606,"title":"Python","slug":"python","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33606"}},"secondaryCategoryTaxonomy":{"categoryId":0,"title":null,"slug":null,"_links":null},"tertiaryCategoryTaxonomy":{"categoryId":0,"title":null,"slug":null,"_links":null},"trendingArticles":null,"inThisArticle":[],"relatedArticles":{"fromBook":[{"articleId":250588,"title":"How to Get Additional Python Libraries","slug":"get-additional-python-libraries","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/250588"}},{"articleId":250582,"title":"Printing Lists Using Python","slug":"printing-lists-using-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/250582"}},{"articleId":250578,"title":"Understanding How Permanent Storage Works for Python Programming","slug":"understanding-permanent-storage-works-python-programming","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/250578"}},{"articleId":250575,"title":"Extending Python Classes to Make New Classes","slug":"extending-python-classes-make-new-classes","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/250575"}},{"articleId":250571,"title":"Understanding the Python Class as a Packaging Method","slug":"understanding-python-class-packaging-method","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/250571"}}],"fromCategory":[{"articleId":264919,"title":"How to Define and Use Python Lists","slug":"how-to-define-and-use-python-lists","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264919"}},{"articleId":264911,"title":"How to Use Lambda Functions in Python","slug":"how-to-use-lambda-functions-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264911"}},{"articleId":264906,"title":"Your Guide to the Python Standard Library","slug":"your-guide-to-the-python-standard-library","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264906"}},{"articleId":264894,"title":"A Beginner’s Guide to Python Versions","slug":"a-beginners-guide-to-python-versions","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264894"}},{"articleId":264888,"title":"How to Build a Simple Neural Network in Python","slug":"how-to-build-a-simple-neural-network-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264888"}}]},"hasRelatedBookFromSearch":false,"relatedBook":{"bookId":281830,"slug":"beginning-programming-with-python-for-dummies-2nd-edition","isbn":"9781119457893","categoryList":["technology","programming-web-design","python"],"amazon":{"default":"https://www.amazon.com/gp/product/1119457890/ref=as_li_tl?ie=UTF8&tag=wiley01-20","ca":"https://www.amazon.ca/gp/product/1119457890/ref=as_li_tl?ie=UTF8&tag=wiley01-20","indigo_ca":"http://www.tkqlhce.com/click-9208661-13710633?url=https://www.chapters.indigo.ca/en-ca/books/product/1119457890-item.html&cjsku=978111945484","gb":"https://www.amazon.co.uk/gp/product/1119457890/ref=as_li_tl?ie=UTF8&tag=wiley01-20","de":"https://www.amazon.de/gp/product/1119457890/ref=as_li_tl?ie=UTF8&tag=wiley01-20"},"image":{"src":"https://www.dummies.com/wp-content/uploads/beginning-programming-with-python-for-dummies-2nd-edition-cover-9781119457893-203x255.jpg","width":203,"height":255},"title":"Beginning Programming with Python For Dummies, 2nd Edition","testBankPinActivationLink":"","bookOutOfPrint":false,"authorsInfo":"\n <p><b data-author-id=\"9109\">John Paul Mueller</b> is a freelance author and technical editor with more than 107 books and 600 articles to his credit. His subjects range from networking and artificial intelligence to database management and heads-down programming. He also consults and writes certification exams. Visit his website at http://www.johnmuellerbooks.com/. </p>","authors":[{"authorId":9109,"name":"John Paul Mueller","slug":"john-paul-mueller","description":"John Paul Mueller has written more than 100 books and more than 600 articles on topics ranging from functional programming techniques to application development using C++. ","_links":{"self":"https://dummies-api.dummies.com/v2/authors/9109"}}],"_links":{"self":"https://dummies-api.dummies.com/v2/books/"}},"collections":[],"articleAds":{"footerAd":"<div class=\"du-ad-region row\" id=\"article_page_adhesion_ad\"><div class=\"du-ad-unit col-md-12\" data-slot-id=\"article_page_adhesion_ad\" data-refreshed=\"false\" \r\n data-target = \"[{&quot;key&quot;:&quot;cat&quot;,&quot;values&quot;:[&quot;technology&quot;,&quot;programming-web-design&quot;,&quot;python&quot;]},{&quot;key&quot;:&quot;isbn&quot;,&quot;values&quot;:[&quot;9781119457893&quot;]}]\" id=\"du-slot-6217bb4e383f9\"></div></div>","rightAd":"<div class=\"du-ad-region row\" id=\"article_page_right_ad\"><div class=\"du-ad-unit col-md-12\" data-slot-id=\"article_page_right_ad\" data-refreshed=\"false\" \r\n data-target = \"[{&quot;key&quot;:&quot;cat&quot;,&quot;values&quot;:[&quot;technology&quot;,&quot;programming-web-design&quot;,&quot;python&quot;]},{&quot;key&quot;:&quot;isbn&quot;,&quot;values&quot;:[&quot;9781119457893&quot;]}]\" id=\"du-slot-6217bb4e38d9c\"></div></div>"},"articleType":{"articleType":"Articles","articleList":null,"content":null,"videoInfo":{"videoId":null,"name":null,"accountId":null,"playerId":null,"thumbnailUrl":null,"description":null,"uploadDate":null}},"sponsorship":{"sponsorshipPage":false,"backgroundImage":{"src":null,"width":0,"height":0},"brandingLine":"","brandingLink":"","brandingLogo":{"src":null,"width":0,"height":0}},"primaryLearningPath":"Solve","lifeExpectancy":"One year","lifeExpectancySetFrom":"2021-09-13T00:00:00+00:00","dummiesForKids":"no","sponsoredContent":"no","adInfo":"","adPairKey":[]},"status":"publish","visibility":"public","articleId":148235},{"headers":{"creationTime":"2016-03-26T07:16:02+00:00","modifiedTime":"2022-01-25T14:55:27+00:00","timestamp":"2022-02-24T17:07:26+00:00"},"data":{"breadcrumbs":[{"name":"Technology","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33512"},"slug":"technology","categoryId":33512},{"name":"Programming & Web Design","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33592"},"slug":"programming-web-design","categoryId":33592},{"name":"Python","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33606"},"slug":"python","categoryId":33606}],"title":"How to Install Python on Your Computer","strippedTitle":"how to install python on your computer","slug":"how-to-install-python-on-your-computer","canonicalUrl":"","seo":{"metaDescription":"","noIndex":0,"noFollow":0},"content":"Whether you use a Mac, Windows, or Linux OS (operating system), you can find and install <a href=\"https://www.dummies.com/article/technology/programming-web-design/python/write-a-simple-program-in-python-139547\" target=\"_blank\" rel=\"noopener\">Python </a>on your computer. The following sections give you instructions for each OS.\r\n<h2 id=\"tab1\" >How to install Python on Mac OSX</h2>\r\nTo find and start Python on Mac OSX computers, follow these steps:\r\n<ol class=\"level-one\">\r\n \t<li>\r\n<p class=\"first-para\">Press Cmd+spacebar to open Spotlight.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">Type the word <span class=\"code\">terminal</span>.</p>\r\n<p class=\"child-para\">Or, from the Finder, select Finder→Go→Utilities→Terminal.</p>\r\n<p class=\"child-para\">The Terminal window opens.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">In the terminal, type <span class=\"code\">python</span>.</p>\r\n<p class=\"child-para\">The Python interpreter that's built in to Mac OSX opens.</p>\r\n</li>\r\n</ol>\r\n<h2 id=\"tab2\" >How to install Python on Windows</h2>\r\nUnfortunately, Python doesn't come on Windows. If you're running Windows, then you need to download and install Python by following the instructions here. Installing Python on Windows isn't difficult. If you can download a file from a website, you have the skills to install Python.\r\n\r\nFortunately, the Python Foundation (the peeps who guide the development of Python) makes installable files available from its website.\r\n<p class=\"Tip\">Firefox and Internet Explorer responded differently to the Python download website, so the instructions are based on which of these browsers you use. If you use a whole other browser altogether, try the Internet Explorer instructions.</p>\r\n\r\n<h3>Installing with Firefox</h3>\r\nTo install Python on a Windows machine with Firefox, follow these steps:\r\n<ol class=\"level-one\">\r\n \t<li>\r\n<p class=\"first-para\">Visit <a href=\"http://www.python.org/downloads\">www.python.org/downloads</a>.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">Click the button that says Download Python 2.7.9.</p>\r\n<p class=\"child-para\">Or, if it's there, click a more recent version number that starts with 2.7.</p>\r\n<p class=\"child-para\">Clicking this button automatically downloads and saves an <span class=\"code\">msi</span> file for you. If not, try the instructions for Internet Explorer. See Figure 1.</p>\r\n\r\n<div class=\"imageBlock\" style=\"width: 535px;\">\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/497374.image0.png\" alt=\"<b>Figure 1</b><b>:</b> Download Python with Firefox.\" width=\"535\" height=\"448\" />\r\n<div class=\"imageCaption\"><b>Figure 1</b><b>:</b> Download Python with Firefox.</div>\r\n</div></li>\r\n \t<li>\r\n<p class=\"first-para\">When the download's complete, click the icon for Firefox's download tool.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">Click the file called <span class=\"code\">python-2.7.9.msi</span> (or the more recent version, if you downloaded one).</p>\r\n<p class=\"child-para\">Python 2.7.9 installs on your computer.</p>\r\n</li>\r\n</ol>\r\n<h3>Installing with Internet Explorer</h3>\r\nTo install Python on a Windows machine with Internet Explorer, follow these steps:\r\n<ol class=\"level-one\">\r\n \t<li>\r\n<p class=\"first-para\">Visit <a href=\"http://www.python.org/downloads\">www.python.org/downloads</a>.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">From the menu bar, select Downloads→Windows.</p>\r\n<p class=\"child-para\">You can see the menu options in Figure 2.</p>\r\n\r\n<div class=\"imageBlock\" style=\"width: 535px;\">\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/497375.image1.png\" alt=\"<b>Figure </b><b>2:</b> Download Python with Internet Explorer.\" width=\"535\" height=\"304\" />\r\n<div class=\"imageCaption\"><b>Figure </b><b>2:</b> Download Python with Internet Explorer.</div>\r\n</div></li>\r\n \t<li>\r\n<p class=\"first-para\">Scroll down to the heading Python 2.7.9-2014-12-10.</p>\r\n<p class=\"child-para\">Or scroll to a more recent version, which starts with Python 2.7, if one is available.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">Under this heading, click the link titled <span class=\"code\">Download Windows x86 MSI Installer</span>.</p>\r\n<p class=\"child-para\">See Figure 3. This is a link for a 32-bit installation, which makes things work better with third-party libraries. Use the 32-bit installer even if you have a 64-bit machine and even if you have no idea what this paragraph is talking about.</p>\r\n\r\n<div class=\"imageBlock\" style=\"width: 535px;\">\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/497376.image2.png\" alt=\"<b>Figure </b><b>3:</b> Python x86 MSI Installer.\" width=\"535\" height=\"464\" />\r\n<div class=\"imageCaption\"><b>Figure </b><b>3:</b> Python x86 MSI Installer.</div>\r\n</div></li>\r\n</ol>\r\n<ol class=\"level-one\">\r\n \t<li>\r\n<p class=\"first-para\">If you're asked to choose whether to run or save the file, choose Run.</p>\r\n<p class=\"child-para\">This downloads <span class=\"code\">python2.7.9.msi</span> and starts running the installer.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">If you get a security warning when the installer begins (or at random times during the installation), choose Run.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">Accept the default installation options that the installer provides.</p>\r\n</li>\r\n</ol>\r\n<h2 id=\"tab3\" >How to install Python for Linux</h2>\r\n<p class=\"Warning\">If you're running Linux, confirm that you have version 2.7.9 of Python installed, rather than version 3. This shouldn't be a problem because Python 2.7 is installed by default in recent versions of OpenSuSE, Ubuntu, and Red Hat Fedora.</p>\r\nIn the nutty odd case when someone has Python 3 but not Python 2.7, read your distribution's documentation for how to use the package manager and get Python 2.7 and IDLE.","description":"Whether you use a Mac, Windows, or Linux OS (operating system), you can find and install <a href=\"https://www.dummies.com/article/technology/programming-web-design/python/write-a-simple-program-in-python-139547\" target=\"_blank\" rel=\"noopener\">Python </a>on your computer. The following sections give you instructions for each OS.\r\n<h2 id=\"tab1\" >How to install Python on Mac OSX</h2>\r\nTo find and start Python on Mac OSX computers, follow these steps:\r\n<ol class=\"level-one\">\r\n \t<li>\r\n<p class=\"first-para\">Press Cmd+spacebar to open Spotlight.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">Type the word <span class=\"code\">terminal</span>.</p>\r\n<p class=\"child-para\">Or, from the Finder, select Finder→Go→Utilities→Terminal.</p>\r\n<p class=\"child-para\">The Terminal window opens.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">In the terminal, type <span class=\"code\">python</span>.</p>\r\n<p class=\"child-para\">The Python interpreter that's built in to Mac OSX opens.</p>\r\n</li>\r\n</ol>\r\n<h2 id=\"tab2\" >How to install Python on Windows</h2>\r\nUnfortunately, Python doesn't come on Windows. If you're running Windows, then you need to download and install Python by following the instructions here. Installing Python on Windows isn't difficult. If you can download a file from a website, you have the skills to install Python.\r\n\r\nFortunately, the Python Foundation (the peeps who guide the development of Python) makes installable files available from its website.\r\n<p class=\"Tip\">Firefox and Internet Explorer responded differently to the Python download website, so the instructions are based on which of these browsers you use. If you use a whole other browser altogether, try the Internet Explorer instructions.</p>\r\n\r\n<h3>Installing with Firefox</h3>\r\nTo install Python on a Windows machine with Firefox, follow these steps:\r\n<ol class=\"level-one\">\r\n \t<li>\r\n<p class=\"first-para\">Visit <a href=\"http://www.python.org/downloads\">www.python.org/downloads</a>.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">Click the button that says Download Python 2.7.9.</p>\r\n<p class=\"child-para\">Or, if it's there, click a more recent version number that starts with 2.7.</p>\r\n<p class=\"child-para\">Clicking this button automatically downloads and saves an <span class=\"code\">msi</span> file for you. If not, try the instructions for Internet Explorer. See Figure 1.</p>\r\n\r\n<div class=\"imageBlock\" style=\"width: 535px;\">\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/497374.image0.png\" alt=\"<b>Figure 1</b><b>:</b> Download Python with Firefox.\" width=\"535\" height=\"448\" />\r\n<div class=\"imageCaption\"><b>Figure 1</b><b>:</b> Download Python with Firefox.</div>\r\n</div></li>\r\n \t<li>\r\n<p class=\"first-para\">When the download's complete, click the icon for Firefox's download tool.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">Click the file called <span class=\"code\">python-2.7.9.msi</span> (or the more recent version, if you downloaded one).</p>\r\n<p class=\"child-para\">Python 2.7.9 installs on your computer.</p>\r\n</li>\r\n</ol>\r\n<h3>Installing with Internet Explorer</h3>\r\nTo install Python on a Windows machine with Internet Explorer, follow these steps:\r\n<ol class=\"level-one\">\r\n \t<li>\r\n<p class=\"first-para\">Visit <a href=\"http://www.python.org/downloads\">www.python.org/downloads</a>.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">From the menu bar, select Downloads→Windows.</p>\r\n<p class=\"child-para\">You can see the menu options in Figure 2.</p>\r\n\r\n<div class=\"imageBlock\" style=\"width: 535px;\">\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/497375.image1.png\" alt=\"<b>Figure </b><b>2:</b> Download Python with Internet Explorer.\" width=\"535\" height=\"304\" />\r\n<div class=\"imageCaption\"><b>Figure </b><b>2:</b> Download Python with Internet Explorer.</div>\r\n</div></li>\r\n \t<li>\r\n<p class=\"first-para\">Scroll down to the heading Python 2.7.9-2014-12-10.</p>\r\n<p class=\"child-para\">Or scroll to a more recent version, which starts with Python 2.7, if one is available.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">Under this heading, click the link titled <span class=\"code\">Download Windows x86 MSI Installer</span>.</p>\r\n<p class=\"child-para\">See Figure 3. This is a link for a 32-bit installation, which makes things work better with third-party libraries. Use the 32-bit installer even if you have a 64-bit machine and even if you have no idea what this paragraph is talking about.</p>\r\n\r\n<div class=\"imageBlock\" style=\"width: 535px;\">\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/497376.image2.png\" alt=\"<b>Figure </b><b>3:</b> Python x86 MSI Installer.\" width=\"535\" height=\"464\" />\r\n<div class=\"imageCaption\"><b>Figure </b><b>3:</b> Python x86 MSI Installer.</div>\r\n</div></li>\r\n</ol>\r\n<ol class=\"level-one\">\r\n \t<li>\r\n<p class=\"first-para\">If you're asked to choose whether to run or save the file, choose Run.</p>\r\n<p class=\"child-para\">This downloads <span class=\"code\">python2.7.9.msi</span> and starts running the installer.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">If you get a security warning when the installer begins (or at random times during the installation), choose Run.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">Accept the default installation options that the installer provides.</p>\r\n</li>\r\n</ol>\r\n<h2 id=\"tab3\" >How to install Python for Linux</h2>\r\n<p class=\"Warning\">If you're running Linux, confirm that you have version 2.7.9 of Python installed, rather than version 3. This shouldn't be a problem because Python 2.7 is installed by default in recent versions of OpenSuSE, Ubuntu, and Red Hat Fedora.</p>\r\nIn the nutty odd case when someone has Python 3 but not Python 2.7, read your distribution's documentation for how to use the package manager and get Python 2.7 and IDLE.","blurb":"","authors":[{"authorId":9026,"name":"Brendan Scott","slug":"brendan-scott","description":"Brendan Scott is a dad who loves Python and wants kids to experience its magic, too. He started python4kids.brendanscott.com to teach his oldest child to code. He maintains it to help other young people learn Python. ","_links":{"self":"https://dummies-api.dummies.com/v2/authors/9026"}}],"primaryCategoryTaxonomy":{"categoryId":33606,"title":"Python","slug":"python","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33606"}},"secondaryCategoryTaxonomy":{"categoryId":0,"title":null,"slug":null,"_links":null},"tertiaryCategoryTaxonomy":{"categoryId":0,"title":null,"slug":null,"_links":null},"trendingArticles":null,"inThisArticle":[{"label":"How to install Python on Mac OSX","target":"#tab1"},{"label":"How to install Python on Windows","target":"#tab2"},{"label":"How to install Python for Linux","target":"#tab3"}],"relatedArticles":{"fromBook":[{"articleId":207407,"title":"Python For Kids For Dummies Cheat Sheet","slug":"python-for-kids-for-dummies-cheat-sheet","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/207407"}},{"articleId":141581,"title":"Use Python to Help with Your Math Homework","slug":"use-python-to-help-with-your-math-homework","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/141581"}},{"articleId":141474,"title":"Python 2.7 Keyword Subset and Examples","slug":"python-2-7-keyword-subset-and-examples","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/141474"}},{"articleId":141443,"title":"Using Tkinter Widgets in Python","slug":"using-tkinter-widgets-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/141443"}},{"articleId":139551,"title":"How to Interrupt a Program in Python","slug":"how-to-interrupt-a-program-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/139551"}}],"fromCategory":[{"articleId":264919,"title":"How to Define and Use Python Lists","slug":"how-to-define-and-use-python-lists","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264919"}},{"articleId":264911,"title":"How to Use Lambda Functions in Python","slug":"how-to-use-lambda-functions-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264911"}},{"articleId":264906,"title":"Your Guide to the Python Standard Library","slug":"your-guide-to-the-python-standard-library","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264906"}},{"articleId":264894,"title":"A Beginner’s Guide to Python Versions","slug":"a-beginners-guide-to-python-versions","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264894"}},{"articleId":264888,"title":"How to Build a Simple Neural Network in Python","slug":"how-to-build-a-simple-neural-network-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264888"}}]},"hasRelatedBookFromSearch":false,"relatedBook":{"bookId":281835,"slug":"python-for-kids-for-dummies","isbn":"9781119093107","categoryList":["technology","programming-web-design","python"],"amazon":{"default":"https://www.amazon.com/gp/product/1119093104/ref=as_li_tl?ie=UTF8&tag=wiley01-20","ca":"https://www.amazon.ca/gp/product/1119093104/ref=as_li_tl?ie=UTF8&tag=wiley01-20","indigo_ca":"http://www.tkqlhce.com/click-9208661-13710633?url=https://www.chapters.indigo.ca/en-ca/books/product/1119093104-item.html&cjsku=978111945484","gb":"https://www.amazon.co.uk/gp/product/1119093104/ref=as_li_tl?ie=UTF8&tag=wiley01-20","de":"https://www.amazon.de/gp/product/1119093104/ref=as_li_tl?ie=UTF8&tag=wiley01-20"},"image":{"src":"https://www.dummies.com/wp-content/uploads/python-for-kids-for-dummies-cover-9781119093107-203x255.jpg","width":203,"height":255},"title":"Python For Kids For Dummies","testBankPinActivationLink":"","bookOutOfPrint":false,"authorsInfo":"\n <p>Brendan Scott is a dad who loves Python and wants kids to get some of its magic too. He started pythonforkids.brendanscott.com to help teach his oldest child to code. He maintains it to help other young people learn Python.</p> ","authors":[{"authorId":9026,"name":"Brendan Scott","slug":"brendan-scott","description":"Brendan Scott is a dad who loves Python and wants kids to experience its magic, too. He started python4kids.brendanscott.com to teach his oldest child to code. He maintains it to help other young people learn Python. ","_links":{"self":"https://dummies-api.dummies.com/v2/authors/9026"}}],"_links":{"self":"https://dummies-api.dummies.com/v2/books/"}},"collections":[],"articleAds":{"footerAd":"<div class=\"du-ad-region row\" id=\"article_page_adhesion_ad\"><div class=\"du-ad-unit col-md-12\" data-slot-id=\"article_page_adhesion_ad\" data-refreshed=\"false\" \r\n data-target = \"[{&quot;key&quot;:&quot;cat&quot;,&quot;values&quot;:[&quot;technology&quot;,&quot;programming-web-design&quot;,&quot;python&quot;]},{&quot;key&quot;:&quot;isbn&quot;,&quot;values&quot;:[&quot;9781119093107&quot;]}]\" id=\"du-slot-6217bb4e3032a\"></div></div>","rightAd":"<div class=\"du-ad-region row\" id=\"article_page_right_ad\"><div class=\"du-ad-unit col-md-12\" data-slot-id=\"article_page_right_ad\" data-refreshed=\"false\" \r\n data-target = \"[{&quot;key&quot;:&quot;cat&quot;,&quot;values&quot;:[&quot;technology&quot;,&quot;programming-web-design&quot;,&quot;python&quot;]},{&quot;key&quot;:&quot;isbn&quot;,&quot;values&quot;:[&quot;9781119093107&quot;]}]\" id=\"du-slot-6217bb4e30cee\"></div></div>"},"articleType":{"articleType":"Articles","articleList":null,"content":null,"videoInfo":{"videoId":null,"name":null,"accountId":null,"playerId":null,"thumbnailUrl":null,"description":null,"uploadDate":null}},"sponsorship":{"sponsorshipPage":false,"backgroundImage":{"src":null,"width":0,"height":0},"brandingLine":"","brandingLink":"","brandingLogo":{"src":null,"width":0,"height":0}},"primaryLearningPath":"Explore","lifeExpectancy":"Two years","lifeExpectancySetFrom":"2022-01-25T00:00:00+00:00","dummiesForKids":"no","sponsoredContent":"no","adInfo":"","adPairKey":[]},"status":"publish","visibility":"public","articleId":139548},{"headers":{"creationTime":"2016-03-26T07:16:01+00:00","modifiedTime":"2022-01-25T14:52:34+00:00","timestamp":"2022-02-24T17:07:26+00:00"},"data":{"breadcrumbs":[{"name":"Technology","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33512"},"slug":"technology","categoryId":33512},{"name":"Programming & Web Design","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33592"},"slug":"programming-web-design","categoryId":33592},{"name":"Python","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33606"},"slug":"python","categoryId":33606}],"title":"Write a Simple Program in Python","strippedTitle":"write a simple program in python","slug":"write-a-simple-program-in-python","canonicalUrl":"","seo":{"metaDescription":"Just getting started with Python? Learn how to write your first program and follow in the footsteps of many great programmers.","noIndex":0,"noFollow":0},"content":"Tradition dictates that Hello World! be the first program that you write when you're learning a <a href=\"https://www.dummies.com/programming/python/10-major-uses-of-python/\" target=\"_blank\" rel=\"noopener\">new programming language like Python</a>. You're following in the footsteps of many great programmers when you create this project.\r\n\r\nTo create your Hello World! program, follow these steps:\r\n<ol class=\"level-one\">\r\n \t<li>\r\n<p class=\"first-para\">Open your Start menu and choose Python (command line).</p>\r\n<p class=\"child-para\">You should get a prompt that looks like <span class=\"code\">>>></span>.</p>\r\n<p class=\"child-para\">At the moment, you're doing everything in interactive mode in the Python interpreter. That's where the <span class=\"code\">>>></span> comes in. Python shows you <span class=\"code\">>>></span> when you're supposed to type something.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">At the prompt, type the following. Use a single quote at the start and the end — it's beside the Enter key:</p>\r\n\r\n<pre class=\"code\">print('Hello World!')</pre>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">Press the Enter key.</p>\r\n<p class=\"child-para\">Python runs the code you typed.</p>\r\n</li>\r\n</ol>\r\nYou see the output shown in Figure 1. Congratulations — you've written your first program. Welcome to the <a href=\"https://www.dummies.com/article/technology/programming-web-design/python/how-to-install-python-on-your-computer-139548\" target=\"_blank\" rel=\"noopener\">Python</a>-programmers-in-training club.\r\n<div class=\"imageBlock\" style=\"width: 535px;\">\r\n\r\n[caption id=\"\" align=\"alignnone\" width=\"535\"]<img src=\"https://www.dummies.com/wp-content/uploads/497387.image0.png\" alt=\"<b>Figure </b><b>1:</b> Your Hello World! program is ready for more instructions.\" width=\"535\" height=\"267\" /> Figure 1: Your Hello World! program is ready for more instructions.[/caption]\r\n\r\n<div></div>\r\n<div class=\"imageCaption\">If you don't see what's in Figure 1, check that you typed in the text from Step 2 exactly as it's written:</div>\r\n</div>\r\n<ul class=\"level-one\">\r\n \t<li>\r\n<p class=\"first-para\">Check that the parentheses and single quotes are in the right places.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">Check that for each opening parenthesis there is a closing parenthesis. (Otherwise, you're left hanging.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">Check that for each opening quote there's a closing quote.</p>\r\n</li>\r\n</ul>\r\n<p class=\"Remember\">Programming languages have their own grammar and punctuation rules. These rules are the language's <i>syntax</i>. Humans, can work most stuff out even if perfect not you're is grammar (See? You figured out what that sentence was trying to say), but Python pretty much freaks out if you get the syntax wrong.</p>","description":"Tradition dictates that Hello World! be the first program that you write when you're learning a <a href=\"https://www.dummies.com/programming/python/10-major-uses-of-python/\" target=\"_blank\" rel=\"noopener\">new programming language like Python</a>. You're following in the footsteps of many great programmers when you create this project.\r\n\r\nTo create your Hello World! program, follow these steps:\r\n<ol class=\"level-one\">\r\n \t<li>\r\n<p class=\"first-para\">Open your Start menu and choose Python (command line).</p>\r\n<p class=\"child-para\">You should get a prompt that looks like <span class=\"code\">>>></span>.</p>\r\n<p class=\"child-para\">At the moment, you're doing everything in interactive mode in the Python interpreter. That's where the <span class=\"code\">>>></span> comes in. Python shows you <span class=\"code\">>>></span> when you're supposed to type something.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">At the prompt, type the following. Use a single quote at the start and the end — it's beside the Enter key:</p>\r\n\r\n<pre class=\"code\">print('Hello World!')</pre>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">Press the Enter key.</p>\r\n<p class=\"child-para\">Python runs the code you typed.</p>\r\n</li>\r\n</ol>\r\nYou see the output shown in Figure 1. Congratulations — you've written your first program. Welcome to the <a href=\"https://www.dummies.com/article/technology/programming-web-design/python/how-to-install-python-on-your-computer-139548\" target=\"_blank\" rel=\"noopener\">Python</a>-programmers-in-training club.\r\n<div class=\"imageBlock\" style=\"width: 535px;\">\r\n\r\n[caption id=\"\" align=\"alignnone\" width=\"535\"]<img src=\"https://www.dummies.com/wp-content/uploads/497387.image0.png\" alt=\"<b>Figure </b><b>1:</b> Your Hello World! program is ready for more instructions.\" width=\"535\" height=\"267\" /> Figure 1: Your Hello World! program is ready for more instructions.[/caption]\r\n\r\n<div></div>\r\n<div class=\"imageCaption\">If you don't see what's in Figure 1, check that you typed in the text from Step 2 exactly as it's written:</div>\r\n</div>\r\n<ul class=\"level-one\">\r\n \t<li>\r\n<p class=\"first-para\">Check that the parentheses and single quotes are in the right places.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">Check that for each opening parenthesis there is a closing parenthesis. (Otherwise, you're left hanging.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">Check that for each opening quote there's a closing quote.</p>\r\n</li>\r\n</ul>\r\n<p class=\"Remember\">Programming languages have their own grammar and punctuation rules. These rules are the language's <i>syntax</i>. Humans, can work most stuff out even if perfect not you're is grammar (See? You figured out what that sentence was trying to say), but Python pretty much freaks out if you get the syntax wrong.</p>","blurb":"","authors":[{"authorId":9026,"name":"Brendan Scott","slug":"brendan-scott","description":"Brendan Scott is a dad who loves Python and wants kids to experience its magic, too. He started python4kids.brendanscott.com to teach his oldest child to code. He maintains it to help other young people learn Python. ","_links":{"self":"https://dummies-api.dummies.com/v2/authors/9026"}}],"primaryCategoryTaxonomy":{"categoryId":33606,"title":"Python","slug":"python","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33606"}},"secondaryCategoryTaxonomy":{"categoryId":0,"title":null,"slug":null,"_links":null},"tertiaryCategoryTaxonomy":{"categoryId":0,"title":null,"slug":null,"_links":null},"trendingArticles":null,"inThisArticle":[],"relatedArticles":{"fromBook":[{"articleId":207407,"title":"Python For Kids For Dummies Cheat Sheet","slug":"python-for-kids-for-dummies-cheat-sheet","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/207407"}},{"articleId":141581,"title":"Use Python to Help with Your Math Homework","slug":"use-python-to-help-with-your-math-homework","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/141581"}},{"articleId":141474,"title":"Python 2.7 Keyword Subset and Examples","slug":"python-2-7-keyword-subset-and-examples","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/141474"}},{"articleId":141443,"title":"Using Tkinter Widgets in Python","slug":"using-tkinter-widgets-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/141443"}},{"articleId":139551,"title":"How to Interrupt a Program in Python","slug":"how-to-interrupt-a-program-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/139551"}}],"fromCategory":[{"articleId":264919,"title":"How to Define and Use Python Lists","slug":"how-to-define-and-use-python-lists","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264919"}},{"articleId":264911,"title":"How to Use Lambda Functions in Python","slug":"how-to-use-lambda-functions-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264911"}},{"articleId":264906,"title":"Your Guide to the Python Standard Library","slug":"your-guide-to-the-python-standard-library","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264906"}},{"articleId":264894,"title":"A Beginner’s Guide to Python Versions","slug":"a-beginners-guide-to-python-versions","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264894"}},{"articleId":264888,"title":"How to Build a Simple Neural Network in Python","slug":"how-to-build-a-simple-neural-network-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264888"}}]},"hasRelatedBookFromSearch":false,"relatedBook":{"bookId":281835,"slug":"python-for-kids-for-dummies","isbn":"9781119093107","categoryList":["technology","programming-web-design","python"],"amazon":{"default":"https://www.amazon.com/gp/product/1119093104/ref=as_li_tl?ie=UTF8&tag=wiley01-20","ca":"https://www.amazon.ca/gp/product/1119093104/ref=as_li_tl?ie=UTF8&tag=wiley01-20","indigo_ca":"http://www.tkqlhce.com/click-9208661-13710633?url=https://www.chapters.indigo.ca/en-ca/books/product/1119093104-item.html&cjsku=978111945484","gb":"https://www.amazon.co.uk/gp/product/1119093104/ref=as_li_tl?ie=UTF8&tag=wiley01-20","de":"https://www.amazon.de/gp/product/1119093104/ref=as_li_tl?ie=UTF8&tag=wiley01-20"},"image":{"src":"https://www.dummies.com/wp-content/uploads/python-for-kids-for-dummies-cover-9781119093107-203x255.jpg","width":203,"height":255},"title":"Python For Kids For Dummies","testBankPinActivationLink":"","bookOutOfPrint":false,"authorsInfo":"\n <p>Brendan Scott is a dad who loves Python and wants kids to get some of its magic too. He started pythonforkids.brendanscott.com to help teach his oldest child to code. He maintains it to help other young people learn Python.</p> ","authors":[{"authorId":9026,"name":"Brendan Scott","slug":"brendan-scott","description":"Brendan Scott is a dad who loves Python and wants kids to experience its magic, too. He started python4kids.brendanscott.com to teach his oldest child to code. He maintains it to help other young people learn Python. ","_links":{"self":"https://dummies-api.dummies.com/v2/authors/9026"}}],"_links":{"self":"https://dummies-api.dummies.com/v2/books/"}},"collections":[],"articleAds":{"footerAd":"<div class=\"du-ad-region row\" id=\"article_page_adhesion_ad\"><div class=\"du-ad-unit col-md-12\" data-slot-id=\"article_page_adhesion_ad\" data-refreshed=\"false\" \r\n data-target = \"[{&quot;key&quot;:&quot;cat&quot;,&quot;values&quot;:[&quot;technology&quot;,&quot;programming-web-design&quot;,&quot;python&quot;]},{&quot;key&quot;:&quot;isbn&quot;,&quot;values&quot;:[&quot;9781119093107&quot;]}]\" id=\"du-slot-6217bb4e27e7a\"></div></div>","rightAd":"<div class=\"du-ad-region row\" id=\"article_page_right_ad\"><div class=\"du-ad-unit col-md-12\" data-slot-id=\"article_page_right_ad\" data-refreshed=\"false\" \r\n data-target = \"[{&quot;key&quot;:&quot;cat&quot;,&quot;values&quot;:[&quot;technology&quot;,&quot;programming-web-design&quot;,&quot;python&quot;]},{&quot;key&quot;:&quot;isbn&quot;,&quot;values&quot;:[&quot;9781119093107&quot;]}]\" id=\"du-slot-6217bb4e2882b\"></div></div>"},"articleType":{"articleType":"Articles","articleList":null,"content":null,"videoInfo":{"videoId":null,"name":null,"accountId":null,"playerId":null,"thumbnailUrl":null,"description":null,"uploadDate":null}},"sponsorship":{"sponsorshipPage":false,"backgroundImage":{"src":null,"width":0,"height":0},"brandingLine":"","brandingLink":"","brandingLogo":{"src":null,"width":0,"height":0}},"primaryLearningPath":"Explore","lifeExpectancy":"One year","lifeExpectancySetFrom":"2022-01-25T00:00:00+00:00","dummiesForKids":"no","sponsoredContent":"no","adInfo":"","adPairKey":[]},"status":"publish","visibility":"public","articleId":139547},{"headers":{"creationTime":"2016-03-26T10:49:10+00:00","modifiedTime":"2021-12-29T14:22:25+00:00","timestamp":"2022-02-24T17:07:21+00:00"},"data":{"breadcrumbs":[{"name":"Technology","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33512"},"slug":"technology","categoryId":33512},{"name":"Programming & Web Design","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33592"},"slug":"programming-web-design","categoryId":33592},{"name":"Python","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33606"},"slug":"python","categoryId":33606}],"title":"How to Format Strings in Python","strippedTitle":"how to format strings in python","slug":"how-to-format-strings-in-python","canonicalUrl":"","seo":{"metaDescription":"","noIndex":0,"noFollow":0},"content":"You can format strings in a number of ways using Python. The main emphasis of formatting is to present the string in a form that is both pleasing to the user and easy to understand. Formatting doesn’t mean adding effects in this case, but refers merely to the presentation of the data. For example, the user might want a fixed-point number rather than a decimal number as output.\r\n\r\nYou have quite a few ways to format strings. However, the focus of most formatting is the <span class=\"code\">format()</span> function. You create a formatting specification as part of the string and then use the <span class=\"code\">format()</span> function to add data to that string. A format specification may be as simple as two curly brackets <span class=\"code\">{}</span> that specify a placeholder for data.\r\n\r\nYou can number the placeholder to create special effects. For example, <span class=\"code\">{0}</span> would contain the first data element in a string. When the data elements are numbered, you can even repeat them so that the same data appears more than once in the string.\r\n<p class=\"Remember\">The formatting specification follows a colon. When you want to create just a formatting specification, the curly brackets contain just the colon and whatever formatting you want to use.</p>\r\nFor example, <span class=\"code\">{:f}</span> would create a fixed-point number as output. If you want to number the entries, the number that precedes the colon: <span class=\"code\">{0:f}</span> creates a fixed-point number output for data element one. The formatting specification follows this form, with the italicized elements serving as placeholders here:\r\n<pre class=\"code\">[[fill]align][sign][#][0][width][,][.precision][type]</pre>\r\nThis <a href=\"https://docs.python.org/3/library/string.html\" target=\"_blank\" rel=\"noopener\">specification</a> provides you with the in-depth details, but here’s an overview of what the various entries mean:\r\n<ul class=\"level-one\">\r\n \t<li>\r\n<p class=\"first-para\"><b>fill:</b> Defines the fill character used when displaying data that is too small to fit within the assigned space.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>align:</b> Specifies the alignment of data within the display space. You can use these alignments:</p>\r\n\r\n<ul class=\"level-two\">\r\n \t<li>\r\n<p class=\"first-para\"><b><:</b> Left aligned</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>>:</b> Right aligned</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>^:</b> Centered</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>=:</b> Justified</p>\r\n</li>\r\n</ul>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>sign:</b> Determines the use of signs for the output:</p>\r\n\r\n<ul class=\"level-two\">\r\n \t<li>\r\n<p class=\"first-para\"><b>+:</b> Positive numbers have a plus sign and negative numbers have a minus sign.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>-:</b> Negative numbers have a minus sign.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b><</b>space<b>>:</b> Positive numbers are preceded by a space and negative numbers have a minus sign.</p>\r\n</li>\r\n</ul>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>#:</b> Specifies that the output should use the alternative display format for numbers. For example, hexadecimal numbers will have a 0x prefix added to them.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>0:</b> Specifies that the output should be sign aware and padded with zeros as needed to provide consistent output.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>width:</b> Determines the full width of the data field (even if the data won’t fit in the space provided).</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>,:</b> Specifies that numeric data should have commas as a thousands separator.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>.precision:</b> Determines the number of characters after the decimal point.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>type:</b> Specifies the output type, even if the input type doesn’t match. The types are split into three groups:</p>\r\n\r\n<ul class=\"level-two\">\r\n \t<li>\r\n<p class=\"first-para\"><i>String:</i> Use an <span class=\"code\">s</span> or nothing at all to specify a string.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><i>Integer:</i> The integer types are as follows: <span class=\"code\">b</span> (binary); <span class=\"code\">c</span> (character); <span class=\"code\">d</span> (decimal); <span class=\"code\">o</span> (octal); <span class=\"code\">x</span> (hexadecimal with lowercase letters); <span class=\"code\">X</span> (hexadecimal with uppercase letters); and <span class=\"code\">n</span> (locale-sensitive decimal that uses the appropriate characters for the thousands separator).</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><i>Floating point:</i> The floating-point types are as follows: <span class=\"code\">e</span> (exponent using a lowercase e as a separator); <span class=\"code\">E</span> (exponent using an uppercase E as a separator); <span class=\"code\">f</span> (lowercase fixed point); <span class=\"code\">F</span> (uppercase fixed point); <span class=\"code\">g</span> (lowercase general format); <span class=\"code\">G</span> (uppercase general format); <span class=\"code\">n</span> (local-sensitive general format that uses the appropriate characters for the decimal and thousands separators); and <span class=\"code\">%</span> (percentage).</p>\r\n</li>\r\n</ul>\r\n</li>\r\n</ul>\r\nThe formatting specification elements must appear in the correct order or Python won’t know what to do with them. If you specify the alignment before the fill character, Python displays an error message rather than performing the required formatting. The following steps help you see how the formatting specification works and demonstrate the order you need to follow in using the various formatting specification criteria.\r\n<ol class=\"level-one\">\r\n \t<li>\r\n<p class=\"first-para\">Open a Python File window.</p>\r\n<p class=\"child-para\">You see an editor in which you can type the example code.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">Type the following code into the window — pressing Enter after each line:</p>\r\n\r\n<pre class=\"code\">Formatted = \"{:d}\"\r\nprint(Formatted.format(7000))\r\nFormatted = \"{:,d}\"\r\nprint(Formatted.format(7000))\r\nFormatted = \"{:^15,d}\"\r\nprint(Formatted.format(7000))\r\nFormatted = \"{:*^15,d}\"\r\nprint(Formatted.format(7000))\r\nFormatted = \"{:*^15.2f}\"\r\nprint(Formatted.format(7000))\r\nFormatted = \"{:*>15X}\"\r\nprint(Formatted.format(7000))\r\nFormatted = \"{:*<#15x}\"\r\nprint(Formatted.format(7000))\r\nFormatted = \"A {0} {1} and a {0} {2}.\"\r\nprint(Formatted.format(\"blue\", \"car\", \"truck\"))</pre>\r\n<p class=\"child-para\">The example starts simply with a field formatted as a decimal value. It then adds a thousands separator to the output. The next step is to make the field wider than needed to hold the data and to center the data within the field. Finally, the field has an asterisk added to pad the output.</p>\r\n<p class=\"child-para\">Of course, the example contains other data types. The next step is to display the same data in fixed-point format. The example also shows the output in both uppercase and lowercase hexadecimal format. The uppercase output is right aligned and the lowercase output is left aligned.</p>\r\n<p class=\"child-para\">Finally, the example shows how you can use numbered fields to your advantage. In this case, it creates an interesting string output that repeats one of the input values.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">Click Run Cell.</p>\r\n<p class=\"child-para\">Python outputs data in various forms, as shown in the figure below.</p>\r\n<img class=\"alignnone size-full wp-image-290062\" src=\"https://www.dummies.com/wp-content/uploads/formating-strings-python.jpg\" alt=\"Formatting strings in Python\" width=\"630\" height=\"455\" /></li>\r\n</ol>","description":"You can format strings in a number of ways using Python. The main emphasis of formatting is to present the string in a form that is both pleasing to the user and easy to understand. Formatting doesn’t mean adding effects in this case, but refers merely to the presentation of the data. For example, the user might want a fixed-point number rather than a decimal number as output.\r\n\r\nYou have quite a few ways to format strings. However, the focus of most formatting is the <span class=\"code\">format()</span> function. You create a formatting specification as part of the string and then use the <span class=\"code\">format()</span> function to add data to that string. A format specification may be as simple as two curly brackets <span class=\"code\">{}</span> that specify a placeholder for data.\r\n\r\nYou can number the placeholder to create special effects. For example, <span class=\"code\">{0}</span> would contain the first data element in a string. When the data elements are numbered, you can even repeat them so that the same data appears more than once in the string.\r\n<p class=\"Remember\">The formatting specification follows a colon. When you want to create just a formatting specification, the curly brackets contain just the colon and whatever formatting you want to use.</p>\r\nFor example, <span class=\"code\">{:f}</span> would create a fixed-point number as output. If you want to number the entries, the number that precedes the colon: <span class=\"code\">{0:f}</span> creates a fixed-point number output for data element one. The formatting specification follows this form, with the italicized elements serving as placeholders here:\r\n<pre class=\"code\">[[fill]align][sign][#][0][width][,][.precision][type]</pre>\r\nThis <a href=\"https://docs.python.org/3/library/string.html\" target=\"_blank\" rel=\"noopener\">specification</a> provides you with the in-depth details, but here’s an overview of what the various entries mean:\r\n<ul class=\"level-one\">\r\n \t<li>\r\n<p class=\"first-para\"><b>fill:</b> Defines the fill character used when displaying data that is too small to fit within the assigned space.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>align:</b> Specifies the alignment of data within the display space. You can use these alignments:</p>\r\n\r\n<ul class=\"level-two\">\r\n \t<li>\r\n<p class=\"first-para\"><b><:</b> Left aligned</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>>:</b> Right aligned</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>^:</b> Centered</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>=:</b> Justified</p>\r\n</li>\r\n</ul>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>sign:</b> Determines the use of signs for the output:</p>\r\n\r\n<ul class=\"level-two\">\r\n \t<li>\r\n<p class=\"first-para\"><b>+:</b> Positive numbers have a plus sign and negative numbers have a minus sign.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>-:</b> Negative numbers have a minus sign.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b><</b>space<b>>:</b> Positive numbers are preceded by a space and negative numbers have a minus sign.</p>\r\n</li>\r\n</ul>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>#:</b> Specifies that the output should use the alternative display format for numbers. For example, hexadecimal numbers will have a 0x prefix added to them.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>0:</b> Specifies that the output should be sign aware and padded with zeros as needed to provide consistent output.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>width:</b> Determines the full width of the data field (even if the data won’t fit in the space provided).</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>,:</b> Specifies that numeric data should have commas as a thousands separator.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>.precision:</b> Determines the number of characters after the decimal point.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>type:</b> Specifies the output type, even if the input type doesn’t match. The types are split into three groups:</p>\r\n\r\n<ul class=\"level-two\">\r\n \t<li>\r\n<p class=\"first-para\"><i>String:</i> Use an <span class=\"code\">s</span> or nothing at all to specify a string.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><i>Integer:</i> The integer types are as follows: <span class=\"code\">b</span> (binary); <span class=\"code\">c</span> (character); <span class=\"code\">d</span> (decimal); <span class=\"code\">o</span> (octal); <span class=\"code\">x</span> (hexadecimal with lowercase letters); <span class=\"code\">X</span> (hexadecimal with uppercase letters); and <span class=\"code\">n</span> (locale-sensitive decimal that uses the appropriate characters for the thousands separator).</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><i>Floating point:</i> The floating-point types are as follows: <span class=\"code\">e</span> (exponent using a lowercase e as a separator); <span class=\"code\">E</span> (exponent using an uppercase E as a separator); <span class=\"code\">f</span> (lowercase fixed point); <span class=\"code\">F</span> (uppercase fixed point); <span class=\"code\">g</span> (lowercase general format); <span class=\"code\">G</span> (uppercase general format); <span class=\"code\">n</span> (local-sensitive general format that uses the appropriate characters for the decimal and thousands separators); and <span class=\"code\">%</span> (percentage).</p>\r\n</li>\r\n</ul>\r\n</li>\r\n</ul>\r\nThe formatting specification elements must appear in the correct order or Python won’t know what to do with them. If you specify the alignment before the fill character, Python displays an error message rather than performing the required formatting. The following steps help you see how the formatting specification works and demonstrate the order you need to follow in using the various formatting specification criteria.\r\n<ol class=\"level-one\">\r\n \t<li>\r\n<p class=\"first-para\">Open a Python File window.</p>\r\n<p class=\"child-para\">You see an editor in which you can type the example code.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">Type the following code into the window — pressing Enter after each line:</p>\r\n\r\n<pre class=\"code\">Formatted = \"{:d}\"\r\nprint(Formatted.format(7000))\r\nFormatted = \"{:,d}\"\r\nprint(Formatted.format(7000))\r\nFormatted = \"{:^15,d}\"\r\nprint(Formatted.format(7000))\r\nFormatted = \"{:*^15,d}\"\r\nprint(Formatted.format(7000))\r\nFormatted = \"{:*^15.2f}\"\r\nprint(Formatted.format(7000))\r\nFormatted = \"{:*>15X}\"\r\nprint(Formatted.format(7000))\r\nFormatted = \"{:*<#15x}\"\r\nprint(Formatted.format(7000))\r\nFormatted = \"A {0} {1} and a {0} {2}.\"\r\nprint(Formatted.format(\"blue\", \"car\", \"truck\"))</pre>\r\n<p class=\"child-para\">The example starts simply with a field formatted as a decimal value. It then adds a thousands separator to the output. The next step is to make the field wider than needed to hold the data and to center the data within the field. Finally, the field has an asterisk added to pad the output.</p>\r\n<p class=\"child-para\">Of course, the example contains other data types. The next step is to display the same data in fixed-point format. The example also shows the output in both uppercase and lowercase hexadecimal format. The uppercase output is right aligned and the lowercase output is left aligned.</p>\r\n<p class=\"child-para\">Finally, the example shows how you can use numbered fields to your advantage. In this case, it creates an interesting string output that repeats one of the input values.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\">Click Run Cell.</p>\r\n<p class=\"child-para\">Python outputs data in various forms, as shown in the figure below.</p>\r\n<img class=\"alignnone size-full wp-image-290062\" src=\"https://www.dummies.com/wp-content/uploads/formating-strings-python.jpg\" alt=\"Formatting strings in Python\" width=\"630\" height=\"455\" /></li>\r\n</ol>","blurb":"","authors":[{"authorId":9109,"name":"John Paul Mueller","slug":"john-paul-mueller","description":"John Paul Mueller has written more than 100 books and more than 600 articles on topics ranging from functional programming techniques to application development using C++. ","_links":{"self":"https://dummies-api.dummies.com/v2/authors/9109"}}],"primaryCategoryTaxonomy":{"categoryId":33606,"title":"Python","slug":"python","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33606"}},"secondaryCategoryTaxonomy":{"categoryId":0,"title":null,"slug":null,"_links":null},"tertiaryCategoryTaxonomy":{"categoryId":0,"title":null,"slug":null,"_links":null},"trendingArticles":null,"inThisArticle":[],"relatedArticles":{"fromBook":[{"articleId":250588,"title":"How to Get Additional Python Libraries","slug":"get-additional-python-libraries","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/250588"}},{"articleId":250582,"title":"Printing Lists Using Python","slug":"printing-lists-using-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/250582"}},{"articleId":250578,"title":"Understanding How Permanent Storage Works for Python Programming","slug":"understanding-permanent-storage-works-python-programming","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/250578"}},{"articleId":250575,"title":"Extending Python Classes to Make New Classes","slug":"extending-python-classes-make-new-classes","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/250575"}},{"articleId":250571,"title":"Understanding the Python Class as a Packaging Method","slug":"understanding-python-class-packaging-method","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/250571"}}],"fromCategory":[{"articleId":264919,"title":"How to Define and Use Python Lists","slug":"how-to-define-and-use-python-lists","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264919"}},{"articleId":264911,"title":"How to Use Lambda Functions in Python","slug":"how-to-use-lambda-functions-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264911"}},{"articleId":264906,"title":"Your Guide to the Python Standard Library","slug":"your-guide-to-the-python-standard-library","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264906"}},{"articleId":264894,"title":"A Beginner’s Guide to Python Versions","slug":"a-beginners-guide-to-python-versions","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264894"}},{"articleId":264888,"title":"How to Build a Simple Neural Network in Python","slug":"how-to-build-a-simple-neural-network-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264888"}}]},"hasRelatedBookFromSearch":false,"relatedBook":{"bookId":281830,"slug":"beginning-programming-with-python-for-dummies-2nd-edition","isbn":"9781119457893","categoryList":["technology","programming-web-design","python"],"amazon":{"default":"https://www.amazon.com/gp/product/1119457890/ref=as_li_tl?ie=UTF8&tag=wiley01-20","ca":"https://www.amazon.ca/gp/product/1119457890/ref=as_li_tl?ie=UTF8&tag=wiley01-20","indigo_ca":"http://www.tkqlhce.com/click-9208661-13710633?url=https://www.chapters.indigo.ca/en-ca/books/product/1119457890-item.html&cjsku=978111945484","gb":"https://www.amazon.co.uk/gp/product/1119457890/ref=as_li_tl?ie=UTF8&tag=wiley01-20","de":"https://www.amazon.de/gp/product/1119457890/ref=as_li_tl?ie=UTF8&tag=wiley01-20"},"image":{"src":"https://www.dummies.com/wp-content/uploads/beginning-programming-with-python-for-dummies-2nd-edition-cover-9781119457893-203x255.jpg","width":203,"height":255},"title":"Beginning Programming with Python For Dummies, 2nd Edition","testBankPinActivationLink":"","bookOutOfPrint":false,"authorsInfo":"\n <p><b data-author-id=\"9109\">John Paul Mueller</b> is a freelance author and technical editor with more than 107 books and 600 articles to his credit. His subjects range from networking and artificial intelligence to database management and heads-down programming. He also consults and writes certification exams. Visit his website at http://www.johnmuellerbooks.com/. </p>","authors":[{"authorId":9109,"name":"John Paul Mueller","slug":"john-paul-mueller","description":"John Paul Mueller has written more than 100 books and more than 600 articles on topics ranging from functional programming techniques to application development using C++. ","_links":{"self":"https://dummies-api.dummies.com/v2/authors/9109"}}],"_links":{"self":"https://dummies-api.dummies.com/v2/books/"}},"collections":[],"articleAds":{"footerAd":"<div class=\"du-ad-region row\" id=\"article_page_adhesion_ad\"><div class=\"du-ad-unit col-md-12\" data-slot-id=\"article_page_adhesion_ad\" data-refreshed=\"false\" \r\n data-target = \"[{&quot;key&quot;:&quot;cat&quot;,&quot;values&quot;:[&quot;technology&quot;,&quot;programming-web-design&quot;,&quot;python&quot;]},{&quot;key&quot;:&quot;isbn&quot;,&quot;values&quot;:[&quot;9781119457893&quot;]}]\" id=\"du-slot-6217bb4936c85\"></div></div>","rightAd":"<div class=\"du-ad-region row\" id=\"article_page_right_ad\"><div class=\"du-ad-unit col-md-12\" data-slot-id=\"article_page_right_ad\" data-refreshed=\"false\" \r\n data-target = \"[{&quot;key&quot;:&quot;cat&quot;,&quot;values&quot;:[&quot;technology&quot;,&quot;programming-web-design&quot;,&quot;python&quot;]},{&quot;key&quot;:&quot;isbn&quot;,&quot;values&quot;:[&quot;9781119457893&quot;]}]\" id=\"du-slot-6217bb493760e\"></div></div>"},"articleType":{"articleType":"Articles","articleList":null,"content":null,"videoInfo":{"videoId":null,"name":null,"accountId":null,"playerId":null,"thumbnailUrl":null,"description":null,"uploadDate":null}},"sponsorship":{"sponsorshipPage":false,"backgroundImage":{"src":null,"width":0,"height":0},"brandingLine":"","brandingLink":"","brandingLogo":{"src":null,"width":0,"height":0}},"primaryLearningPath":"Advance","lifeExpectancy":"One year","lifeExpectancySetFrom":"2021-12-27T00:00:00+00:00","dummiesForKids":"no","sponsoredContent":"no","adInfo":"","adPairKey":[]},"status":"publish","visibility":"public","articleId":148335},{"headers":{"creationTime":"2016-03-26T10:49:26+00:00","modifiedTime":"2021-12-28T20:08:48+00:00","timestamp":"2022-02-24T17:07:21+00:00"},"data":{"breadcrumbs":[{"name":"Technology","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33512"},"slug":"technology","categoryId":33512},{"name":"Programming & Web Design","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33592"},"slug":"programming-web-design","categoryId":33592},{"name":"Python","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33606"},"slug":"python","categoryId":33606}],"title":"How to Create Your First Python Application","strippedTitle":"how to create your first python application","slug":"how-to-create-your-first-python-application","canonicalUrl":"","seo":{"metaDescription":"","noIndex":0,"noFollow":0},"content":"It’s time to create your first Python application. Your initial IDLE Shell window won’t work for creating an application, so you can begin by creating a new Edit window for the application. You’ll type the required commands and then save the file to disk.\r\n<h2 id=\"tab1\" ><b>Open a new window</b></h2>\r\nThe initial IDLE Shell window is just fine for experimentation, but you need a nice, clean Edit window for typing your first application. The IDLE Shell window is interactive, which means that it gives you immediate feedback for any commands you type.\r\n\r\nThe Edit window provides a static environment, where you type commands, save them, and then run them after you type enough commands to create an application. The two windows serve distinctly different purposes.\r\n\r\nChoose File→New File to create a new window. A new window opens. Notice that the title bar says untitled instead of IDLE Shell 3.1.0. A Python Shell window will always have the word “Shell” in the title bar. The two windows also have some unique toolbar entries. For example, an Edit window includes the Run command, which you use later to test your application.\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444874.image0.jpg\" alt=\"image0.jpg\" width=\"535\" height=\"204\" />\r\n\r\nWorking with the Edit window is just like working with any other text editor. You have access to basic editing commands such as Copy, Cut, and Paste. Pressing Enter moves to the next line rather than executing a command as it would when working in the Python Shell window. That’s because the Edit window is a static environment — one where you type commands and save them for later reuse.\r\n\r\nThe Edit window also provides special commands to format the text. What you need to know now is that these formatting commands act differently from those in a standard text editor because they help you control the appearance of code rather than of generic text. Many of the formatting features work automatically, so you don’t need to worry about them now.\r\n\r\nFinally, the Edit window provides access to commands that tell Python to perform the steps in the procedure you create one at a time. This process is called <i>running</i> the application.\r\n<h2 id=\"tab2\" ><b>Typing the command</b></h2>\r\nAs with the Python Shell window, you can simply type a command into the Edit window. To see how this works, type <b>print(</b>. Notice that the Edit window provides you with helpful information about the <span class=\"code\">print()</span> command. The information is a little terse, so you may not understand it now.\r\n\r\nFor now, the word <span class=\"code\">value</span> is the one that you need to focus on. The <span class=\"code\">print()</span> command needs a value before it can print anything.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444875.image1.jpg\" alt=\"image1.jpg\" width=\"535\" height=\"264\" />\r\n\r\nFinish the command by typing <b>“This is a simple Python application.”)</b> and pressing Enter. This is one of the simplest applications you can create using Python.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444876.image2.jpg\" alt=\"image2.jpg\" width=\"535\" height=\"264\" />\r\n<h2 id=\"tab3\" ><b>Saving the file</b></h2>\r\nYou could run the application now if you wanted to. However, saving your application before you run it is always a good idea. That way, if you make a mistake that causes Python or the system to freeze for some reason, your application code is still safe. Saving the application makes it easier to go back later to determine what went wrong, make corrections, and try running the application again.\r\n\r\nChoose File→Save to display the Save As dialog box. The Edit window automatically chooses the Python3 10 folder to save the application in. However, this is where the Python code resides, and saving your application code in the same folder is a bad idea.\r\n\r\nIf you want, create a directory structure with similar names using a technique that works for your platform as you follow along.\r\n\r\nType <b>FirstApp.py</b> in the Filename field of the Save As dialog box and click Save. Your application code is now saved on disk and you can access it anytime you want.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444877.image3.jpg\" alt=\"image3.jpg\" width=\"535\" height=\"396\" />\r\n\r\nWhen you return to the Edit window, the title bar text changes. Notice that the title bar includes the full path to the application.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444878.image4.jpg\" alt=\"image4.jpg\" width=\"535\" height=\"264\" />","description":"It’s time to create your first Python application. Your initial IDLE Shell window won’t work for creating an application, so you can begin by creating a new Edit window for the application. You’ll type the required commands and then save the file to disk.\r\n<h2 id=\"tab1\" ><b>Open a new window</b></h2>\r\nThe initial IDLE Shell window is just fine for experimentation, but you need a nice, clean Edit window for typing your first application. The IDLE Shell window is interactive, which means that it gives you immediate feedback for any commands you type.\r\n\r\nThe Edit window provides a static environment, where you type commands, save them, and then run them after you type enough commands to create an application. The two windows serve distinctly different purposes.\r\n\r\nChoose File→New File to create a new window. A new window opens. Notice that the title bar says untitled instead of IDLE Shell 3.1.0. A Python Shell window will always have the word “Shell” in the title bar. The two windows also have some unique toolbar entries. For example, an Edit window includes the Run command, which you use later to test your application.\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444874.image0.jpg\" alt=\"image0.jpg\" width=\"535\" height=\"204\" />\r\n\r\nWorking with the Edit window is just like working with any other text editor. You have access to basic editing commands such as Copy, Cut, and Paste. Pressing Enter moves to the next line rather than executing a command as it would when working in the Python Shell window. That’s because the Edit window is a static environment — one where you type commands and save them for later reuse.\r\n\r\nThe Edit window also provides special commands to format the text. What you need to know now is that these formatting commands act differently from those in a standard text editor because they help you control the appearance of code rather than of generic text. Many of the formatting features work automatically, so you don’t need to worry about them now.\r\n\r\nFinally, the Edit window provides access to commands that tell Python to perform the steps in the procedure you create one at a time. This process is called <i>running</i> the application.\r\n<h2 id=\"tab2\" ><b>Typing the command</b></h2>\r\nAs with the Python Shell window, you can simply type a command into the Edit window. To see how this works, type <b>print(</b>. Notice that the Edit window provides you with helpful information about the <span class=\"code\">print()</span> command. The information is a little terse, so you may not understand it now.\r\n\r\nFor now, the word <span class=\"code\">value</span> is the one that you need to focus on. The <span class=\"code\">print()</span> command needs a value before it can print anything.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444875.image1.jpg\" alt=\"image1.jpg\" width=\"535\" height=\"264\" />\r\n\r\nFinish the command by typing <b>“This is a simple Python application.”)</b> and pressing Enter. This is one of the simplest applications you can create using Python.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444876.image2.jpg\" alt=\"image2.jpg\" width=\"535\" height=\"264\" />\r\n<h2 id=\"tab3\" ><b>Saving the file</b></h2>\r\nYou could run the application now if you wanted to. However, saving your application before you run it is always a good idea. That way, if you make a mistake that causes Python or the system to freeze for some reason, your application code is still safe. Saving the application makes it easier to go back later to determine what went wrong, make corrections, and try running the application again.\r\n\r\nChoose File→Save to display the Save As dialog box. The Edit window automatically chooses the Python3 10 folder to save the application in. However, this is where the Python code resides, and saving your application code in the same folder is a bad idea.\r\n\r\nIf you want, create a directory structure with similar names using a technique that works for your platform as you follow along.\r\n\r\nType <b>FirstApp.py</b> in the Filename field of the Save As dialog box and click Save. Your application code is now saved on disk and you can access it anytime you want.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444877.image3.jpg\" alt=\"image3.jpg\" width=\"535\" height=\"396\" />\r\n\r\nWhen you return to the Edit window, the title bar text changes. Notice that the title bar includes the full path to the application.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444878.image4.jpg\" alt=\"image4.jpg\" width=\"535\" height=\"264\" />","blurb":"","authors":[{"authorId":9109,"name":"John Paul Mueller","slug":"john-paul-mueller","description":"John Paul Mueller has written more than 100 books and more than 600 articles on topics ranging from functional programming techniques to application development using C++. ","_links":{"self":"https://dummies-api.dummies.com/v2/authors/9109"}}],"primaryCategoryTaxonomy":{"categoryId":33606,"title":"Python","slug":"python","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33606"}},"secondaryCategoryTaxonomy":{"categoryId":0,"title":null,"slug":null,"_links":null},"tertiaryCategoryTaxonomy":{"categoryId":0,"title":null,"slug":null,"_links":null},"trendingArticles":null,"inThisArticle":[{"label":"Open a new window","target":"#tab1"},{"label":"Typing the command","target":"#tab2"},{"label":"Saving the file","target":"#tab3"}],"relatedArticles":{"fromBook":[],"fromCategory":[{"articleId":264919,"title":"How to Define and Use Python Lists","slug":"how-to-define-and-use-python-lists","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264919"}},{"articleId":264911,"title":"How to Use Lambda Functions in Python","slug":"how-to-use-lambda-functions-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264911"}},{"articleId":264906,"title":"Your Guide to the Python Standard Library","slug":"your-guide-to-the-python-standard-library","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264906"}},{"articleId":264894,"title":"A Beginner’s Guide to Python Versions","slug":"a-beginners-guide-to-python-versions","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264894"}},{"articleId":264888,"title":"How to Build a Simple Neural Network in Python","slug":"how-to-build-a-simple-neural-network-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264888"}}]},"hasRelatedBookFromSearch":false,"relatedBook":{"bookId":0,"slug":null,"isbn":null,"categoryList":null,"amazon":null,"image":null,"title":null,"testBankPinActivationLink":null,"bookOutOfPrint":false,"authorsInfo":null,"authors":null,"_links":null},"collections":[],"articleAds":{"footerAd":"<div class=\"du-ad-region row\" id=\"article_page_adhesion_ad\"><div class=\"du-ad-unit col-md-12\" data-slot-id=\"article_page_adhesion_ad\" data-refreshed=\"false\" \r\n data-target = \"[{&quot;key&quot;:&quot;cat&quot;,&quot;values&quot;:[&quot;technology&quot;,&quot;programming-web-design&quot;,&quot;python&quot;]},{&quot;key&quot;:&quot;isbn&quot;,&quot;values&quot;:[null]}]\" id=\"du-slot-6217bb491e9e0\"></div></div>","rightAd":"<div class=\"du-ad-region row\" id=\"article_page_right_ad\"><div class=\"du-ad-unit col-md-12\" data-slot-id=\"article_page_right_ad\" data-refreshed=\"false\" \r\n data-target = \"[{&quot;key&quot;:&quot;cat&quot;,&quot;values&quot;:[&quot;technology&quot;,&quot;programming-web-design&quot;,&quot;python&quot;]},{&quot;key&quot;:&quot;isbn&quot;,&quot;values&quot;:[null]}]\" id=\"du-slot-6217bb491f355\"></div></div>"},"articleType":{"articleType":"Articles","articleList":null,"content":null,"videoInfo":{"videoId":null,"name":null,"accountId":null,"playerId":null,"thumbnailUrl":null,"description":null,"uploadDate":null}},"sponsorship":{"sponsorshipPage":false,"backgroundImage":{"src":null,"width":0,"height":0},"brandingLine":"","brandingLink":"","brandingLogo":{"src":null,"width":0,"height":0}},"primaryLearningPath":"Advance","lifeExpectancy":"One year","lifeExpectancySetFrom":"2021-11-22T00:00:00+00:00","dummiesForKids":"no","sponsoredContent":"no","adInfo":"","adPairKey":[]},"status":"publish","visibility":"public","articleId":148370},{"headers":{"creationTime":"2016-03-26T10:49:23+00:00","modifiedTime":"2021-12-28T16:54:09+00:00","timestamp":"2022-02-24T17:07:20+00:00"},"data":{"breadcrumbs":[{"name":"Technology","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33512"},"slug":"technology","categoryId":33512},{"name":"Programming & Web Design","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33592"},"slug":"programming-web-design","categoryId":33592},{"name":"Python","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33606"},"slug":"python","categoryId":33606}],"title":"How to Start IDLE in Python","strippedTitle":"how to start idle in python","slug":"how-to-start-idle-in-python","canonicalUrl":"","seo":{"metaDescription":"","noIndex":0,"noFollow":0},"content":"You find IDLE in the Python 3.10 folder on your system as IDLE (Python 3.10 64-bit). When you click or double-click this entry (depending on your platform), you see the IDLE editor. The two lines of text contain information about the Python host and provide suggestions on the commands you can try.\r\n\r\nThe precise information you see differs by platform. What you see may differ depending on the version of Python you use, the platform you use, how you have IDLE configured, and how you have your system configured.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444862.image0.jpg\" alt=\"image0.jpg\" width=\"379\" height=\"400\" />\r\n<h2 id=\"tab1\" >Using standard commands</h2>\r\nIDLE provides all the same commands as the command-line version of Python. It doesn’t list them all because the assumption is that you’ll use the GUI features of IDLE to make things easy. However, if you want, you can type <b>help()</b> and press Enter to enter help mode, even though this command isn’t listed as one of the initial commands for IDLE as it is for the command-line version.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444863.image1.jpg\" alt=\"image1.jpg\" width=\"379\" height=\"400\" />\r\n<h2 id=\"tab2\" >Understanding color coding</h2>\r\nColor coding lets you see commands with greater ease and differentiate commands from other sorts of text. Press Enter to get out of help mode. As with the command-line version, you see descriptive text each time you perform an action.\r\n\r\nNow, type <b>print ('This is some text.')</b> and press Enter. You see the expected output, just as you normally would. Notice the color coding, though. The <span class=\"code\">print()</span> command is in purple text to show that it’s a command. The text within the <span class=\"code\">print()</span> command is green to show that it’s data and not a command.\r\n\r\nThe output is shown in blue. The color coding makes things a lot easier, which is just one of many reasons that using IDLE is easier than using the command line.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444864.image2.jpg\" alt=\"image2.jpg\" width=\"379\" height=\"400\" />\r\n<h2 id=\"tab3\" >Getting GUI help</h2>\r\nIDLE makes obtaining the help you need easy. Look at the Help menu and you see three entries for obtaining help:\r\n<ul class=\"level-one\">\r\n \t<li>\r\n<p class=\"first-para\"><b>About IDLE:</b> Provides you with the latest information about IDLE.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>IDLE Help:</b> Shows you a text file containing information about working with the IDLE IDE. For example, this is where you find a list of the IDLE commands.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>Python Docs:</b> Contains information required to work with Python commands and other elements.</p>\r\n</li>\r\n</ul>\r\nChoose Help→About IDLE to see the About IDLE dialog box. Near the middle of the dialog box, you see URLs for obtaining additional help. Each of the buttons displays a text file containing useful information, especially in the README and NEWS files. Click Close to exit this dialog box.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444865.image3.jpg\" alt=\"image3.jpg\" width=\"274\" height=\"400\" />\r\n\r\nPrecisely what you see when you choose Help→Python Docs depends on the platform you use. The Python Docs file contains information about how to work with and use Python to create applications. It even has a tutorial section in which you can find additional helpful tips.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444866.image4.jpg\" alt=\"image4.jpg\" width=\"535\" height=\"365\" />\r\n<h2 id=\"tab4\" >Configuring IDLE</h2>\r\nIDLE is basically a fancy text editor, when you think about it, so it’s not surprising that you can configure it to perform the task of editing text better. Choose Options→Configure IDLE to see the IDLE Preferences dialog box. This is where you can choose things like what font IDLE uses when displaying text.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444867.image5.jpg\" alt=\"image5.jpg\" width=\"325\" height=\"400\" />\r\nIDLE uses color coding to make reading and understanding the code easier. This tab lets you choose the colors used to perform highlighting. Notice that you can save your selections as a theme. You can create different themes for different needs. For example, you may use one theme when you use your laptop or other computing device in bright conditions and another theme in low light conditions.\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444868.image6.jpg\" alt=\"image6.jpg\" width=\"325\" height=\"400\" />\r\n\r\nEven though you won’t see shortcut keys, IDLE does support them. The shortcut keys on your platform may differ. IDLE comes with built-in key sets for Windows, Mac, OS X, and Unix. You can choose any of these themes by clicking the small button next to the IDLE Classic Windows entry. You can also create your own custom theme that’s based on another application you use.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444869.image7.jpg\" alt=\"image7.jpg\" width=\"325\" height=\"400\" />\r\n\r\nThe Windows tab controls window preferences. For example, you can tell IDLE to open a Python Shell window (so that you can experiment) or an Edit window (so that you can write an application). The default is to open a Python Shell window so that you can experiment with Python and try new techniques.\r\n\r\nThe Shell/Ed tab is where you can control whether IDLE prompts you to save files before running applications (a good idea in case the application causes the system to freeze) and the size of the initial window when you create one. The defaults you see normally work just fine, so there really isn’t a good reason to change them.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444870.image8.jpg\" alt=\"image8.jpg\" width=\"325\" height=\"400\" />\r\n\r\nThe Help Menu Extensions feature in the Extensions tab lets you create new help sources. You can create a link to an online source, such as <a href=\"https://docs.python.org/release/3.3.4/\" target=\"_blank\" rel=\"noopener\">Python’s online documentation</a>. To add a new source, click Add. You see the New Help Source dialog box where you can add the text that appears on the Help menu for this information source and the location of that source on a hard drive or online.\r\n\r\nWhen you finish adding the source, click OK and you’ll see it added to the IDLE Help menu. There are also buttons on the General tab of the IDLE Preferences dialog box for editing and removing help sources.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444871.image9.jpg\" alt=\"image9.jpg\" width=\"399\" height=\"276\" />","description":"You find IDLE in the Python 3.10 folder on your system as IDLE (Python 3.10 64-bit). When you click or double-click this entry (depending on your platform), you see the IDLE editor. The two lines of text contain information about the Python host and provide suggestions on the commands you can try.\r\n\r\nThe precise information you see differs by platform. What you see may differ depending on the version of Python you use, the platform you use, how you have IDLE configured, and how you have your system configured.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444862.image0.jpg\" alt=\"image0.jpg\" width=\"379\" height=\"400\" />\r\n<h2 id=\"tab1\" >Using standard commands</h2>\r\nIDLE provides all the same commands as the command-line version of Python. It doesn’t list them all because the assumption is that you’ll use the GUI features of IDLE to make things easy. However, if you want, you can type <b>help()</b> and press Enter to enter help mode, even though this command isn’t listed as one of the initial commands for IDLE as it is for the command-line version.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444863.image1.jpg\" alt=\"image1.jpg\" width=\"379\" height=\"400\" />\r\n<h2 id=\"tab2\" >Understanding color coding</h2>\r\nColor coding lets you see commands with greater ease and differentiate commands from other sorts of text. Press Enter to get out of help mode. As with the command-line version, you see descriptive text each time you perform an action.\r\n\r\nNow, type <b>print ('This is some text.')</b> and press Enter. You see the expected output, just as you normally would. Notice the color coding, though. The <span class=\"code\">print()</span> command is in purple text to show that it’s a command. The text within the <span class=\"code\">print()</span> command is green to show that it’s data and not a command.\r\n\r\nThe output is shown in blue. The color coding makes things a lot easier, which is just one of many reasons that using IDLE is easier than using the command line.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444864.image2.jpg\" alt=\"image2.jpg\" width=\"379\" height=\"400\" />\r\n<h2 id=\"tab3\" >Getting GUI help</h2>\r\nIDLE makes obtaining the help you need easy. Look at the Help menu and you see three entries for obtaining help:\r\n<ul class=\"level-one\">\r\n \t<li>\r\n<p class=\"first-para\"><b>About IDLE:</b> Provides you with the latest information about IDLE.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>IDLE Help:</b> Shows you a text file containing information about working with the IDLE IDE. For example, this is where you find a list of the IDLE commands.</p>\r\n</li>\r\n \t<li>\r\n<p class=\"first-para\"><b>Python Docs:</b> Contains information required to work with Python commands and other elements.</p>\r\n</li>\r\n</ul>\r\nChoose Help→About IDLE to see the About IDLE dialog box. Near the middle of the dialog box, you see URLs for obtaining additional help. Each of the buttons displays a text file containing useful information, especially in the README and NEWS files. Click Close to exit this dialog box.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444865.image3.jpg\" alt=\"image3.jpg\" width=\"274\" height=\"400\" />\r\n\r\nPrecisely what you see when you choose Help→Python Docs depends on the platform you use. The Python Docs file contains information about how to work with and use Python to create applications. It even has a tutorial section in which you can find additional helpful tips.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444866.image4.jpg\" alt=\"image4.jpg\" width=\"535\" height=\"365\" />\r\n<h2 id=\"tab4\" >Configuring IDLE</h2>\r\nIDLE is basically a fancy text editor, when you think about it, so it’s not surprising that you can configure it to perform the task of editing text better. Choose Options→Configure IDLE to see the IDLE Preferences dialog box. This is where you can choose things like what font IDLE uses when displaying text.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444867.image5.jpg\" alt=\"image5.jpg\" width=\"325\" height=\"400\" />\r\nIDLE uses color coding to make reading and understanding the code easier. This tab lets you choose the colors used to perform highlighting. Notice that you can save your selections as a theme. You can create different themes for different needs. For example, you may use one theme when you use your laptop or other computing device in bright conditions and another theme in low light conditions.\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444868.image6.jpg\" alt=\"image6.jpg\" width=\"325\" height=\"400\" />\r\n\r\nEven though you won’t see shortcut keys, IDLE does support them. The shortcut keys on your platform may differ. IDLE comes with built-in key sets for Windows, Mac, OS X, and Unix. You can choose any of these themes by clicking the small button next to the IDLE Classic Windows entry. You can also create your own custom theme that’s based on another application you use.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444869.image7.jpg\" alt=\"image7.jpg\" width=\"325\" height=\"400\" />\r\n\r\nThe Windows tab controls window preferences. For example, you can tell IDLE to open a Python Shell window (so that you can experiment) or an Edit window (so that you can write an application). The default is to open a Python Shell window so that you can experiment with Python and try new techniques.\r\n\r\nThe Shell/Ed tab is where you can control whether IDLE prompts you to save files before running applications (a good idea in case the application causes the system to freeze) and the size of the initial window when you create one. The defaults you see normally work just fine, so there really isn’t a good reason to change them.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444870.image8.jpg\" alt=\"image8.jpg\" width=\"325\" height=\"400\" />\r\n\r\nThe Help Menu Extensions feature in the Extensions tab lets you create new help sources. You can create a link to an online source, such as <a href=\"https://docs.python.org/release/3.3.4/\" target=\"_blank\" rel=\"noopener\">Python’s online documentation</a>. To add a new source, click Add. You see the New Help Source dialog box where you can add the text that appears on the Help menu for this information source and the location of that source on a hard drive or online.\r\n\r\nWhen you finish adding the source, click OK and you’ll see it added to the IDLE Help menu. There are also buttons on the General tab of the IDLE Preferences dialog box for editing and removing help sources.\r\n\r\n<img src=\"https://www.dummies.com/wp-content/uploads/444871.image9.jpg\" alt=\"image9.jpg\" width=\"399\" height=\"276\" />","blurb":"","authors":[{"authorId":9109,"name":"John Paul Mueller","slug":"john-paul-mueller","description":"John Paul Mueller has written more than 100 books and more than 600 articles on topics ranging from functional programming techniques to application development using C++. ","_links":{"self":"https://dummies-api.dummies.com/v2/authors/9109"}}],"primaryCategoryTaxonomy":{"categoryId":33606,"title":"Python","slug":"python","_links":{"self":"https://dummies-api.dummies.com/v2/categories/33606"}},"secondaryCategoryTaxonomy":{"categoryId":0,"title":null,"slug":null,"_links":null},"tertiaryCategoryTaxonomy":{"categoryId":0,"title":null,"slug":null,"_links":null},"trendingArticles":null,"inThisArticle":[{"label":"Using standard commands","target":"#tab1"},{"label":"Understanding color coding","target":"#tab2"},{"label":"Getting GUI help","target":"#tab3"},{"label":"Configuring IDLE","target":"#tab4"}],"relatedArticles":{"fromBook":[{"articleId":250588,"title":"How to Get Additional Python Libraries","slug":"get-additional-python-libraries","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/250588"}},{"articleId":250582,"title":"Printing Lists Using Python","slug":"printing-lists-using-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/250582"}},{"articleId":250578,"title":"Understanding How Permanent Storage Works for Python Programming","slug":"understanding-permanent-storage-works-python-programming","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/250578"}},{"articleId":250575,"title":"Extending Python Classes to Make New Classes","slug":"extending-python-classes-make-new-classes","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/250575"}},{"articleId":250571,"title":"Understanding the Python Class as a Packaging Method","slug":"understanding-python-class-packaging-method","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/250571"}}],"fromCategory":[{"articleId":264919,"title":"How to Define and Use Python Lists","slug":"how-to-define-and-use-python-lists","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264919"}},{"articleId":264911,"title":"How to Use Lambda Functions in Python","slug":"how-to-use-lambda-functions-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264911"}},{"articleId":264906,"title":"Your Guide to the Python Standard Library","slug":"your-guide-to-the-python-standard-library","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264906"}},{"articleId":264894,"title":"A Beginner’s Guide to Python Versions","slug":"a-beginners-guide-to-python-versions","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264894"}},{"articleId":264888,"title":"How to Build a Simple Neural Network in Python","slug":"how-to-build-a-simple-neural-network-in-python","categoryList":["technology","programming-web-design","python"],"_links":{"self":"https://dummies-api.dummies.com/v2/articles/264888"}}]},"hasRelatedBookFromSearch":false,"relatedBook":{"bookId":281830,"slug":"beginning-programming-with-python-for-dummies-2nd-edition","isbn":"9781119457893","categoryList":["technology","programming-web-design","python"],"amazon":{"default":"https://www.amazon.com/gp/product/1119457890/ref=as_li_tl?ie=UTF8&tag=wiley01-20","ca":"https://www.amazon.ca/gp/product/1119457890/ref=as_li_tl?ie=UTF8&tag=wiley01-20","indigo_ca":"http://www.tkqlhce.com/click-9208661-13710633?url=https://www.chapters.indigo.ca/en-ca/books/product/1119457890-item.html&cjsku=978111945484","gb":"https://www.amazon.co.uk/gp/product/1119457890/ref=as_li_tl?ie=UTF8&tag=wiley01-20","de":"https://www.amazon.de/gp/product/1119457890/ref=as_li_tl?ie=UTF8&tag=wiley01-20"},"image":{"src":"https://www.dummies.com/wp-content/uploads/beginning-programming-with-python-for-dummies-2nd-edition-cover-9781119457893-203x255.jpg","width":203,"height":255},"title":"Beginning Programming with Python For Dummies, 2nd Edition","testBankPinActivationLink":"","bookOutOfPrint":false,"authorsInfo":"\n <p><b data-author-id=\"9109\">John Paul Mueller</b> is a freelance author and technical editor with more than 107 books and 600 articles to his credit. His subjects range from networking and artificial intelligence to database management and heads-down programming. He also consults and writes certification exams. Visit his website at http://www.johnmuellerbooks.com/. </p>","authors":[{"authorId":9109,"name":"John Paul Mueller","slug":"john-paul-mueller","description":"John Paul Mueller has written more than 100 books and more than 600 articles on topics ranging from functional programming techniques to application development using C++. ","_links":{"self":"https://dummies-api.dummies.com/v2/authors/9109"}}],"_links":{"self":"https://dummies-api.dummies.com/v2/books/"}},"collections":[],"articleAds":{"footerAd":"<div class=\"du-ad-region row\" id=\"article_page_adhesion_ad\"><div class=\"du-ad-unit col-md-12\" data-slot-id=\"article_page_adhesion_ad\" data-refreshed=\"false\" \r\n data-target = \"[{&quot;key&quot;:&quot;cat&quot;,&quot;values&quot;:[&quot;technology&quot;,&quot;programming-web-design&quot;,&quot;python&quot;]},{&quot;key&quot;:&quot;isbn&quot;,&quot;values&quot;:[&quot;9781119457893&quot;]}]\" id=\"du-slot-6217bb48b65a7\"></div></div>","rightAd":"<div class=\"du-ad-region row\" id=\"article_page_right_ad\"><div class=\"du-ad-unit col-md-12\" data-slot-id=\"article_page_right_ad\" data-refreshed=\"false\" \r\n data-target = \"[{&quot;key&quot;:&quot;cat&quot;,&quot;values&quot;:[&quot;technology&quot;,&quot;programming-web-design&quot;,&quot;python&quot;]},{&quot;key&quot;:&quot;isbn&quot;,&quot;values&quot;:[&quot;9781119457893&quot;]}]\" id=\"du-slot-6217bb48b6f2a\"></div></div>"},"articleType":{"articleType":"Articles","articleList":null,"content":null,"videoInfo":{"videoId":null,"name":null,"accountId":null,"playerId":null,"thumbnailUrl":null,"description":null,"uploadDate":null}},"sponsorship":{"sponsorshipPage":false,"backgroundImage":{"src":null,"width":0,"height":0},"brandingLine":"","brandingLink":"","brandingLogo":{"src":null,"width":0,"height":0}},"primaryLearningPath":"Advance","lifeExpectancy":"One year","lifeExpectancySetFrom":"2021-11-22T00:00:00+00:00","dummiesForKids":"no","sponsoredContent":"no","adInfo":"","adPairKey":[]},"status":"publish","visibility":"public","articleId":148365}],"_links":{"self":{"self":"https://dummies-api.dummies.com/v2/categories/33606/categoryArticles?sortField=time&sortOrder=1&size=10&offset=0"},"next":{"self":"https://dummies-api.dummies.com/v2/categories/33606/categoryArticles?sortField=time&sortOrder=1&size=10&offset=10"},"last":{"self":"https://dummies-api.dummies.com/v2/categories/33606/categoryArticles?sortField=time&sortOrder=1&size=10&offset=74"}}},"objectTitle":"","status":"success","pageType":"article-category","objectId":"33606","page":1,"sortField":"time","sortOrder":1,"categoriesIds":[],"articleTypes":[],"filterData":{"categoriesFilter":[{"itemId":0,"itemName":"All Categories","count":84}],"articleTypeFilter":[{"articleType":"All Types","count":84},{"articleType":"Articles","count":75},{"articleType":"Cheat Sheet","count":4},{"articleType":"Step by Step","count":5}]},"filterDataLoadedStatus":"success","pageSize":10},"adsState":{"pageScripts":{"headers":{"timestamp":"2022-05-24T18:59:04+00:00"},"adsId":0,"data":{"scripts":[{"pages":["all"],"location":"header","script":"<!--Optimizely Script-->\r\n<script src=\"https://cdn.optimizely.com/js/10563184655.js\"></script>","enabled":false},{"pages":["all"],"location":"header","script":"<!-- comScore Tag -->\r\n<script>var _comscore = _comscore || [];_comscore.push({ c1: \"2\", c2: \"15097263\" });(function() {var s = document.createElement(\"script\"), el = document.getElementsByTagName(\"script\")[0]; s.async = true;s.src = (document.location.protocol == \"https:\" ? \"https://sb\" : \"http://b\") + \".scorecardresearch.com/beacon.js\";el.parentNode.insertBefore(s, el);})();</script><noscript><img src=\"https://sb.scorecardresearch.com/p?c1=2&c2=15097263&cv=2.0&cj=1\" /></noscript>\r\n<!-- / comScore Tag -->","enabled":true},{"pages":["all"],"location":"footer","script":"<!--BEGIN QUALTRICS WEBSITE FEEDBACK SNIPPET-->\r\n<script type='text/javascript'>\r\n(function(){var g=function(e,h,f,g){\r\nthis.get=function(a){for(var a=a+\"=\",c=document.cookie.split(\";\"),b=0,e=c.length;b<e;b++){for(var d=c[b];\" \"==d.charAt(0);)d=d.substring(1,d.length);if(0==d.indexOf(a))return d.substring(a.length,d.length)}return null};\r\nthis.set=function(a,c){var b=\"\",b=new Date;b.setTime(b.getTime()+6048E5);b=\"; expires=\"+b.toGMTString();document.cookie=a+\"=\"+c+b+\"; path=/; \"};\r\nthis.check=function(){var a=this.get(f);if(a)a=a.split(\":\");else if(100!=e)\"v\"==h&&(e=Math.random()>=e/100?0:100),a=[h,e,0],this.set(f,a.join(\":\"));else return!0;var c=a[1];if(100==c)return!0;switch(a[0]){case \"v\":return!1;case \"r\":return c=a[2]%Math.floor(100/c),a[2]++,this.set(f,a.join(\":\")),!c}return!0};\r\nthis.go=function(){if(this.check()){var a=document.createElement(\"script\");a.type=\"text/javascript\";a.src=g;document.body&&document.body.appendChild(a)}};\r\nthis.start=function(){var t=this;\"complete\"!==document.readyState?window.addEventListener?window.addEventListener(\"load\",function(){t.go()},!1):window.attachEvent&&window.attachEvent(\"onload\",function(){t.go()}):t.go()};};\r\ntry{(new g(100,\"r\",\"QSI_S_ZN_5o5yqpvMVjgDOuN\",\"https://zn5o5yqpvmvjgdoun-wiley.siteintercept.qualtrics.com/SIE/?Q_ZID=ZN_5o5yqpvMVjgDOuN\")).start()}catch(i){}})();\r\n</script><div id='ZN_5o5yqpvMVjgDOuN'><!--DO NOT REMOVE-CONTENTS PLACED HERE--></div>\r\n<!--END WEBSITE FEEDBACK SNIPPET-->","enabled":false},{"pages":["all"],"location":"header","script":"<!-- Hotjar Tracking Code for http://www.dummies.com -->\r\n<script>\r\n (function(h,o,t,j,a,r){\r\n h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};\r\n h._hjSettings={hjid:257151,hjsv:6};\r\n a=o.getElementsByTagName('head')[0];\r\n r=o.createElement('script');r.async=1;\r\n r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;\r\n a.appendChild(r);\r\n })(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');\r\n</script>","enabled":false},{"pages":["article"],"location":"header","script":"<!-- //Connect Container: dummies --> <script src=\"//get.s-onetag.com/bffe21a1-6bb8-4928-9449-7beadb468dae/tag.min.js\" async defer></script>","enabled":true},{"pages":["homepage"],"location":"header","script":"<meta name=\"facebook-domain-verification\" content=\"irk8y0irxf718trg3uwwuexg6xpva0\" />","enabled":true},{"pages":["homepage","article","category","search"],"location":"footer","script":"<!-- Facebook Pixel Code -->\r\n<noscript>\r\n<img height=\"1\" width=\"1\" src=\"https://www.facebook.com/tr?id=256338321977984&ev=PageView&noscript=1\"/>\r\n</noscript>\r\n<!-- End Facebook Pixel Code -->","enabled":true}]}},"pageScriptsLoadedStatus":"success"},"searchState":{"searchList":[],"searchStatus":"initial","relatedArticlesList":[],"relatedArticlesStatus":"initial"},"routeState":{"name":"ArticleCategory","path":"/category/articles/python-33606/","hash":"","query":{},"params":{"category":"python-33606"},"fullPath":"/category/articles/python-33606/","meta":{"routeType":"category","breadcrumbInfo":{"suffix":"Articles","baseRoute":"/category/articles"},"prerenderWithAsyncData":true},"from":{"name":null,"path":"/","hash":"","query":{},"params":{},"fullPath":"/","meta":{}}},"sfmcState":{"newsletterSignupStatus":"initial"}}
Logo
  • Articles Open Article Categories
  • Books Open Book Categories
  • Collections Open Collections list
  • Custom Solutions
  • Home
  • Technology Articles
  • Programming & Web Design Articles
  • Python Articles

Python Articles

Don't be scared, it's not poisonous. Python is one of the easiest languages you can learn. Check out our articles on Python here.

Articles From Python

page 1
page 2
page 3
page 4
page 5
page 6
page 7
page 8
page 9

Filter Results

84 results
84 results
Python Beginning Programming with Python For Dummies Cheat Sheet

Cheat Sheet / Updated 04-26-2022

Python is an incredibly flexible language that has significant third-party support and is used in a broad range of applications. The applications you build will run on any platform that Python supports without any modification as long as you create a pure Python solution. Of course, you want to ensure that your applications have the best chance possible of working exactly as you anticipated everywhere they're run, which is why you need the information in this cheat sheet.

View Cheat Sheet
Python Python for Data Science For Dummies Cheat Sheet

Cheat Sheet / Updated 02-24-2022

Python is an incredible programming language that you can use to perform data science tasks with a minimum of effort. The huge number of available libraries means that the low-level code you normally need to write is likely already available from some other source. All you need to focus on is getting the job done. With that in mind, this cheat sheet helps you access the most commonly needed reminders for making your programming experience fast and easy.

View Cheat Sheet
Python Python For Kids For Dummies Cheat Sheet

Cheat Sheet / Updated 02-24-2022

Python coding helps you with things you do every day, like math homework. Python programming can also help with things like making web pages: Thank goodness for widgets and keywords!

View Cheat Sheet
Python What is a Python Module?

Article / Updated 01-25-2022

For all the hoopla about Python modules, a module is actually a pretty simple thing. In fact, a Python module is just a file with a .py extension that contains Python code. That’s it. So any time you write Python code and save it in a .py file, you’ve basically created a module. That’s not to say you always have to use that code as a module. It can certainly be treated as a standalone app. But if you wanted to create your own Python module, with just code that you need often in your own work, you could certainly do so. A Python module is also just a file with a .py filename extension. The name of the module is the same as the filename (without the .py). Like any .py file, the module contains Python code. As a working example, let’s suppose you want to have three functions to simplify formatting dates and currency values. You can make up any name you like for each function. For our working example, we’ll use these three names: to_date(any_str): Lets you pass in any string (any_str) date in mm/dd/yy or mm/dd/yyyy format and sends back a Python datetime.date that you can use for date calculations. mdy(any_date): Lets you pass in any Python date or datetime, and returns a string date formatted in mm/dd/yyyy format for display on the screen. to_curr(any_num, len): Lets you pass in any Python float or integer number and returns a string with a leading dollar sign, commas in thousands places, and two digits for the pennies. The len is an optional number for length. If provided, the return value will be padded on the left with spaces to match the length specified So here is all the code for that: # Contains custom functions for dates and currency values. import datetime as dt def to_date(any_str): """ Convert mm/dd/yy or mm/dd/yyyy string to datetime.date, or None if invalid date. """ try: if len(any_str) == 10: the_date = dt.datetime.strptime(any_str,'%m/%d/%Y').date() else: the_date = dt.datetime.strptime(any_str,'%m/%d/%y').date() except (ValueError, TypeError): the_date = None return the_date def mdy(any_date): """ Returns a string date in mm/dd/yyyy format. Pass in Python date or string date in mm/dd/yyyy format """ if type(any_date) == str: any_date = to_date(anydate) # Make sure its a dateime being forwarded if isinstance(any_date,dt.date): s_date = f"{any_date:'%m/%d/%Y'}" else: s_date = "Invalid date" return s_date def to_curr(anynum, len=0): """ Returns a number as a string with $ and commas. Length is optional """ s = "Invalid amount" try: x = float(anynum) except ValueError: x= None if isinstance(x,float): s = '$' + f"{x:,.2f}" if len > 0: s=s.rjust(len) return s You can create the same file yourself and name it myfunctions.py if you want to follow along. Notice that the file contains only functions. So if you run it, it won't do anything on the screen because there is no code in there that calls any of those functions. To use those functions in any Python app or program you write, first make sure you copy that myfunc.py file to the same folder as the rest of the Python code that you’re writing. Then, when you create a new page, you can import myfunc as a module just as you would any other module created by somebody else. Just use import myfunc You will have to use the module name in front of any of the functions that you call from that module. So if you want to make the code a little more readable, you can use this instead: import myfunc as my With that as your opening line, you can refer to any function in your custom Python module with my. as the prefix. For example, my.to_date() to call the to_date function. Here is a page that imports the module and then tests out all three functions using that my syntax: # Import all the code from myfunc.py as my. import myfunc as my # Need dates in this code from datetime import datetime as dt # Some simple test data. string_date="12/31/2019" # Convert string date to datetime.date print(my.to_date(string_date)) today = dt.today() # Show today's date in mm/dd/yyyy format. print(my.mdy(today)) dollar_amt=12345.678 # Show this big number in currency format. print(my.to_curr(dollar_amt)) You can also skip using the prefix if you import items by name. In this case, that means you could call to_date() and mdy() and to_curr() without using the my. prefix. The first line of code would need to be from myfunc import to_date, mdy, to_curr The rest of the code would be the same as in the previous example, except you can leave off the my. prefixes as in the following code: # Import all the code from myfunc.py by name. from myfunc import to_date, mdy, to_curr # Need dates in this code from datetime import datetime as dt # Some simple test data. string_date="12/31/2019" # Convert string date to datetime.date print(to_date(string_date)) today = dt.today() # Show today's date in mm/dd/yyyy format. print(mdy(today)) dollar_amt=12345.678 # Show this big number in currency format. print(to_curr(dollar_amt)) Check out these 10 amazing Python programming resources for more information.

View Article
Python How to Delete a File in Python

Article / Updated 01-25-2022

While you can use Python to delete information from files, you may find you no longer need the file at all. The following steps describe how to delete files that you no longer need. Open a Python File window. You see an editor in which you can type the example code. Type the following code into the window — pressing Enter after each line: Choose Run→Run Module The application displays the File Removed! message. When you look in the directory that originally contained the ChangedFile.csv file, you see that the file is gone. The task looks simple in this case, and it is. All you need to do to remove a file is call os.remove() with the appropriate filename and path (Python defaults to the current directory, so you don’t need to specify a path if the file you want to remove is in the default directory). The ease with which you can perform this task is almost scary because it’s too easy. Putting safeguards in place is always a good idea. You may want to remove other items, so here are other functions you should know about: os.rmdir(): Removes the specified directory. The directory must be empty or Python will display an exception message. shutil.rmtree(): Removes the specified directory, all subdirectories, and all files. This function is especially dangerous because it removes everything without checking (Python assumes that you know what you’re doing). As a result, you can easily lose data using this function.

View Article
Python How to Install Python on Your Computer

Article / Updated 01-25-2022

Whether you use a Mac, Windows, or Linux OS (operating system), you can find and install Python on your computer. The following sections give you instructions for each OS. How to install Python on Mac OSX To find and start Python on Mac OSX computers, follow these steps: Press Cmd+spacebar to open Spotlight. Type the word terminal. Or, from the Finder, select Finder→Go→Utilities→Terminal. The Terminal window opens. In the terminal, type python. The Python interpreter that's built in to Mac OSX opens. How to install Python on Windows Unfortunately, Python doesn't come on Windows. If you're running Windows, then you need to download and install Python by following the instructions here. Installing Python on Windows isn't difficult. If you can download a file from a website, you have the skills to install Python. Fortunately, the Python Foundation (the peeps who guide the development of Python) makes installable files available from its website. Firefox and Internet Explorer responded differently to the Python download website, so the instructions are based on which of these browsers you use. If you use a whole other browser altogether, try the Internet Explorer instructions. Installing with Firefox To install Python on a Windows machine with Firefox, follow these steps: Visit www.python.org/downloads. Click the button that says Download Python 2.7.9. Or, if it's there, click a more recent version number that starts with 2.7. Clicking this button automatically downloads and saves an msi file for you. If not, try the instructions for Internet Explorer. See Figure 1. Figure 1: Download Python with Firefox. When the download's complete, click the icon for Firefox's download tool. Click the file called python-2.7.9.msi (or the more recent version, if you downloaded one). Python 2.7.9 installs on your computer. Installing with Internet Explorer To install Python on a Windows machine with Internet Explorer, follow these steps: Visit www.python.org/downloads. From the menu bar, select Downloads→Windows. You can see the menu options in Figure 2. Figure 2: Download Python with Internet Explorer. Scroll down to the heading Python 2.7.9-2014-12-10. Or scroll to a more recent version, which starts with Python 2.7, if one is available. Under this heading, click the link titled Download Windows x86 MSI Installer. See Figure 3. This is a link for a 32-bit installation, which makes things work better with third-party libraries. Use the 32-bit installer even if you have a 64-bit machine and even if you have no idea what this paragraph is talking about. Figure 3: Python x86 MSI Installer. If you're asked to choose whether to run or save the file, choose Run. This downloads python2.7.9.msi and starts running the installer. If you get a security warning when the installer begins (or at random times during the installation), choose Run. Accept the default installation options that the installer provides. How to install Python for Linux If you're running Linux, confirm that you have version 2.7.9 of Python installed, rather than version 3. This shouldn't be a problem because Python 2.7 is installed by default in recent versions of OpenSuSE, Ubuntu, and Red Hat Fedora. In the nutty odd case when someone has Python 3 but not Python 2.7, read your distribution's documentation for how to use the package manager and get Python 2.7 and IDLE.

View Article
Python Write a Simple Program in Python

Article / Updated 01-25-2022

Tradition dictates that Hello World! be the first program that you write when you're learning a new programming language like Python. You're following in the footsteps of many great programmers when you create this project. To create your Hello World! program, follow these steps: Open your Start menu and choose Python (command line). You should get a prompt that looks like >>>. At the moment, you're doing everything in interactive mode in the Python interpreter. That's where the >>> comes in. Python shows you >>> when you're supposed to type something. At the prompt, type the following. Use a single quote at the start and the end — it's beside the Enter key: print('Hello World!') Press the Enter key. Python runs the code you typed. You see the output shown in Figure 1. Congratulations — you've written your first program. Welcome to the Python-programmers-in-training club. If you don't see what's in Figure 1, check that you typed in the text from Step 2 exactly as it's written: Check that the parentheses and single quotes are in the right places. Check that for each opening parenthesis there is a closing parenthesis. (Otherwise, you're left hanging. Check that for each opening quote there's a closing quote. Programming languages have their own grammar and punctuation rules. These rules are the language's syntax. Humans, can work most stuff out even if perfect not you're is grammar (See? You figured out what that sentence was trying to say), but Python pretty much freaks out if you get the syntax wrong.

View Article
Python How to Format Strings in Python

Article / Updated 12-29-2021

You can format strings in a number of ways using Python. The main emphasis of formatting is to present the string in a form that is both pleasing to the user and easy to understand. Formatting doesn’t mean adding effects in this case, but refers merely to the presentation of the data. For example, the user might want a fixed-point number rather than a decimal number as output. You have quite a few ways to format strings. However, the focus of most formatting is the format() function. You create a formatting specification as part of the string and then use the format() function to add data to that string. A format specification may be as simple as two curly brackets {} that specify a placeholder for data. You can number the placeholder to create special effects. For example, {0} would contain the first data element in a string. When the data elements are numbered, you can even repeat them so that the same data appears more than once in the string. The formatting specification follows a colon. When you want to create just a formatting specification, the curly brackets contain just the colon and whatever formatting you want to use. For example, {:f} would create a fixed-point number as output. If you want to number the entries, the number that precedes the colon: {0:f} creates a fixed-point number output for data element one. The formatting specification follows this form, with the italicized elements serving as placeholders here: [[fill]align][sign][#][0][width][,][.precision][type] This specification provides you with the in-depth details, but here’s an overview of what the various entries mean: fill: Defines the fill character used when displaying data that is too small to fit within the assigned space. align: Specifies the alignment of data within the display space. You can use these alignments: <: Left aligned >: Right aligned ^: Centered =: Justified sign: Determines the use of signs for the output: +: Positive numbers have a plus sign and negative numbers have a minus sign. -: Negative numbers have a minus sign. <space>: Positive numbers are preceded by a space and negative numbers have a minus sign. #: Specifies that the output should use the alternative display format for numbers. For example, hexadecimal numbers will have a 0x prefix added to them. 0: Specifies that the output should be sign aware and padded with zeros as needed to provide consistent output. width: Determines the full width of the data field (even if the data won’t fit in the space provided). ,: Specifies that numeric data should have commas as a thousands separator. .precision: Determines the number of characters after the decimal point. type: Specifies the output type, even if the input type doesn’t match. The types are split into three groups: String: Use an s or nothing at all to specify a string. Integer: The integer types are as follows: b (binary); c (character); d (decimal); o (octal); x (hexadecimal with lowercase letters); X (hexadecimal with uppercase letters); and n (locale-sensitive decimal that uses the appropriate characters for the thousands separator). Floating point: The floating-point types are as follows: e (exponent using a lowercase e as a separator); E (exponent using an uppercase E as a separator); f (lowercase fixed point); F (uppercase fixed point); g (lowercase general format); G (uppercase general format); n (local-sensitive general format that uses the appropriate characters for the decimal and thousands separators); and % (percentage). The formatting specification elements must appear in the correct order or Python won’t know what to do with them. If you specify the alignment before the fill character, Python displays an error message rather than performing the required formatting. The following steps help you see how the formatting specification works and demonstrate the order you need to follow in using the various formatting specification criteria. Open a Python File window. You see an editor in which you can type the example code. Type the following code into the window — pressing Enter after each line: Formatted = "{:d}" print(Formatted.format(7000)) Formatted = "{:,d}" print(Formatted.format(7000)) Formatted = "{:^15,d}" print(Formatted.format(7000)) Formatted = "{:*^15,d}" print(Formatted.format(7000)) Formatted = "{:*^15.2f}" print(Formatted.format(7000)) Formatted = "{:*>15X}" print(Formatted.format(7000)) Formatted = "{:*<#15x}" print(Formatted.format(7000)) Formatted = "A {0} {1} and a {0} {2}." print(Formatted.format("blue", "car", "truck")) The example starts simply with a field formatted as a decimal value. It then adds a thousands separator to the output. The next step is to make the field wider than needed to hold the data and to center the data within the field. Finally, the field has an asterisk added to pad the output. Of course, the example contains other data types. The next step is to display the same data in fixed-point format. The example also shows the output in both uppercase and lowercase hexadecimal format. The uppercase output is right aligned and the lowercase output is left aligned. Finally, the example shows how you can use numbered fields to your advantage. In this case, it creates an interesting string output that repeats one of the input values. Click Run Cell. Python outputs data in various forms, as shown in the figure below.

View Article
Python How to Create Your First Python Application

Article / Updated 12-28-2021

It’s time to create your first Python application. Your initial IDLE Shell window won’t work for creating an application, so you can begin by creating a new Edit window for the application. You’ll type the required commands and then save the file to disk. Open a new window The initial IDLE Shell window is just fine for experimentation, but you need a nice, clean Edit window for typing your first application. The IDLE Shell window is interactive, which means that it gives you immediate feedback for any commands you type. The Edit window provides a static environment, where you type commands, save them, and then run them after you type enough commands to create an application. The two windows serve distinctly different purposes. Choose File→New File to create a new window. A new window opens. Notice that the title bar says untitled instead of IDLE Shell 3.1.0. A Python Shell window will always have the word “Shell” in the title bar. The two windows also have some unique toolbar entries. For example, an Edit window includes the Run command, which you use later to test your application. Working with the Edit window is just like working with any other text editor. You have access to basic editing commands such as Copy, Cut, and Paste. Pressing Enter moves to the next line rather than executing a command as it would when working in the Python Shell window. That’s because the Edit window is a static environment — one where you type commands and save them for later reuse. The Edit window also provides special commands to format the text. What you need to know now is that these formatting commands act differently from those in a standard text editor because they help you control the appearance of code rather than of generic text. Many of the formatting features work automatically, so you don’t need to worry about them now. Finally, the Edit window provides access to commands that tell Python to perform the steps in the procedure you create one at a time. This process is called running the application. Typing the command As with the Python Shell window, you can simply type a command into the Edit window. To see how this works, type print(. Notice that the Edit window provides you with helpful information about the print() command. The information is a little terse, so you may not understand it now. For now, the word value is the one that you need to focus on. The print() command needs a value before it can print anything. Finish the command by typing “This is a simple Python application.”) and pressing Enter. This is one of the simplest applications you can create using Python. Saving the file You could run the application now if you wanted to. However, saving your application before you run it is always a good idea. That way, if you make a mistake that causes Python or the system to freeze for some reason, your application code is still safe. Saving the application makes it easier to go back later to determine what went wrong, make corrections, and try running the application again. Choose File→Save to display the Save As dialog box. The Edit window automatically chooses the Python3 10 folder to save the application in. However, this is where the Python code resides, and saving your application code in the same folder is a bad idea. If you want, create a directory structure with similar names using a technique that works for your platform as you follow along. Type FirstApp.py in the Filename field of the Save As dialog box and click Save. Your application code is now saved on disk and you can access it anytime you want. When you return to the Edit window, the title bar text changes. Notice that the title bar includes the full path to the application.

View Article
Python How to Start IDLE in Python

Article / Updated 12-28-2021

You find IDLE in the Python 3.10 folder on your system as IDLE (Python 3.10 64-bit). When you click or double-click this entry (depending on your platform), you see the IDLE editor. The two lines of text contain information about the Python host and provide suggestions on the commands you can try. The precise information you see differs by platform. What you see may differ depending on the version of Python you use, the platform you use, how you have IDLE configured, and how you have your system configured. Using standard commands IDLE provides all the same commands as the command-line version of Python. It doesn’t list them all because the assumption is that you’ll use the GUI features of IDLE to make things easy. However, if you want, you can type help() and press Enter to enter help mode, even though this command isn’t listed as one of the initial commands for IDLE as it is for the command-line version. Understanding color coding Color coding lets you see commands with greater ease and differentiate commands from other sorts of text. Press Enter to get out of help mode. As with the command-line version, you see descriptive text each time you perform an action. Now, type print ('This is some text.') and press Enter. You see the expected output, just as you normally would. Notice the color coding, though. The print() command is in purple text to show that it’s a command. The text within the print() command is green to show that it’s data and not a command. The output is shown in blue. The color coding makes things a lot easier, which is just one of many reasons that using IDLE is easier than using the command line. Getting GUI help IDLE makes obtaining the help you need easy. Look at the Help menu and you see three entries for obtaining help: About IDLE: Provides you with the latest information about IDLE. IDLE Help: Shows you a text file containing information about working with the IDLE IDE. For example, this is where you find a list of the IDLE commands. Python Docs: Contains information required to work with Python commands and other elements. Choose Help→About IDLE to see the About IDLE dialog box. Near the middle of the dialog box, you see URLs for obtaining additional help. Each of the buttons displays a text file containing useful information, especially in the README and NEWS files. Click Close to exit this dialog box. Precisely what you see when you choose Help→Python Docs depends on the platform you use. The Python Docs file contains information about how to work with and use Python to create applications. It even has a tutorial section in which you can find additional helpful tips. Configuring IDLE IDLE is basically a fancy text editor, when you think about it, so it’s not surprising that you can configure it to perform the task of editing text better. Choose Options→Configure IDLE to see the IDLE Preferences dialog box. This is where you can choose things like what font IDLE uses when displaying text. IDLE uses color coding to make reading and understanding the code easier. This tab lets you choose the colors used to perform highlighting. Notice that you can save your selections as a theme. You can create different themes for different needs. For example, you may use one theme when you use your laptop or other computing device in bright conditions and another theme in low light conditions. Even though you won’t see shortcut keys, IDLE does support them. The shortcut keys on your platform may differ. IDLE comes with built-in key sets for Windows, Mac, OS X, and Unix. You can choose any of these themes by clicking the small button next to the IDLE Classic Windows entry. You can also create your own custom theme that’s based on another application you use. The Windows tab controls window preferences. For example, you can tell IDLE to open a Python Shell window (so that you can experiment) or an Edit window (so that you can write an application). The default is to open a Python Shell window so that you can experiment with Python and try new techniques. The Shell/Ed tab is where you can control whether IDLE prompts you to save files before running applications (a good idea in case the application causes the system to freeze) and the size of the initial window when you create one. The defaults you see normally work just fine, so there really isn’t a good reason to change them. The Help Menu Extensions feature in the Extensions tab lets you create new help sources. You can create a link to an online source, such as Python’s online documentation. To add a new source, click Add. You see the New Help Source dialog box where you can add the text that appears on the Help menu for this information source and the location of that source on a hard drive or online. When you finish adding the source, click OK and you’ll see it added to the IDLE Help menu. There are also buttons on the General tab of the IDLE Preferences dialog box for editing and removing help sources.

View Article
page 1
page 2
page 3
page 4
page 5
page 6
page 7
page 8
page 9

Quick Links

  • About For Dummies
  • Contact Us
  • Activate A Book Pin

Connect

Opt in to our newsletter!

By entering your email address and clicking the “Submit” button, you agree to the Terms of Use and Privacy Policy & to receive electronic communications from Dummies.com, which may include marketing promotions, news and updates.

About Dummies

Dummies has always stood for taking on complex concepts and making them easy to understand. Dummies helps everyone be more knowledgeable and confident in applying what they know. Whether it's to pass that big test, qualify for that big promotion or even master that cooking technique; people who rely on dummies, rely on it to learn the critical skills and relevant information necessary for success.

Terms of Use
Privacy Policy
Cookies Settings
Do Not Sell My Personal Info - CA Only