Skip to content

Commit b309668

Browse files
authored
Merge pull request #12 from the-code-experiments/develop
Develop
2 parents 747aa63 + 95c7d8b commit b309668

File tree

13 files changed

+137
-0
lines changed

13 files changed

+137
-0
lines changed

codes/session_4/inputOutput.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Open terminal > python3 inputOutput.py
2+
# Start typing below commands and see the output
3+
4+
x = 10 * 45
5+
y = 20 * 90
6+
7+
s = 'The value of x is ' + repr(x) + ' , and y is ' + repr(y)
8+
9+
print(s)
10+
11+
for a in range(1, 10):
12+
print(repr(a).rjust(2), repr(a*a).rjust(3), end=' ')
13+
print(repr(a*a*a).rjust(4))
14+
15+
print('\n\n')
16+
17+
for b in range(1, 10):
18+
print('{0:2d} {1:3d} {2:4d}'.format(b, b*b, b*b*b))
19+
20+
print('\n\n')
21+
22+
import math
23+
print('The value of PI is approximately %5.3f' % math.pi)
24+
25+
print('I love working on {} as well as {}!'.format('JavaScript', 'Python'))
26+
27+
print('I love working on {0} as well as {1}!'.format('JavaScript', 'Python'))
28+
29+
print('I love working on {1} as well as {0}!'.format('JavaScript', 'Python'))
30+
31+
print('My name is {name}, and I\'am a {job}'.format(name='Ashwin', job='Software Engineer'))
32+
33+
print('My name is {name}, and I love working in {0}, and {1}'.format('JavaScript', 'Python', name='Ashwin'))
34+
35+
table = {'Ashwin': 'Engineer', 'Saju': 'Architect', 'Ajay': 'Manager'}
36+
37+
for name, role in table.items():
38+
print('{0:10} -> {1:10s}'.format(name, role))

codes/session_5/hello.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Hello World!
2+
My name is Ashwin Hegde, I work as a Software Engineer
3+
My primary language is JavaScript and Secondary language is Python
4+
Python is needed for scripting, Machine Learning and Quantum Programming

codes/session_5/read.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Open terminal > python3 read.py
2+
# Start typing below commands and see the output
3+
4+
with open('hello.txt') as f:
5+
data = f.read()
6+
print(data)
7+
f.closed

codes/session_5/readline.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Open terminal > python3 readline.py
2+
# Start typing below commands and see the output
3+
4+
f = open('hello.txt', 'r')
5+
print(f.readline())
6+
print(f.readline())
7+
print(f.readline())
8+

codes/session_5/sample.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello World! My name is Ashwin

codes/session_5/seekTell.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Open terminal > python3 seekTell.py
2+
# Start typing below commands and see the output
3+
4+
f = open('sample.txt', 'r')
5+
6+
f.seek(8, 0) # 0 mean beginning of the file
7+
print(f.read())
8+
9+
print('\n\n')
10+
11+
# f.seek(8, 1) # 1 mean current position
12+
# print(f.read())
13+
14+
# print('\n\n')
15+
16+
# f.seek(-3, 2) # 2 mean end of the file
17+
# print(f.read())
18+

codes/session_5/write.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Open terminal > python3 write.py
2+
# Start typing below commands and see the output
3+
4+
fw = open('hello.txt', 'a')
5+
totalWrittenChar = fw.write('\nPython is needed for scripting, Machine Learning and Quantum Programming')
6+
print('Total written characters are ' + repr(totalWrittenChar))
7+
fw.close()
8+
9+
with open('hello.txt') as fr:
10+
data = fr.read()
11+
print(data)
12+
fr.close()

codes/session_6/errorEg1.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Open terminal > python3 errorEg1.py
2+
# Start typing below commands and see the output
3+
4+
while True:
5+
try:
6+
x = int(input('Please enter a number: '))
7+
break
8+
except ValueError:
9+
print('Invalid number, please try again')

codes/session_6/errorEg2.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Open terminal > python3 errorEg2.py
2+
# Start typing below commands and see the output
3+
4+
import sys
5+
6+
for arg in sys.argv[1:]:
7+
try:
8+
f = open(arg, 'r')
9+
print(f.read())
10+
except OSError:
11+
print('[OS error]: ', arg)
12+
else:
13+
print(arg, 'has', len(f.readline()), 'lines')
14+
f.close()

codes/session_6/errorEg3.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def thisWillFail():
2+
x = 1 / 0
3+
4+
try:
5+
thisWillFail()
6+
except ZeroDivisionError as err:
7+
print('Handling runtime error: ', err)

0 commit comments

Comments
 (0)