From bdbff25c5850e41552e5fc517c99fe3933dc591c Mon Sep 17 00:00:00 2001 From: Steven Basart Date: Sun, 13 Jul 2014 14:08:12 -0400 Subject: [python3/en] added int div, modulo, and scoping Added integer or truncation division, the modulo operator which were missing from operators section. Added function scoping in the functions section. --- python3.html.markdown | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/python3.html.markdown b/python3.html.markdown index 778076f8..eca49862 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -2,6 +2,7 @@ language: python3 contributors: - ["Louie Dinh", "http://pythonpracticeprojects.com"] + - ["Steven Basart", "http://sbasart.com"] filename: learnpython3.py --- @@ -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 @@ -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) + +setX(43) +setGlobalX(6) + + # Python has first class functions def create_adder(x): def adder(y): -- cgit v1.2.3 From 42c93bf407d91cdb549ef5053c3db7f4a58bcd7e Mon Sep 17 00:00:00 2001 From: Steven Basart Date: Sun, 13 Jul 2014 14:45:14 -0400 Subject: [python3/en] added int div, modulo, and scoping Added integer or truncation division, the modulo operator which were missing from operators section. Added function scoping in the functions section. --- python3.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python3.html.markdown b/python3.html.markdown index eca49862..8d9b8e77 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -426,7 +426,7 @@ def setGlobalX(num): global x print (x) # => 5 x = num # global var x is now set to 6 - print (x) + print (x) # => 6 setX(43) setGlobalX(6) -- cgit v1.2.3 From 2a9a4f39babe369395de580d0bd7f405a0283803 Mon Sep 17 00:00:00 2001 From: Steven Basart Date: Sun, 13 Jul 2014 14:51:34 -0400 Subject: [python/en] added int div, modulo, and scoping Added integer or truncation division, the modulo operator which were missing from operators section. Added function scoping in the functions section. --- python.html.markdown | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/python.html.markdown b/python.html.markdown index 210c9619..d1b88b25 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -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 + +# Modulo operation +7 % 3 # => 1 + # Enforce precedence with parentheses (1 + 3) * 2 # => 8 @@ -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): -- cgit v1.2.3 From 0dcc5640b6e1a9bba857114e1257a29aa9829ce9 Mon Sep 17 00:00:00 2001 From: Steven Basart Date: Sun, 13 Jul 2014 14:52:31 -0400 Subject: [python/en] added int div, modulo, and scoping Added integer or truncation division, the modulo operator which were missing from operators section. Added function scoping in the functions section. --- python.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python.html.markdown b/python.html.markdown index d1b88b25..aa077e57 100644 --- a/python.html.markdown +++ b/python.html.markdown @@ -47,7 +47,7 @@ to Python 2.x. Look for another tour of Python 3 soon! # Truncation or Integer division 5 // 3 # => 1 -5.0 // 3.0 # => 1.0 +5.0 // 3.0 # => 1.0 # works on floats too # Modulo operation 7 % 3 # => 1 -- cgit v1.2.3 From f08663db5cd965924e0fb4f344dea5c7ada9057e Mon Sep 17 00:00:00 2001 From: Steven Basart Date: Sun, 13 Jul 2014 16:00:28 -0400 Subject: Update python3.html.markdown --- python3.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python3.html.markdown b/python3.html.markdown index 8d9b8e77..7657295d 100644 --- a/python3.html.markdown +++ b/python3.html.markdown @@ -2,7 +2,7 @@ language: python3 contributors: - ["Louie Dinh", "http://pythonpracticeprojects.com"] - - ["Steven Basart", "http://sbasart.com"] + - ["Steven Basart", "http://github.com/xksteven"] filename: learnpython3.py --- -- cgit v1.2.3