Python
Jump to navigation
Jump to search
Contents
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
Quick Intro to .py Files
Commands simply typed into the console run but are not saved. This is problematic when debugging programs because in the console if a command is mistyped, all previous commands may have to be retyped to get the program back to the correct state. To alleviate this problem, and to save your commands for future use, you will generally save commands in a Python script. You can read more in Pratt Pundit about creating .py
}-files for scripts and functions - specifically in the page Python:Script.
- On the left side of the Spyder window, you will see an editor window that contains an untitled
.py
file in it. You can create new files as needed with the new file icon at top left or theFile->New file
command. - You can now type commands, just as if you were typing in the command window. The difference is, these commands will not execute until you save the script and run it. When the script is run, Python will execute all the commands in the file. Type the lines below in your new script. Note that your file does not yet have a name.
x = 3 y = 4 z = 5 print(x, y, z) a = x + y x = 10 b = y + z c = x + z print(a, b, c, x, y, z)
- Before you save or run the script, try to interpret the code for yourself and determine what the variables
x
,y
, andz
will be at the firstprint()
command and what variablesa
,b
,c
,x
,y
, andz
, will be when the script is finished. Write these down to compare with the answers Python gives. - Save and run this script as
run_test.py
. To save the script, click on the save icon (the floppy disk third from the left) or go to theFile
menu and selectSave
. If you are looking at this page as a result of being in EGR 103 for the Fall 2023 semester, in theSave file
window that comes up, find your local Box folder, navigate to theEGR103F23
folder and then theLab02Files
folder within it. Save the file in that folder asrun_test.py
. - Once you have saved the file, click the
Run
button, which looks like a green play arrow. Alternatively, you can hit the F5 key on your keyboard, as long as your computer is set up to have function buttons work like...function buttons. You will notice that everything in the script runs just as if you had typed each of the lines of code in the script at the command prompt. You can see how the variables have been stored by typingwho
orwhos
. - Now go to the command window and type
runfile("run_test.py")
. You can run a Python program without having it open in the editing window as long as Python can see it. For right now, that means as long as it is in the current directory. To see the current directory, typepwd()
in the console. The current directory is also shown at the top right of the Spyder window.