Skip to content

Commit 47c53c3

Browse files
committed
Python - Zero to Hero
1 parent d8c74fd commit 47c53c3

25 files changed

+497
-0
lines changed

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
11
"# python-learnings"
2+
3+
Errors :
4+
ValueError
5+
NameError
6+
FileNotFoundError
7+
8+
9+
Static Code Analysis:
10+
pylint myexample.py -r y

TestCap.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unittest
2+
import cap
3+
4+
class Testcap(unittest.TestCase):
5+
6+
def test_one_word(self):
7+
inp = 'python'
8+
res = cap.cap_text(inp)
9+
self.assertEqual(res, 'Python')
10+
11+
def test_multiple_words(self):
12+
inp = 'welcome to python'
13+
res = cap.cap_text(inp)
14+
self.assertEqual(res, 'Welcome To Python')
15+
16+
if __name__ == '__main__':
17+
unittest.main()

__pycache__/cap.cpython-312.pyc

474 Bytes
Binary file not shown.

arithmeticex.py

+11
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,14 @@
3434
age = input("Enter age: ")
3535
print("Your name is "+ name + " of age "+ age)
3636

37+
a=10
38+
print(type(a))
39+
40+
a=10.2
41+
print(type(a))
42+
43+
my_income=100
44+
tax_rate=0.1
45+
my_taxes= my_income * tax_rate
46+
print(my_taxes)
47+

booleanex.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
True
2+
False
3+
print(True)
4+
print(type(True))
5+
#print(true) NameError: name 'true' is not defined. Did you mean: 'True'?
6+
7+
print(2 > 1)
8+
print(1 == 1)
9+
print(2 < 1)
10+
11+
print("*"*21)
12+
a=None
13+
print(a)

cap.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def cap_text(input : str):
2+
return input.title()
3+
4+
print("hi".capitalize())
5+
print("monty hi".capitalize())

comparisionopex.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
print(2 == 2)
2+
print(2 == 3)
3+
print(2 == 2.0)
4+
print('*'*21)
5+
print(2 != 2)
6+
print(2 != 3)
7+
print('*'*21)
8+
print(3>2)
9+
print(1>2)
10+
print(2>2)
11+
print('*'*21)
12+
print(3<2)
13+
print(1<2)
14+
print(2<2)
15+
print('*'*21)
16+
print(3>=2)
17+
print(1>=2)
18+
print(2>=2)
19+
print('*'*21)
20+
print(3<=2)
21+
print(1<=2)
22+
print(2<=2)
23+
print('*'*21)
24+
25+
print( 1==1 and 2==2)
26+
print( 1==1 and 1==2)
27+
print( 1==2 and 2==2)
28+
print (1==2 and 2==1)
29+
print('*'*21)
30+
print( 1==1 or 2==2)
31+
print( 1==1 or 1==2)
32+
print( 1==2 or 2==2)
33+
print (1==2 or 2==1)
34+
print('*'*21)
35+
print(not 1==1)
36+
print(not 1==2)

continuebreakpassex.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
nums = [1,2,3,4]
2+
3+
for num in nums:
4+
pass #Do nothing
5+
6+
for num in nums:
7+
if num==2:
8+
continue
9+
print(num)
10+
11+
word = "Hello World"
12+
for letter in word:
13+
if letter == 'l':
14+
continue
15+
print(letter)
16+
17+
print("*"*21)
18+
word = "Hello World"
19+
for letter in word:
20+
if letter == 'l':
21+
break
22+
print(letter)
23+
24+
x=0
25+
while x < 100 :
26+
if x ==4:
27+
break
28+
print(x)
29+
x +=1

dictionaryex2.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
my_dict = {'key1':'value1', 'key2':'value2'}
2+
print(my_dict)
3+
print(my_dict["key1"])
4+
print(my_dict["key2"])
5+
6+
my_dict = {'key1' : 'value1' , 'letterlist' : ['a','b','c'], 'nesteddict' : {'apple': 2.99, 'banana' : 1.99, 'milk':3.99}}
7+
letter = 'c'
8+
print(letter.upper())
9+
print(my_dict['letterlist'][2].upper())
10+
print(my_dict['nesteddict']['apple'])
11+
print(my_dict['nesteddict']['milk'])
12+
13+
dic = {'k1':100, 'k2':200}
14+
dic['k3'] = 300
15+
print(dic)
16+
dic['k2'] = 2000
17+
print(dic)
18+
19+
print(dic.keys())
20+
print(dic.values())
21+
print(dic.items())
22+

forex2.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
nums = [1,2,3,4,5,6,7,8,9,10]
2+
3+
for num in nums:
4+
if num%2==0:
5+
print(num)
6+
else:
7+
print(f"odd num:{num}")
8+
9+
print("*"*21)
10+
list_sum=0
11+
for num in nums:
12+
list_sum = list_sum + num
13+
print(list_sum)
14+
15+
print("*"*21)
16+
word="Hello World"
17+
for letter in word:
18+
print(letter)
19+
20+
print("*"*21)
21+
tupnum = (1,2,3)
22+
for num in tupnum:
23+
print(num)
24+
print("*"*21)
25+
##tuple unpacking
26+
my_list = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
27+
for a, b, c in my_list:
28+
print(a)
29+
print(b)
30+
print(c)
31+
32+
print("*"*21)
33+
my_dict= {'k1':1, 'k2':2, 'k3':3}
34+
for item in my_dict:
35+
print(item)
36+
for item in my_dict.items():
37+
print(item)
38+
for key,value in my_dict.items():
39+
print(key)
40+
print(value)
41+
for value in my_dict.values():
42+
print(value)

ifelifelseex.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
hungry=False
2+
if hungry:
3+
print("Dog is hungry")
4+
else:
5+
print("Dog is not hungry")
6+
7+
name="John"
8+
if name == "Naga":
9+
print("Naga is a programmer")
10+
elif name == "Ash":
11+
print("Ash is an artist")
12+
elif name == "Dhoni":
13+
print("Dhoni is a cricketer")
14+
else:
15+
print("Who are you?")

iofileex.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
my_file = open("myfile.txt")
2+
#open("unknownfile.txt") #FileNotFoundError: [Errno 2] No such file or directory: 'unknownfile.txt'
3+
4+
print(my_file.read())
5+
print("*"*21)
6+
print(my_file.read())
7+
print("*"*21)
8+
my_file.seek(0)
9+
print(my_file.read())
10+
11+
my_file.seek(0)
12+
contents = my_file.read()
13+
print(contents)
14+
print("*"*21)
15+
16+
my_file.seek(0)
17+
18+
print(my_file.readlines())
19+
my_file.close()
20+
print("*"*21)
21+
22+
with open('myfile.txt') as my_file2:
23+
content2 = my_file2.readlines()
24+
25+
print(content2)
26+
27+
with open('myfile.txt', mode='r') as f:
28+
print(f.readlines())
29+
30+
with open('myfile2.txt', mode='w') as f:
31+
print(f.write("New file got created"))
32+
33+
with open('myfile2.txt', mode='a') as f:
34+
print(f.write("\n2nd line"))
35+
print(f.write("\n3rd line"))
36+
37+
print("*"*21)
38+
with open('myfile2.txt', mode='r+') as f:
39+
print(f.readlines())
40+
41+
42+

listex2.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
my_list = [1,2,3]
2+
print(my_list)
3+
4+
my_list = ["one", 2, 3.0, False]
5+
print(my_list)
6+
7+
my_list = [1, 2, 3]
8+
9+
another_list = [4, 5, 6]
10+
11+
#Combine lists / concatenate lists
12+
comb_list = my_list + another_list
13+
print(comb_list)
14+
15+
#indexing
16+
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
17+
print(my_list[0])
18+
print(my_list[1])
19+
20+
#slicing
21+
print(my_list[2:7])
22+
print(my_list[2:7:2])
23+
24+
#mutate list
25+
my_list[0] = 99
26+
print(my_list)
27+
28+
#len
29+
print(len(my_list))
30+
31+
my_list.append(10)
32+
my_list.append(11)
33+
print(my_list)
34+
35+
popped_item = my_list.pop()
36+
print(popped_item)
37+
print(my_list)
38+
39+
popped_item = my_list.pop(0)
40+
print(popped_item)
41+
print(my_list)
42+
43+
#sort() - modifies list inplace. No return type
44+
my_list = ["a", "e", "b", "x", "c"]
45+
#sorted_list = my_list.sort() #Wrong
46+
my_list.sort()
47+
print(my_list)
48+
print("*"*21)
49+
50+
num_list = [10, 2, 99, 1, 5, 3, 0, 104]
51+
print(num_list)
52+
num_list.sort()
53+
print(num_list)
54+
55+
#reverse() - reverses the list inplace. No return type
56+
num_list.reverse()
57+
print(num_list)

myfile.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This is my file.
2+
This is second line.
3+
Oh we are already on Line 3.

myfile2.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
New file got created
2+
2nd line
3+
3rd line

mysample.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'''
2+
Sample Python Script
3+
'''
4+
5+
'''
6+
Sample function
7+
'''
8+
def my_func():
9+
num1=1
10+
num2=2
11+
print(num1)
12+
print(num2)
13+
14+
my_func()

printformatex.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
print("World {} {}".format("is","beautiful"))
2+
print("world {0} {1} .{2} {3}".format("is","beautiful","go","live"))
3+
print("{name} {i} {b}.{g} {l}".format(name="world", i="is", g="go", l="live", b="beautiful"))
4+
5+
#Float Formatting
6+
rval=0.125364792
7+
print("r value is {r:1.2f}".format(r=rval))
8+
9+
rval = 100.29083121212
10+
print("r val is {r:1.2f}".format(r=rval))
11+
12+
#Formatted String Literals
13+
hero="Krishna"
14+
villain="Shakuni"
15+
print(f"Mahabharata was driven by {hero} and {villain}")
16+
17+
nation="India"
18+
year="2011"
19+
print(f"Team {nation} won world cup in {year}")

setex.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
my_set = set()
2+
print(my_set)
3+
print(type(my_set))
4+
5+
my_set.add(1)
6+
my_set.add("Hi")
7+
my_set.add(2)
8+
print(my_set)
9+
my_set.add(2)
10+
print(my_set)
11+
12+
my_list = [1,2,1,1,1,1,1,2,2,2,2,3,3,3,3,3]
13+
my_set = set(my_list)
14+
print(my_set)
15+
16+
print(set("mississippi"))

0 commit comments

Comments
 (0)