Python Basics

Software to download and install:

Python 3: https://www.python.org/downloads/

PyCharm: https://www.jetbrains.com/pycharm/

Linux:

You will need to use the pip3 package manager as well. It’s the Python3 version of something called PyPi. I’ve encountered Linux distros that only had pip for Python 2 rather than Python 3. In that case, you will need to do this:

sudo apt-get install pip3

To run a Python program at the command line, use this:

python3 myprogram.py

To change your terminal window to the correct directory for running your Python program do this on Windows:

dir C:\Users\Alice\Desktop\MyApp

Or on macOS or Linux:

cd /home/alice/Desktop/MyApp

But change it for the actual name and folder, as those are just examples.

For more command line information, see chapter 8.

In some cases, on Windows, your path variable might be set up so that python means python3 instead of python2. Typically, “python” by itself means python2, and “python3” means python 3. But my setup is a little weird. Long story short, you don’t ever want to use python 2.

Python file types:

.py – Python scripts. Most common file type.

.pyc – Compiled Python file. Not used very often, because Python is an interpreted language, meaning you can run Python without compiling it. Compiling Python is possible, but not necessary. Don’t bother with it.

Python boilerplate:

#!/usr/bin/env python3

import sys

def main():

print(“hello world”)

if __name__ == ‘__main__’:

try:

main()

except KeyboardInterrupt:

print(“\nQuitting. Goodbye.”)

sys.exit()

You can technically use loose Python in a script with no shebang, no main function, and so on. But you really ought to use the above for a basic structure for your program. If your program needs to wrap up before exiting completely when a user issues a command interrupt with ctrl+c, you can put the wrapping-up code in the except KeyboardInterrupt block. Or you can customize the message for when someone exits the program. Keep in mind that programs that end on their own won’t see that. It’s only if there’s a loop or waiting for user input or something, and then the user quits with ctrl+c instead of through normal means in the program.

Python if __name__ == “__main__”:

if __name__ == ‘__main__’:

try:

main()

except KeyboardInterrupt:

print(“\nQuitting. Goodbye.”)

sys.exit()

The reason why you’ll see this line a lot (if __name__ == ‘__main__’:) is because your script might be run directly, as a driver class, or it might be imported from something else.

In this case, I added exception handling for KeyboardInterrupt, which means that when the user wants to end the program by using ctrl+c, they will get the message that tells them the program is quitting. A program will need to import sys (short for system) in order to use sys.exit() though.

Python printing:

print(“hello world”)

Python single-line comments:

# this is a single-line comment

Python multi-line comments:

”’

multi

line

comment

”’

Python import:

Examples of modules you might want to import into your program (do it after the shebang but before your main method):

import os

import sys

import time

import datetime

import json

from math import PI

from abc import *

from math import *

from math import sin, cos, tan

import urllib

import unittest

import re #regular expressions

import inspect

You can even download non-standard modules or write your own modules. However, try to stick with the standard library for now.

For more information, view the official documentation for the Python standard library:

https://docs.python.org/3/library/index.html

That page has links to specific modules that you can import. If you see an import statement in Python, it corresponds to a module, or at least something from within a module.

That being said, sometimes the official documentation can be a little wordy and abstract, so I sometimes look things up on Youtube as well.

← Previous | Next →

Python Topic List

Main Topic List

Leave a Reply

Your email address will not be published. Required fields are marked *