diff options
-rw-r--r-- | bash.html.markdown | 12 | ||||
-rw-r--r-- | java.html.markdown | 2 | ||||
-rw-r--r-- | perl6.html.markdown | 10 | ||||
-rw-r--r-- | python.html.markdown | 27 | ||||
-rw-r--r-- | python3.html.markdown | 26 |
5 files changed, 51 insertions, 26 deletions
diff --git a/bash.html.markdown b/bash.html.markdown index 15d1c068..845ebead 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -7,6 +7,7 @@ contributors: - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"] - ["Denis Arh", "https://github.com/darh"] - ["akirahirose", "https://twitter.com/akirahirose"] + - ["Anton Strömkvist", "http://lutic.org/"] filename: LearnBash.sh --- @@ -81,6 +82,17 @@ fi echo "Always executed" || echo "Only executed if first command fails" echo "Always executed" && echo "Only executed if first command does NOT fail" +# To use && and || with if statements, you need multiple pairs of square brackets: +if [ $NAME == "Steve" ] && [ $AGE -eq 15 ] +then + echo "This will run if $NAME is Steve AND $AGE is 15." +fi + +if [ $NAME == "Daniya" ] || [ $NAME == "Zach" ] +then + echo "This will run if $NAME is Daniya OR Zach." +fi + # Expressions are denoted with the following format: echo $(( 10 + 5 )) diff --git a/java.html.markdown b/java.html.markdown index 50875491..3484aee5 100644 --- a/java.html.markdown +++ b/java.html.markdown @@ -103,7 +103,7 @@ public class LearnJava { //The array size must be decided upon instantiation //The following formats work for declaring an arrow //<datatype> [] <var name> = new <datatype>[<array size>]; - //<datetype> <var name>[] = new <datatype>[<array size>]; + //<datatype> <var name>[] = new <datatype>[<array size>]; int [] intArray = new int[10]; String [] stringArray = new String[1]; boolean boolArray [] = new boolean[100]; diff --git a/perl6.html.markdown b/perl6.html.markdown index 567c4629..92219708 100644 --- a/perl6.html.markdown +++ b/perl6.html.markdown @@ -92,7 +92,7 @@ sub as-many($head, *@rest) { # the `*@` slurpy will basically "take everything e # but not *after*. say @rest.join(' / ') ~ " !"; } -say as-many('Happy', 'Happy', 'Birthday'); #=> Happy Birthday ! +say as-many('Happy', 'Happy', 'Birthday'); #=> Happy / Birthday ! # Note that the splat did not consume the parameter before. ## You can call a function with an array using the "argument list flattening" operator `|` @@ -127,7 +127,7 @@ sub with-named($normal-arg, :$named) { } with-named(1, named => 6); #=> 7 # There's one gotcha to be aware of, here: -# If you quote your key, Perl 6 won't be able to see it as compile time, +# If you quote your key, Perl 6 won't be able to see it at compile time, # and you'll have a single Pair object as a positional paramater. with-named(2, :named(5)); #=> 7 @@ -171,9 +171,9 @@ named-def(def => 15); #=> 15 ### Containers # In Perl 6, values are actually stored in "containers". -# the assignment operator asks the container on the left to store the value on its right +# The assignment operator asks the container on the left to store the value on its right. # When passed around, containers are marked as immutable. Which means that, in a function, -# you'll get an error if you try to mutate one of your argument. +# you'll get an error if you try to mutate one of your arguments. # If you really need to, you can ask for a mutable container using `is rw` : sub mutate($n is rw) { $n++; @@ -374,7 +374,7 @@ sub foo(@array [$fst, $snd]) { say "My first is $fst, my second is $snd ! All in all, I'm @array[]."; # (^ remember the `[]` to interpolate the array) } -foo(@tail); #=> My first is 2, my second is 3 ! All in all, I'm 1 2 +foo(@tail); #=> My first is 2, my second is 3 ! All in all, I'm 2 3 # If you're not using the array itself, you can also keep it anonymous, much like a scalar: diff --git a/python.html.markdown b/python.html.markdown index 5bec5190..9057dde2 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -45,9 +45,11 @@ to Python 2.x. Look for another tour of Python 3 soon! 2.0 # This is a float 11.0 / 4.0 # => 2.75 ahhh...much better -# Truncation or Integer division +# Result of integer division truncated down both for positive and negative. 5 // 3 # => 1 5.0 // 3.0 # => 1.0 # works on floats too +-5 // 3 # => -2 +-5.0 // 3.0 # => -2.0 # Modulo operation 7 % 3 # => 1 @@ -439,7 +441,10 @@ class Human(object): # A class attribute. It is shared by all instances of this class species = "H. sapiens" - # Basic initializer + # Basic initializer, this is called when this class is instantiated. + # Note that the double leading and trailing underscores denote objects + # or attributes that are used by python but that live in user-controlled + # namespaces. You should not invent such names on your own. def __init__(self, name): # Assign the argument to the instance's name attribute self.name = name @@ -523,11 +528,15 @@ def double_numbers(iterable): # Instead of generating and returning all values at once it creates one in each # iteration. This means values bigger than 15 wont be processed in # double_numbers. -# Note range is a generator too. Creating a list 1-900000000 would take lot of -# time to be made -_range = range(1, 900000000) +# Note xrange is a generator that does the same thing range does. +# Creating a list 1-900000000 would take lot of time and space to be made. +# xrange creates an xrange generator object instead of creating the entire list like range does. +# We use a trailing underscore in variable names when we want to use a name that +# would normally collide with a python keyword +xrange_ = xrange(1, 900000000) + # will double all numbers until a result >=30 found -for i in double_numbers(_range): +for i in double_numbers(xrange_): print(i) if i >= 30: break @@ -540,10 +549,10 @@ for i in double_numbers(_range): from functools import wraps -def beg(_say): - @wraps(_say) +def beg(target_function): + @wraps(target_function) def wrapper(*args, **kwargs): - msg, say_please = _say(*args, **kwargs) + msg, say_please = target_function(*args, **kwargs) if say_please: return "{} {}".format(msg, "Please! I am poor :(") return msg diff --git a/python3.html.markdown b/python3.html.markdown index 531d3b5a..b494dc1e 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -38,9 +38,11 @@ Note: This article applies to Python 3 specifically. Check out the other tutoria # Except division which returns floats by default 35 / 5 # => 7.0 -# Truncation or Integer division +# Result of integer division truncated down both for positive and negative. 5 // 3 # => 1 -5.0 // 3.0 # => 1.0 +5.0 // 3.0 # => 1.0 # works on floats too +-5 // 3 # => -2 +-5.0 // 3.0 # => -2.0 # When you use a float, results are floats 3 * 2.0 # => 6.0 @@ -51,7 +53,6 @@ Note: This article applies to Python 3 specifically. Check out the other tutoria # Enforce precedence with parentheses (1 + 3) * 2 # => 8 - # Boolean values are primitives True False @@ -60,7 +61,6 @@ False not True # => False not False # => True - # Equality is == 1 == 1 # => True 2 == 1 # => False @@ -79,7 +79,6 @@ not False # => True 1 < 2 < 3 # => True 2 < 3 < 2 # => False - # Strings are created with " or ' "This is a string." 'This is also a string.' @@ -471,7 +470,10 @@ class Human(object): # A class attribute. It is shared by all instances of this class species = "H. sapiens" - # Basic initializer + # Basic initializer, this is called when this class is instantiated. + # Note that the double leading and trailing underscores denote objects + # or attributes that are used by python but that live in user-controlled + # namespaces. You should not invent such names on your own. def __init__(self, name): # Assign the argument to the instance's name attribute self.name = name @@ -557,9 +559,11 @@ def double_numbers(iterable): # double_numbers. # Note range is a generator too. Creating a list 1-900000000 would take lot of # time to be made -_range = range(1, 900000000) +# We use a trailing underscore in variable names when we want to use a name that +# would normally collide with a python keyword +range_ = range(1, 900000000) # will double all numbers until a result >=30 found -for i in double_numbers(_range): +for i in double_numbers(range_): print(i) if i >= 30: break @@ -572,10 +576,10 @@ for i in double_numbers(_range): from functools import wraps -def beg(_say): - @wraps(_say) +def beg(target_function): + @wraps(target_function) def wrapper(*args, **kwargs): - msg, say_please = _say(*args, **kwargs) + msg, say_please = target_function(*args, **kwargs) if say_please: return "{} {}".format(msg, "Please! I am poor :(") return msg |