Skip to content

Commit 6847d49

Browse files
first commit
0 parents  commit 6847d49

28 files changed

+561
-0
lines changed

1.hello.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
print 'Hello Python!' # prints the message on the shell (output)
2+
print ("Hello Python!")

10.ComparisonLogicalOperators.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
a, b = 2, 5 # Cool variable assignment style
2+
3+
if a == 2 and b == 5:
4+
print 'and logical operator'
5+
6+
if a > 1 or b < 5:
7+
print 'or logical operator'
8+
9+
if a <> b:
10+
print 'Woah! This is similar to !='
11+
12+
if not(a == 3):
13+
print 'not makes false condition true and vice versa'

11.isAndin.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
string = "abcde"
2+
if 'a' in string: # a is in string or not. if it is present then true
3+
print 'yes! a in string'
4+
5+
if 'ab' in string:
6+
print 'yes! ab in string'
7+
8+
myList1 = [1, 2, 3]
9+
myList2 = [1, 2, 3]
10+
if myList1 is myList2: # it is not similar to == so this if will not execute
11+
print 'Yes! my two list are having same contents'
12+
13+
anotherlist1 = anotherlist2 = [1, 2, 3]
14+
if anotherlist1 is anotherlist2: # but it is similar to ==
15+
print 'This works actually!'

12.forLoop.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
for any_var in range(0, 10):
2+
print 'any_var in for loop:', any_var # o/p = 0123456789
3+
# ---------------------------------------------------------
4+
5+
for any_var in range(0, 10, 1):
6+
print 'any_var in for loop with step 1:', any_var # o/p = 0123456789
7+
# ---------------------------------------------------------
8+
9+
for any_var in range(0, 10, 2):
10+
print 'any_var in for loop with step 2:', any_var # o/p = 0, 2, 4, 6, 8
11+
# ---------------------------------------------------------
12+
13+
for any_var in range(0, 10, -1): # This for loop doesnot execute
14+
print 'any_var in for loop (0 to 10) with step -1:', any_var # No output
15+
# ---------------------------------------------------------
16+
17+
for any_var in range(10, 0, - 1): # 10 9 8 7 6 5 4 3 2 1
18+
print 'any_var in for loop (10 to 0) with step -1:', any_var # print 1 less than the last limit
19+
# ---------------------------------------------------------
20+
21+
for _ in range(0, 10): # If I do not care about the variable
22+
print 'any task which we want to repeat for 10 times' # This message will get printed 10 times
23+
# ---------------------------------------------------------

13.whileLoop.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
my_var = 0
2+
3+
while my_var < 10: # Infinite while loop can be given by while True
4+
print 'my_var value in while loop:', my_var
5+
my_var += 1

14.DefiningFunctions.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def my_function(): # This is function definition and must be before calling the function
2+
print 'my_function has been called!'
3+
4+
5+
def add(a, b): # defining another function
6+
c = a + b
7+
return c
8+
9+
10+
def mix_function(int_var, string_var, float_var=3.14): # float_var is a default argument
11+
print 'In mix_function!'
12+
print 'int_var =', int_var # 15
13+
print 'string_var =', string_var # prasad
14+
print 'float_var =', float_var # 3.14 if 3rd float type argument is not passsed
15+
16+
17+
my_function() # function call
18+
result = add(5, 10) # Calling the function and catching the returned value as wel
19+
print 'Result of addition:', result
20+
21+
mix_function(15, 'prasad')
22+
mix_function(15, 'prasad', 1.414)

15.variableLengthArguments.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def my_function(formalArgument, *anyVariableType): # *variable holds any variable arguments
2+
print 'Formal Argument Output is:', formalArgument
3+
print 'Now variable-length arguments:'
4+
for var in anyVariableType: # This tuple remains empty if no additional arguments are passed
5+
print var
6+
print 'Any Variable Type output:', anyVariableType
7+
8+
9+
my_function('hi') # This was taken by formalArgument
10+
print '--------First Function call has been completed!--------'
11+
12+
my_function(10.5, 'hello', 30, 3.14, 'prasad') # 10.5 will be catched by formalAguments,
13+
#'hello', 30, 3.14, 'prasad' will be caught by tuple-variable length arguments

16.anonymousFunctionLambda.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Lambda forms can take any number of arguments but return just one
2+
# Can not be directly called to print because lambda requires an expression
3+
# Just Like inline function in C++
4+
5+
6+
def sum(arg1, arg2): return arg1 + arg2 # Function definition is different
7+
8+
9+
# all arguments should be used in the definition
10+
# similar to: def sum(arg1, arg2): return arg1 + arg2
11+
print 'Total 1 =', sum(5, 2)
12+
print 'Total 2 =', sum(10, 23)

17.GlobalVsLocal.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
my_list = [1, 2, 3]
2+
my_var = 5
3+
global var2
4+
var2 = 8
5+
6+
non_global_var = 7
7+
8+
9+
def add_to_list(fun_list):
10+
fun_list.append(4) # Making changes to the list by appending
11+
print 'In add_list function, fun_list =:', fun_list # [1, 2, 3, 4]
12+
print 'In add_list function, my_list =:', my_list # [1, 2, 3, 4]
13+
14+
15+
def change_var(var):
16+
print 'In change_var function, var =', var # 5
17+
my_var = 10
18+
print 'In change_var function, my_var =', my_var # 10
19+
20+
21+
def change_var_globally(var):
22+
print 'In change_var_globally function, var =', var # 5
23+
global my_var # global variable declaration in function works only
24+
my_var = 10
25+
print 'In change_var_globally function, my_var =', my_var # 10
26+
27+
28+
def check_global_var():
29+
# I am not declaring any variable, still it is detecting the my_var because it was made set global
30+
print 'Checking global variable in another function:', my_var # 15 it works fine anywhere
31+
print 'Global variable declared outside of functions:', var2 # 8
32+
# var2 = 9 I am not able to 'change' the global var declared outside of function becuase it will be local for this definition
33+
34+
print 'non_global variable declared outside of the function:', non_global_var
35+
36+
37+
print 'Before add_to_list call, my_list =', my_list # [1, 2, 3]
38+
add_to_list(my_list)
39+
print 'After calling add_to_list, my_list =', my_list # [1, 2, 3, 4]
40+
41+
print 'Before change_var call, my_var =', my_var # 5
42+
change_var(my_var)
43+
print 'After calling change_var, my_var =', my_var # 5
44+
# I get the same value though i change the variable value in function because it is not declared as global
45+
46+
# now try to change the variable value by making it global in function definition
47+
change_var_globally(my_var)
48+
print 'After calling change_var_globally, my_var =', my_var # 10
49+
# I get changed value for the variable after declaring that variable as global in defination
50+
51+
my_var = 15 # changing the global variable value declared in defination
52+
check_global_var()
53+
54+
'''
55+
Conclusion:
56+
1. global variable declared in function will have read as well as write access all over the program
57+
2. global variable declared outside the function definition will have read access all over the Programs but not able to write in any function
58+
3. global variable declared outside the function will be similar to normal variable
59+
4. normal variable can be used in the function (in fact it will work like global until any write operation. When you assign something to it, it becomes local)
60+
'''

18.Class.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Person:
2+
my_member_variable = 5
3+
4+
def setName(self, firstName, lastName):
5+
self.firstName = firstName # need to creat self object within the class
6+
self.lastName = lastName # and it is going to perform the operation
7+
8+
def showName(self): # self argument is always present in any member funtion arguments
9+
print 'In class, showName method:', self.firstName, self.lastName
10+
11+
12+
name = Person() # creating object of Person class
13+
name.setName("Prasad", "Dalavi") # accessing the class method using object name
14+
name.showName()
15+
print 'class my_member_variable =', name.my_member_variable # 5

19.ConstructorDestructor.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Person:
2+
def __init__(self):
3+
print("Object is created. So constructor is called automatically.")
4+
5+
def __del__(self):
6+
print("Object is destroyed.")
7+
8+
def setName(self, firstName, lastName):
9+
self.firstName = firstName # need to create self object within the class
10+
self.lastName = lastName # and it will be used all over the class like global variable
11+
12+
def showName(self): # self argument is always present in any member funtion arguments
13+
print 'In class, showName method:', self.firstName, self.lastName
14+
15+
16+
name = Person() # creating object of Person class. freaky method...he he. its not like java
17+
# to pass the arguments to the constructor we have to write name = Person(arguments)
18+
# how strange but the destructor can also take the arguments!!!
19+
name.setName("Prasad", "Dalavi")
20+
name.showName()

2.StringOperations.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
print("String Concepts !")
2+
3+
# Declare strings
4+
a = 'I am a single quoted string'
5+
b = "I am a double quoted string"
6+
c = """I am a tripple quoted string"""
7+
print(a) # I am a single quoted string
8+
print(b) # I am a double quoted string
9+
print(c) # I am a tripple quoted string
10+
# This gives me the same result
11+
12+
# If I want to use quotes in a single inverted comman in a single quoted line then I should use escape character \
13+
d = 'I am a single quoted string. Aren\'t I ?'
14+
print(d) # I am a single quoted string. Aren't I ?
15+
16+
h = "Hello"
17+
p = " Python"
18+
print(h + p) # Hello Python
19+
print("Lenth of string:"), len(h) # gives the length of string which will be 5 in this case
20+
21+
x = "Hello "
22+
y = 07 # which is a number
23+
print(x + str(y)) # Hello 7 ...typecasting and concatenation of string
24+
25+
my_string = 'comma, seperation'
26+
value1, value2 = my_string.split(',') # seperated the string by ','
27+
comma_seperated_string = value1 + value2
28+
print 'Comma Seperated String:', comma_seperated_string # comma seperation

20.Inheritance.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Parent1:
2+
value1 = "this is value 1"
3+
value2 = "this is value 2"
4+
5+
6+
class Parent2:
7+
value3 = "this is value 3"
8+
value4 = "this is value 4"
9+
10+
11+
class Child (Parent1, Parent2): # extending derived class from base classes
12+
pass
13+
14+
15+
parent1_object = Parent1() # creating an object of parent class
16+
print(parent1_object.value1)
17+
print parent1_object.value2
18+
19+
child_object = Child() # creating an object of child class
20+
print("Now Printing the value using child or base class")
21+
print(child_object.value3)
22+
print(child_object.value4)

21.overriding.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Parent:
2+
def my_method_1(self):
3+
print 'This is parent class method 1'
4+
5+
def my_method_2(self):
6+
print 'This is parent class method 2'
7+
8+
9+
class Child(Parent): # Child class inheriting the properties from parent class
10+
def my_method_2(self):
11+
print 'This is child class method 2'
12+
13+
14+
child_object = Child()
15+
child_object.my_method_1() # Child class using Parent class properties (methods)
16+
child_object.my_method_2() # Parent class method has been overrided by Child class

22.overloading.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Human:
2+
def sayHello(self, name=None):
3+
4+
if name is not None:
5+
print 'Hello ' + name
6+
else:
7+
print 'Hello '
8+
9+
10+
obj = Human() # Create instance
11+
obj.sayHello() # Call the method
12+
obj.sayHello('Guido') # Method overloading i.e. same method is called but with different parameter
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class myClass:
2+
__myPrivateVariable = 0 # two underscore indicates that it's a private member variable
3+
myPublicVariable = 0 # this is my normal member variable
4+
5+
def __init__(self): # Constructor of a class will be called when the object of a class is created
6+
self.__myPrivateVariable = 10 # Changing the private member value in Constructor
7+
self.myPublicVariable = 100
8+
9+
def showVariable(self):
10+
print 'Private Variable Value =', self.__myPrivateVariable
11+
print 'Public Variable Value =', self.myPublicVariable
12+
13+
def setPrivateVariable(self, value):
14+
self.__myPrivateVariable = value
15+
16+
17+
classObject = myClass() # Creating an object of a class
18+
# Now, Trying to access and change the private member variable which is not possible
19+
classObject.__myPrivateVariable = 20
20+
classObject.myPublicVariable = 200 # Trying to access and change the public member which is possible
21+
classObject.showVariable() # I will be able to change the public member variable only
22+
23+
# I can change the private variable value by using class member function
24+
classObject.setPrivateVariable(30) # Setting private member value to 30
25+
classObject.showVariable()
26+
print 'Accessing private variable directly:', classObject._myClass__myPrivateVariable
27+
# Private members can not be accessed outside the class but can be accessed using methods of the class
28+
# or using object._className__attrName

24.MultilinePrint.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
print '''
2+
This is a multiline Print where i can do anything.....
3+
4+
5+
----------------------------
6+
| |
7+
| |
8+
| |
9+
| |
10+
| |
11+
| BOX |
12+
| |
13+
| |
14+
| |
15+
| |
16+
| |
17+
| |
18+
----------------------------
19+
20+
21+
Woah! It's really cool....
22+
'''

25.DateTime.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import time
2+
import calendar
3+
4+
print 'Number of ticks since 12:00 am, January 1,1970(epoch) =', time.time()
5+
6+
print '''
7+
#------------------Time Tuple---------------------------#
8+
Field Values
9+
Hour 0 to 23
10+
Minute 0 to 59
11+
Second 0 to 61 (60 or 61 are leap-seconds)
12+
Day of Week 0 to 6 (0 is Monday)
13+
Day of Year 1 to 366 (Julien Day)
14+
#-------------------------------------------------------#
15+
'''
16+
print 'Local Current Time =', time.localtime(time.time()) # time.struct_time(tm_year=2017, tm_mon=12, tm_mday=26, tm_hour=16, tm_min=18, tm_sec=26, tm_wday=1, tm_yday=360, tm_isdst=0)
17+
# tm_yday = 1 to 366 (Julien Day)
18+
19+
print '''
20+
#--------------struct_time Structure--------------------#
21+
tm_year 2008
22+
tm_mon 1 to 12
23+
tm_mday 1 to 31
24+
tm_hour 0 to 23
25+
tm_min 0 to 59
26+
tm_sec 0 to 61 (60 or 61 are leap-seconds)
27+
tm_wday 0 to 6 (0 is Monday)
28+
tm_yday 1 to 366 (Julian day)
29+
tm_isdst -1, 0, 1, -1 means library determines DST
30+
#--------------------------------------------------------#
31+
'''
32+
print 'Getting Formatted Time =', time.asctime(time.localtime(time.time())) # Tue Dec 26 16:18:26 2017
33+
34+
35+
print calendar.month(2018, 1)

0 commit comments

Comments
 (0)