omegazero

basics.py

Jun 21st, 2011
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.33 KB | None | 0 0
  1. """
  2. Welcome to Python! Anything inside of three quotation marks is multiline comment.
  3. Comments are NOT evaluated by Python when the program runs. Usually they contain
  4. notes or details about your program in regular language - like explaining
  5. that an algorithm sorts data a particular way or something. They also function
  6. as notes to other programmers who read your code.
  7.  
  8. Below you will see two different variables (aBoolean and anInteger), just how algebra has x, y, or a.
  9. We can give variables much more descriptive names though, as we aren't limited to a single letter.
  10.  
  11. One variable (aBoolean) is set to True, and the variable anInteger is set to 42.
  12. The capitalization of True is very key (as is its complement, False)!
  13. Boolean values are _ALWAYS_ either True or False, never a maybe.
  14. Some times you need a "maybe" for programs - for that, we use more versatile variables
  15. such as integers that can be more than 2 values. We would then create a set of control
  16. structures that would define what each value meant. A common way to do this
  17. would be something like saying if the integer value is 1 is true, 0 is false, and -1 is maybe.
  18. Their values are changeable. That means that aBoolean can be set to False or True after it already has one value.
  19. anInteger can take on any integer value.
  20.  
  21. Python, however, is what we call a "weak-typed" language. It means that nearly any variable can actually become a
  22. boolean, an object, or a decimal value. The value you declare the variable with does not impede the values
  23. it can become. Some languages, like Java, are "strong-typed", meaning if the variable starts as
  24. an integer, it can only ever be an integer.
  25.  
  26. You can declare a variable any where by simply putting "theVariableNameYouWant = value"
  27. The single equals sign is used for assigning values.
  28. """
  29.  
  30. aBoolean = True
  31. anInteger = 42
  32.  
  33. """
  34. Next up will we call a built-in function of Python, called print. Just like in math, where you would have
  35. f(x) = y, computer functions work in similar ways - except we can give them much better names than f.
  36. Think of it much like print(x) = output x to the console. So print "Hello World!" puts "Hello World!" on
  37. the console. All non-variables need to be inside of quotes, although more on this later.
  38. """
  39.  
  40. print "Hello World!"
  41.  
  42. """
  43. Next up, we want to print out one of our variables.
  44. You have to surround the variable anInteger with the str() function
  45. because anInteger is not a letter or string of letters. There is a big distinction between
  46. types of variables (aka data types) with computers. Letters are very different than numbers
  47. because at the very lowest level on a computer (binary), the letters are also numbers.
  48. There is a special table (the ASCII table) that converts each character into it's
  49. numerical equivalent. For example, the letter "A" converts to the number 65.
  50. Now if you tried to print out anInteger when its value is 42 without str(), you would end
  51. up with an asterisk (although you would actually get an error! Python doesn't allow you to do that!).
  52. This is because 42 maps to an asterisk on the ASCII table.
  53. That's why we have to specify that we want the variable anInteger to act like a string [of letters]
  54. in this case, and take the _ASCII_ value of whatever anInteger is. The ASCII value of the number 42
  55. is 52 for the 4, and 50 for the 2. Computers process characters sequentially. You can find the
  56. ASCII conversion table at http://www.asciitable.com/ As a side note, there is a more complex
  57. lettering system called Unicode that allows you to use multiple languages with different characters (for
  58. example, an umlaut or stress mark).
  59. """
  60. print "anInteger's value is "+str(anInteger)
  61.  
  62. #This is a single line comment. All single line comments start with a hash
  63. #Let's change anInteger's value
  64. anInteger = 41
  65.  
  66. #You can print out multiple variables on one line by continually putting plusses.
  67. #When you combine two things or more things into one statement, it's called concatenation
  68. print "aBoolean is "+str(aBoolean)+" and anInteger is now "+str(anInteger)
  69.  
  70. #Here's a new variable called someWords that is set to a string.
  71. someWords = "a b c 'a' is actually in the 0th position"
  72.  
  73. #We don't need to use str() here because it's already a string in ASCII
  74. print someWords
  75.  
  76. #You can print out single letters of a string by using the variable name, then appending braces ([])
  77. #with the position of the letter you want to use. Nearly all number sequences start at zero.
  78. print someWords[0]
  79.  
  80. #The code below will print out abc
  81. #We call the position of each letter it's index.
  82. #So a is at index 0, b is at index 2, and c is at index 4.
  83. #index 1 and 3 are spaces!
  84. print someWords[0]+someWords[2]+someWords[4]
  85.  
  86. #Earlier I mentioned that Python is a weak-typed language. This is an example...
  87. #The type(x) function will tell you what data type (boolean, string, integer, etc) the variable is
  88. #Also, just like math, you can have a function of a function, like f(g(x))
  89.  
  90. print "The value of variable aBoolean is "+str(aBoolean)+". What is the type of aBoolean? "+str(type(aBoolean))
  91. aBoolean = "Well now I'm a string!"
  92. print "The value of variable aBoolean is "+str(aBoolean)+". What is the type of aBoolean? "+str(type(aBoolean))
  93.  
  94. """
  95. If you tried to do that in a different language (for example, Java or C) it would give a compiler error and tell
  96. you that you cannot make a boolean into a string. That's an example of a strong-typed language.
  97. There isn't really any _particular_ advantage to either language; some times one is better for the
  98. program you need to write than the other. There are certain gaurantees with strong-typed. Check
  99. http://en.wikipedia.org/wiki/Strongly_typed_programming_language for more information.
  100.  
  101. Alright, now some integer stuff to make your life easier.
  102.  
  103. Order of operations works exactly as you would expect it to in real math. If you surround a
  104. statement with paranthesis, then it will execute first.
  105.  
  106. You can take the opposite of a boolean by putting "not" in front of it.
  107. not True is False, and not False is True.
  108.  
  109. Placing "and" between two or more booleans (or boolean statements) will mean BOTH have to be True to get a True result.
  110. True and True == True
  111. True and False == False
  112. False and False == False
  113. Placing "or" between two or more booleans (or boolean statements) will mean EITHER can be True to get a True result.
  114. True or True == True
  115. True or False == True
  116. False or False == False
  117. not False or False == True (as not False is actually True)
  118. """
  119.  
  120. print ""
  121. print "Is 4 < 5? "+str(4<5)       #True
  122. print "Is 4 < 5 < 6? "+str(4<5<6) #True
  123. print "Is 4 < 6 < 5? "+str(4<6<5) #False
  124. print "6+5*6/3 = "+str(6+5*6/3) #16: 5*6 = 30/3 = 10 + 6 = 16
  125. print "6+5*6/3 < 20? "+str(6+5*6/3 < 20) #True - comparison operators (<, >, ==, !=) evaluate LAST in order of operations
  126. print "6+5*6/3 < 20/2? "+str(6+5*6/3 < 20/2)+"\n" #False - just to double check :)
  127.  
  128. #Be careful with integer division - it DOES NOT work like you expect it to on computers
  129. #you would expect 2.6 repeating...
  130. someDecimal = 8/3
  131.  
  132. #What you get is 2. Integer division will truncate (cut off) all the decimals
  133. #This is because the computer stores decimal data and integers differently, so you have to force
  134. #it to be a decimal.
  135. print "What is someDecimal? "+str(someDecimal)+", type is "+str(type(someDecimal))
  136.  
  137. #Now if you want a decimal, simply make one of the numbers a decimal!
  138. someDecimal = 8/3.0
  139.  
  140. #It's going to say it's type is float (i.e. floating-point number). It just means it's a decimal.
  141. print "What is someDecimal now? "+str(someDecimal)+", type is "+str(type(someDecimal))
  142.  
  143.  
  144. #This is a strange behavior. 4 is a valid value so it evaluates to true in a boolean sense.
  145. #The "not" forces it to act like a boolean
  146. print ""
  147. print "What is not 4? "+str(not 4) #False
  148. print "What is not not 4? "+str(not not 4) #True - the double negative works.
  149. print "What is not 0? "+str(not 0) #True - 0 is considered false in Python. Just a caveat to watch for.
  150.  
  151. #DeMorgan's law is really important to computer scientists!
  152. #This says if you take the negation of a whole statement, like a and b, you get not a or not b.
  153. #The connector statement ALSO switches with the values.
  154. print ""
  155. print "DeMorgan's Law: not a and not b => !(a or b)"
  156. a = True
  157. b = False
  158. print "a = "+str(a)+", b = "+str(b)
  159. print "a and b = "+str(a and b) #False
  160. print "not (a and b) = "+str(not (a and b)) #True
  161. print "not a or not b = "+str(not a or not b) #True
Advertisement
Add Comment
Please, Sign In to add comment