mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
Merge pull request #4 from lodin/master
Added floats and slices. FIlled out the tuples section a bit.
This commit is contained in:
commit
630cdcda41
@ -34,6 +34,10 @@ to Python 2.x. Look for another tour of Python 3 soon!
|
|||||||
# automatically.
|
# automatically.
|
||||||
11 / 4 #=> 2
|
11 / 4 #=> 2
|
||||||
|
|
||||||
|
# To fix division we need to learn about floats.
|
||||||
|
2.0 # This is a float
|
||||||
|
5.0 / 2.0 #=> 2.5 ahhh...much better
|
||||||
|
|
||||||
# Enforce precedence with parentheses
|
# Enforce precedence with parentheses
|
||||||
(1 + 3) * 2 #=> 8
|
(1 + 3) * 2 #=> 8
|
||||||
|
|
||||||
@ -90,13 +94,26 @@ li.append(1) #li is now [1]
|
|||||||
li.append(2) #li is now [1, 2]
|
li.append(2) #li is now [1, 2]
|
||||||
li.append(4) #li is now [1, 2, 4]
|
li.append(4) #li is now [1, 2, 4]
|
||||||
li.append(3) #li is now [1, 2, 4, 3]
|
li.append(3) #li is now [1, 2, 4, 3]
|
||||||
|
# Remove from the end with pop
|
||||||
|
li.pop() #=> 3 and li is now [1, 2, 4]
|
||||||
|
# Let's put it back
|
||||||
|
li.append(3) # li is now [1, 2, 4, 3] again.
|
||||||
|
|
||||||
# Access a list like you would any array
|
# Access a list like you would any array
|
||||||
li[0] #=> 1
|
li[0] #=> 1
|
||||||
|
# Look at the last element
|
||||||
|
li[-1] #=> 4
|
||||||
# Looking out of bounds is an IndexError
|
# Looking out of bounds is an IndexError
|
||||||
li[4] # Raises an IndexError
|
li[4] # Raises an IndexError
|
||||||
|
|
||||||
# Remove elements from a list with del
|
# You can look at ranges with slice syntax. It's an closed/open range for you mathy types.
|
||||||
|
li[1:3] #=> [2, 4]
|
||||||
|
# Omit the beginning
|
||||||
|
li[:3] #=> [1, 2, 4]
|
||||||
|
# Omit the end
|
||||||
|
li[2:] #=> [4, 3]
|
||||||
|
|
||||||
|
# Remove arbitrary elements from a list with del
|
||||||
del li[2] # li is now [1, 2, 3]
|
del li[2] # li is now [1, 2, 3]
|
||||||
|
|
||||||
# You can add lists
|
# You can add lists
|
||||||
@ -111,11 +128,24 @@ li.extend(other_li) # Now li is [1, 2, 3, 4, 5, 6]
|
|||||||
# Examine the length with len
|
# Examine the length with len
|
||||||
len(li) #=> 6
|
len(li) #=> 6
|
||||||
|
|
||||||
# Tuples are like lists but are immutable
|
# Tuples are like lists but are immutable.
|
||||||
tup = (1, 2, 3)
|
tup = (1, 2, 3)
|
||||||
tup[0] #=> 1
|
tup[0] #=> 1
|
||||||
tup[0] = 3 # Raises a TypeError
|
tup[0] = 3 # Raises a TypeError
|
||||||
|
|
||||||
|
# You can do all those list thingies on tuples too
|
||||||
|
len(tup) #=> 3
|
||||||
|
tup + (4, 5, 6) #=> (1, 2, 3, 4, 5, 6)
|
||||||
|
tup[:2] #=> (1, 2)
|
||||||
|
2 in tup #=> True
|
||||||
|
|
||||||
|
# However, you can unpack tuples into variables
|
||||||
|
a, b, c = (1, 2, 3) # a is now 1, b is now 2 and c is now 3
|
||||||
|
# Tuples are created by default if you leave out the parentheses
|
||||||
|
d, e, f = 4, 5, 6
|
||||||
|
# Now look how easy it is to swap to values
|
||||||
|
e, d = d, e # d is now 5 and e is now 4
|
||||||
|
|
||||||
|
|
||||||
# Dictionaries store mappings
|
# Dictionaries store mappings
|
||||||
empty_dict = {}
|
empty_dict = {}
|
||||||
@ -150,9 +180,10 @@ filled_set.add(5) # filled_set is now set([1, 2, 3, 4, 5])
|
|||||||
# Do set intersection with &
|
# Do set intersection with &
|
||||||
other_set = set([3, 4, 5 ,6])
|
other_set = set([3, 4, 5 ,6])
|
||||||
filled_set & other_set #=> set([3, 4, 5])
|
filled_set & other_set #=> set([3, 4, 5])
|
||||||
|
|
||||||
# Do set union with |
|
# Do set union with |
|
||||||
filled_set | other_set #=> set([1, 2, 3, 4, 5, 6])
|
filled_set | other_set #=> set([1, 2, 3, 4, 5, 6])
|
||||||
|
# Do set difference with -
|
||||||
|
set([1,2,3,4]) - set([2,3,5]) #=> set([1, 4])
|
||||||
|
|
||||||
# Check for existence in a set with in
|
# Check for existence in a set with in
|
||||||
2 in filled_set #=> True
|
2 in filled_set #=> True
|
||||||
@ -221,7 +252,7 @@ add(5, 6) #=> 11 and prints out "x is 5 and y is 6"
|
|||||||
# Another way to call functions is with keyword arguments
|
# Another way to call functions is with keyword arguments
|
||||||
add(y=6, x=5) # Keyword arguments can arrive in any order.
|
add(y=6, x=5) # Keyword arguments can arrive in any order.
|
||||||
|
|
||||||
# You can define functions that take a variable number of arguments
|
# You can define functions that take a variable number of positional arguments
|
||||||
def varargs(*args):
|
def varargs(*args):
|
||||||
return args
|
return args
|
||||||
|
|
||||||
@ -271,7 +302,7 @@ class Human(object):
|
|||||||
# Assign the argument to the instance's name attribute
|
# Assign the argument to the instance's name attribute
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
# A method. All methods take self as the first argument
|
# An instance method. All methods take self as the first argument
|
||||||
def say(self, msg):
|
def say(self, msg):
|
||||||
return "%s: %s" % (self.name, msg)
|
return "%s: %s" % (self.name, msg)
|
||||||
|
|
||||||
@ -281,26 +312,26 @@ class Human(object):
|
|||||||
def get_species(cls):
|
def get_species(cls):
|
||||||
return cls.species
|
return cls.species
|
||||||
|
|
||||||
# Static methods are called without a class or instance reference
|
# A static method is called without a class or instance reference
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def grunt():
|
def grunt():
|
||||||
return "*grunt*"
|
return "*grunt*"
|
||||||
|
|
||||||
|
|
||||||
# Instantiate a class
|
# Instantiate a class
|
||||||
h = Human(name="Harry")
|
i = Human(name="Ian")
|
||||||
print h.say("hi") # prints out "Harry: hi"
|
print i.say("hi") # prints out "Ian: hi"
|
||||||
|
|
||||||
i = Human("Ian")
|
j = Human("Joel")
|
||||||
print i.say("hello") #prints out "Ian: hello"
|
print j.say("hello") #prints out "Joel: hello"
|
||||||
|
|
||||||
# Call our class method
|
# Call our class method
|
||||||
h.get_species() #=> "H. sapiens"
|
i.get_species() #=> "H. sapiens"
|
||||||
|
|
||||||
# Change the shared attribute
|
# Change the shared attribute
|
||||||
h.species = "H. neanderthalensis"
|
i.species = "H. neanderthalensis"
|
||||||
h.get_species() #=> "H. neanderthalensis"
|
|
||||||
i.get_species() #=> "H. neanderthalensis"
|
i.get_species() #=> "H. neanderthalensis"
|
||||||
|
j.get_species() #=> "H. neanderthalensis"
|
||||||
|
|
||||||
# Call the static method
|
# Call the static method
|
||||||
Human.grunt() #=> "*grunt*"
|
Human.grunt() #=> "*grunt*"
|
||||||
|
Loading…
Reference in New Issue
Block a user