Python
Jump to navigation
Jump to search
Introduction
This page is currently just a sandbox for all things Python and Computational Methods. Information will be spun out of it as time goes on.
Installation and Preferences
- Currently (9/1/2023) using Spyder (Python 3.9.11) which came with Anaconda - Installation - works on Linux, Windows, and MAC
- Colors are in Tools->Preferences->Syntax coloring; I use Zenburn
Quick Intro to Console
- Start the Spyder IDE. If you are on a Windows machine, you can go to the Start menu, find the Anaconda3 collection, and find the Spyder icon from there or you can use the search option to look for "Spyder" and choose the app. The logo looks like a spider web with a...python. Regardless of platform, you can start the Anaconda Navigator. Spyder will be one of the options within it.
If that does not work, on Windows machines go to the Anaconda3/Anaconda Prompt and on a macOS machine go to the magnifying glass and openTerminal
. Typespyder
and Spyder should start up that way. - There should be several components to the Spyder window on your screen. What you are most interested in right now is the IPython console. This is where you can type commands directly into Python. The command prompt in the console is the word
In
followed by a number that tells you how many commands you have issued in the console.
Go ahead and typehelp()
in the command window at the command prompt and press enter. This will return a statement about thehelp
command in Python. Note that you can get help on several aspects of Python from the console. You can also get help through the Help menu at the top of the screen. Hit return to get out of the help function. - To get associated with the IPython console and commands, type the following commands at the prompt.
x = 2
- This command will set a variable named
x
equal to an integer with the value 2 in it. Ifx
already existed, its previous contents are destroyed and replaced with the new type and value; otherwise, Python will create a new variable calledx
and start from scratch. Regardless,x
will be \texttt{2} when this command is finished. - To see what
x
is, you can look in the Variable explorer window. The Variable explorer window will give you the name, type, size, and possibly contents of a variable. Currently, it should show a variable named x of type int and size 1 with a value of 2 in it.
- This command will set a variable named
x
- A user is able to find the value of any variable simply by typing that variable's name into the console.
print(x)
- You can also see the value of a variable by using the
print()
command. In this case, the console simply prints the value rather than returning it as an output. Note that in a script, you must useprint
and cannot simply have the variable by itself on a line.
- You can also see the value of a variable by using the
y = x + 3
- When assigning values to a variable, the user may implement already-defined variables in the calculation for that variable. Given the formula above,
y
should now be equal to 5. You can check this by typingy
into the command window.
It is important to note here thaty
is not created as a function ofx
- rather, it is created from a calculation including the value contained withinx
.
Regardless of future changes to thex
variable, they
variable will remain the same. To prove this, typex = 10
and theny
at the command prompt;y
is still 5.
- When assigning values to a variable, the user may implement already-defined variables in the calculation for that variable. Given the formula above,
who
- The user can see the names of the variables that have already been assigned by typing
who
.
- The user can see the names of the variables that have already been assigned by typing
whos
- By typing
whos
the user can find out of what type each variable assigned is and its data or information.
- By typing