Add an early draft of Deven's Guide to Python
This commit is contained in:
parent
a2f93a2948
commit
da338263f8
159
homepage/python.html
Normal file
159
homepage/python.html
Normal file
@ -0,0 +1,159 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="content-type" content="text/html; charset=windows-1252">
|
||||||
|
<meta charset="US-ASCII">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<title>Deven's Guide to Python</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<p><small>Written by <a href="https://blake.instantfloppy.net">Deven Blake</a></small></p>
|
||||||
|
<h1>Deven's Guide to Python</h1>
|
||||||
|
<h3>Updated 2020-06-04</h3>
|
||||||
|
|
||||||
|
<hr width="25%" size="1" align="left">
|
||||||
|
|
||||||
|
<h2 id="introduction">What is Python?</h2>
|
||||||
|
<p><a href="https://en.wikipedia.org/wiki/Python_(programming_language)">
|
||||||
|
Python is an interpreted, high-level, general-purpose programming language.
|
||||||
|
Created by Guido van Rossum and first released in 1991, Python's design philosophy emphasizes code readability with its notable use of significant whitespace.
|
||||||
|
Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.
|
||||||
|
</a></p>
|
||||||
|
<p>
|
||||||
|
Basically, Python is a programming language that's supposed to be easy to read and easy to use.
|
||||||
|
As you'll learn, Python mandates the use of readable code and is pretty easily understood.
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
I learned Python3 through the local community college in CPT127-51N (an on-line class), though my usage of Python dates back to 2014 or so and I had already taken a Python2 course in high school.
|
||||||
|
My knowledge of Python is that of a beginner and this guide is meant only to help beginner programmers understand Python.
|
||||||
|
The intent is to make a guide that teaches Python efficiently.
|
||||||
|
If you would like to learn Python more slowly, I recommend <i><a href="https://www.amazon.com/Python-Programming-Absolute-Beginner-3rd/dp/1435455002">Python Programming for the Absolute Beginner</a></i>, a book from which I learned the basics of Python3 in middle school, or <i><a href="https://www.amazon.com/Starting-Out-Python-Tony-Gaddis/dp/0134444329">Starting Out with Python</a></i>, a book from which I learned some more stuff about Python3 in CPT127.
|
||||||
|
<i>Starting Out</i> is pretty expensive though and <i>Python for the Absolute Beginner</i> isn't cheap either, so remember that you can nearly always learn anything for free on-line.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<h2 id="installation">How to install Python</h2>
|
||||||
|
<p>
|
||||||
|
On Debian-based Linux you can simply <code>apt install python3 python3-pip</code>.
|
||||||
|
On Linux and UNIX operating systems, you can compile the source code found on the official Python website.
|
||||||
|
For the proprietary macOS and Windows operating systems there are installers available.
|
||||||
|
There are also packages available for AIX, IBM i, iOS and iPad OS, OS/390 and z/OS, Solaris, VMS, and HP-UX on the Python website, though they may not be as well-supported as the more common operating systems' packages.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<hr width="25%" size="1" align="left">
|
||||||
|
|
||||||
|
<h1 id="guide">The Guide to Python</h1>
|
||||||
|
<p>
|
||||||
|
For brevity's sake, this guide will not delve into Python libraries.
|
||||||
|
There will be short tutorials for some important commands contained in modules like <code>math</code> and <code>time</code> but that's about it.
|
||||||
|
This will also not talk about objects or classes because (a) I don't like object-oriented programming and (b) I don't know how to program with objects in Python.
|
||||||
|
Yes, I am apathetically ignorant when it comes to OOP (<b>o</b>bject-<b>o</b>riented</b> <b>p</b>rogramming); I am not a good role model and I'm not your father.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<h2 id="variables">Variables</h2>
|
||||||
|
<p>
|
||||||
|
Python doesn't have variable declarations or explicit typing.
|
||||||
|
While in, for example, C, a variable named <code>a</code> that stores <code>5</code> could be declared with <code>int a = 5;</code>, that same variable could be declared in Python with just <code>a = 5</code>.
|
||||||
|
That means <code>a</code> could, across a program, hold <code>5</code>, <code>5.0</code>, <code>[1,2,3,4,5]</code>, or <code>"HI"</code>.
|
||||||
|
Though this makes Python very newbie-friendly, this has been the subject of intense debate in programmer circles, because <s>it's stupid</s> it enables programmers that aren't following best practices to reuse variables like <code>a</code> or <code>b</code> a ton of times in programs and make their code almost unreadable.
|
||||||
|
</p>
|
||||||
|
<h3 id="variable-types">Variable types</h3>
|
||||||
|
<p>
|
||||||
|
While variables aren't explicitly typed, they <i>are</i> implicitly typed.
|
||||||
|
You can see the type of a variable with <code>type(<i>variable</i>)</code>.
|
||||||
|
For example, <code>type(4)</code> would return <code><class 'int'></code>.
|
||||||
|
<code>type(hello)</code> would return class <code>'str'</code>, and <code>type('a')</code> would <i>also</i> return class <code>'str'</code>.
|
||||||
|
(As opposed to other languages, Python doesn't have a <code>char</code> type.
|
||||||
|
Only single-length strings.)
|
||||||
|
<code>type([1])</code> returns class <code>'list'</code> and <code>type(4.0)</code> returns class <code>'float'</code>.
|
||||||
|
Type conversion can be done using <code>int()</code>, <code>float()</code>, or <code>str()</code>, to integer, floating-point, and string variable types respectively.
|
||||||
|
There are probably other type conversions but I don't use them.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<h2 id="printing">Printing to the console</h2>
|
||||||
|
<p>
|
||||||
|
<code>print(<i>thing</i>)</code> will print <code>thing</code> to the terminal.
|
||||||
|
<code><i>thing</i></code> used to (in Python2) have to be a string, however modern Python versions will automatically convert any type of variable to a string before printing.
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<code>print()</code> by default will add a newline (<code>\n</code>) to the end of the string it's printing.
|
||||||
|
It's possible to change the ending character with <code>end=</code>.
|
||||||
|
For example, <code>print("HELLO WORLD", end='')</code> will print "<code>HELLO WORLD</code> without the newline.
|
||||||
|
<code>print("HELLO WORL", end='D')</code> will print "<code>HELLO WORLD</code>" the same way <code>print("HELLO WORLD", end='')</code> did.
|
||||||
|
</p>
|
||||||
|
<h3 id="string-concatenation">String concatenation</h3>
|
||||||
|
<p>
|
||||||
|
To "join" two strings together in Python, just use the <code>+</code> operator.
|
||||||
|
For example, <code>print("HELLO " + "WORLD")</code> will just print "<code>HELLO WORLD</code>".
|
||||||
|
It is worth noting that <code>print("HELLO" + "WORLD")</code> <i>without</i> the space in that "<code>HELLO </code>" string will produce <code>HELLOWORLD</code>.
|
||||||
|
<b>You can only concatenate a string with another string. <code>print("THIS IS THE NUMBER FIVE " + 5)</code> will not work.</b>
|
||||||
|
</p>
|
||||||
|
<h3>Printing multiple <code>things</code></h3>
|
||||||
|
<p>
|
||||||
|
The ideal way to print multiple <code>things</code> is to use a comma to separate the terms in your <code>print()</code> statement.
|
||||||
|
For example, <code>print("THIS IS THE NUMBER FIVE", 5)</code> will print <code>THIS IS THE NUMBER FIVE 5</code>.
|
||||||
|
This relies on Python's automatic conversion of any variable to a string before it prints anything.
|
||||||
|
<code>print("HELLO" + 5)</code> <b>does not</b> work the same way, because you cannot concatenate a string with an integer.
|
||||||
|
In order to reproduce <code>print("THIS IS THE NUMBER FIVE", 5)</code> with string concatenation, you would need to write <code>print("THIS IS THE NUMBER FIVE " + str(5))</code>.
|
||||||
|
</p>
|
||||||
|
<h3><code>print(x+y)</code> versus <code>print(x,y)</code></h3>
|
||||||
|
<p>
|
||||||
|
Personally, I only ever use <code>print(x,y)</code> when I'm lazy.
|
||||||
|
It's a completely valid way to print things, I just prefer to convert and concatenate because it gives me more control over what's being printed.
|
||||||
|
When you use concatenation, you can do something like:
|
||||||
|
</p>
|
||||||
|
<p><code>
|
||||||
|
a = 1<br />
|
||||||
|
b = 2<br />
|
||||||
|
c = 3<br />
|
||||||
|
print(str(a) + "." + str(b) + "." + str(c))
|
||||||
|
</code></p>
|
||||||
|
<p>
|
||||||
|
Which will return <code>1.2.3</code>, whereas:
|
||||||
|
</p>
|
||||||
|
<p><code>
|
||||||
|
a = 1<br />
|
||||||
|
b = 2<br />
|
||||||
|
c = 3<br />
|
||||||
|
print(a, ".", b, ".", c)
|
||||||
|
</code></p>
|
||||||
|
<p>
|
||||||
|
Will return <code>1 . 2 . 3</code>.
|
||||||
|
These little differences can make a big deal in how your program is presented.
|
||||||
|
</p>
|
||||||
|
<h3 id="print-multiple">Printing across multiple lines</h3>
|
||||||
|
<p>
|
||||||
|
There are three ways to accomplish multi-line printing.
|
||||||
|
The first, easy way, is to just use multiple <code>print()</code> statements, because <code>print()</code> automatically adds a newline to the end of the print.
|
||||||
|
</p>
|
||||||
|
<p><code>
|
||||||
|
print("HELLO")<br />
|
||||||
|
print("WORLD")<br />
|
||||||
|
</code></p>
|
||||||
|
<p>Will return:</p>
|
||||||
|
<p><code>
|
||||||
|
HELLO<br />
|
||||||
|
WORLD
|
||||||
|
</code></p>
|
||||||
|
<p>
|
||||||
|
You can also use the newline escape character, <code>\n</code>. This is similar to the <code><br /></code> tag in HTML.
|
||||||
|
</p>
|
||||||
|
<p><code>print("HELLO\nWORLD")</code></p>
|
||||||
|
<p>Will return:</p>
|
||||||
|
<p><code>
|
||||||
|
HELLO<br />
|
||||||
|
WORLD
|
||||||
|
</code></p>
|
||||||
|
<p>
|
||||||
|
Finally, you can use the triple-apostrophe (<code>'''</code>) to do the same thing without any codes.
|
||||||
|
</p>
|
||||||
|
<p><code>
|
||||||
|
print('''HELLO<br />
|
||||||
|
WORLD''')
|
||||||
|
</code></p>
|
||||||
|
<p>Will return:</p>
|
||||||
|
<p><code>
|
||||||
|
HELLO<br />
|
||||||
|
WORLD
|
||||||
|
</code></p>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue
Block a user