mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-04-26 23:23:55 +00:00
Merge pull request #1504 from dillonjbyrne/patch-1
[python3/en] Cleaned up formatting and clarified output
This commit is contained in:
commit
ae4e4e81f4
@ -68,15 +68,15 @@ not False # => True
|
||||
|
||||
# Boolean Operators
|
||||
# Note "and" and "or" are case-sensitive
|
||||
True and False #=> False
|
||||
False or True #=> True
|
||||
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
|
||||
0 and 2 # => 0
|
||||
-5 or 0 # => -5
|
||||
0 == False # => True
|
||||
2 == True # => False
|
||||
1 == True # => True
|
||||
|
||||
# Equality is ==
|
||||
1 == 1 # => True
|
||||
@ -119,18 +119,18 @@ b == a # => True, a's and b's objects are equal
|
||||
"This is a string"[0] # => 'T'
|
||||
|
||||
# .format can be used to format strings, like this:
|
||||
"{} can be {}".format("strings", "interpolated")
|
||||
"{} can be {}".format("Strings", "interpolated") # => "Strings can be 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"
|
||||
# => "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"
|
||||
"{name} wants to eat {food}".format(name="Bob", food="lasagna") # => "Bob wants to eat lasagna"
|
||||
|
||||
# If your Python 3 code also needs to run on Python 2.5 and below, you can also
|
||||
# still use the old style of formatting:
|
||||
"%s can be %s the %s way" % ("strings", "interpolated", "old")
|
||||
"%s can be %s the %s way" % ("Strings", "interpolated", "old") # => "Strings can be interpolated the old way"
|
||||
|
||||
|
||||
# None is an object
|
||||
@ -145,8 +145,8 @@ None is None # => True
|
||||
# All other values are True
|
||||
bool(0) # => False
|
||||
bool("") # => False
|
||||
bool([]) #=> False
|
||||
bool({}) #=> False
|
||||
bool([]) # => False
|
||||
bool({}) # => False
|
||||
|
||||
|
||||
####################################################
|
||||
@ -154,7 +154,7 @@ bool({}) #=> False
|
||||
####################################################
|
||||
|
||||
# Python has a print function
|
||||
print("I'm Python. Nice to meet you!")
|
||||
print("I'm Python. Nice to meet you!") # => I'm Python. Nice to meet you!
|
||||
|
||||
# By default the print function also prints out a newline at the end.
|
||||
# Use the optional argument end to change the end character.
|
||||
@ -296,7 +296,7 @@ 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.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
|
||||
@ -435,7 +435,7 @@ with open("myfile.txt") as f:
|
||||
|
||||
filled_dict = {"one": 1, "two": 2, "three": 3}
|
||||
our_iterable = filled_dict.keys()
|
||||
print(our_iterable) #=> range(1,10). This is an object that implements our Iterable interface
|
||||
print(our_iterable) # => range(1,10). This is an object that implements our Iterable interface
|
||||
|
||||
# We can loop over it.
|
||||
for i in our_iterable:
|
||||
@ -449,17 +449,17 @@ our_iterator = iter(our_iterable)
|
||||
|
||||
# Our iterator is an object that can remember the state as we traverse through it.
|
||||
# We get the next object with "next()".
|
||||
next(our_iterator) #=> "one"
|
||||
next(our_iterator) # => "one"
|
||||
|
||||
# It maintains state as we iterate.
|
||||
next(our_iterator) #=> "two"
|
||||
next(our_iterator) #=> "three"
|
||||
next(our_iterator) # => "two"
|
||||
next(our_iterator) # => "three"
|
||||
|
||||
# After the iterator has returned all of its data, it gives you a StopIterator Exception
|
||||
next(our_iterator) # Raises StopIteration
|
||||
|
||||
# You can grab all the elements of an iterator by calling list() on it.
|
||||
list(filled_dict.keys()) #=> Returns ["one", "two", "three"]
|
||||
list(filled_dict.keys()) # => Returns ["one", "two", "three"]
|
||||
|
||||
|
||||
####################################################
|
||||
|
Loading…
Reference in New Issue
Block a user