Skip to content

Commit

Permalink
Merge pull request adambard#672 from xksteven/master
Browse files Browse the repository at this point in the history
[python3/en] added int div, modulo, and scoping
  • Loading branch information
levibostian committed Jul 14, 2014
2 parents 36dae81 + f08663d commit 0608752
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
23 changes: 23 additions & 0 deletions python.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ to Python 2.x. Look for another tour of Python 3 soon!
2.0 # This is a float
11.0 / 4.0 # => 2.75 ahhh...much better

# Truncation or Integer division
5 // 3 # => 1
5.0 // 3.0 # => 1.0 # works on floats too

# Modulo operation
7 % 3 # => 1

# Enforce precedence with parentheses
(1 + 3) * 2 # => 8

Expand Down Expand Up @@ -380,6 +387,22 @@ all_the_args(*args) # equivalent to foo(1, 2, 3, 4)
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)

# Function Scope
x = 5

def setX(num):
# Local var x not the same as global variable x
x = num # => 43
print (x) # => 43

def setGlobalX(num):
global x
print (x) # => 5
x = num # global var x is now set to 6
print (x) # => 6

setX(43)
setGlobalX(6)

# Python has first class functions
def create_adder(x):
Expand Down
26 changes: 26 additions & 0 deletions python3.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
language: python3
contributors:
- ["Louie Dinh", "http://pythonpracticeprojects.com"]
- ["Steven Basart", "http://github.com/xksteven"]
filename: learnpython3.py
---

Expand Down Expand Up @@ -37,9 +38,16 @@ Note: This article applies to Python 3 specifically. Check out the other tutoria
# Except division which returns floats by default
35 / 5 # => 7.0

# Truncation or Integer division
5 // 3 # => 1
5.0 // 3.0 # => 1.0

# When you use a float, results are floats
3 * 2.0 # => 6.0

# Modulo operation
7 % 3 # => 1

# Enforce precedence with parentheses
(1 + 3) * 2 # => 8

Expand Down Expand Up @@ -406,6 +414,24 @@ 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)


# Function Scope
x = 5

def setX(num):
# Local var x not the same as global variable x
x = num # => 43
print (x) # => 43

def setGlobalX(num):
global x
print (x) # => 5
x = num # global var x is now set to 6
print (x) # => 6

setX(43)
setGlobalX(6)


# Python has first class functions
def create_adder(x):
def adder(y):
Expand Down

0 comments on commit 0608752

Please sign in to comment.