mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-04-27 07:33:57 +00:00
[python/en] some clean up, add a Modulo example
This commit is contained in:
parent
33cd1f57ef
commit
ad92cb8d76
@ -50,17 +50,19 @@ Note: This article applies to Python 3 specifically. Check out [here](http://lea
|
|||||||
10.0 / 3 # => 3.3333333333333335
|
10.0 / 3 # => 3.3333333333333335
|
||||||
|
|
||||||
# Modulo operation
|
# Modulo operation
|
||||||
7 % 3 # => 1
|
7 % 3 # => 1
|
||||||
|
# i % j have the same sign as j, unlike C
|
||||||
|
-7 % 3 # => 2
|
||||||
|
|
||||||
# Exponentiation (x**y, x to the yth power)
|
# Exponentiation (x**y, x to the yth power)
|
||||||
2**3 # => 8
|
2**3 # => 8
|
||||||
|
|
||||||
# Enforce precedence with parentheses
|
# Enforce precedence with parentheses
|
||||||
1 + 3 * 2 # => 7
|
1 + 3 * 2 # => 7
|
||||||
(1 + 3) * 2 # => 8
|
(1 + 3) * 2 # => 8
|
||||||
|
|
||||||
# Boolean values are primitives (Note: the capitalization)
|
# Boolean values are primitives (Note: the capitalization)
|
||||||
True # => True
|
True # => True
|
||||||
False # => False
|
False # => False
|
||||||
|
|
||||||
# negate with not
|
# negate with not
|
||||||
@ -126,7 +128,7 @@ b == a # => True, a's and b's objects are equal
|
|||||||
"This is a string."
|
"This is a string."
|
||||||
'This is also a string.'
|
'This is also a string.'
|
||||||
|
|
||||||
# Strings can be added too! But try not to do this.
|
# Strings can be added too
|
||||||
"Hello " + "world!" # => "Hello world!"
|
"Hello " + "world!" # => "Hello world!"
|
||||||
# String literals (but not variables) can be concatenated without using '+'
|
# String literals (but not variables) can be concatenated without using '+'
|
||||||
"Hello " "world!" # => "Hello world!"
|
"Hello " "world!" # => "Hello world!"
|
||||||
@ -140,10 +142,9 @@ len("This is a string") # => 16
|
|||||||
# You can also format using f-strings or formatted string literals (in Python 3.6+)
|
# You can also format using f-strings or formatted string literals (in Python 3.6+)
|
||||||
name = "Reiko"
|
name = "Reiko"
|
||||||
f"She said her name is {name}." # => "She said her name is Reiko"
|
f"She said her name is {name}." # => "She said her name is Reiko"
|
||||||
# You can basically put any Python statement inside the braces and it will be output in the string.
|
# You can basically put any Python expression inside the braces and it will be output in the string.
|
||||||
f"{name} is {len(name)} characters long." # => "Reiko is 5 characters long."
|
f"{name} is {len(name)} characters long." # => "Reiko is 5 characters long."
|
||||||
|
|
||||||
|
|
||||||
# None is an object
|
# None is an object
|
||||||
None # => None
|
None # => None
|
||||||
|
|
||||||
@ -173,7 +174,6 @@ print("Hello, World", end="!") # => Hello, World!
|
|||||||
|
|
||||||
# Simple way to get input data from console
|
# Simple way to get input data from console
|
||||||
input_string_var = input("Enter some data: ") # Returns the data as a string
|
input_string_var = input("Enter some data: ") # Returns the data as a string
|
||||||
# Note: In earlier versions of Python, input() method was named as raw_input()
|
|
||||||
|
|
||||||
# There are no declarations, only assignments.
|
# There are no declarations, only assignments.
|
||||||
# Convention is to use lower_case_with_underscores
|
# Convention is to use lower_case_with_underscores
|
||||||
@ -506,7 +506,7 @@ print(contents)
|
|||||||
|
|
||||||
with open('myfile2.txt', "r+") as file:
|
with open('myfile2.txt', "r+") as file:
|
||||||
contents = json.load(file) # reads a json object from a file
|
contents = json.load(file) # reads a json object from a file
|
||||||
print(contents)
|
print(contents)
|
||||||
# print: {"aa": 12, "bb": 21}
|
# print: {"aa": 12, "bb": 21}
|
||||||
|
|
||||||
|
|
||||||
@ -919,7 +919,7 @@ class Batman(Superhero, Bat):
|
|||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
# Typically to inherit attributes you have to call super:
|
# Typically to inherit attributes you have to call super:
|
||||||
# super(Batman, self).__init__(*args, **kwargs)
|
# super(Batman, self).__init__(*args, **kwargs)
|
||||||
# However we are dealing with multiple inheritance here, and super()
|
# However we are dealing with multiple inheritance here, and super()
|
||||||
# only works with the next base class in the MRO list.
|
# only works with the next base class in the MRO list.
|
||||||
# So instead we explicitly call __init__ for all ancestors.
|
# So instead we explicitly call __init__ for all ancestors.
|
||||||
|
Loading…
Reference in New Issue
Block a user