Difference between revisions of "Python"

From PrattWiki
Jump to navigation Jump to search
 
(2 intermediate revisions by the same user not shown)
Line 3: Line 3:
  
 
== Installation and Preferences==
 
== Installation and Preferences==
* Currently using Spyder (Python 3.8) which came with Anaconda - [https://anaconda.org/anaconda/spyder Intsallation] - works on Linux, Windows, and MAC
+
* 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.
 +
 +
== 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 <code>.py</code>}-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 <code>.py</code> file in it.  You can create new files as needed with the new file icon at top left or the <code>File->New file</code> 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.<syntaxhighlight lang=python>
 +
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)
 +
</syntaxhighlight>
 +
# Before you save or run the script, try to interpret the code for yourself and determine what the variables <code>x</code>, <code>y</code>, and  <code>z</code> will be at the first <code>print()</code> command and what variables <code>a</code>,  <code>b</code>, <code>c</code>, <code>x</code>, <code>y</code>, and <code>z</code>, will be when the script is finished.  Write these down to compare with the answers Python gives.
 +
# Save and run this script as <code>run_test.py</code>.  To save the script, click on the save icon (the floppy disk third from the left) or go to the <code>File</code> menu and select <code>Save</code>. If you are looking at this page as a result of being in EGR 103 for the Fall 2023 semester, in the <code>Save file</code> window that comes up, find your local Box folder, navigate to the <code>EGR103F23</code> folder and then the <code>Lab02Files</code> folder within it.  Save the file in that folder as <code>run_test.py</code>.
 +
# Once you have saved the file, click the <code>Run</code> 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 typing <code>who</code> or <code>whos</code>.
 +
# Now go to the command window and type <code>runfile("run_test.py")</code>.  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, type <code>pwd()</code> in the console. The current directory is also shown at the top right of the Spyder window.

Latest revision as of 00:47, 13 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

  1. 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 open Terminal. Type spyder and Spyder should start up that way.
  2. 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 type help() in the command window at the command prompt and press enter. This will return a statement about the help 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.
  3. 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. If x already existed, its previous contents are destroyed and replaced with the new type and value; otherwise, Python will create a new variable called x 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.
    • 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 use print and cannot simply have the variable by itself on a line.
    • 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 typing y into the command window.
      It is important to note here that y is not created as a function of x - rather, it is created from a calculation including the value contained within x.
      Regardless of future changes to the x variable, the y variable will remain the same. To prove this, type x = 10 and then y at the command prompt; y is still 5.
    • who
      The user can see the names of the variables that have already been assigned by typing who.
    • whos
      By typing whos the user can find out of what type each variable assigned is and its data or information.

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.

  1. 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 the File->New file command.
  2. 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)
    
  3. Before you save or run the script, try to interpret the code for yourself and determine what the variables x, y, and z will be at the first print() command and what variables a, b, c, x, y, and z, will be when the script is finished. Write these down to compare with the answers Python gives.
  4. 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 the File menu and select Save. If you are looking at this page as a result of being in EGR 103 for the Fall 2023 semester, in the Save file window that comes up, find your local Box folder, navigate to the EGR103F23 folder and then the Lab02Files folder within it. Save the file in that folder as run_test.py.
  5. 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 typing who or whos.
  6. 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, type pwd() in the console. The current directory is also shown at the top right of the Spyder window.