Miscellaneous Python Basics

From OpenCircuits
Jump to: navigation, search

Contents

Comments

A program is really a set of instructions to the computer telling it what to do. But sometimes you want to write something to yourself that the computer will ignore: a comment. To do this just put a pound sign "#" in front of the text you want to comment. So here are a few examples: ( You can run them if you want, try this with at least some, mess with them a bit, you will remember better if you do this. )

# here is Hello World
print "Hello World"


# Hello World on 2 lines
print "Hello"
print "World"   # this is the second word

The # can be any where on a line, but only the text after the # is part of the comment. You can have as many # on a line as you want but only the first does much.


Sometimes you have some code that does not work, but you want to leave around. Or you may have some code that sometimes you want and other times you do not want. For both of these situations you can "comment out" the code when you do not want.

print "sometimes I want lots of ..."
#print "..................................."   # comment in and out
print "sometimes not"

Running Examples

Often you will see material with example code. It can be good for you to try running the code. But How???

  • Sometimes the material will have a download link, you can download the code into a Spyder project ( I have one call downloads ) and then open the file and run it.
  • You can often copy and pasted it. Copy it from the page and:
    • Paste it into the python console in Spyder. The console is a pane in Spyder that usually says something like "Python 1" above it. It should run one line at a time.
    • Paste it into an empty file ( this is the way I like, I keep a file called scratch.py around just for that purpose. Run the file. Clear out the text ( select all, delete ) when you are don with it.
    • What can go wrong: sometimes when you copy you get junk like line numbers, extra spaces, and even invisible characters that will mess up the code. Sometimes the example is just wrong. In this wiki I hope we are free of these problems.

Please try to run the example code below.

Variables

A slightly more sophisticated way of printing hello world is a bit indirect. We put the text into a "variable". Here the variable is going to be called hw and the code is:

hw = "Hello World"
print hw

When you write "Hello World" it is sometimes called a literal because it is literally just what it looks like. On the other hand hw is a variable and might be almost anything. You can get a idea of what a variable is by printing it, just like the above.

A more complicated example: ( please copy to spyder and run )

hw = "Hello Sue"
print hw
hw = 5
print hw
hw2 = 99.
print hw2

Something that is new is that hw is sometimes text ( in programming jargon a string ) and sometimes a number. Lets look at the same example with comments:

# above example with comments
hw = "Hello Sue"        # hw is now "Hello Sue", not "Hello World" but not much different
print hw                # hw will print as  "Hello Sue"
hw = 5                  # hw is now a number 5
print hw                # prints as 5, "5" which is a string would print the same way
hw2 = 99.               # a new variable hw2 is now 99.
print hw2               # prints 99.

What do you think this does. Try it, make sure you understand.

# something a bit new
hw = "Hello Sue"        # hw is now "Hello Sue", not "Hello World" but not much different
print "hw"                # will print as ....

Types

So numbers and strings ( text ) are different types. You can find out what type something is by using the magic function type(). Do it like this:

# types
hw = "Hello Sue"        # hw is now "Hello Sue", not "Hello World" but not much different
print hw                # will print as ....
print "hw is of type: "
print type( hw )        # does not print hw instead with print the type of hw

# more compact way of doing the above
hw = "Hello Sue"        # hw is now "Hello Sue", not "Hello World" but not much different
print hw, "hw is of type", type(hw)  # 


.... not done yet

You Might Be Interested In

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox