Real Python

We’re going to start off this series by going through whatever fancies me on Real Python.

Their Introduction to Python seems like a reasonable place to start, so off we go.

Repl.it

We can start sharing and playing with code instantly with repl.it.

IDLE 3

Interacting With Python IDLE

It turns out IDLE is an IDE for Python is perhaps usually installed with most distributions by default, so I figure I’d kick the tires on it. When I went to first open it on the command line it showed me this error:

1
2
3
❯ idle3
** IDLE can't import Tkinter.
Your Python may not be configured for Tk. **

Once I installed tk-dev I was able to open it just fine:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
❯ sudo apt install tk-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libexpat1-dev libfontconfig1-dev libfreetype-dev libfreetype6-dev libice-dev
  libpng-dev libpng-tools libpthread-stubs0-dev libsm-dev libtcl8.6 libtk8.6
  libx11-dev libxau-dev libxcb1-dev libxdmcp-dev libxext-dev libxft-dev
  libxrender-dev libxss-dev libxt-dev tcl tcl-dev tcl8.6 tcl8.6-dev tk tk8.6
  tk8.6-dev x11proto-core-dev x11proto-dev x11proto-scrnsaver-dev
  x11proto-xext-dev xorg-sgml-doctools xtrans-dev
Suggested packages:
  freetype2-doc libice-doc libsm-doc libx11-doc libxcb-doc libxext-doc
  libxt-doc tcl-doc tcl-tclreadline tcl8.6-doc tk-doc tk8.6-doc
The following NEW packages will be installed:
  libexpat1-dev libfontconfig1-dev libfreetype-dev libfreetype6-dev libice-dev
  libpng-dev libpng-tools libpthread-stubs0-dev libsm-dev libtcl8.6 libtk8.6
  libx11-dev libxau-dev libxcb1-dev libxdmcp-dev libxext-dev libxft-dev
  libxrender-dev libxss-dev libxt-dev tcl tcl-dev tcl8.6 tcl8.6-dev tk tk-dev
  tk8.6 tk8.6-dev x11proto-core-dev x11proto-dev x11proto-scrnsaver-dev
  x11proto-xext-dev xorg-sgml-doctools xtrans-dev

Then re-install python so that it compiles with the tk dependency:

1
2
3
4
5
❯ pyenv install 3.8.6
pyenv: /home/ascherger/.pyenv/versions/3.8.6 already exists
continue with installation? (y/N) y
Installing Python-3.8.6...
Installed Python-3.8.6 to /home/ascherger/.pyenv/versions/3.8.6

It then opens right up:

1
❯ idle3

idle3 opens a window that looks like a repl

Data Types

Data Types are a great place to start when building up an understanding of what primitive tools you have at your disposal in a language. They also usually come with a few odd traps or gotchas.

Integers

PrefixInterpretationBase
0b or 0BBinary2
0o or 0OOctal8
0x or 0XHex16

Floats

Issues and Limitations

Otherwise, they work as expected, just like in most languages, round-off error exists because creating something like “1/10” in binary is nearly impossible without us all agreeing on how to represent it. The IEEE 754 specification is what Python (and most languages) have agreed upon.

Complex Numbers

Seeing as how Python is used by the scientific community, it is of no surprise that it has a way to represent imaginary numbers.

Strings

Work just like you expect, of course with the use of \ allows you to break up strings which is nice, but also lets you violate the requirements for whitespace in method definitions, so I imagine a more “Python” way might be to use a String builder pattern, but who knows.

Raw Strings are pretty cool in that it will ignore all the escape characters if you prefix the string with an r or R.