What is Python?
How to install Python
Deven's Guide to Python
Variables
Variable types
Printing to the console
String concatenation
Printing across multiple lines
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.
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 Python Programming for the Absolute Beginner, a book from which I learned the basics of Python3 in middle school, or Starting Out with Python, a book from which I learned some more stuff about Python3 in CPT127. Starting Out is pretty expensive though and Python for the Absolute Beginner isn't cheap either, so remember that you can nearly always learn anything for free on-line.
On Debian-based Linux you can simply apt install python3 python3-pip
.
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.
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 math
and time
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 (object-oriented programming); I am not a good role model and I'm not your father.
Python doesn't have variable declarations or explicit typing.
While in, for example, C, a variable named a
that stores 5
could be declared with int a = 5;
, that same variable could be declared in Python with just a = 5
.
That means a
could, across a program, hold 5
, 5.0
, [1,2,3,4,5]
, or "HI"
.
Though this makes Python very newbie-friendly, this has been the subject of intense debate in programmer circles, because it's stupid it enables programmers that aren't following best practices to reuse variables like a
or b
a ton of times in programs and make their code almost unreadable.
While variables aren't explicitly typed, they are implicitly typed.
You can see the type of a variable with type(variable)
.
For example, type(4)
would return <class 'int'>
.
type(hello)
would return class 'str'
, and type('a')
would also return class 'str'
.
(As opposed to other languages, Python doesn't have a char
type.
Only single-length strings.)
type([1])
returns class 'list'
and type(4.0)
returns class 'float'
.
Type conversion can be done using int()
, float()
, or str()
, to integer, floating-point, and string variable types respectively.
There are probably other type conversions but I don't use them.
print(thing)
will print thing
to the terminal.
thing
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.
print()
by default will add a newline (\n
) to the end of the string it's printing.
It's possible to change the ending character with end=
.
For example, print("HELLO WORLD", end='')
will print "HELLO WORLD
without the newline.
print("HELLO WORL", end='D')
will print "HELLO WORLD
" the same way print("HELLO WORLD", end='')
did.
To "join" two strings together in Python, just use the +
operator.
For example, print("HELLO " + "WORLD")
will just print "HELLO WORLD
".
It is worth noting that print("HELLO" + "WORLD")
without the space in that "HELLO
" string will produce HELLOWORLD
.
You can only concatenate a string with another string. print("THIS IS THE NUMBER FIVE " + 5)
will not work.
things
The ideal way to print multiple things
is to use a comma to separate the terms in your print()
statement.
For example, print("THIS IS THE NUMBER FIVE", 5)
will print THIS IS THE NUMBER FIVE 5
.
This relies on Python's automatic conversion of any variable to a string before it prints anything.
print("HELLO" + 5)
does not work the same way, because you cannot concatenate a string with an integer.
In order to reproduce print("THIS IS THE NUMBER FIVE", 5)
with string concatenation, you would need to write print("THIS IS THE NUMBER FIVE " + str(5))
.
print(x+y)
versus print(x,y)
Personally, I only ever use print(x,y)
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:
a = 1
b = 2
c = 3
print(str(a) + "." + str(b) + "." + str(c))
Which will return 1.2.3
, whereas:
a = 1
b = 2
c = 3
print(a, ".", b, ".", c)
Will return 1 . 2 . 3
.
These little differences can make a big deal in how your program is presented.
There are three ways to accomplish multi-line printing.
The first, easy way, is to just use multiple print()
statements, because print()
automatically adds a newline to the end of the print.
print("HELLO")
print("WORLD")
Will return:
HELLO
WORLD
You can also use the newline escape character, \n
.
This is similar to the <br />
tag in HTML.
print("HELLO\nWORLD")
Will return:
HELLO
WORLD
Finally, you can use the triple-apostrophe ('''
) to do the same thing without any codes.
print('''HELLO
WORLD''')
Will return:
HELLO
WORLD
Functions are the most powerful part of any programming language.
In Python functions are themselves variables, with the type <class 'function'>
.
In Python a very simple function we can study is the following:
def f():
return
This function's name is f
and it does everything after the line with def
(so, just return
).
That line with def
is actually officially called a function header.
I think that's a stupid name personally, I just call it the "line with def" and everybody gets what I'm talking about.
def
is a special thing in Python that defines 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 cold itself.
"Coldness" is a function that can be performed by matter under a specific circumstance,
namely, its particles having a below-average level of excitement.