diff options
Diffstat (limited to 'python3.html.markdown')
| -rw-r--r-- | python3.html.markdown | 91 | 
1 files changed, 66 insertions, 25 deletions
| diff --git a/python3.html.markdown b/python3.html.markdown index b494dc1e..a112912f 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -3,23 +3,24 @@ language: python3  contributors:      - ["Louie Dinh", "http://pythonpracticeprojects.com"]      - ["Steven Basart", "http://github.com/xksteven"] +    - ["Andre Polykanine", "https://github.com/Oire"]  filename: learnpython3.py  --- -Python was created by Guido Van Rossum in the early 90's. It is now one of the most popular +Python was created by Guido Van Rossum in the early 90s. It is now one of the most popular  languages in existence. I fell in love with Python for its syntactic clarity. It's basically  executable pseudocode.  Feedback would be highly appreciated! You can reach me at [@louiedinh](http://twitter.com/louiedinh) or louiedinh [at] [google's email service] -Note: This article applies to Python 3 specifically. Check out the other tutorial if you want to learn the old Python 2.7 +Note: This article applies to Python 3 specifically. Check out [here](http://learnxinyminutes.com/docs/python/) if you want to learn the old Python 2.7  ```python  # Single line comments start with a number symbol.  """ Multiline strings can be written -    using three "'s, and are often used +    using three "s, and are often used      as comments  """ @@ -38,7 +39,7 @@ Note: This article applies to Python 3 specifically. Check out the other tutoria  # Except division which returns floats by default  35 / 5  # => 7.0 -# Result of integer division truncated down both for positive and negative.  +# 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 @@ -50,6 +51,9 @@ Note: This article applies to Python 3 specifically. Check out the other tutoria  # Modulo operation  7 % 3 # => 1 +# Exponentiation (x to the yth power) +2**4 # => 16 +  # Enforce precedence with parentheses  (1 + 3) * 2  # => 8 @@ -61,6 +65,18 @@ False  not True  # => False  not False  # => True +# Boolean Operators +# Note "and" and "or" are case-sensitive +True and False #=> False +False or True #=> True + +# Note using Bool operators with ints +0 and 2 #=> 0 +-5 or 0 #=> -5 +0 == False #=> True +2 == True #=> False +1 == True #=> True +  # Equality is ==  1 == 1  # => True  2 == 1  # => False @@ -85,6 +101,8 @@ not False  # => True  # Strings can be added too! But try not to do this.  "Hello " + "world!"  # => "Hello world!" +# Strings can be added without using '+' +"Hello " "world!"  # => "Hello world!"  # A string can be treated like a list of characters  "This is a string"[0]  # => 'T' @@ -93,7 +111,9 @@ not False  # => True  "{} can be {}".format("strings", "interpolated")  # You can repeat the formatting arguments to save some typing. -"{0} be nimble, {0} be quick, {0} jump over the {1}".format("Jack", "candle stick") #=> "Jack be nimble, Jack be quick, Jack jump over the candle stick" +"{0} be nimble, {0} be quick, {0} jump over the {1}".format("Jack", "candle stick") +#=> "Jack be nimble, Jack be quick, Jack jump over the candle stick" +  # You can use keywords if you don't want to count.  "{name} wants to eat {food}".format(name="Bob", food="lasagna") #=> "Bob wants to eat lasagna" @@ -125,7 +145,8 @@ bool({}) #=> False  # Python has a print function  print("I'm Python. Nice to meet you!") -# No need to declare variables before assigning to them. Convention is to use lower_case_with_underscores +# No need to declare variables before assigning to them. +# Convention is to use lower_case_with_underscores  some_var = 5  some_var  # => 5 @@ -165,7 +186,7 @@ li[2:]  # => [4, 3]  li[:3]  # => [1, 2, 4]  # Select every second entry  li[::2]   # =>[1, 4] -# Revert the list +# Return a reversed copy of the list  li[::-1]   # => [3, 4, 2, 1]  # Use any combination of these to make advanced slices  # li[start:end:step] @@ -174,7 +195,8 @@ li[::-1]   # => [3, 4, 2, 1]  del li[2]   # li is now [1, 2, 3]  # You can add lists -li + other_li   # => [1, 2, 3, 4, 5, 6] - Note: values for li and for other_li are not modified. +# Note: values for li and for other_li are not modified. +li + other_li   # => [1, 2, 3, 4, 5, 6]  # Concatenate lists with "extend()"  li.extend(other_li)   # Now li is [1, 2, 3, 4, 5, 6] @@ -191,7 +213,7 @@ tup = (1, 2, 3)  tup[0]   # => 1  tup[0] = 3  # Raises a TypeError -# You can do all those list thingies on tuples too +# You can do most of the list operations on tuples too  len(tup)   # => 3  tup + (4, 5, 6)   # => (1, 2, 3, 4, 5, 6)  tup[:2]   # => (1, 2) @@ -213,14 +235,17 @@ filled_dict = {"one": 1, "two": 2, "three": 3}  # Look up values with []  filled_dict["one"]   # => 1 -# Get all keys as a list with "keys()". We need to wrap the call in list() because we are getting back an iterable. We'll talk about those later. +# Get all keys as an iterable with "keys()". We need to wrap the call in list() +# to turn it into a list. We'll talk about those later.  Note - Dictionary key +# ordering is not guaranteed. Your results might not match this exactly.  list(filled_dict.keys())   # => ["three", "two", "one"] -# Note - Dictionary key ordering is not guaranteed. -# Your results might not match this exactly. -# Get all values as a list with "values()". Once again we need to wrap it in list() to get it out of the iterable. + +# Get all values as an iterable with "values()". Once again we need to wrap it +# in list() to get it out of the iterable. Note - Same as above regarding key +# ordering.  list(filled_dict.values())   # => [3, 2, 1] -# Note - Same as above regarding key ordering. +  # Check for existence of keys in a dictionary with "in"  "one" in filled_dict   # => True @@ -240,6 +265,10 @@ filled_dict.get("four", 4)   # => 4  filled_dict.setdefault("five", 5)  # filled_dict["five"] is set to 5  filled_dict.setdefault("five", 6)  # filled_dict["five"] is still 5 +# Adding to a dictionary +filled_dict.update({"four":4}) #=> {"one": 1, "two": 2, "three": 3, "four": 4} +#filled_dict["four"] = 4  #another way to add to dict +  # Remove keys from a dictionary with del  del filled_dict["one"]  # Removes the key "one" from filled dict @@ -249,10 +278,10 @@ empty_set = set()  # Initialize a set with a bunch of values. Yeah, it looks a bit like a dict. Sorry.  some_set = {1, 1, 2, 2, 3, 4}   # some_set is now {1, 2, 3, 4} -#Can set new variables to a set +# Can set new variables to a set  filled_set = some_set -# Add one more item to the set  +# Add one more item to the set  filled_set.add(5)   # filled_set is now {1, 2, 3, 4, 5}  # Do set intersection with & @@ -299,7 +328,7 @@ for animal in ["dog", "cat", "mouse"]:      print("{} is a mammal".format(animal))  """ -"range(number)" returns a list of numbers +"range(number)" returns an iterable of numbers  from zero to the given number  prints:      0 @@ -311,6 +340,18 @@ for i in range(4):      print(i)  """ +"range(lower, upper)" returns an iterable of numbers +from the lower number to the upper number +prints: +    4 +    5 +    6 +    7 +""" +for i in range(4, 8): +    print(i) + +"""  While loops go until a condition is no longer met.  prints:      0 @@ -367,7 +408,6 @@ our_iterator.__next__() # Raises StopIteration  list(filled_dict.keys())  #=> Returns ["one", "two", "three"] -  ####################################################  ## 4. Functions  #################################################### @@ -383,7 +423,6 @@ add(5, 6)   # => prints out "x is 5 and y is 6" and returns 11  # Another way to call functions is with keyword arguments  add(y=6, x=5)   # Keyword arguments can arrive in any order. -  # You can define functions that take a variable number of  # positional arguments  def varargs(*args): @@ -391,7 +430,6 @@ def varargs(*args):  varargs(1, 2, 3)   # => (1, 2, 3) -  # You can define functions that take a variable number of  # keyword arguments, as well  def keyword_args(**kwargs): @@ -420,14 +458,14 @@ all_the_args(**kwargs)   # equivalent to foo(a=3, b=4)  all_the_args(*args, **kwargs)   # equivalent to foo(1, 2, 3, 4, a=3, b=4) -# Function Scope                                                                 +# Function Scope  x = 5  def setX(num):      # Local var x not the same as global variable x      x = num # => 43      print (x) # => 43 -     +  def setGlobalX(num):      global x      print (x) # => 5 @@ -456,6 +494,7 @@ map(add_10, [1, 2, 3])   # => [11, 12, 13]  filter(lambda x: x > 5, [3, 4, 5, 6, 7])   # => [6, 7]  # We can use list comprehensions for nice maps and filters +# List comprehension stores the output as a list which can itself be a nested list  [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] @@ -473,7 +512,9 @@ class Human(object):      # 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. +    # namespaces. Methods(or objects or attributes) like: __init__, __str__, +    # __repr__ etc. are called magic methods (or sometimes called dunder methods) +    # 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 @@ -559,7 +600,7 @@ 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 -# We use a trailing underscore in variable names when we want to use a name that  +# 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 @@ -607,8 +648,8 @@ print(say(say_please=True))  # Can you buy me a beer? Please! I am poor :(  * [The Official Docs](http://docs.python.org/3/)  * [Hitchhiker's Guide to Python](http://docs.python-guide.org/en/latest/) -* [Python Module of the Week](http://pymotw.com/3/)  * [A Crash Course in Python for Scientists](http://nbviewer.ipython.org/5920182) +* [Python Course](http://www.python-course.eu/index.php)  ### Dead Tree | 
