{"id":3331,"date":"2014-12-05T10:59:08","date_gmt":"2014-12-05T10:59:08","guid":{"rendered":"http:\/\/blog.bachi.net\/?p=3331"},"modified":"2018-09-22T18:36:30","modified_gmt":"2018-09-22T18:36:30","slug":"python-3-4","status":"publish","type":"post","link":"https:\/\/blog.bachi.net\/?p=3331","title":{"rendered":"Python 3"},"content":{"rendered":"<p><a href=\"https:\/\/docs.python.org\/3.4\/contents.html\">Official Python Documentation<\/a><\/p>\n<h3>Python3 Tutorial<\/h3>\n<p><a href=\"https:\/\/en.wikiversity.org\/wiki\/Python_Concepts\/Numbers\">Python Concepts\/Numbers<\/a><\/p>\n<h4>type<\/h4>\n<pre class=\"brush: python; title: type; notranslate\" title=\"type\">\r\n&gt;&gt;&gt; type(6)\r\n&lt;class 'int'&gt;\r\n\r\n&gt;&gt;&gt; type(6.4)\r\n&lt;class 'float'&gt;\r\n \r\n&gt;&gt;&gt; type('6.4')\r\n&lt;class 'str'&gt;\r\n\r\n&gt;&gt;&gt; type(b'6.4')\r\n&lt;class 'bytes'&gt;\r\n\r\n&gt;&gt;&gt; type(&#x5B;'6.4'])\r\n&lt;class 'list'&gt;\r\n<\/pre>\n<h4>print<\/h4>\n<p><a href=\"http:\/\/www.python-kurs.eu\/python3_print.php\">print<\/a><br \/>\n<a href=\"https:\/\/www.python-course.eu\/python3_formatted_output.php\">Formatted Output<\/a><br \/>\n<a href=\"https:\/\/www.python.org\/dev\/peps\/pep-0378\/\">PEP 378 &#8212; Format Specifier for Thousands Separator<\/a><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n### comma separated list of values\r\n# sep=&quot; &quot; -&gt; separated by blanks (default behaviour)\r\n&gt;&gt;&gt; print(q, p, p * q)\r\n459 0.098 44.982\r\n\r\n&gt;&gt;&gt; print(q, p, p * q, sep=&quot;,&quot;)\r\n459,0.098,44.982\r\n\r\n&gt;&gt;&gt; print(q, p, p * q, sep=&quot; ! &quot;)\r\n459 ! 0.098 ! 44.982\r\n\r\n# string concatenation\r\n&gt;&gt;&gt; print(str(q) + &quot; &quot; + str(p) + &quot; &quot; + str(p * q))\r\n459 0.098 44.982\r\n\r\n### The Old Way or the non-existing printf and sprintf\r\n&gt;&gt;&gt; print('Art: %5d, Price per Unit: %8.2f' % (453, 59.058))\r\nArt:   453, Price per Unit:    59.06\r\n\r\n&gt;&gt;&gt; print(&quot;%#5x&quot; % 12336)\r\n0x3030\r\n\r\n### The Pythonic Way: The string method &quot;format&quot;\r\n&gt;&gt;&gt; print('Art: {0:5d}, Price per Unit: {1:8.2f}'.format(453, 59.058))\r\nArt:   453, Price per Unit:    59.06\r\n\r\n&gt;&gt;&gt; print('Art: {a:5d}, Price per Unit: {b:8.2f}'.format(a=453, b=59.058))\r\nArt:   453, Price per Unit:    59.06\r\nArt:   453, Price per Unit:    59.06\r\n\r\n<\/pre>\n<h4>locale<\/h4>\n<p><a href=\"https:\/\/www.peterbe.com\/plog\/thousands-commafy-large-numbers\">Fastest way to thousands-commafy large numbers in Python\/PyPy<\/a>, 13 October 2012<\/p>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/14547631\/python-locale-error-unsupported-locale-setting\">Python locale error: unsupported locale setting<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/955986\/what-is-the-correct-way-to-set-pythons-locale-on-windows\">What is the correct way to set Python&#8217;s locale on Windows?<\/a><br \/>\n<a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/c-runtime-library\/language-strings?view=vs-2017\">Language Strings<\/a><br \/>\n<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/x99tb11d.aspx\">VS2017: setlocale<\/a><br \/>\n<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/standard\/base-types\/standard-numeric-format-strings\">.NET Standard Numeric Format Strings<\/a><\/p>\n<table>\n<tr>\n<td>\u2018n\u2019<\/td>\n<td>Number. This is the same as \u2018d\u2019, except that it uses the current locale setting to insert the appropriate number separator characters.<\/td>\n<\/tr>\n<\/table>\n<pre class=\"brush: python; title: Locale; notranslate\" title=\"Locale\">\r\n# On Windows 10\r\n&gt;&gt;&gt; print(locale.getlocale())\r\n('German_Switzerland', '1252')\r\n\r\n&gt;&gt;&gt; locale.setlocale(locale.LC_ALL, 'german-swiss')\r\n'German_Switzerland.1252'\r\n\r\n&gt;&gt;&gt; x = 1234567890\r\n&gt;&gt;&gt; print(&quot;The value is {0:6,n}&quot;.format(x))\r\nTraceback (most recent call last):\r\n  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;\r\nValueError: Cannot specify ',' with 'n'.\r\n\r\n&gt;&gt;&gt; print(&quot;The value is {0:6n}&quot;.format(x))\r\nThe value is 1234567890\r\n\r\n<\/pre>\n<h3>Different Tutorials<\/h3>\n<p><a href=\"http:\/\/effbot.org\/zone\/python-list.htm\">An Introduction to Python Lists<\/a><br \/>\n<a href=\"http:\/\/www.tutorialspoint.com\/python\/python_command_line_arguments.htm\">Python &#8211; Command Line Arguments<\/a><\/p>\n<h3>IP-Address Validation<\/h4>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/11593022\/regular-expressions-in-python-for-dissecting-ip\">Regular Expressions in Python for dissecting IP<\/a><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nfirst, second, third, fourth = str(ipaddy).split('.')\r\n<\/pre>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/319279\/how-to-validate-ip-address-in-python\">How to validate IP address in Python?<\/a><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&gt;&gt;&gt; import ipaddress\r\n\r\n&gt;&gt;&gt; ipaddress.ip_address('127.0.0.1')\r\nIPv4Address('127.0.0.1')\r\n\r\n&gt;&gt;&gt; ipaddress.ip_address('277.0.0.1')\r\nTraceback (most recent call last):\r\n  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;\r\n  File &quot;\/usr\/lib\/python3.3\/ipaddress.py&quot;, line 54, in ip_address\r\n    address)\r\nValueError: '277.0.0.1' does not appear to be an IPv4 or IPv6 address\r\n\r\n&gt;&gt;&gt; ipaddress.ip_address('foobar')\r\nTraceback (most recent call last):\r\n  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;\r\n  File &quot;\/usr\/lib\/python3.3\/ipaddress.py&quot;, line 54, in ip_address\r\n    address)\r\nValueError: 'foobar' does not appear to be an IPv4 or IPv6 address\r\n<\/pre>\n<p><a href=\"https:\/\/docs.python.org\/3\/library\/ipaddress.html\">21.28. ipaddress \u2014 IPv4\/IPv6 manipulation library<\/a><br \/>\n<a href=\"https:\/\/github.com\/drkjam\/netaddr\/\">`netaddr` is a Python library for representing and manipulating network addresses<\/a><br \/>\n<a href=\"http:\/\/pythonhosted.org\/\/netaddr\/\">netaddr 0.7.11 documentation<\/a><br \/>\n<a href=\"https:\/\/code.google.com\/p\/netaddr\/wiki\/IPTutorial\">IP Address Tutorial<\/a> (21.08.2009)<br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/2714942\/python-3-ipaddr-netaddr-modules\">python 3: ipaddr\/netaddr modules<\/a><\/p>\n<h3>String to Number<\/h3>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/15444945\/convert-strings-to-int-or-float-in-python-3\">Convert strings to int or float in python 3?<\/a><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ntry:\r\n    integer = int(input('something: '))\r\n    print('2 + {} = {}'.format(integer, integer + 2))\r\nexcept ValueError as e:\r\n    print(&quot;ooops - you didn't enter something I could make an int of...&quot;)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Official Python Documentation Python3 Tutorial Python Concepts\/Numbers type &gt;&gt;&gt; type(6) &lt;class &#8216;int&#8217;&gt; &gt;&gt;&gt; type(6.4) &lt;class &#8216;float&#8217;&gt; &gt;&gt;&gt; type(&#8216;6.4&#8217;) &lt;class &#8216;str&#8217;&gt; &gt;&gt;&gt; type(b&#8217;6.4&#8242;) &lt;class &#8216;bytes&#8217;&gt; &gt;&gt;&gt; type(&#x5B;&#8217;6.4&#8242;]) &lt;class &#8216;list&#8217;&gt; print print Formatted Output PEP 378 &#8212; Format Specifier for Thousands Separator ### comma separated list of values # sep=&quot; &quot; -&gt; separated by blanks (default behaviour) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3331","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/3331","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3331"}],"version-history":[{"count":13,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/3331\/revisions"}],"predecessor-version":[{"id":8397,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/3331\/revisions\/8397"}],"wp:attachment":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}