1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-02-07 00:31:56 +01:00

Merge pull request #360 from adambrenecki/python-fixes

[python/en] Clarify setdefault, fix comment syntax typo, change print to print()
This commit is contained in:
Adam Bard 2013-09-26 20:06:45 -07:00
commit be98045c1b

View File

@ -112,8 +112,10 @@ None is None #=> True
## 2. Variables and Collections
####################################################
# Printing is pretty easy
print "I'm Python. Nice to meet you!"
# Python has a print function, available in versions 2.7 and 3...
print("I'm Python. Nice to meet you!")
# and an older print statement, in all 2.x versions but removed from 3.
print "I'm also Python!"
# No need to declare variables before assigning to them.
@ -224,7 +226,7 @@ filled_dict.get("four") #=> None
filled_dict.get("one", 4) #=> 1
filled_dict.get("four", 4) #=> 4
# "setdefault()" method is a safe way to add new key-value pair into dictionary
# "setdefault()" inserts into a dictionary only if the given key isn't present
filled_dict.setdefault("five", 5) #filled_dict["five"] is set to 5
filled_dict.setdefault("five", 6) #filled_dict["five"] is still 5
@ -235,7 +237,7 @@ empty_set = set()
some_set = set([1,2,2,3,4]) # some_set is now set([1, 2, 3, 4])
# Since Python 2.7, {} can be used to declare a set
filled_set = {1, 2, 2, 3, 4} # => {1 2 3 4}
filled_set = {1, 2, 2, 3, 4} # => {1, 2, 3, 4}
# Add more items to a set
filled_set.add(5) # filled_set is now {1, 2, 3, 4, 5}
@ -265,11 +267,11 @@ some_var = 5
# Here is an if statement. Indentation is significant in python!
# prints "some_var is smaller than 10"
if some_var > 10:
print "some_var is totally bigger than 10."
print("some_var is totally bigger than 10.")
elif some_var < 10: # This elif clause is optional.
print "some_var is smaller than 10."
print("some_var is smaller than 10.")
else: # This is optional too.
print "some_var is indeed 10."
print("some_var is indeed 10.")
"""
@ -281,10 +283,10 @@ prints:
"""
for animal in ["dog", "cat", "mouse"]:
# You can use % to interpolate formatted strings
print "%s is a mammal" % animal
print("%s is a mammal" % animal)
"""
"range(number)" returns a list of numbers
"range(number)" returns a list of numbers
from zero to the given number
prints:
0
@ -293,7 +295,7 @@ prints:
3
"""
for i in range(4):
print i
print(i)
"""
While loops go until a condition is no longer met.
@ -305,7 +307,7 @@ prints:
"""
x = 0
while x < 4:
print x
print(x)
x += 1 # Shorthand for x = x + 1
# Handle exceptions with a try/except block
@ -324,7 +326,7 @@ except IndexError as e:
# Use "def" to create new functions
def add(x, y):
print "x is %s and y is %s" % (x, y)
print("x is %s and y is %s" % (x, y))
return x + y # Return values with a return statement
# Calling functions with parameters
@ -351,8 +353,8 @@ keyword_args(big="foot", loch="ness") #=> {"big": "foot", "loch": "ness"}
# You can do both at once, if you like
def all_the_args(*args, **kwargs):
print args
print kwargs
print(args)
print(kwargs)
"""
all_the_args(1, 2, a=3, b=4) prints:
(1, 2)
@ -420,10 +422,10 @@ class Human(object):
# Instantiate a class
i = Human(name="Ian")
print i.say("hi") # prints out "Ian: hi"
print(i.say("hi")) # prints out "Ian: hi"
j = Human("Joel")
print j.say("hello") #prints out "Joel: hello"
print(j.say("hello")) #prints out "Joel: hello"
# Call our class method
i.get_species() #=> "H. sapiens"
@ -443,12 +445,12 @@ Human.grunt() #=> "*grunt*"
# You can import modules
import math
print math.sqrt(16) #=> 4
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 floor(3.7) #=> 3.0
print(ceil(3.7)) #=> 4.0
print(floor(3.7)) #=> 3.0
# You can import all functions from a module.
# Warning: this is not recommended
@ -459,7 +461,7 @@ 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
# can write your own, and import them. The name of the
# module is the same as the name of the file.
# You can find out which functions and attributes