Skip to content

Commit 8abf84c

Browse files
Add files via upload
1 parent 3249921 commit 8abf84c

File tree

4 files changed

+363
-0
lines changed

4 files changed

+363
-0
lines changed

loopingstatementspython.txt

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
vowels="AEIOU"
2+
for iter in vowels:
3+
print("char:", iter)
4+
5+
6+
int_list = [1, 2, 3, 4, 5, 6]
7+
sum = 0
8+
for iter in int_list:
9+
sum += iter
10+
print("Sum =", sum)
11+
print("Avg =", sum/len(int_list))
12+
13+
14+
for iter in range(0, 3):
15+
print("iter: %d" % (iter))
16+
17+
18+
books = ['C', 'C++', 'Java', 'Python']
19+
for index in range(len(books)):
20+
print('Book (%d):' % index, books[index])
21+
22+
###FOR_ELSE
23+
birds = ['Belle', 'Coco', 'Juniper', 'Lilly', 'Snow']
24+
ignoreElse = False
25+
26+
for theBird in birds:
27+
print(theBird )
28+
if ignoreElse and theBird is 'Snow':
29+
break
30+
else:
31+
print("No birds left.")
32+
33+
34+
##WHILE
35+
n = 5
36+
while n > 0:
37+
n -=1
38+
print(n)
39+
40+
#WHILE BREAK
41+
n = 5
42+
while n > 0:
43+
n -= 1
44+
if n == 2:
45+
break
46+
print(n)
47+
print('Loop ended.')
48+
49+
#WHILE CONTINUE
50+
n = 5
51+
while n > 0:
52+
n -= 1
53+
if n == 2:
54+
continue
55+
print(n)
56+
print('Loop ended.')
57+
58+
#WHILE ELSE
59+
n = 5
60+
while n > 0:
61+
n -= 1
62+
print(n)
63+
if n == 2:
64+
break
65+
else:
66+
print('Loop done.')
67+
68+
69+
#ONELINE WHILE
70+
n =5
71+
while n > 0: n -= 1; print(n)
72+
73+
74+
#INTERACTIVE LOOPS
75+
#WHILE
76+
lines = list()
77+
testAnswer = input('Press y if you want to enter more lines: ')
78+
while testAnswer == 'y':
79+
line = input('Next line: ')
80+
lines.append(line)
81+
testAnswer = input('Press y if you want to enter more lines: ')
82+
83+
print('Your lines were:')
84+
for line in lines:
85+
print(line)
86+
87+
#FOR
88+
lines = list()
89+
n = int(input('How many lines do you want to enter? '))
90+
for i in range(n):
91+
line = input('Next line: ')
92+
lines.append(line)
93+
94+
print('Your lines were:') # check now
95+
for line in lines:
96+
print(line)

pythonclassdefinition.txt

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#CLASSES
2+
#constructor declaration
3+
def __init__(self):
4+
# body of the constructor
5+
6+
#self constructors - definition & declaration
7+
class GeekforGeeks:
8+
geek = ""
9+
10+
# default constructor
11+
def __init__(self):
12+
self.geek = "GeekforGeeks"
13+
14+
# a method for printing data members
15+
def print_Geek(self):
16+
print(self.geek)
17+
18+
19+
# creating object of the class
20+
obj = GeekforGeeks()
21+
22+
# calling the instance method using the object obj
23+
obj.print_Geek()
24+
25+
#Parameterized Constructors
26+
##PARAMETER PASSING
27+
lass Addition:
28+
first = 0
29+
second = 0
30+
answer = 0
31+
32+
# parameterized constructor
33+
def __init__(self, f, s):
34+
self.first = f
35+
self.second = s
36+
37+
def display(self):
38+
print("First number = " + str(self.first))
39+
print("Second number = " + str(self.second))
40+
print("Addition of two numbers = " + str(self.answer))
41+
42+
def calculate(self):
43+
self.answer = self.first + self.second
44+
45+
# creating object of the class
46+
# this will invoke parameterized constructor
47+
obj = Addition(1000, 2000)
48+
49+
# perform Addition
50+
obj.calculate()
51+
52+
# display result
53+
obj.display()
54+
55+
#CLASS
56+
class Shark:
57+
def swim(self):
58+
print("The shark is swimming.")
59+
60+
def be_awesome(self):
61+
print("The shark is being awesome.")
62+
63+
64+
def main():
65+
sammy = Shark()
66+
sammy.swim()
67+
sammy.be_awesome()
68+
69+
if __name__ == "__main__":
70+
main()
71+
72+
73+
#Definition1
74+
class Shark:
75+
def __init__(self, name):
76+
self.name = name
77+
78+
def swim(self):
79+
# Reference the name
80+
print(self.name + " is swimming.")
81+
82+
def be_awesome(self):
83+
# Reference the name
84+
print(self.name + " is being awesome.")
85+
86+
87+
88+
#Definition2 - positional argument
89+
class Shark:
90+
def __init__(self, name):
91+
self.name = name
92+
93+
def swim(self):
94+
print(self.name + " is swimming.")
95+
96+
def be_awesome(self):
97+
print(self.name + " is being awesome.")
98+
99+
100+
def main():
101+
# Set name of Shark object
102+
sammy = Shark("Sammy")
103+
sammy.swim()
104+
sammy.be_awesome()
105+
106+
if __name__ == "__main__":
107+
main()
108+
109+
#Definition3 - class instantiation
110+
class Shark:
111+
def __init__(self, name):
112+
self.name = name
113+
114+
def swim(self):
115+
print(self.name + " is swimming.")
116+
117+
def be_awesome(self):
118+
print(self.name + " is being awesome.")
119+
120+
def main():
121+
sammy = Shark("Sammy")
122+
sammy.be_awesome()
123+
stevie = Shark("Stevie")
124+
stevie.swim()
125+
126+
if __name__ == "__main__":
127+
main()
128+

pythonfunctiondefinition.txt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
##FUNCTIONS
2+
3+
def sumProblem(x, y):
4+
sum = x + y
5+
sentence = 'The sum of {} and {} is {}.'.format(x, y, sum)
6+
print(sentence)
7+
8+
def main():
9+
sumProblem(2, 3)
10+
sumProblem(1234567890123, 535790269358)
11+
a = int(input("Enter an integer: "))
12+
b = int(input("Enter another integer: "))
13+
sumProblem(a, b)
14+
15+
main()
16+
17+
#STRING FUNCTION
18+
def lastFirst(firstName, lastName):
19+
separator = ', '
20+
result = lastName + separator + firstName
21+
return result
22+
23+
print(lastFirst('Benjamin', 'Franklin'))
24+
print(lastFirst('Andrew', 'Harrington'))
25+
26+
#SCOPE _ LOCAL
27+
def main():
28+
x = 3
29+
f()
30+
31+
def f():
32+
print(x) # error: f does not know about the x defined in main
33+
34+
main()
35+
36+
#CORRECTING BAD SCOPE
37+
def main():
38+
x = 3
39+
f(x)
40+
41+
def f(x):
42+
print(x)
43+
44+
main()
45+
46+
#GLOBAL CONSTANTS
47+
PI = 3.14159265358979 # global constant -- only place the value of PI is set
48+
49+
def circleArea(radius):
50+
return PI*radius*radius # use value of global constant PI
51+
52+
def circleCircumference(radius):
53+
return 2*PI*radius # use value of global constant PI
54+
55+
def main():
56+
print('circle area with radius 5:', circleArea(5))
57+
print('circumference with radius 5:', circleCircumference(5))
58+
59+
main()

pythonsetcommands.txt

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
thisset = {"apple", "banana", "cherry"}
2+
print(thisset)
3+
4+
5+
thisset = {"apple", "banana", "cherry"}
6+
7+
for x in thisset:
8+
print(x)
9+
10+
11+
thisset = {"apple", "banana", "cherry"}
12+
13+
print("banana" in thisset)
14+
15+
16+
17+
thisset = {"apple", "banana", "cherry"}
18+
19+
thisset.add("orange")
20+
21+
print(thisset)
22+
23+
24+
thisset = {"apple", "banana", "cherry"}
25+
26+
thisset.update(["orange", "mango", "grapes"])
27+
28+
print(thisset)
29+
30+
31+
thisset = {"apple", "banana", "cherry"}
32+
33+
print(len(thisset))
34+
35+
36+
thisset = {"apple", "banana", "cherry"}
37+
38+
thisset.remove("banana")
39+
40+
print(thisset)
41+
42+
43+
thisset = {"apple", "banana", "cherry"}
44+
45+
thisset.discard("banana")
46+
47+
print(thisset)
48+
49+
50+
51+
52+
53+
thisset = {"apple", "banana", "cherry"}
54+
55+
x = thisset.pop()
56+
57+
print(x)
58+
59+
print(thisset)
60+
61+
62+
63+
thisset = {"apple", "banana", "cherry"}
64+
65+
thisset.clear()
66+
67+
print(thisset)
68+
69+
70+
71+
thisset = {"apple", "banana", "cherry"}
72+
73+
del thisset
74+
75+
print(thisset)
76+
77+
78+
79+
thisset = set(("apple", "banana", "cherry")) # note the double round-brackets
80+
print(thisset)

0 commit comments

Comments
 (0)