1
0
src/homepage/python.html
2020-11-17 11:23:26 -05:00

199 lines
10 KiB
HTML

<!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><A HREF="/">~ Return</A></P>
<H1>Deven's Guide to Python</H1>
<H3>Updated 2020-11-17</H3>
<HR width="25%" size="1" align="left" />
<H2>Table of contents</H2>
<p>
<b><a href="#introduction">What is Python?</a></b><br />
<b><a href="#installation">How to install Python</a></b><br />
<b><a href="#guide">Deven's Guide to Python</a></b><br />
<a href="#variables">Variables</a><br />
<small>
<a href="#variable-types">Variable types</a><br />
</small>
<a href="#printing">Printing to the console</a><br />
<small>
<a href="#string-concatenation">String concatenation</a><br />
<a href="#print-multiple">Printing across multiple lines</a><br />
</small>
</p>
<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>&lt;class 'int'&gt;</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>&lt;br /&gt;</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>
<H2 ID="functions">Functions</H2>
<P>
Functions are the most powerful part of any programming language.
In Python functions are themselves variables, with the type <code>&lt;class 'function'&gt;</code>.
In Python a very simple function we can study is the following:
</P>
<P><CODE>
def f():<BR />
&emsp; return
</CODE></P>
<P>
This function's name is <CODE>f</CODE> and it does everything after the line with <CODE>def</CODE> (so, just <CODE>return</CODE>).
That line with <CODE>def</CODE> is actually officially called a <I>function header</I>.
I think that's a stupid name personally, I just call it the "line with def" and everybody gets what I'm talking about.
</P>
<H3>def</H3>
<P>
<CODE>def</CODE> is a special thing in Python that <B>def</B>ines a function.
Y'know words in English? How do those work? I can say something is "cold",
but that structure of vowels and consonants isn't <I>cold</I> itself.
"Coldness" is a <I>function</I> that can be performed by matter under a specific circumstance,
namely, its particles having a below-average level of excitement.
</P>
</BODY>
</html>