Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Welcome to Python! Anything inside of three quotation marks is multiline comment.
- Comments are NOT evaluated by Python when the program runs. Usually they contain
- notes or details about your program in regular language - like explaining
- that an algorithm sorts data a particular way or something. They also function
- as notes to other programmers who read your code.
- Below you will see two different variables (aBoolean and anInteger), just how algebra has x, y, or a.
- We can give variables much more descriptive names though, as we aren't limited to a single letter.
- One variable (aBoolean) is set to True, and the variable anInteger is set to 42.
- The capitalization of True is very key (as is its complement, False)!
- Boolean values are _ALWAYS_ either True or False, never a maybe.
- Some times you need a "maybe" for programs - for that, we use more versatile variables
- such as integers that can be more than 2 values. We would then create a set of control
- structures that would define what each value meant. A common way to do this
- would be something like saying if the integer value is 1 is true, 0 is false, and -1 is maybe.
- Their values are changeable. That means that aBoolean can be set to False or True after it already has one value.
- anInteger can take on any integer value.
- Python, however, is what we call a "weak-typed" language. It means that nearly any variable can actually become a
- boolean, an object, or a decimal value. The value you declare the variable with does not impede the values
- it can become. Some languages, like Java, are "strong-typed", meaning if the variable starts as
- an integer, it can only ever be an integer.
- You can declare a variable any where by simply putting "theVariableNameYouWant = value"
- The single equals sign is used for assigning values.
- """
- aBoolean = True
- anInteger = 42
- """
- Next up will we call a built-in function of Python, called print. Just like in math, where you would have
- f(x) = y, computer functions work in similar ways - except we can give them much better names than f.
- Think of it much like print(x) = output x to the console. So print "Hello World!" puts "Hello World!" on
- the console. All non-variables need to be inside of quotes, although more on this later.
- """
- print "Hello World!"
- """
- Next up, we want to print out one of our variables.
- You have to surround the variable anInteger with the str() function
- because anInteger is not a letter or string of letters. There is a big distinction between
- types of variables (aka data types) with computers. Letters are very different than numbers
- because at the very lowest level on a computer (binary), the letters are also numbers.
- There is a special table (the ASCII table) that converts each character into it's
- numerical equivalent. For example, the letter "A" converts to the number 65.
- Now if you tried to print out anInteger when its value is 42 without str(), you would end
- up with an asterisk (although you would actually get an error! Python doesn't allow you to do that!).
- This is because 42 maps to an asterisk on the ASCII table.
- That's why we have to specify that we want the variable anInteger to act like a string [of letters]
- in this case, and take the _ASCII_ value of whatever anInteger is. The ASCII value of the number 42
- is 52 for the 4, and 50 for the 2. Computers process characters sequentially. You can find the
- ASCII conversion table at http://www.asciitable.com/ As a side note, there is a more complex
- lettering system called Unicode that allows you to use multiple languages with different characters (for
- example, an umlaut or stress mark).
- """
- print "anInteger's value is "+str(anInteger)
- #This is a single line comment. All single line comments start with a hash
- #Let's change anInteger's value
- anInteger = 41
- #You can print out multiple variables on one line by continually putting plusses.
- #When you combine two things or more things into one statement, it's called concatenation
- print "aBoolean is "+str(aBoolean)+" and anInteger is now "+str(anInteger)
- #Here's a new variable called someWords that is set to a string.
- someWords = "a b c 'a' is actually in the 0th position"
- #We don't need to use str() here because it's already a string in ASCII
- print someWords
- #You can print out single letters of a string by using the variable name, then appending braces ([])
- #with the position of the letter you want to use. Nearly all number sequences start at zero.
- print someWords[0]
- #The code below will print out abc
- #We call the position of each letter it's index.
- #So a is at index 0, b is at index 2, and c is at index 4.
- #index 1 and 3 are spaces!
- print someWords[0]+someWords[2]+someWords[4]
- #Earlier I mentioned that Python is a weak-typed language. This is an example...
- #The type(x) function will tell you what data type (boolean, string, integer, etc) the variable is
- #Also, just like math, you can have a function of a function, like f(g(x))
- print "The value of variable aBoolean is "+str(aBoolean)+". What is the type of aBoolean? "+str(type(aBoolean))
- aBoolean = "Well now I'm a string!"
- print "The value of variable aBoolean is "+str(aBoolean)+". What is the type of aBoolean? "+str(type(aBoolean))
- """
- If you tried to do that in a different language (for example, Java or C) it would give a compiler error and tell
- you that you cannot make a boolean into a string. That's an example of a strong-typed language.
- There isn't really any _particular_ advantage to either language; some times one is better for the
- program you need to write than the other. There are certain gaurantees with strong-typed. Check
- http://en.wikipedia.org/wiki/Strongly_typed_programming_language for more information.
- Alright, now some integer stuff to make your life easier.
- Order of operations works exactly as you would expect it to in real math. If you surround a
- statement with paranthesis, then it will execute first.
- You can take the opposite of a boolean by putting "not" in front of it.
- not True is False, and not False is True.
- Placing "and" between two or more booleans (or boolean statements) will mean BOTH have to be True to get a True result.
- True and True == True
- True and False == False
- False and False == False
- Placing "or" between two or more booleans (or boolean statements) will mean EITHER can be True to get a True result.
- True or True == True
- True or False == True
- False or False == False
- not False or False == True (as not False is actually True)
- """
- print ""
- print "Is 4 < 5? "+str(4<5) #True
- print "Is 4 < 5 < 6? "+str(4<5<6) #True
- print "Is 4 < 6 < 5? "+str(4<6<5) #False
- print "6+5*6/3 = "+str(6+5*6/3) #16: 5*6 = 30/3 = 10 + 6 = 16
- print "6+5*6/3 < 20? "+str(6+5*6/3 < 20) #True - comparison operators (<, >, ==, !=) evaluate LAST in order of operations
- print "6+5*6/3 < 20/2? "+str(6+5*6/3 < 20/2)+"\n" #False - just to double check :)
- #Be careful with integer division - it DOES NOT work like you expect it to on computers
- #you would expect 2.6 repeating...
- someDecimal = 8/3
- #What you get is 2. Integer division will truncate (cut off) all the decimals
- #This is because the computer stores decimal data and integers differently, so you have to force
- #it to be a decimal.
- print "What is someDecimal? "+str(someDecimal)+", type is "+str(type(someDecimal))
- #Now if you want a decimal, simply make one of the numbers a decimal!
- someDecimal = 8/3.0
- #It's going to say it's type is float (i.e. floating-point number). It just means it's a decimal.
- print "What is someDecimal now? "+str(someDecimal)+", type is "+str(type(someDecimal))
- #This is a strange behavior. 4 is a valid value so it evaluates to true in a boolean sense.
- #The "not" forces it to act like a boolean
- print ""
- print "What is not 4? "+str(not 4) #False
- print "What is not not 4? "+str(not not 4) #True - the double negative works.
- print "What is not 0? "+str(not 0) #True - 0 is considered false in Python. Just a caveat to watch for.
- #DeMorgan's law is really important to computer scientists!
- #This says if you take the negation of a whole statement, like a and b, you get not a or not b.
- #The connector statement ALSO switches with the values.
- print ""
- print "DeMorgan's Law: not a and not b => !(a or b)"
- a = True
- b = False
- print "a = "+str(a)+", b = "+str(b)
- print "a and b = "+str(a and b) #False
- print "not (a and b) = "+str(not (a and b)) #True
- print "not a or not b = "+str(not a or not b) #True
Advertisement
Add Comment
Please, Sign In to add comment