|
28 | 28 | [38;5;67m10.0[39m[38;5;252m [39m[38;5;252m/[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m [39m[38;5;246;03m# => 3.3333333333333335[39;00m |
29 | 29 |
|
30 | 30 | [38;5;246;03m# Modulo operation[39;00m |
31 | | -[38;5;67m7[39m[38;5;252m [39m[38;5;252m%[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m [39m[38;5;246;03m# => 1[39;00m |
| 31 | +[38;5;67m7[39m[38;5;252m [39m[38;5;252m%[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m [39m[38;5;246;03m# => 1[39;00m |
| 32 | +[38;5;246;03m# i % j have the same sign as j, unlike C[39;00m |
| 33 | +[38;5;252m-[39m[38;5;67m7[39m[38;5;252m [39m[38;5;252m%[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m [39m[38;5;246;03m# => 2[39;00m |
32 | 34 |
|
33 | 35 | [38;5;246;03m# Exponentiation (x**y, x to the yth power)[39;00m |
34 | 36 | [38;5;67m2[39m[38;5;252m*[39m[38;5;252m*[39m[38;5;67m3[39m[38;5;252m [39m[38;5;246;03m# => 8[39;00m |
35 | 37 |
|
36 | 38 | [38;5;246;03m# Enforce precedence with parentheses[39;00m |
37 | | -[38;5;67m1[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m [39m[38;5;246;03m# => 7[39;00m |
| 39 | +[38;5;67m1[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m [39m[38;5;246;03m# => 7[39;00m |
38 | 40 | [38;5;252m([39m[38;5;67m1[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;67m3[39m[38;5;252m)[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m [39m[38;5;67m2[39m[38;5;252m [39m[38;5;246;03m# => 8[39;00m |
39 | 41 |
|
40 | 42 | [38;5;246;03m# Boolean values are primitives (Note: the capitalization)[39;00m |
41 | | -[38;5;70;01mTrue[39;00m[38;5;252m [39m[38;5;246;03m# => True[39;00m |
| 43 | +[38;5;70;01mTrue[39;00m[38;5;252m [39m[38;5;246;03m# => True[39;00m |
42 | 44 | [38;5;70;01mFalse[39;00m[38;5;252m [39m[38;5;246;03m# => False[39;00m |
43 | 45 |
|
44 | 46 | [38;5;246;03m# negate with not[39;00m |
|
104 | 106 | [38;5;214m"[39m[38;5;214mThis is a string.[39m[38;5;214m"[39m |
105 | 107 | [38;5;214m'[39m[38;5;214mThis is also a string.[39m[38;5;214m'[39m |
106 | 108 |
|
107 | | -[38;5;246;03m# Strings can be added too! But try not to do this.[39;00m |
| 109 | +[38;5;246;03m# Strings can be added too[39;00m |
108 | 110 | [38;5;214m"[39m[38;5;214mHello [39m[38;5;214m"[39m[38;5;252m [39m[38;5;252m+[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mworld![39m[38;5;214m"[39m[38;5;252m [39m[38;5;246;03m# => "Hello world!"[39;00m |
109 | 111 | [38;5;246;03m# String literals (but not variables) can be concatenated without using '+'[39;00m |
110 | 112 | [38;5;214m"[39m[38;5;214mHello [39m[38;5;214m"[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mworld![39m[38;5;214m"[39m[38;5;252m [39m[38;5;246;03m# => "Hello world!"[39;00m |
|
118 | 120 | [38;5;246;03m# You can also format using f-strings or formatted string literals (in Python 3.6+)[39;00m |
119 | 121 | [38;5;252mname[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mReiko[39m[38;5;214m"[39m |
120 | 122 | [38;5;214mf[39m[38;5;214m"[39m[38;5;214mShe said her name is [39m[38;5;214m{[39m[38;5;252mname[39m[38;5;214m}[39m[38;5;214m.[39m[38;5;214m"[39m[38;5;252m [39m[38;5;246;03m# => "She said her name is Reiko"[39;00m |
121 | | -[38;5;246;03m# You can basically put any Python statement inside the braces and it will be output in the string.[39;00m |
| 123 | +[38;5;246;03m# You can basically put any Python expression inside the braces and it will be output in the string.[39;00m |
122 | 124 | [38;5;214mf[39m[38;5;214m"[39m[38;5;214m{[39m[38;5;252mname[39m[38;5;214m}[39m[38;5;214m is [39m[38;5;214m{[39m[38;5;31mlen[39m[38;5;252m([39m[38;5;252mname[39m[38;5;252m)[39m[38;5;214m}[39m[38;5;214m characters long.[39m[38;5;214m"[39m[38;5;252m [39m[38;5;246;03m# => "Reiko is 5 characters long."[39;00m |
123 | 125 |
|
124 | | - |
125 | 126 | [38;5;246;03m# None is an object[39;00m |
126 | 127 | [38;5;70;01mNone[39;00m[38;5;252m [39m[38;5;246;03m# => None[39;00m |
127 | 128 |
|
|
151 | 152 |
|
152 | 153 | [38;5;246;03m# Simple way to get input data from console[39;00m |
153 | 154 | [38;5;252minput_string_var[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;31minput[39m[38;5;252m([39m[38;5;214m"[39m[38;5;214mEnter some data: [39m[38;5;214m"[39m[38;5;252m)[39m[38;5;252m [39m[38;5;246;03m# Returns the data as a string[39;00m |
154 | | -[38;5;246;03m# Note: In earlier versions of Python, input() method was named as raw_input()[39;00m |
155 | 155 |
|
156 | 156 | [38;5;246;03m# There are no declarations, only assignments.[39;00m |
157 | 157 | [38;5;246;03m# Convention is to use lower_case_with_underscores[39;00m |
|
484 | 484 |
|
485 | 485 | [38;5;70;01mwith[39;00m[38;5;252m [39m[38;5;31mopen[39m[38;5;252m([39m[38;5;214m'[39m[38;5;214mmyfile2.txt[39m[38;5;214m'[39m[38;5;252m,[39m[38;5;252m [39m[38;5;214m"[39m[38;5;214mr+[39m[38;5;214m"[39m[38;5;252m)[39m[38;5;252m [39m[38;5;70;01mas[39;00m[38;5;252m [39m[38;5;252mfile[39m[38;5;252m:[39m |
486 | 486 | [38;5;252m [39m[38;5;252mcontents[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;252mjson[39m[38;5;252m.[39m[38;5;252mload[39m[38;5;252m([39m[38;5;252mfile[39m[38;5;252m)[39m[38;5;252m [39m[38;5;246;03m# reads a json object from a file[39;00m |
487 | | -[38;5;31mprint[39m[38;5;252m([39m[38;5;252mcontents[39m[38;5;252m)[39m[38;5;252m [39m |
| 487 | +[38;5;31mprint[39m[38;5;252m([39m[38;5;252mcontents[39m[38;5;252m)[39m |
488 | 488 | [38;5;246;03m# print: {"aa": 12, "bb": 21}[39;00m |
489 | 489 |
|
490 | 490 |
|
|
751 | 751 | [38;5;252m [39m[38;5;246;03m# Call the static method[39;00m |
752 | 752 | [38;5;252m [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252mHuman[39m[38;5;252m.[39m[38;5;252mgrunt[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m [39m[38;5;246;03m# => "*grunt*"[39;00m |
753 | 753 |
|
754 | | -[38;5;252m [39m[38;5;246;03m# Cannot call static method with instance of object[39;00m |
755 | | -[38;5;252m [39m[38;5;246;03m# because i.grunt() will automatically put "self" (the object i) as an argument[39;00m |
756 | | -[38;5;252m [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252mi[39m[38;5;252m.[39m[38;5;252mgrunt[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m [39m[38;5;246;03m# => TypeError: grunt() takes 0 positional arguments but 1 was given[39;00m |
| 754 | +[38;5;252m [39m[38;5;246;03m# Static methods can be called by instances too[39;00m |
| 755 | +[38;5;252m [39m[38;5;31mprint[39m[38;5;252m([39m[38;5;252mi[39m[38;5;252m.[39m[38;5;252mgrunt[39m[38;5;252m([39m[38;5;252m)[39m[38;5;252m)[39m[38;5;252m [39m[38;5;246;03m# => "*grunt*"[39;00m |
757 | 756 |
|
758 | 757 | [38;5;252m [39m[38;5;246;03m# Update the property for this instance[39;00m |
759 | 758 | [38;5;252m [39m[38;5;252mi[39m[38;5;252m.[39m[38;5;252mage[39m[38;5;252m [39m[38;5;252m=[39m[38;5;252m [39m[38;5;67m42[39m |
|
898 | 897 |
|
899 | 898 | [38;5;252m [39m[38;5;70;01mdef[39;00m[38;5;252m [39m[38;5;68m__init__[39m[38;5;252m([39m[38;5;31mself[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252margs[39m[38;5;252m,[39m[38;5;252m [39m[38;5;252m*[39m[38;5;252m*[39m[38;5;252mkwargs[39m[38;5;252m)[39m[38;5;252m:[39m |
900 | 899 | [38;5;252m [39m[38;5;246;03m# Typically to inherit attributes you have to call super:[39;00m |
901 | | -[38;5;252m [39m[38;5;246;03m# super(Batman, self).__init__(*args, **kwargs) [39;00m |
| 900 | +[38;5;252m [39m[38;5;246;03m# super(Batman, self).__init__(*args, **kwargs)[39;00m |
902 | 901 | [38;5;252m [39m[38;5;246;03m# However we are dealing with multiple inheritance here, and super()[39;00m |
903 | 902 | [38;5;252m [39m[38;5;246;03m# only works with the next base class in the MRO list.[39;00m |
904 | 903 | [38;5;252m [39m[38;5;246;03m# So instead we explicitly call __init__ for all ancestors.[39;00m |
|
0 commit comments