Difference between revisions of "Python"
Jump to navigation
Jump to search
Line 3: | Line 3: | ||
== Installation and Preferences== | == Installation and Preferences== | ||
− | * Currently using Spyder (Python 3. | + | * Currently (9/1/2023) using Spyder (Python 3.9.11) which came with Anaconda - [https://anaconda.org/anaconda/spyder Installation] - works on Linux, Windows, and MAC |
* Colors are in Tools->Preferences->Syntax coloring; I use Zenburn | * 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.<br> 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 open <code>Terminal</code>. Type <code>spyder</code> 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 <code>In</code> followed by a number that tells you how many commands you have issued in the console. <br> Go ahead and type <code>help()</code> in the command window at the command prompt and press enter. This will return a statement about the <code>help</code> 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. | ||
+ | #* <code>x = 2</code> | ||
+ | #*:This command will set a variable named <code>x</code> equal to an integer with the value 2 in it. If <code>x</code> already existed, its previous contents are destroyed and replaced with the new type and value; otherwise, Python will create a new variable called <code>x</code> and start from scratch. Regardless, <code>x</code> will be \texttt{2} when this command is finished. | ||
+ | #*:To see what <code>x</code> 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. | ||
+ | #*<code>x</code> | ||
+ | #*:A user is able to find the value of any variable simply by typing that variable's name into the console. | ||
+ | #*<code>print(x)</code> | ||
+ | #*:You can also see the value of a variable by using the <code>print()</code> command. In this case, the console simply prints the value rather than returning it as an output. Note that in a ''script'', you must use <code>print</code> and cannot simply have the variable by itself on a line. | ||
+ | #*<code>y = x + 3</code> | ||
+ | #*:When assigning values to a variable, the user may implement already-defined variables in the calculation for that variable. Given the formula above, <code>y</code> should now be equal to 5. You can check this by typing <code>y</code> into the command window. <br>It is important to note here that <code>y</code> is not created as a ''function'' of <code>x</code> - rather, it is created from a calculation including the value contained within <code>x</code>. <br>Regardless of future changes to the <code>x</code> variable, the <code>y</code> variable will remain the same. To prove this, type <code>x = 10</code> and then <code>y</code> at the command prompt; <code>y</code> is still 5. | ||
+ | #*<code>who</code> | ||
+ | #*:The user can see the names of the variables that have already been assigned by typing <code>who</code>. | ||
+ | #*<code>whos</code> | ||
+ | #*:By typing <code>whos</code> the user can find out of what type each variable assigned is and its data or information. |
Revision as of 19:29, 12 September 2023
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