Python

A Compendium of Python Commands

May 10, 2022

Robert Smithson, Dia Beacon

I love the Python programming language. It is the most intuitive of all the languages, I feel. After you use it awhile, you can pretty much guess the proper syntax for an operation, and you'd probably be right.

Still I need a handy reference guide. So here is a collection of basic Python operations, presented not so much as a form of instruction, but as a guide for quick consultation. I also cover the creation of lists and dictionaries, functions and file input and output.

I cribbed the material from the documentation, as well as from the Codecademy site.

For many of these examples, I'm using the Python Shell. The “>>>” is the shell prompt, the unannotated lines below that are the response from the shell. These samples used Python 3.9 and 3.10, both from the Anaconda distribution for Windows.

SIMPLE OPERATIONS:

Mathematical operations:
2+2=4

An equal sign assigns value to a variable:
Bob=1, Bob

Variables (including text strings) can be updated with "+=":
Bob=1, Bob+=5, Bob 6

The modulo operator ("%") provides the remainder of a division:
34%3 = 1

You can do complex numbers:
(4+5j)

Variable names can be part of a formula:
 A = 10 B = 20 A * B 200

Text strings can be concatenated ("+") :
concatenation

Variables are case sensitive. Strings need to be in double or single quotes.

Strings are indexed:
Indexed Strings

Length of a string can be determined thusly:
string length

Strings can be mathematically manipulated:
mixed string

Numbers need to be converted into a text string to be read as text:
number to string

Multi-line text can be printed with triple quotes:
multi-line string

LISTS:

Now covered on its own page

DICTIONARIES:

Create a Dictionary:
Dictionaries are sets of key/value pairs, with the item ("key") on the left and the "value" on the right. A value can be a number, but also can be a string or a Boolean ("true").

Dictionary

Print a Dictionary:
The entire set will be printed in no specific order.
Dictionary print


Print just the keys:
Dictionary print keys

Print just the values:
Dictionary print values

LOOPS/CONDITIONALS:

If/Then:
If Then statement

If/Else:
If Else statement

If/Elif:
If Elif statement

For Loop:
For Loop

While loop:
While Loop

Functions:
The "print_grades" function is defined in the first block, and is called in the bottom line, just after the dataset is entered. The function cycles through each data point, printing each grade:
Function

The variables passed into functions are called parameters. When the function is called, they are passed in as variables. You can also define default values within the parameters themselves. They can be over-written by an externally-defined variable:
Function

Variables defined inside a function have "local scope." Those defined outside have "global scope."

You can get multiple return variables from a function by declaring multiple variables and then set them to the return call:
Multi-Return Function

break

READING/WRITING FILES:

Reading the Files in a Directory, With Filter:


print glob.glob('*.jpg')
['03-Succulent-DirtyBandits-Blah.jpg', '03-Succulent-Ezo-SinOfEnvy.jpg']

Creates a File:
f= open(FileNameVariable,"w+")
Opens a File
f2= open("/File/Location/", "r")
Reads data in the file previously opened:
Content =f2.read()
Write data to the file created:
f.write(Content)
Closes both files:
f.close()
f2.close()

break

More Tech Tuts