Python

From PrattWiki
Revision as of 19:29, 12 September 2023 by DukeEgr93 (talk | contribs)
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

  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.