Comments and Variables

Definitions to Learn:

Comments:  pieces of your program that are "invisible" and left as notes to programmers.  Comments are useful for writing your name in the program, labeling sections, and even to temporarily hide code you might want to use later.

Variables:  a variable is like a labelled box in computer memory.  That box holds information as a number or letters.

Integer:  this is a variable number that can be positive or negative

String:  this is a letter or word and does not hold a numerical value.  The number 8 as a string would mean the word eight, not the value 8 (like an integer).

 


Lesson 1 Program (type the following code into your program window)

 # Lesson 1

print “Welcome to my first Python program”
w= “blah”
x = “stuff”
y = 1
z = 2

print x, y, z

print y + z
print 1 + 2

print w + x


Program Explanation

Line Code Explanation
1 #Lesson 1 The hashtag or # means this line is a comment and is ignored by the program, but is a note for other programmers.
2 print "Welcome to my first Python program" print makes whatever is in quotations appear in the output screen.  You must have the quote to start and end it.
3 w = "blah" This creates a variable called w to hold the string or word called "blah".  Anytime you see a w, it now means "blah"
4 x = "stuff" This creates a variable called x to hold the string called "stuff".
5 y = 1

This creates a variable called y to hold an integer value of 1.  This is 1 the quantity, not 1 the word.  

y = 1 is NOT the same as saying y = "1".  

6 z = 2 This creates a variable called z to hold an integer value of 2.
7 print x,y,z This puts the values of variables x, y and z onto the output screen.
8 print y + z Since y = 1 and z = 2, this adds the values and outputs them.  This is why you see a 3.
9 print 1 + 2 This is to show you can also just add numbers straight in the program and it does not have to be variables
10 print w + x This merges the strings where w = "blah" and x = "stuff" to give you an output that says blahstuff

Visual Explanation of Output


Lessons Learned

From this simple program you should have learned the following:

  1. Comments are made with #
  2. print is the command to put words on the screen that are between the quotes “ “
  3. variables are declared simply by (name) =
  4. if the variable name equals something in quotes, that variable is a string
  5. if the variable name equals a number, that variable is an integer
  6. to output a variable, you just have type print and the variable name
  7. if you want to output multiple variables, put a comma after the previous one
  8. putting a + sign between two variables will add the two if integers
  9. putting a + sign between two strings will print them without a space
  10. you can’t put a + sign between a string(w) and integer (x)