mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-13 18:24:39 +02:00
Merge pull request #1077 from geoffliu/master
[Python3/en] Python3 doc cleanup
This commit is contained in:
@@ -39,7 +39,7 @@ Note: This article applies to Python 3 specifically. Check out [here](http://lea
|
|||||||
# Except division which returns floats by default
|
# Except division which returns floats by default
|
||||||
35 / 5 # => 7.0
|
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 // 3 # => 1
|
||||||
5.0 // 3.0 # => 1.0 # works on floats too
|
5.0 // 3.0 # => 1.0 # works on floats too
|
||||||
-5 // 3 # => -2
|
-5 // 3 # => -2
|
||||||
@@ -73,8 +73,8 @@ False or True #=> True
|
|||||||
# Note using Bool operators with ints
|
# Note using Bool operators with ints
|
||||||
0 and 2 #=> 0
|
0 and 2 #=> 0
|
||||||
-5 or 0 #=> -5
|
-5 or 0 #=> -5
|
||||||
0 == False #=> True
|
0 == False #=> True
|
||||||
2 == True #=> False
|
2 == True #=> False
|
||||||
1 == True #=> True
|
1 == True #=> True
|
||||||
|
|
||||||
# Equality is ==
|
# Equality is ==
|
||||||
@@ -145,7 +145,7 @@ bool({}) #=> False
|
|||||||
# Python has a print function
|
# Python has a print function
|
||||||
print("I'm Python. Nice to meet you!")
|
print("I'm Python. Nice to meet you!")
|
||||||
|
|
||||||
# No need to declare variables before assigning to them.
|
# No need to declare variables before assigning to them.
|
||||||
# Convention is to use lower_case_with_underscores
|
# Convention is to use lower_case_with_underscores
|
||||||
some_var = 5
|
some_var = 5
|
||||||
some_var # => 5
|
some_var # => 5
|
||||||
@@ -186,7 +186,7 @@ li[2:] # => [4, 3]
|
|||||||
li[:3] # => [1, 2, 4]
|
li[:3] # => [1, 2, 4]
|
||||||
# Select every second entry
|
# Select every second entry
|
||||||
li[::2] # =>[1, 4]
|
li[::2] # =>[1, 4]
|
||||||
# Revert the list
|
# Return a reversed copy of the list
|
||||||
li[::-1] # => [3, 4, 2, 1]
|
li[::-1] # => [3, 4, 2, 1]
|
||||||
# Use any combination of these to make advanced slices
|
# Use any combination of these to make advanced slices
|
||||||
# li[start:end:step]
|
# li[start:end:step]
|
||||||
@@ -196,7 +196,7 @@ del li[2] # li is now [1, 2, 3]
|
|||||||
|
|
||||||
# You can add lists
|
# You can add lists
|
||||||
# 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]
|
li + other_li # => [1, 2, 3, 4, 5, 6]
|
||||||
|
|
||||||
# Concatenate lists with "extend()"
|
# Concatenate lists with "extend()"
|
||||||
li.extend(other_li) # Now li is [1, 2, 3, 4, 5, 6]
|
li.extend(other_li) # Now li is [1, 2, 3, 4, 5, 6]
|
||||||
@@ -213,7 +213,7 @@ 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
|
# You can do most of the list operations on tuples too
|
||||||
len(tup) # => 3
|
len(tup) # => 3
|
||||||
tup + (4, 5, 6) # => (1, 2, 3, 4, 5, 6)
|
tup + (4, 5, 6) # => (1, 2, 3, 4, 5, 6)
|
||||||
tup[:2] # => (1, 2)
|
tup[:2] # => (1, 2)
|
||||||
@@ -235,15 +235,15 @@ filled_dict = {"one": 1, "two": 2, "three": 3}
|
|||||||
# Look up values with []
|
# Look up values with []
|
||||||
filled_dict["one"] # => 1
|
filled_dict["one"] # => 1
|
||||||
|
|
||||||
# Get all keys as a list with "keys()".
|
# Get all keys as an iterable with "keys()". We need to wrap the call in list()
|
||||||
# We need to wrap the call in list() because we are getting back an iterable. We'll talk about those later.
|
# to turn it into a list. We'll talk about those later. Note - Dictionary key
|
||||||
# Note - Dictionary key ordering is not guaranteed.
|
# ordering is not guaranteed. Your results might not match this exactly.
|
||||||
# Your results might not match this exactly.
|
|
||||||
list(filled_dict.keys()) # => ["three", "two", "one"]
|
list(filled_dict.keys()) # => ["three", "two", "one"]
|
||||||
|
|
||||||
|
|
||||||
# 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
|
||||||
# Note - Same as above regarding key ordering.
|
# in list() to get it out of the iterable. Note - Same as above regarding key
|
||||||
|
# ordering.
|
||||||
list(filled_dict.values()) # => [3, 2, 1]
|
list(filled_dict.values()) # => [3, 2, 1]
|
||||||
|
|
||||||
|
|
||||||
@@ -281,7 +281,7 @@ 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
|
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}
|
filled_set.add(5) # filled_set is now {1, 2, 3, 4, 5}
|
||||||
|
|
||||||
# Do set intersection with &
|
# Do set intersection with &
|
||||||
@@ -328,7 +328,7 @@ for animal in ["dog", "cat", "mouse"]:
|
|||||||
print("{} is a mammal".format(animal))
|
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
|
from zero to the given number
|
||||||
prints:
|
prints:
|
||||||
0
|
0
|
||||||
@@ -340,7 +340,7 @@ for i in range(4):
|
|||||||
print(i)
|
print(i)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
"range(lower, upper)" returns a list of numbers
|
"range(lower, upper)" returns an iterable of numbers
|
||||||
from the lower number to the upper number
|
from the lower number to the upper number
|
||||||
prints:
|
prints:
|
||||||
4
|
4
|
||||||
@@ -458,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)
|
all_the_args(*args, **kwargs) # equivalent to foo(1, 2, 3, 4, a=3, b=4)
|
||||||
|
|
||||||
|
|
||||||
# Function Scope
|
# Function Scope
|
||||||
x = 5
|
x = 5
|
||||||
|
|
||||||
def setX(num):
|
def setX(num):
|
||||||
# Local var x not the same as global variable x
|
# Local var x not the same as global variable x
|
||||||
x = num # => 43
|
x = num # => 43
|
||||||
print (x) # => 43
|
print (x) # => 43
|
||||||
|
|
||||||
def setGlobalX(num):
|
def setGlobalX(num):
|
||||||
global x
|
global x
|
||||||
print (x) # => 5
|
print (x) # => 5
|
||||||
@@ -512,8 +512,8 @@ class Human(object):
|
|||||||
# Basic initializer, this is called when this class is instantiated.
|
# Basic initializer, this is called when this class is instantiated.
|
||||||
# Note that the double leading and trailing underscores denote objects
|
# Note that the double leading and trailing underscores denote objects
|
||||||
# or attributes that are used by python but that live in user-controlled
|
# or attributes that are used by python but that live in user-controlled
|
||||||
# namespaces. Methods(or objects or attributes) like: __init__, __str__,
|
# namespaces. Methods(or objects or attributes) like: __init__, __str__,
|
||||||
# __repr__ etc. are called magic methods (or sometimes called dunder methods)
|
# __repr__ etc. are called magic methods (or sometimes called dunder methods)
|
||||||
# You should not invent such names on your own.
|
# You should not invent such names on your own.
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
# Assign the argument to the instance's name attribute
|
# Assign the argument to the instance's name attribute
|
||||||
@@ -600,7 +600,7 @@ def double_numbers(iterable):
|
|||||||
# double_numbers.
|
# double_numbers.
|
||||||
# Note range is a generator too. Creating a list 1-900000000 would take lot of
|
# Note range is a generator too. Creating a list 1-900000000 would take lot of
|
||||||
# time to be made
|
# 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
|
# would normally collide with a python keyword
|
||||||
range_ = range(1, 900000000)
|
range_ = range(1, 900000000)
|
||||||
# will double all numbers until a result >=30 found
|
# will double all numbers until a result >=30 found
|
||||||
|
Reference in New Issue
Block a user