Skip to content

Commit 6847d49

Browse files
first commit
0 parents  commit 6847d49

28 files changed

+561
-0
lines changed

1.hello.py

Lines changed: 2 additions & 0 deletions
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

Lines changed: 13 additions & 0 deletions
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

Lines changed: 15 additions & 0 deletions
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

Lines changed: 23 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 22 additions & 0 deletions
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

Lines changed: 13 additions & 0 deletions
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

Lines changed: 12 additions & 0 deletions
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

Lines changed: 60 additions & 0 deletions
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

Lines changed: 15 additions & 0 deletions
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

0 commit comments

Comments
 (0)