-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathPythonSyntax.py
1298 lines (1034 loc) · 30.9 KB
/
PythonSyntax.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf -*-
# https://gist.github.com/TutorialDoctor/dda76e58fc43d7375856
from __future__ import print_function
intro = '''Remove tripple quotes around the sections to run the code therein\n'''
__version__ = "0.2"
__py_version__ = "2.7x"
print("Version: "+__version__+'\nPython: '+__py_version__+'\n')
print(intro)
def title1(name=''):
print('\n' + name.upper() + ':')
print('-'*65)
def title2(name=''):
print('\n' + name.capitalize())
print('-'*65)
def newline():
print('')
#----------------------------------------------------------------------
# TABLE OF CONTENTS
#----------------------------------------------------------------------
# *** BEGINNER ***
"""
PRINTING
INPUT
OPERATORS
COMMENTS
VARIABLES
FUNCTIONS
CONDITIONALS
LOOPS
CLASSES
MODULES
"""
# *** INTERMEDIATE ***
"""
LIST COMPREHENSION
ARGUMENT PARAMETERS
KEYWORD ARGUMENTS
DOCSTRINGS
FILES
PARSING CSV FILES
DATA MODELS
"""
# *** ADVANCED ***
"""
Comming soon
"""
# *** POPULAR MODULES ***
"""
datetime
re
copy
pickle
os
socket
json
webbrowser
urllib
pydoc
lib2to3
timeit
"""
# *** EXAMPLES ***
"""
Comming soon
"""
#----------------------------------------------------------------------
print("\n*** Beginner code follows this line: ***\n")
# *** BEGINNER ***
# PRINTING
title1('printing')
#----------------------------------------------------------------------
"""
# Print a string
print "Hello World"
# Print multiple strings
print "Hello","World"
# Join/Concatenate two strings (gives errors if not strings)
print "Hello" + "World"
# Joining two strings with spaces
print "Hello" + " World"
# Printing numbers
print 27
# Another way to print (only way in python 3+)
print("Hello World")
#I've updated the print statements in this script for python 3 users
# Print an empty line (useful for separating code output)
print('')
"""
#----------------------------------------------------------------------
# INPUT
title1('input')
#----------------------------------------------------------------------
"""
# Get number input (works for words and numbers in python 3+)
input("How old are you ")
# Get word/string input
raw_input("What is your name ")
"""
#----------------------------------------------------------------------
# OPERATORS
title1('operators')
#----------------------------------------------------------------------
"""
# Adding
print(1+2)
# Subtracting
print(4-3)
# Multiplying
print(3*3)
# Dividing (not accurate if one of the terms isn't a decimal)
print(18/3)
# Remainder/Modulus
print(9%4)
# Power
print(2**8)
# Suare root
print(144**(1/2.0)) # must use at least one float
# Comparisons
print(2<4) #less than
print(4>9) #greather than
print(5==6) #is equal to
print(3==3)
print(4!=4) #not equal to
print(4!=9)
"""
#----------------------------------------------------------------------
# COMMENTS/NOTES
title1('comments')
#----------------------------------------------------------------------
"""A comment is a note for future or peer reference. The # symbol turns a line into a comment. Comments are not recognized as code, and are often used to disable bits of code. Tripple quotes are a multiline comment. They allow you to write comments that span multiple lines."""
#----------------------------------------------------------------------
# VARIABLES
title1('variables')
#----------------------------------------------------------------------
# Types
#----------------------------------------------------------------------
"""
# Character
at = "@"
print(at)
# String (wrapped in quotes)
name = "Raphael"
print(name)
# Integer (no quotes quotes)
age = 29
print(age)
# Float
height = 6.3
print(height)
# Boolean
is_cool = True
print(is_cool)
# List/Array
array = [] #an empty array
colors = ["red","orange","yellow","green","blue","indigo","violet"]
numbers = [0,1,2,3,4,5,6,7,8,9]
mixed_array = [9,"alpha",63.3,True]
print(array,colors,numbers,mixed_array)
# Tuple
location = (0,0)
print(location)
# Dictionary
dictionary = {"key":"value"}
print(dictionary)
# Overwriting a variable
is_cool = False
name = "Tutorial Doctor"
age = 10585 #in days
height = 6.1
print(is_cool,name,age,height)
# Set a boolean to the opposite of itself
is_cool = not is_cool #False
print(is_cool)
# Casting (changing the type of a variable)
# To a float
age = float(age)
print(age)
# To an integer
height = int(height)
print(height)
# To a string
is_cool = str(is_cool)
print(is_cool)
# Other Casting functions
#bool() #To a boolean
#list() #To a list
#dict() #To a dictionary
#long() #To a long
# Printing the type of a variable
print(type(is_cool))
print(type(height),type(age),type(colors),type(dictionary))
# Python keywords cannot be used as variable names.
# Variable names can't start with a number
# Variable names can't start with special characters
# Use undrescores or camelcasing to separate parts of a variable name
# ID of a variable
print(id(name))
# Declaring multiple variables at once
a,s,l = 29,'Male','Georgia'
print(a)
print(s)
print(l)
print a,s,l
# Setting variables to same value at once
a=b=c = 45
print a,b,c
"""
#----------------------------------------------------------------------
# Strings
title2('strings')
#----------------------------------------------------------------------
"""
# Empty string (two ways)
first_name = ""
last_name = ''
# Assign value to a string
first_name = "Raphael"
last_name = "Smith"
occupation = "Programmer"
# Adding strings (concatenation)
print(first_name + last_name)
# Adding a space between string variables
print(first_name + " " + last_name)
# Starting a new line (escape characters)
print(first_name + "\n" + last_name)
# Escaping
message = "I\'m not old enough"
# The backslash escapes the apostrophe so it doesn't get inerpreted as a quote
print(message)
# String Formatting (adding variables inside of a string)
print("Hello %s %s") %(first_name,last_name) #string variables
# Number formatting
age = 29
print("I am %d years old") %(age) #digit variable
print("I am %3d years old") %(age)
print("I am %03d years old") %(age) #leading zeros
print("I am %f years old") %(age) #float
print("I am %.2f years old") %(age) #truncate decimal places
print("I am %.6f yeras old") %(age) #truncate
# Another way to format
greeting = "My name is {} and I am {} years old".format(first_name,age)
print(greeting)
# Another way to use the format() function
greeting = "My name is {name} and I am {age} years old".format(name="Josiah",age=39)
print(greeting)
# Print a string several times
print(first_name*10)
# Get an index of a string (indices start at 0)
print(first_name[0]) #prints the fist item in the string
print(first_name[1]) #prints the second item
# Indexing backwards
print(first_name[-1]) #prints the last item in the string
print(first_name[-2]) #prints the second to last item in the string
# Multi-line String
sentence = '''Multi-Line strings are sometimes used as multi-line comments, since python doesn\'t have syntax for multi-line comments. They are usually used for long strings of text, and as docstrings (strings at the beginning of functions that are used for documenting what the function does)'''
print sentence
# More legal syntax
fourth_letter = "Python"[3]
print(fourth_letter)
#----------------------------------------------------------------------
# String Functions
#----------------------------------------------------------------------
# Capitalize
name = "raphael"
bio = "My name is {}, how are you today?".format(name)
print(name.capitalize())
# Uppercase
print(name.upper())
# Lowercase
print(name.lower())
# Length of string
print(len(name))
# Split a string into individidual words
bio_words = bio.split() # returns a list/array of the words
print(bio_words)
# Joining split words into a single string
empty_string = " "
joined_words = empty_string.join(bio_words)
print(joined_words)
# Mostly seen this way
joined_words_2 = " ".join(bio_words)
print(joined_words_2)
# You can use the above method to sort of add postifixes
joined_words_3 = "$ ".join(bio_words)
print(joined_words_3)
# Replace items in a string
sentence = 'Jello, how are you today?'
corection = sentence.replace('J','H')
print(corection)
"""
#----------------------------------------------------------------------
# Integers
title2('integers')
#----------------------------------------------------------------------
"""
timer = 0
# Increment (Add 1 to the time variable)
timer = timer + 1
# Another way
timer +=1
print(timer)
# Decrement (Subtract increment)
timer = timer - 1
# Or: time -=1
print(timer)
# Multiply increment
timer *= 4
print(timer)
# Divide Decrement
timer /= 2
print(timer)
"""
#----------------------------------------------------------------------
# Floats
title2('floats')
#----------------------------------------------------------------------
None
#----------------------------------------------------------------------
# Lists
title2('lists')
#----------------------------------------------------------------------
"""
# Create a list
inventory = ["flashlight","map","knife","string"]
clothes = ["shirt","pants","shoes","bandana","hat"]
# Get index of a list (starts at 0)
print(inventory[0])
# Get last item in list
print(inventory[-1])
# List index range (starting at... up to...)
print(inventory[1:4]) # 2nd to the fourth item
# List index range (starting at...)
print(inventory[2:])
# List index range (up to...)
print(inventory[:4])
#----------------------------------------------------------------------
# List Functions
#----------------------------------------------------------------------
# Append to a list
inventory.append("rope")
print(inventory)
# Alternately:
# inventory[len(inventory):] = ["Joe"]
# Remove from a list
inventory.remove("knife")
print(inventory)
# Insert item at location
inventory.insert(0,"knife")
print(inventory)
# Reverse List
inventory.reverse()
print(inventory)
# Sort a List (alphabetical or numerical depending)
inventory.sort()
print(inventory)
# Remove an item at a location
inventory.pop(1)
print(inventory)
# Return the index of an item in the list
print(inventory.index("rope"))
# Extend a list with another list
inventory.extend(clothes)
print(inventory)
# Alternately:
# inventory[len(inventory):] = clothes
# Count how many times an item appears in a list
print(inventory.count("knife"))
print(inventory.count("rope"))
# Loop through a list
# Remove list from a list
"""
#----------------------------------------------------------------------
# Tuples (Immutable) can't update indicies
title2('tuples')
#----------------------------------------------------------------------
"""
x=0
y=0
position = (x,y)
"""
#----------------------------------------------------------------------
# Dictionaries
title2('dictionaries')
#----------------------------------------------------------------------
"""
# Create a dictionary: dictionary_name = {key:value}
# Empty dictionary
groceries = {"bread":"Nature's Own"}
# Print the value of a dictionary key
print(groceries["bread"])
# Update Dictionary
groceries.update({"milk":"whole"})
groceries.update({"meat":"ham"})
print(groceries)
# Update key if it is in the dictionary
groceries.update({"milk":"2%"})
print(groceries)
# Get all keys (returns a list)
print(groceries.keys())
# Get all values (returns a list)
print(groceries.values())
# Convert a dictonary to a list of tuples
print(groceries.items())
# Length of a dictionary
print(len(groceries))
# Delete dictionary key
del groceries['meat']
print(groceries)
# Iterate over a dictionary
print(iter(groceries))
print(groceries.iterkeys())
print(groceries.itervalues())
# Remove item, and return it's value
print(groceries.pop("bread"))
# Alternately:
# groceries.popitem()
# Display keys of a dictionary
print(groceries.viewkeys())
# Display values of a dictionary
print(groceries.viewvalues())
# Display items of a dictionary
print(groceries.viewitems())
"""
#----------------------------------------------------------------------
# Booleans: On and Off
title2('booleans')
#----------------------------------------------------------------------
"""
"Coming Soon"
"""
#----------------------------------------------------------------------
# FUNCTIONS: Actions (WIP)
title1('functions')
#----------------------------------------------------------------------
"""
# Arguments & Parameters
#----------------------------------------------------------------------
# An argument is a variable that can be put into a function
# The things going in are called arguments, and the thing(s) they go into are the parameters
# a and b are the paremeters and 2 and 4 are the arguments.
def sum(a,b):
print(a+b)
print(sum(2,4))
#----------------------------------------------------------------------
# Return
#----------------------------------------------------------------------
def product(a,b):
return a*b
print(product(3,4))
#----------------------------------------------------------------------
# Lambda
#----------------------------------------------------------------------
x = lambda a,b: a+b
print(x(6,8))
#----------------------------------------------------------------------
# Local and Global Variables
#----------------------------------------------------------------------
world = 'BIG'
def change():
# get the global variable
global world
# change it
world = 'small'
# You have to all the function to chagne it.
change()
print(world)
# Argument Parameters (Spell check)
#----------------------------------------------------------------------
# Putting a star in front of the parameter allows a variable amount of arguments
def display(*x):
print(x)
display(1,2,3,4,True,"Nothing")
#----------------------------------------------------------------------
# Keyword Arguments
#----------------------------------------------------------------------
# Pass variable amounts of key-value pairs to a function
def info(**information):
return information
gathered_info=info(name="Tutorial Doctor",age=29,height=6.3,cool=True)
print gathered_info['name']
#----------------------------------------------------------------------
# Named Arguments (for clarity on input)
#----------------------------------------------------------------------
def Info(name='',age=0):
return(name,age)
print(Info(age=45,name='Joe'))
#----------------------------------------------------------------------
# Generators
#----------------------------------------------------------------------
# A python generator is used to give a function return multiple values
def F():
yield 1
yield 2
f = F()
f.next()
print f.next()
#----------------------------------------------------------------------
# Built In
#----------------------------------------------------------------------
# Execute string as code
code = "print('Hello user...')"
exec(code)
#Execute a file in the directory of
#execfile('script.py') #file must exist of course, so create it
# Convert integer to a binary string
print(bin(64))
"""
#----------------------------------------------------------------------
# CONDITONALS: Logic/Control Flow
title1('conditionals')
#----------------------------------------------------------------------
# If, Else if(elif), and Else
title2('if, elif, else')
#----------------------------------------------------------------------
"""
age = 9
# 'if' something...
if age>=18:
print('You are too old.')
# otherwise, if something else...
elif age<=7:
print('You are too young.')
# if anything else...
else:
print('You are the right age.')
# The elif statement is optional.
#----------------------------------------------------------------------
# Try, Except, and Finally: Error Checking
title2('try, except, finally')
#----------------------------------------------------------------------
# Try to do something, except if there is an error; do something else. Finally...
try:
print(variable)
except:
print('You have to define the variable first silly.')
variable = 'a_no_longer_undefined_variable'
print('Here, I fixed it for you:\n{}').format(variable)
finally:
print('You have succesfully checked for errors using try and except')
# The variable is now defined, so we can print it
print(variable + '... is defined')
"""
#----------------------------------------------------------------------
# LOOPS: Repeating Instructions
title1('loops')
#----------------------------------------------------------------------
# While Loop: As long as...
title2('while loop')
#----------------------------------------------------------------------
"""
timer =0
while timer <10:
print(timer)
timer = timer + 1
"""
#----------------------------------------------------------------------
# For Loop: Until...
title2('for loop')
#----------------------------------------------------------------------
"""
for number in range(20):
print(number)
newline()
for number in range(0,20,2):
print(number)
newline()
name = 'Raphael'
for letter in name:
print(letter)
newline()
number = 2929392
# The following for loop doesn't work
try:
for digit in number:
print digit
except:
print("You can't loop through numbers")
items = ['gold','wood','ivory','wool']
inventory = {'weapon':'knife','light':'flashlight','navigation':'map'}
# Loop through each value in a list
for item in items:
print(item)
newline()
# Loop through each key and it's index in a list
for item in items:
print items.index(item),item
newline()
# Loop through each key in a dictionary
for eachthing in inventory:
print(eachthing)
newline()
# Loop through each value in a dictionary
for eachthing in inventory:
print(inventory[eachthing])
newline()
# Loop through each key and value in a dictionary
for eachthing in inventory:
print(eachthing,inventory[eachthing])
newline()
# With numbers
numbers = [3,53,534,2253]
for number in numbers:
print(number/2.0)
# A list comprehension is a for loop (comming up)
half=[number/2.0 for number in numbers]
print(half)
"""
#----------------------------------------------------------------------
# CLASSES
title1('classes')
#----------------------------------------------------------------------
"""
# Create a class
class Being(object):
pass
# Create a class that inherits from another class
class Human(Being):
pass
# Create a class with initial/default properties
class Girl(Human):
def __init__(self):
self.type = 'I am Girl'
class Boy(Human):
def __init__(self):
self.type = 'I am Boy'
# The __init__ function is built in
# self refers to the object itself (joe or sarah or...)
# Create an instance of a class
joe = Boy()
sarah = Girl()
# Giving instances properties (outside of the class)
joe.name = 'Joe'
joe.age = 17
print(joe.type) #this is that initial property we made inside the class
sarah.name = 'Saralee'
sarah.age= 7
print(sarah.type)
print(joe.name,joe.age)
print(sarah.name,sarah.age)
newline()
# Get the class of an instance
print(joe.__class__)
# Get the name of the class of an instance
print(sarah.__class__.__name__)
# Get the type of an object
print(type(joe))
# Get the base classes of a class (superclasses)
print(Boy.__bases__)
newline()
# Giving classes properties
Boy.sign = 'o->'
Girl.sign = 'o+'
print(joe.sign)
print(sarah.sign)
# Properties can be arrays
Human.types = [Boy,Girl]
print(Human.types)
# Properties can be dictionaries
Human.info = {'class':'Humanoidus-Strangus','extinct':False}
print(Human.info)
# Properties can be numbers
Human.age=0
print joe.age #prints 17, but 0 if joe.age wasn't already set by us.
# Super class of an object
None
# When it comes to classes inheritance, remember contrapositives.
#All A is B, but not all B is A
# more coming (methods)
"""
#----------------------------------------------------------------------
# MODULES
title1('modules')
#----------------------------------------------------------------------
"""
# Import a module
import math
# Using a module
radius= 3
Area = math.pi*radius**2
Circumference = 2*math.pi*radius
print(Circumference,Area)
# Custom
None
# To run the current script only if it is not from imported modules...
# Check if this script is the main script or not
if __name == '__main__':
pass #function or code here
"""
#----------------------------------------------------------------------
print("\n*** Intermediate code follows this line: ***\n")
# *** INTERMEDIATE ***
# LIST COMPREHENSION
title1('list comprehension')
#----------------------------------------------------------------------
"""
numbers = [1,2,3,4,5,6,7,8,9]
squares = [x*x for x in numbers]
print(squares)
# Read as: squares equals x times x for every item x in numbers
# Alternately read as: for x in numbers, x equals x squared (set that to squares)
# More pythonic:
cubes = [number**3 for number in numbers]
print(cubes)
# With text
names = ["Python","Ruby","C","Javascript"]
prefixed_names = ["Language: " + name for name in names]
print(prefixed_names)
"""
#----------------------------------------------------------------------
# DOCSTRINGS
title1('docstrings')
#----------------------------------------------------------------------
"""
def display():
# A doctstring is a string that documents what the function does
"This function displays something"
print(display.__doc__)
"""
#----------------------------------------------------------------------
# FILES
title1('files')
#----------------------------------------------------------------------
"""
# Files work like notebooks
# It has to have a name
file_name = "notebook.txt"
# You have to open it
# If you want to write in it, you have to use the w mode
notebook = open(file_name,'w+')
# Then you write in it
notebook.write("Writing in my notebook")
# Writing something else into it
notebook.write(" again")
# When you are finished, you have to close it
notebook.close()
# Next time you want to open it for reading (r mode)
file_content=open(file_name,'r')
# Then you read it
print(file_content.read())
# The files are created in the same directory as the script
# Modes
WRITE = 'w'
READ = 'r'
APPEND = 'a'
READWRITE = 'w+'
# Another way (automatically closes the file and handles errors)
with open('spiral_notebook.txt',mode=WRITE) as spiral_notebook:
spiral_notebook.write('This is my spiral notebook')
with open('spiral_notebook.txt',mode=READ) as spiral_notebook:
print(spiral_notebook.read())
"""
#----------------------------------------------------------------------
# Wrappers and Decorators
title1('wrappers and decorators')
# A function that calls a function?
#----------------------------------------------------------------------
"""
"""
#----------------------------------------------------------------------
# DATA MODELS
title1('data models')
#----------------------------------------------------------------------
"""
class Coin(object):
def __init__(self,val):
self.value=val
def __str__(self):
return str(self.value)
def __cmp__(self,other):
return self.value #WIP (for comparisons)
def __add__(self,other):
return self.value+other.value
def __mul__(self,other):
return self.value*other.value
def __div__(self,other):
return self.value/other.value
def __sub__(self,other):
return self.value-other.value
def __len__(self):
return self.value
def __contains__(self,item):
pass #WIP (for in statement)
def __call__(self):
print('You called me?')
print(self.__class__.__name__)
penny = Coin(1)
nickle = Coin(5)
dime = Coin(10)
quarter = Coin(25)
print(quarter-dime)
print(dime*nickle)
print(penny+quarter)
print(quarter/nickle)
penny()