mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
[python/en] Moved to default python2 print statements.
This commit is contained in:
parent
4ee011100b
commit
6b6342a7dc
@ -145,8 +145,10 @@ bool("") # => False
|
|||||||
print "I'm Python. Nice to meet you!"
|
print "I'm Python. Nice to meet you!"
|
||||||
# Python also has a print function, available in versions 2.7 and 3...
|
# Python also has a print function, available in versions 2.7 and 3...
|
||||||
# but for 2.7 you need to add the import (uncommented):
|
# but for 2.7 you need to add the import (uncommented):
|
||||||
# from __future__ import print_function
|
from __future__ import print_function
|
||||||
print("I'm also Python! ")
|
print("I'm also Python! ")
|
||||||
|
# This is useful to maintain compatibility,
|
||||||
|
# but for this doc, we'll use the python 2 print statement
|
||||||
|
|
||||||
# No need to declare variables before assigning to them.
|
# No need to declare variables before assigning to them.
|
||||||
some_var = 5 # Convention is to use lower_case_with_underscores
|
some_var = 5 # Convention is to use lower_case_with_underscores
|
||||||
@ -316,11 +318,11 @@ some_var = 5
|
|||||||
# Here is an if statement. Indentation is significant in python!
|
# Here is an if statement. Indentation is significant in python!
|
||||||
# prints "some_var is smaller than 10"
|
# prints "some_var is smaller than 10"
|
||||||
if some_var > 10:
|
if some_var > 10:
|
||||||
print("some_var is totally bigger than 10.")
|
print "some_var is totally bigger than 10."
|
||||||
elif some_var < 10: # This elif clause is optional.
|
elif some_var < 10: # This elif clause is optional.
|
||||||
print("some_var is smaller than 10.")
|
print "some_var is smaller than 10."
|
||||||
else: # This is optional too.
|
else: # This is optional too.
|
||||||
print("some_var is indeed 10.")
|
print "some_var is indeed 10."
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -332,7 +334,7 @@ prints:
|
|||||||
"""
|
"""
|
||||||
for animal in ["dog", "cat", "mouse"]:
|
for animal in ["dog", "cat", "mouse"]:
|
||||||
# You can use % to interpolate formatted strings
|
# You can use % to interpolate formatted strings
|
||||||
print("%s is a mammal" % animal)
|
print "%s is a mammal" % animal
|
||||||
|
|
||||||
"""
|
"""
|
||||||
"range(number)" returns a list of numbers
|
"range(number)" returns a list of numbers
|
||||||
@ -344,7 +346,7 @@ prints:
|
|||||||
3
|
3
|
||||||
"""
|
"""
|
||||||
for i in range(4):
|
for i in range(4):
|
||||||
print(i)
|
print i
|
||||||
|
|
||||||
"""
|
"""
|
||||||
"range(lower, upper)" returns a list of numbers
|
"range(lower, upper)" returns a list of numbers
|
||||||
@ -356,7 +358,7 @@ prints:
|
|||||||
7
|
7
|
||||||
"""
|
"""
|
||||||
for i in range(4, 8):
|
for i in range(4, 8):
|
||||||
print(i)
|
print i
|
||||||
|
|
||||||
"""
|
"""
|
||||||
While loops go until a condition is no longer met.
|
While loops go until a condition is no longer met.
|
||||||
@ -368,7 +370,7 @@ prints:
|
|||||||
"""
|
"""
|
||||||
x = 0
|
x = 0
|
||||||
while x < 4:
|
while x < 4:
|
||||||
print(x)
|
print x
|
||||||
x += 1 # Shorthand for x = x + 1
|
x += 1 # Shorthand for x = x + 1
|
||||||
|
|
||||||
# Handle exceptions with a try/except block
|
# Handle exceptions with a try/except block
|
||||||
@ -391,7 +393,7 @@ else: # Optional clause to the try/except block. Must follow all except blocks
|
|||||||
|
|
||||||
# Use "def" to create new functions
|
# Use "def" to create new functions
|
||||||
def add(x, y):
|
def add(x, y):
|
||||||
print("x is %s and y is %s" % (x, y))
|
print "x is %s and y is %s" % (x, y)
|
||||||
return x + y # Return values with a return statement
|
return x + y # Return values with a return statement
|
||||||
|
|
||||||
# Calling functions with parameters
|
# Calling functions with parameters
|
||||||
@ -420,8 +422,8 @@ keyword_args(big="foot", loch="ness") # => {"big": "foot", "loch": "ness"}
|
|||||||
|
|
||||||
# You can do both at once, if you like
|
# You can do both at once, if you like
|
||||||
def all_the_args(*args, **kwargs):
|
def all_the_args(*args, **kwargs):
|
||||||
print(args)
|
print args
|
||||||
print(kwargs)
|
print kwargs
|
||||||
"""
|
"""
|
||||||
all_the_args(1, 2, a=3, b=4) prints:
|
all_the_args(1, 2, a=3, b=4) prints:
|
||||||
(1, 2)
|
(1, 2)
|
||||||
@ -517,10 +519,10 @@ class Human(object):
|
|||||||
|
|
||||||
# Instantiate a class
|
# Instantiate a class
|
||||||
i = Human(name="Ian")
|
i = Human(name="Ian")
|
||||||
print(i.say("hi")) # prints out "Ian: hi"
|
print i.say("hi") # prints out "Ian: hi"
|
||||||
|
|
||||||
j = Human("Joel")
|
j = Human("Joel")
|
||||||
print(j.say("hello")) # prints out "Joel: hello"
|
print j.say("hello") # prints out "Joel: hello"
|
||||||
|
|
||||||
# Call our class method
|
# Call our class method
|
||||||
i.get_species() # => "H. sapiens"
|
i.get_species() # => "H. sapiens"
|
||||||
@ -540,12 +542,12 @@ Human.grunt() # => "*grunt*"
|
|||||||
|
|
||||||
# You can import modules
|
# You can import modules
|
||||||
import math
|
import math
|
||||||
print(math.sqrt(16)) # => 4
|
print math.sqrt(16) # => 4
|
||||||
|
|
||||||
# You can get specific functions from a module
|
# You can get specific functions from a module
|
||||||
from math import ceil, floor
|
from math import ceil, floor
|
||||||
print(ceil(3.7)) # => 4.0
|
print ceil(3.7) # => 4.0
|
||||||
print(floor(3.7)) # => 3.0
|
print floor(3.7) # => 3.0
|
||||||
|
|
||||||
# You can import all functions from a module.
|
# You can import all functions from a module.
|
||||||
# Warning: this is not recommended
|
# Warning: this is not recommended
|
||||||
@ -591,7 +593,7 @@ xrange_ = xrange(1, 900000000)
|
|||||||
|
|
||||||
# will double all numbers until a result >=30 found
|
# will double all numbers until a result >=30 found
|
||||||
for i in double_numbers(xrange_):
|
for i in double_numbers(xrange_):
|
||||||
print(i)
|
print i
|
||||||
if i >= 30:
|
if i >= 30:
|
||||||
break
|
break
|
||||||
|
|
||||||
@ -620,8 +622,8 @@ def say(say_please=False):
|
|||||||
return msg, say_please
|
return msg, say_please
|
||||||
|
|
||||||
|
|
||||||
print(say()) # Can you buy me a beer?
|
print say() # Can you buy me a beer?
|
||||||
print(say(say_please=True)) # Can you buy me a beer? Please! I am poor :(
|
print say(say_please=True) # Can you buy me a beer? Please! I am poor :(
|
||||||
```
|
```
|
||||||
|
|
||||||
## Ready For More?
|
## Ready For More?
|
||||||
|
Loading…
Reference in New Issue
Block a user