Skip to content

Commit 3c53902

Browse files
KranthiKranthi
authored andcommitted
change the path of file name and refactor
1 parent ef620eb commit 3c53902

7 files changed

Lines changed: 146 additions & 1 deletion

File tree

‎dictionaries.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# dictionary is a collection of key value pairs.
2+
keyValuePair = {'color': 'green', 'place': 'Chicago'}
3+
print('{}{}'.format('color is::', keyValuePair['color']))
4+
# Changing the value of dictionary key
5+
keyValuePair['place'] = 'Orlando'
6+
print('{}{}'.format('modified place is::', keyValuePair['place']))
7+
8+
# Remove a keyValue pair
9+
del keyValuePair['place']
10+
print('{}{}'.format('modified place is::', keyValuePair))
11+
12+
# get a value if not exist then substitue with a default value
13+
get = keyValuePair.get('place', 'Chicago')
14+
print('{}{}'.format('new key value pair is::', get))
15+
16+
# set a value
17+
keyValuePair.setdefault('place', 'Orlando')
18+
print('{}{}'.format('setting a new key value pair is::', keyValuePair))

‎files.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
fileHandle = open('files/text.file')
2+
3+
# To iterate thru the sequence of lines in the file use a for statement.
4+
# The print statement adds a new line to each line to remove the extra space use rstrip()
5+
# new line is considered as extra white space and is stripped.
6+
7+
for line in fileHandle:
8+
print('{}{}'.format('file contents are::', line.rstrip()))
9+
10+
11+
# Reading the whole file using the length of the file
12+
fileHandle = open('files/text.file')
13+
readLine = fileHandle.read()
14+
print('length of the file:{}'.format(len(readLine)))
15+
print('{}{}'.format('readLine::', readLine[:71]))

‎files/text.file‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hello this is a text file
2+
Welcome to python and this is the second line

‎files2.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fname = input('Enter file name')
2+
try:
3+
fHandle = open(fname)
4+
except:
5+
print('The file can\'t be opened')
6+
quit()

‎isandisnot.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# is and is not operators
2+
# Use is and is not on None or boolean operators but not floating part numbers , integer numbers
3+
found = False
4+
yes = found is True
5+
print("{}{}".format("is Found is::", yes))
6+
7+
# None keyword is used to define null or an object. None keyword is an object and it's a datatype of class NoneType
8+
# Comparing None with any types like empty string, or 0 always returns False
9+
found = ""
10+
if found is None:
11+
print("{}{}".format("is Found is::", found))

‎loops.py‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,49 @@
1616
break
1717
'''
1818

19+
# for loop with numbers
20+
'''for i in [1, 10, 78, 2, 5]:
21+
print('{}{}'.format("current number is::", i))
22+
23+
# for loop with largest numbers.
24+
i = [9, 41, 12, 3, 74, 15]
25+
maxNumber = max(i)
26+
print("{}{}".format("max number is:", maxNumber))'''
27+
28+
29+
# largest so far by looping.
30+
i = [9, 41, 12, 3, 74, 15]
31+
largestSoFar = 0
32+
for number in i:
33+
if number > largestSoFar:
34+
largestSoFar = number
35+
print("{}{}".format("largest so far is::", largestSoFar))
36+
37+
38+
# finding a number with boolean variable
39+
i = [9, 41, 12, 3, 74, 15]
40+
found = False
41+
foundNumber = 0
42+
for number in i:
43+
if number == 15:
44+
found = True
45+
foundNumber = number
46+
break
47+
print("{}{}".format("Number found and its::", number))
48+
49+
50+
# for loop with Strings.
51+
'''
52+
listOfStrings = ['Joseph', 'Sam', 'Kranty', 'Riddi']
53+
for names in listOfStrings:
54+
print("{}{}".format("name is::", names))'''
55+
56+
57+
58+
59+
1960
# Loop with continue
61+
'''
2062
while True:
2163
text = input('Enter text')
2264
if text == 'exit':
@@ -25,4 +67,4 @@
2567
if text == 'break':
2668
print('To top of the loop')
2769
continue
28-
print("done !!")
70+
print("done !!")'''

‎strings.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# length of the string.
2+
name = 'Peter'
3+
print("{}{}".format("Length of the String::", (len(name))))
4+
5+
# Slicing Strings
6+
name = 'Welcome to Python'
7+
# start at index 0 and end before index 4. which is exclusive
8+
print(name[0:4])
9+
10+
# start at index 8 and till end of the String
11+
print(name[8:])
12+
13+
# Prints entire string
14+
print(name[:])
15+
16+
# Logical operator - The in keyword to check if one String is "in" another string
17+
name = 'Welcome'
18+
if 'come' in name:
19+
print('come exists in name')
20+
21+
# == to compare strings
22+
word = 'Hi'
23+
if word == 'hello' or word == 'hi':
24+
print('This is a hello or hi')
25+
26+
if word < 'hello':
27+
print('word is < hello')
28+
else:
29+
print('word is > hello')
30+
31+
# lower case, upper case
32+
print("{}{}".format("lower case ::", word.lower()))
33+
print("{}{}".format("lower case ::", word.upper()))
34+
35+
# find
36+
fruit = "apple"
37+
fruit.find('ap')
38+
39+
# replace() is like a search and replace operation in a word.
40+
name = " Hello Krantz "
41+
modifiedName = name.replace('Hello', 'Welcome')
42+
print("{}{}".format("name is::", modifiedName))
43+
44+
45+
# lstrip() and rstrip() removes the left or right whitespace
46+
# strip() removes both beggining and ending whitespace
47+
48+
# Prefixes
49+
line = "Have a great day !!"
50+
if line.startswith('Have'):
51+
print(' line starts with Have')

0 commit comments

Comments
 (0)