mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-05-06 06:48:32 +00: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:
commit
be98045c1b
@ -112,8 +112,10 @@ None is None #=> True
|
|||||||
## 2. Variables and Collections
|
## 2. Variables and Collections
|
||||||
####################################################
|
####################################################
|
||||||
|
|
||||||
# Printing is pretty easy
|
# Python has a print function, available in versions 2.7 and 3...
|
||||||
print "I'm Python. Nice to meet you!"
|
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.
|
# 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("one", 4) #=> 1
|
||||||
filled_dict.get("four", 4) #=> 4
|
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", 5) #filled_dict["five"] is set to 5
|
||||||
filled_dict.setdefault("five", 6) #filled_dict["five"] is still 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])
|
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
|
# 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
|
# Add more items to a set
|
||||||
filled_set.add(5) # filled_set is now {1, 2, 3, 4, 5}
|
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!
|
# Here is an if statement. Indentation is significant in python!
|
||||||
# prints "some_var is smaller than 10"
|
# prints "some_var is smaller than 10"
|
||||||
if some_var > 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.
|
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.
|
else: # This is optional too.
|
||||||
print "some_var is indeed 10."
|
print("some_var is indeed 10.")
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -281,7 +283,7 @@ prints:
|
|||||||
"""
|
"""
|
||||||
for animal in ["dog", "cat", "mouse"]:
|
for animal in ["dog", "cat", "mouse"]:
|
||||||
# You can use % to interpolate formatted strings
|
# 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
|
||||||
@ -293,7 +295,7 @@ prints:
|
|||||||
3
|
3
|
||||||
"""
|
"""
|
||||||
for i in range(4):
|
for i in range(4):
|
||||||
print i
|
print(i)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
While loops go until a condition is no longer met.
|
While loops go until a condition is no longer met.
|
||||||
@ -305,7 +307,7 @@ prints:
|
|||||||
"""
|
"""
|
||||||
x = 0
|
x = 0
|
||||||
while x < 4:
|
while x < 4:
|
||||||
print x
|
print(x)
|
||||||
x += 1 # Shorthand for x = x + 1
|
x += 1 # Shorthand for x = x + 1
|
||||||
|
|
||||||
# Handle exceptions with a try/except block
|
# Handle exceptions with a try/except block
|
||||||
@ -324,7 +326,7 @@ except IndexError as e:
|
|||||||
|
|
||||||
# Use "def" to create new functions
|
# Use "def" to create new functions
|
||||||
def add(x, y):
|
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
|
return x + y # Return values with a return statement
|
||||||
|
|
||||||
# Calling functions with parameters
|
# 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
|
# You can do both at once, if you like
|
||||||
def all_the_args(*args, **kwargs):
|
def all_the_args(*args, **kwargs):
|
||||||
print args
|
print(args)
|
||||||
print kwargs
|
print(kwargs)
|
||||||
"""
|
"""
|
||||||
all_the_args(1, 2, a=3, b=4) prints:
|
all_the_args(1, 2, a=3, b=4) prints:
|
||||||
(1, 2)
|
(1, 2)
|
||||||
@ -420,10 +422,10 @@ class Human(object):
|
|||||||
|
|
||||||
# Instantiate a class
|
# Instantiate a class
|
||||||
i = Human(name="Ian")
|
i = Human(name="Ian")
|
||||||
print i.say("hi") # prints out "Ian: hi"
|
print(i.say("hi")) # prints out "Ian: hi"
|
||||||
|
|
||||||
j = Human("Joel")
|
j = Human("Joel")
|
||||||
print j.say("hello") #prints out "Joel: hello"
|
print(j.say("hello")) #prints out "Joel: hello"
|
||||||
|
|
||||||
# Call our class method
|
# Call our class method
|
||||||
i.get_species() #=> "H. sapiens"
|
i.get_species() #=> "H. sapiens"
|
||||||
@ -443,12 +445,12 @@ Human.grunt() #=> "*grunt*"
|
|||||||
|
|
||||||
# You can import modules
|
# You can import modules
|
||||||
import math
|
import math
|
||||||
print math.sqrt(16) #=> 4
|
print(math.sqrt(16) )#=> 4
|
||||||
|
|
||||||
# You can get specific functions from a module
|
# You can get specific functions from a module
|
||||||
from math import ceil, floor
|
from math import ceil, floor
|
||||||
print ceil(3.7) #=> 4.0
|
print(ceil(3.7)) #=> 4.0
|
||||||
print floor(3.7) #=> 3.0
|
print(floor(3.7)) #=> 3.0
|
||||||
|
|
||||||
# You can import all functions from a module.
|
# You can import all functions from a module.
|
||||||
# Warning: this is not recommended
|
# Warning: this is not recommended
|
||||||
|
Loading…
Reference in New Issue
Block a user