File tree Expand file tree Collapse file tree 9 files changed +186
-0
lines changed Expand file tree Collapse file tree 9 files changed +186
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Open terminal > python3 iterators.py
2
+ # Start typing below commands and see the output
3
+
4
+ for element in [1 , 2 , 3 ]:
5
+ print (element )
6
+
7
+ for element in (1 , 2 , 3 ):
8
+ print (element )
9
+
10
+ for key in { 'one' : 1 , 'two' : 2 , 'three' : 3 }:
11
+ print (key )
12
+
13
+ for char in '123' :
14
+ print (char )
15
+
16
+ print ('\n \n ' )
17
+
18
+ s = 'XYZ'
19
+ it = iter (s )
20
+ print (next (it ))
21
+ print (next (it ))
22
+ print (next (it ))
23
+ print (next (it )) # StopIteration Exception
Original file line number Diff line number Diff line change
1
+ # Open terminal > python3 classEg1.py
2
+ # Start typing below commands and see the output
3
+
4
+ def scopeTesting ():
5
+ def doLocal ():
6
+ name = '(Do not change) -> LOCAL'
7
+ print (name )
8
+
9
+ def doNonLocal ():
10
+ nonlocal name # Change the name binding to scopeTesting's level
11
+ name = '-> Changed to NON-LOCAL'
12
+
13
+ def doGlobal ():
14
+ global name # Change the name binding to module level
15
+ name = '-> Changed to GLOBAL'
16
+
17
+ name = "-> (Do not Change) DEFAULT" # Default, DO NOT change the name binding to scopeTesting's level"
18
+
19
+ doLocal ()
20
+ print ('After local assignment: ' , name )
21
+
22
+ doNonLocal ()
23
+ print ('After non-local assignment: ' , name )
24
+
25
+ doGlobal ()
26
+ print ('After global assignment: ' , name )
27
+
28
+ scopeTesting ()
29
+ print ('In global scope: ' , name )
Original file line number Diff line number Diff line change
1
+ # Open terminal > python3 classEg2.py
2
+ # Start typing below commands and see the output
3
+
4
+ class Person :
5
+ name = 'Mr. Unknown'
6
+
7
+ def __init__ (self , email ):
8
+ self .email = email
9
+
10
+
11
+ print (p1 .name )
12
+ print (p1 .email )
Original file line number Diff line number Diff line change
1
+ # Open terminal > python3 classEg3.py
2
+ # Start typing below commands and see the output
3
+
4
+ class Person :
5
+ actions = []
6
+
7
+ def addAction (self , action ):
8
+ self .actions .append (action )
9
+
10
+ p1 = Person ()
11
+
12
+ p1 .addAction ('Eating' )
13
+ p1 .addAction ('Walking' )
14
+ p1 .addAction ('Sleeping' )
15
+
16
+ print (p1 .actions )
Original file line number Diff line number Diff line change
1
+ # Open terminal > python3 classEg4.py
2
+ # Start typing below commands and see the output
3
+
4
+ class Person :
5
+ def __init__ (self ):
6
+ self .actions = []
7
+
8
+ def addActions (self , action ):
9
+ self .actions .append (action )
10
+
11
+ p1 = Person ()
12
+
13
+ p1 .addActions ('Eating' )
14
+ p1 .addActions ('Sleeping' )
15
+
16
+ print (p1 .actions )
Original file line number Diff line number Diff line change
1
+ # Open terminal > python3 classEg5.py
2
+ # Start typing below commands and see the output
3
+
4
+ # Base class
5
+ class Person :
6
+ actions = []
7
+
8
+ # def __init__(self):
9
+ # self.actions = [] # Cannot be accessed from Derived class instance
10
+
11
+ def addActions (self , action ):
12
+ self .actions .append (action )
13
+
14
+ # Derived class
15
+ class Student (Person ):
16
+ def __init__ (self ):
17
+ self .name = 'Ashwin'
18
+
19
+ def getName (self ):
20
+ return 'My name is ' + self .name
21
+
22
+ s1 = Student ()
23
+ print (s1 .getName ())
24
+
25
+ s1 .addActions ('Eating' )
26
+ s1 .addActions ('Sleeping' )
27
+ print (s1 .actions )
Original file line number Diff line number Diff line change
1
+ # Open terminal > python3 classEg6.py
2
+ # Start typing below commands and see the output
3
+
4
+ class A :
5
+ name = 'A'
6
+
7
+ def getName (self ):
8
+ return self .name
9
+
10
+ class B (A ):
11
+ name = 'B' # Override class A's name
12
+
13
+ def getName (self ):
14
+ return self .name
15
+
16
+ b = B ()
17
+
18
+ print (b .getName ())
Original file line number Diff line number Diff line change
1
+ # Open terminal > python3 classEg7.py
2
+ # Start typing below commands and see the output
3
+
4
+ class A :
5
+ name = 'A'
6
+
7
+ def getName (self ):
8
+ return self .name
9
+
10
+ class B :
11
+ name = 'B'
12
+
13
+ def getName (self ):
14
+ return self .name
15
+
16
+ class C (A , B ):
17
+ name = 'C' # C Override A Override B
18
+
19
+ def getName (self ):
20
+ return self .name
21
+
22
+ c = C ()
23
+
24
+ print (c .getName ())
Original file line number Diff line number Diff line change
1
+ # Open terminal > python3 classEg8.py
2
+ # Start typing below commands and see the output
3
+
4
+ class Reverse :
5
+ def __init__ (self , data ):
6
+ self .data = data
7
+ self .index = len (data )
8
+
9
+ def __iter__ (self ):
10
+ return self
11
+
12
+ def __next__ (self ):
13
+ if self .index == 0 :
14
+ raise StopIteration
15
+ self .index = self .index - 1
16
+ return self .data [self .index ]
17
+
18
+ r = Reverse ('XYZ' )
19
+
20
+ for char in r :
21
+ print (char )
You can’t perform that action at this time.
0 commit comments