diff options
-rw-r--r-- | clojure.html.markdown | 2 | ||||
-rw-r--r-- | java.html.markdown | 19 | ||||
-rw-r--r-- | python.html.markdown | 71 |
3 files changed, 82 insertions, 10 deletions
diff --git a/clojure.html.markdown b/clojure.html.markdown index 12611fd3..cb202a92 100644 --- a/clojure.html.markdown +++ b/clojure.html.markdown @@ -5,7 +5,7 @@ author_url: http://adambard.com/ filename: test.clj --- -Clojure is a variant of LISP developed for the Java Virtual Machine. It has +Clojure is a Lisp family language developed for the Java Virtual Machine. It has a much stronger emphasis on pure [functional programming](https://en.wikipedia.org/wiki/Functional_programming) than Common Lisp, but includes several [STM](https://en.wikipedia.org/wiki/Software_transactional_memory) utilities to handle state as it comes up. diff --git a/java.html.markdown b/java.html.markdown index 3208971d..3339b6d1 100644 --- a/java.html.markdown +++ b/java.html.markdown @@ -19,10 +19,10 @@ Java is a general-purpose, concurrent, class-based, object-oriented computer pro Multi-line comments look like this. */ -// Import Packages +// Import ArrayList class inside of the java.util package import java.util.ArrayList; -// Import all "sub-packages" -import java.lang.Math.*; +// Import all classes inside of java.lang package +import java.lang.*; // Inside of the learnjava class, is your program's // starting point. The main method. @@ -93,6 +93,9 @@ int [] intArray = new int[10]; String [] stringArray = new String[1]; boolean [] booleanArray = new boolean[100]; +// Another way to declare & initialize an array +int [] y = {9000, 1000, 1337}; + // Indexing an array - Accessing an element System.out.println("intArray @ 0: "+intArray[0]); @@ -118,9 +121,9 @@ int i1 = 1, i2 = 2; // Shorthand for multiple declarations // Arithmetic is straightforward System.out.println("1+2 = "+(i1 + i2)); // => 3 -System.out.println("1+2 = "+(i2 - i1)); // => 1 -System.out.println("1+2 = "+(i2 * i1)); // => 2 -System.out.println("1+2 = "+(i1 / i2)); // => 0 (0.5, but truncated towards 0) +System.out.println("2-1 = "+(i2 - i1)); // => 1 +System.out.println("2*1 = "+(i2 * i1)); // => 2 +System.out.println("1/2 = "+(i1 / i2)); // => 0 (0.5, but truncated towards 0) // Modulo System.out.println("11%3 = "+(11 % 3)); // => 2 @@ -245,7 +248,7 @@ Integer.toString(123);//returns a string version of 123 // Long // String -// Typecsating +// Typecasting // You can also cast java objects, there's a lot of details and // deals with some more intermediate concepts. // Feel free to check it out here: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html @@ -346,4 +349,6 @@ Other Topics To Research: * [Generics](http://docs.oracle.com/javase/tutorial/java/generics/index.html) +* [Java Code Conventions](http://www.oracle.com/technetwork/java/codeconv-138413.html) + * The links provided are just to get an understanding of the topic, feel free to google and find specific examples diff --git a/python.html.markdown b/python.html.markdown index 19e2aebe..e3f04e19 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -93,6 +93,20 @@ not False #=> True # None is an object None #=> None +# Don't use the equality `==` symbol to compare objects to None +# Use `is` instead +"etc" is None #=> False +None is None #=> True + +# The 'is' operator tests for object identity. This isn't +# very useful when dealing with primitive values, but is +# very useful when dealing with objects. + +# None, 0, and empty strings/lists all evaluate to False. +# All other values are True +0 == False #=> True +"" == False #=> True + #################################################### ## 2. Variables and Collections @@ -159,6 +173,9 @@ li.extend(other_li) # Now li is [1, 2, 3, 4, 5, 6] # Examine the length with len len(li) #=> 6 +# Note: lists can contain arbitrary values +li2 = [1, "Hello", [[], "Hi", 5,]] + # Tuples are like lists but are immutable. tup = (1, 2, 3) tup[0] #=> 1 @@ -170,7 +187,7 @@ tup + (4, 5, 6) #=> (1, 2, 3, 4, 5, 6) tup[:2] #=> (1, 2) 2 in tup #=> True -# You can unpack tuples into variables +# You can unpack tuples (or lists) into variables a, b, c = (1, 2, 3) # a is now 1, b is now 2 and c is now 3 # Tuples are created by default if you leave out the parentheses d, e, f = 4, 5, 6 @@ -267,6 +284,18 @@ prints: for animal in ["dog", "cat", "mouse"]: # You can use % to interpolate formatted strings print "%s is a mammal" % animal + +""" +`range(number)` returns a list of numbers +from zero to the given number +prints: + 0 + 1 + 2 + 3 +""" +for i in range(4): + print i """ While loops go until a condition is no longer met. @@ -350,6 +379,8 @@ add_10(3) #=> 13 # There are also anonymous functions (lambda x: x > 2)(3) #=> True +rectangle_area = lambda a, b: a * b +print rectangle_area(3, 4) #=> 12 # There are built-in higher order functions map(add_10, [1,2,3]) #=> [11, 12, 13] @@ -359,6 +390,9 @@ filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7] [add_10(i) for i in [1, 2, 3]] #=> [11, 12, 13] [x for x in [3, 4, 5, 6, 7] if x > 5] #=> [6, 7] +# You can also use dictionary comprehensions +{i: add_10(i) for i in [1, 2, 3]} #=> {1: 11, 2: 12, 3: 13} + #################################################### ## 5. Classes #################################################### @@ -374,7 +408,8 @@ class Human(object): # Assign the argument to the instance's name attribute self.name = name - # An instance method. All methods take self as the first argument + # An instance method. All methods take self as the first argument, + # which refers to the instance of this class def say(self, msg): return "%s: %s" % (self.name, msg) @@ -407,6 +442,34 @@ j.get_species() #=> "H. neanderthalensis" # Call the static method Human.grunt() #=> "*grunt*" + + +#################################################### +## 6. Modules +#################################################### + +# You can import modules +import math +print math.sqrt(16) #=> 4 + +# You can get specific functions from a module +from math import ceil, floor +print ceil(3.7) #=> 4.0 +print floor3.7) #=> 3.0 + +# You can import all functions from a module. +# Warning: this is not recommended +from math import * + +# You can shorten module names +import math as m +math.sqrt(16) == m.sqrt(16) #=> True + +# Python modules are just ordinary python files. You +# can write your own, and import them. The name of the +# module is the same as the name of the file. + + ``` ## Further Reading @@ -417,3 +480,7 @@ Still up for more? Try: * [Dive Into Python](http://www.diveintopython.net/) * [The Official Docs](http://docs.python.org/2.6/) * [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/) + +Python has a huge amount of modules within the standard library. See the +[official documentation](http://docs.python.org/2/library/index.html) or +[Python Module of the Week](http://pymotw.com/2/) for more. |