00 01 Python Course Guide PDF
00 01 Python Course Guide PDF
m m in g w ith
Progra
Pytho n St ep by
Step
Table of Contents
1. Congratulations
2. About in28Minutes
3. Installation Guide
4. Getting Started
7. Introduction To PyCharm
on APIs, Web Services and Microservices with Spring, Spring Boot and
Spring Cloud.
About in28Minutes
What's that?
You need to get insight into the in28Minutes world to answer that.
What do we love?
Hands-on
Step By Step
Code on Github
Interview Guides
Building a connect
Socially Conscious
We love what we do
Installation Guide
Installing Python 3
https://www.python.org/downloads/
Caution
On Windows - ensure that the check box "Add Python 3.6 to PATH" is
Checked
Launching Python 3 Shell
Launch Terminal or Command Prompt
Command
python3 in Mac
python in Windows and Linux.
https://www.jetbrains.com/pycharm/download/
Click Download
First Launch
Recommended Versions
Python Python 3
Github Page :
https://github.com/in28minutes/learn-programming-with-python-
Introduction To Python
Programming With
Multiplication Table
information.
>>> 5 X
5
File "<stdin>", line 1
5 X 5
^
SyntaxError: invalid syntax
>>> 5 * 6
30
>>> 5 + 6
11
>>> 5 - 6
-1
>>> 10 / 2
5.0
>>> 10 ** 3
1000
>>> 5 + 5 + 5
15
>>> 5 + 5 * 5
30
>>> import os
>>> os.system('clear')
0
>>> 24 * 60
1440
>>> 24 * 60 * 60
86400
>>> os.system('clear')
0
>>> 5 + 6 + 10
21 >>> 5 *$
2
File "<stdin>", line 1
5 *$ 2
^
>>> 5 * 6
30
>>> 5 * 6 = 30
File "<stdin>", line 1
>>> 5 * 6
30
>>> Hello
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Hello' is not defined
0
>>> print("5 * 6 = 30")
5 * 6 = 30
>>> print("5*6")
5*6
>>> print(5*6)
30
>>> print('5*6')
5*6 >>> abs 10.5
File "<stdin>", line 1
abs
10.5
^
SyntaxError: invalid syntax
>>> abs(10.5)
10.5
>>> abs("10.5")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: bad operand type for abs(): 'str'
>>> pow 2 5
File "<stdin>", line 1
pow 2 5
^
SyntaxError: invalid syntax
>>> pow(2 5)
File "<stdin>", line 1
pow(2 5)
^
SyntaxError: invalid syntax
>>> pow(2, 5)
32
>>> pow(10, 3)
1000
>>> 10 ** 3
1000
>>> max(34, 45, 67)
67
>>> min(34, 45, 67)
34
>>> pow(2,5) 32
>>> Pow(2,5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Hello
>>> print("hello")
hello
>>> print("hellO")
hellO
>>> print ( "hellO" )
hellO
>>> print ( "hellO World" )
hellO World
>>> print ( "hellO World" )
hellO World
>>> print("Hello"")
File "<stdin>", line 1
print("Hello"")
^
SyntaxError: EOL while scanning string literal
>>> print("Hello\"")
Hello"
>>> print("Hello\nWorld")
Hello
World
>>> print("Hello\tWorld")
Hello World
>>> print("Hello\\World")
Hello\World
>>> print("Hello\\\\\\World")
Hello\\\World >>> print('Hello"')
Hello"World
>>> print("Hello\"World")
Hello"World
>>> os.system('clear')
0
>>> print("5 * 6 = 30")
5 * 6 = 30
>>> print("VALUE".format(5*2))
VALUE
>>> print("VALUE {0}".format(5*2))
VALUE 10
>>> print("VALUE {0}".format(10,20,30))
VALUE 10
>>> print("VALUE {1}".format(10,20,30))
VALUE 20
>>> print("VALUE {2}".format(10,20,30))
VALUE 30
>>> print("5 * 6 = {2}".format(5,6,5*6))
5 * 6 = 30
>>> print("{0} * {1} = {2}".format(5,6,5*6))
5 * 6 = 30
>>> print("{0} * {1} = {2}".format(5,7,5*7))
5 * 7 = 35
>>> print("{0} * {1} = {2}".format(5,8,5*8))
5 * 8 = 40
>>> print("{0} * {1} = {2}".format(5,8,5*8))
5 * 8 = 40
>>> print("{0} * {1} = {2}".format(5,8,5*8,5*9,5*10))
5 * 8 = 40 >>> print("{0} * {1} =
{4}".format(5,8,5*8,5*9,5*10))
5 * 8 = 50
0
>>> print("{0} * {1} = {2}".format(5,7,5*7))
5 * 7 = 35
>>> print("{0} * {1} = {2}".format(5,1,5*1))
5 * 1 = 5
>>> print("{0} * {1} = {2}".format(5,2,5*2))
5 * 2 = 10
>>> print("{0} * {1} = {2}".format(5,3,5*3))
5 * 3 = 15
>>> print("{0} * {1} = {2}".format(5,index,5*index))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'index' is not defined
>>> index = 2
>>> print("{0} * {1} = {2}".format(5,index,5*index))
5 * 2 = 10
>>> index = 3
>>> print("{0} * {1} = {2}".format(5,index,5*index))
5 * 3 = 15
>>> index
3
>>> print("{0} * {1} = {2}".format(5,index,5*index)) 5 * 3
= 15
>>> index = 5
>>> print("{0} * {1} = {2}".format(5,index,5*index)) 5 * 5
= 25
>>> index = 1
>>> print("{0} * {1} = {2}".format(5,index,5*index))
5 * 1 = 5
>>> index = 2
>>> print("{0} * {1} = {2}".format(5,index,5*index))
5 * 2 = 10
>>> index = 3
>>> print("{0} * {1} = {2}".format(5,index,5*index))
5 * 3 = 15
>>> a = 5
>>> b = 6
>>> c = 7
>>> print("5 + 6 + 7 = 18")
5 + 6 + 7 = 18
>>> print("5 + 6 + 7 = 18".format(a,b,c,a+b+c))
5 + 6 + 7 = 18
>>> print("{0} + {1} + {2} = {3}".format(a,b,c,a+b+c))
5 + 6 + 7 = 18
>>> a = 6
>>> b = 7
>>> c = 8
>>> print("{0} + {1} + {2} = {3}".format(a,b,c,a+b+c))
6 + 7 + 8 = 21
>>> os.system('clear')
0
>>> i = 1
>>> i
1 >>> print(i*2)
2
>>> i = 4 >>> print(i*2)
8
>>> count
last):
File "<stdin>", line 1, in <module>
NameError: name 'count' is not defined
>>> print(count)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'count' is not defined
>>> count = 4
>>> print(count)
4
>>> Count
2count
^
SyntaxError: invalid syntax
>>> c12345 = 5
>>> os.system('clear')
0
>>> i = 5
>>> j = i
>>> j
5
>>> j = 2 * i
>>> j
10
>>> j = i
>>> j = 2 * i
>>> j = 3 * i
>>> j
15
>>> 5 = j
File "<stdin>", line 1
SyntaxError: can't assign to literal
>>> j = 10
>>> j
10
>>> num1 = 5 >>> num2 = 3
>>> sum = num1 + num2
>>> sum 8
>>> a = 5
>>> b = 6
>>> c = 7
>>> sum = a + b + c
>>> sum 18 >>> print("5 + 6 + 7 = 18")
5 + 6 + 7 = 18
>>> print("{0} + {1} + {2} = {3}", a, b, c ,sum)
{0} + {1} + {2} = {3} 5 6 7 18
>>> print("{0} + {1} + {2} = {3}".format(a, b, c ,sum))
5 + 6 + 7 = 18
>>> num1
5
>>> num1 = 10
>>> num1
10
>>> number_1
Traceback (most recent call
last):
File "<stdin>", line 1, in <module>
NameError: name 'number_1' is not defined
>>> number_1 = 15
>>> number_1
15
>>> os.system('clear')
0
>>> a = 1
>>> b = 2
>>> c = 3
>>> sum = a + b + c
>>> print("{0} + {1} + {2} = {3}".format(a, b, c ,sum))
1 + 2 + 3 = 6 >>> print(f"")
>>> print(f"value of a is {a}")
value of a is 1
>>> print(f"value of b is {b}")
value of b is 2 >>> print(f"sum of a and b is {a + b}")
sum of a and b is 3 >>> print(f"{a} + {b} + {c} = {sum}")
1 + 2 + 3 = 6
>>> os.system('clear')
0
>>> index = 1
>>> print("{0} * {1} = {2}".format(5,index,5*index))
5 * 1 = 5
>>> index = 2
>>> print("{0} * {1} = {2}".format(5,index,5*index))
5 * 2 = 10
>>> index = 3
>>> print("{0} * {1} = {2}".format(5,index,5*index))
5 * 3 = 15
>>> index = 4
>>> print("{0} * {1} = {2}".format(5,index,5*index))
5 * 4 = 20
>>> index = index + 1
>>> print("{0} * {1} = {2}".format(5,index,5*index))
5 * 5 = 25
>>> index = index + 1
>>> print("{0} * {1} = {2}".format(5,index,5*index))
5 * 6 = 30
>>> index = index + 1
0
>>> range(1,10) range(1, 10)
>>> for i in range(1,10): ... print(i)
... 1
2
3 4
5
6
7
8
9
>>> print("{0} * {1} = {2}".format(5,index,5*index))
5 * 7 = 35
>>> print(f"{5} * {index} = {5*index}")
5 * 7 = 35
>>> for i in range(1,11):
... print(f"{i}")
...
1
2
3
4
5
6
7
8
9
10
>>> for i in range(1,11):
... print(f"5 * {i}")
...
5 * 1
5 * 2
5 * 3
5 * 4 5 * 5
5 * 6 5 * 7 5 * 8
5 * 9
5 * 10 >>> for i in range(1,11):
... print(f"5 * {i} = {5 * i}")
...
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
>>> 5 * 4 * 50
1000
>>> os.system('clear')
0
>>> for i in range(1,10):
... print(i)
...
1
2
3
4
5
6
7
8
9
>>> for i in range(1,10)
File "<stdin>", line 1
for i in
range(1,10)
^
SyntaxError: invalid syntax >>> for i in range(1,10):
... print(i)
File "<stdin>", line 2
print(i)
^
6
7
8
9
>>> for i in range(1,10):
... print(i)
... print(2*i)
...
1
2
2
4
3
6
4
8 5 10
6
12 7
14 8
16
9
18
>>> for i in range(2,5): print(i)
...
2
3
4
>>> for i in range(2,5):
... print(i)
...
2
3
4
>>> for i in range(2,5):
... print(i)
...
2
3
4
>>> for i in range(1,11):
... print(i)
...
1
2
3
4
5 6
7
8 9
10
>>> for i in range (1,11,2): ... print(i) ...
1
3
5
7
9
>>> for i in range (2,11,2):
... print(i)
...
2
4
6
8
10
16
25
36 49 64
81
100
>>> for i in range (10,0,-1):
... print(i*i)
...
100
81
64
49
36
25
16
9
4
1
>>> for i in range (10,0,-2):
...
File "<stdin>", line 2
^
IndentationError: expected an indented block
>>> for i in range (10,0,-2):
... print(i*i)
...
100 64
36
16
4
>>> for i in range(1,11):
... print(f"5 * {i} = {5 * i}")
...
5 * 1 = 5
5 * 2 = 10 5 * 3 = 15
5 * 4 = 20 5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
>>> for i in range (1,11):
6 * 4 = 24
6 * 5 = 30
6 * 6 = 36
6 * 7 = 42
6 * 8 = 48
6 * 9 = 54
6 * 10 = 60
>>> for i in range (1,11):
... print(f"8 * {i} = {8 * i}")
...
8 * 1 = 8
8 * 2 = 16 8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80
>>>
Introduction To Methods -
MultiplicationTable
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
0
>>> print("Hello World")
Hello World
>>> print("Hello World")
Hello World
print_hello_world_twice()
Hello World
Hello World
>>> print_hello_world_twice()
Hello World
Hello World
>>> def print_hello_world_thrice():
... print("Hello World")
... print("Hello World")
... print("Hello World")
...
>>> print_hello_world_thrice()
Hello World
Hello World
Hello World
>>> def print_your_progress():
... print("Statement 1")
... print("Statement 2")
... print("Statement 3")
... print("Statement 4")
...
>>> print_your_progress()
Statement 1
Statement 2
Statement 3
Statement 4
>>> def print_your_progress():
0
>>> print_hello_world_twice()
Hello World
Hello World
>>> def print_hello_world_twice():
... print("Hello World")
... print("Hello World")
...
>>> os.system('clear')
0
>>> print_hello_world_twice()
Hello World
Hello World
>>> print_hello_world_thrice()
Hello World
Hello World
Hello World
>>> def print_hello_world(no_of_times):
... print("Hello World")
... print(no_of_times)
...
>>> print_hello_world()
Traceback (most recent call
last):
File "<stdin>", line 1, in <module>
>>> print_hello_world(5)
Hello World
5
100
>>> def print_hello_world(no_of_times):
Hello World
Hello World
Hello World
Hello World
Hello World
>>> def print_hello_world(no_of_times):
Hello World
Hello World
Hello World
Hello World
>>> def print_hello_world(no_of_times):
... for i in range(1,no_of_times+1):
... print("Hello World")
...
>>> print_hello_world(5)
Hello World
Hello World
Hello World
Hello World
Hello World
>>> print_hello_world(7)
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
>>> os.system('clear')
n+1)
^ SyntaxError: invalid syntax >>>
def print_numbers(n):
3
4
5
>>> def print_squares_of_numbers(n):
... for i in range(1, n+1):
... print(i*i)
...
>>> print_squares_of_numbers(5)
1
4
9
16
25
>>> def print_hello_world(no_of_times):
... for i in range(1,no_of_times+1):
... print("Hello World")
...
>>> def print_string(str, no_of_times):
... for i in range(1,no_of_times+1):
... print(str)
...
Hello World
Hello World
>>> print_string("Welcome to Python", 3)
Welcome to Python
Welcome to Python Welcome to Python >>>
print_string("Welcome to Python")
Welcome to Python
Welcome to Python
Welcome to Python
Welcome to Python
>>> def print_string(str="Hello World", no_of_times=5):
... for i in range(1,no_of_times+1):
... print(str)
...
>>> print_string()
Hello World
Hello World
Hello World
Hello World
Hello World
>>> print_string("Welcome to Python")
Welcome to Python
Welcome to Python
Welcome to Python
Welcome to Python
Welcome to Python
>>> print_string("Welcome to Python", 8)
Welcome to Python
Welcome to Python
0
>>> for i in range (1,11):
... print(f"8 * {i} = {8 * i}")
...
8 * 1 = 8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80
>>> for i in range (1,11):
... print(f"7 * {i} = {7 * i}")
...
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63 7 * 10 = 70
range(1,11)
File "<stdin>", line 2
for i in
range(1,11)
^
SyntaxError: invalid syntax
...
>>> print_multiplication_table(7)
table * 1 = 7
table * 2 = 14
table * 3 = 21
table * 4 = 28
table * 5 = 35
table * 6 = 42
table * 7 = 49
table * 8 = 56
table * 9 = 63
table * 10 = 70
>>> def print_multiplication_table(table):
... for i in range(1,11):
... print(f"{table} * {i} = {table * i}")
...
>>> print_multiplication_table(7)
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28 7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70
>>> def print_multiplication_table(table, start, end): ...
for i in range(start, end+1):
>>> print_multiplication_table(7, 1 , 6)
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70
>>> os.system('clear')
0
>>> def method_to_understand_indentation():
... print(i)
...
>>> method_to_understand_indentation()
1
2
3
4
6
7
8
9
10
>>> def method_to_understand_indentation():
... for i in range(1,11) :
... print(i)
... print(5)
...
>>> method_to_understand_indentation()
1
2
3 4
5
6
7 8
9
10
5 >>> def method_to_understand_indentation():
... for i in range(1,11) :
... print(i)
... print(5)
...
>>> method_to_understand_indentation()
1
5
2
5
3
5
4
5
5
5
6
5
7
5
8
5
9
5
10
5 >>> os.system('clear')
0
>>> def print_string(str="Hello World", no_of_times=5):
...
>>> print_string() Hello World
Hello World
Hello World
Hello World
Hello World
>>> print_string(6)
6
6
6
6
6
>>> print_string(no_of_times=6)
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
>>> print_string(7, 8)
7
7
7
7
7
7 7
7
>>> print_string(7.5, 8)
7.5 7.5
7.5
7.5
7.5
7.5
7.5 7.5
>>> print_string(7.5, "eight")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in print_string
7.5
>>> print_string(7.5, "8")
^
IndentationError: expected an indented block
... print(i)
...
1
2
3
4
5
6
7
8
9
10
>>> def
def():
File "<stdin>", line 1
def def():
^
SyntaxError: invalid syntax >>> def in():
File "<stdin>", line 1
def
in():
^
SyntaxError: invalid syntax
>>> def for():
File "<stdin>", line
1
def
for():
^
SyntaxError: invalid syntax
>>> os.system('clear')
0
>>> def product_of_two_numbers(a,b)
File "<stdin>", line 1
def
product_of_two_numbers(a,b)
^
SyntaxError: invalid syntax
3
>>> max(1,2,3,4)
4
>>> maximum = max(1,2,3,4)
>>> maximum
4
>>> maximum * 5
20
>>> def product_of_two_numbers(a,b):
... product = a * b;
... return product
...
>>> product_of_two_numbers(2,3)
6
>>> product_result = product_of_two_numbers(2,3)
>>> product_result
6
>>> product_result * 10
60
>>> def sum_of_three_numbers(a, b, c)
File "<stdin>", line 1
def sum_of_three_numbers(a, b,
c)
^
SyntaxError: invalid syntax
>>> def sum_of_three_numbers(a, b, c):
... sum = a + b + c
PyCharm Code
/01-first-python-project/multiplication_table.py
print("Hello World")
PyCharm Code
/01-first-python-project/simple_interest.py
print(calculate_simple_interest(10000,5,5))
/01-first-python-project/elif_examples.py
i = 2
if
i==1:
print("i is 1")
elif i==2:
print("i is 2")
elif i == 3:
print("i is 3") else:
print("i is not 1 or 2 or 3")
/01-first-python-project/if_puzzles.py
number = 5
if number < 0:
number = number + 10
number = number + 5
print(number)
# m = 15
#
# if m>20:
# if m<20:
# print("m>20")
# else:
# print("Who am I?")
# l = 15
#
# if (l < 20):
# print("l<20")
#
# if (l > 20):
# print("l>20")
# else:
# print("Who am I?")
# k = 15
# if (k > 20):
# print(1)
# elif (k > 10):
# print(2)
# elif (k < 20):
# print(3) # else:
# print(4)
/01-first-python-project/input.py
print("\n\n1 - Add")
print("2 - Subtract")
print("3 - Divide")
print("4 - Multiply")
# print(number1 + number2)
# print(choice)
if choice==1:
result = number1 + number2
elif choice==2:
result = number1 - number2
elif
choice==3:
result = number1 / number2
elif
choice==4:
result = number1 * number2
else:
result = "Invalid Choice"
print(result)
>>> i = 10
>>> j = 2
>>> i + j
12
>>> i - j
8
0
>>> value1 = 4.5
>>> value2 = 3.2
>>> value1 + value2
7.7
>>> value1 - value2
1.2999999999999998
>>> value1 / value2
1.40625
>>> value1 % value2
1.2999999999999998
>>> i + value1
14.5
>>> i - value1
5.5
>>> i / value1
2.2222222222222223
>>>
>>> import os
>>> os.system('clear')
0
>>> i = 1
>>> i = i + 1
>>> i
>>> i += 1
>>> i
3
>>>
i++
File "<stdin>", line 1
i++
^
SyntaxError: invalid syntax
>>> ++i
3
>>> i += 1
>>> i
4
>>> i -= 1
>>> i
3
>>> i /= 1
>>> i *= 2
>>> i
6.0
>>> type(i)
<class 'float'>
>>> number1 = 5
>>> number2 = 2
>>> number1/number2
2.5
>>> number1//number2
2
>>> number1 //= 2
>>> number1
2
>>> 5 ** 3
125
>>> pow(5,3)
125
>>> 5.6
5.6
>>> int(5.6)
5
>>> round(5.6) 6
>>> round(5.4)
5
>>> round(5.5)
6
>>> round(5.67, 1)
5.7
>>> round(5.678, 2)
5.68
>>> float(5)
5.0
>>> os.clear('system')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'os' has no attribute 'clear'
>>> os.system('clear')
0
>>> True
True
>>> False
False
>>> true
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'true' is not defined
>>> false
Traceback (most recent call
last):
File "<stdin>", line 1, in <module>
NameError: name 'false' is not defined
>>> is_even = True
>>> i < 15
True
>>> i >= 15
False
>>> i >= 10
True
>>> i > 10
False
>>> i <= 10
True
>>> i < 10
False
>>> i == 10
True
>>> i == 11
False
>>> os.system('clear')
0
>>> i = 5
>>> if i>3:
... print(f"{i} is greater than 3")
...
5 is greater than 3
>>> i = 2
>>> if i>3:
... print(f"{i} is greater than 3")
...
>>> if i<10:
... print(f"{i} is less than 10")
...
2 is less than 10
>>> i = 15 >>> if i<10:
... print(f"{i} is less than 10")
...
>>> if(False):
... print("False")
...
>>> if(True):
... print("True")
...
True
>>> a = 5
>>> b = 7
>>> if(a>b):
... print("a is greater than b")
...
>>> a = 9
>>> if(a>b):
... print("a is greater than b")
...
a is greater than b
>>> os.system('clear')
0
>>> a = 1
>>> b = 2
>>> c = 3
>>> d = 5
>>> if a+b > c+d :
... print("a+b > c +d")
...
>>> a = 9
>>> if a+b > c+d :
... print("a+b > c +d")
180):
File "<stdin>", line 1
if(angle1 + angle2 + angle3 =
180):
^
SyntaxError: invalid syntax
>>> if(angle1 + angle2 + angle3 == 180):
... print("Valid Triangle")
...
>>> angle2 = 90
>>> if(angle1 + angle2 + angle3 == 180):
... print("Valid Triangle")
...
Valid Triangle
>>> i = 2
>>> if(i%2==0):
... print("i is even")
...
i is even
>>> i = 3
>>> if(i%2==0):
... print("i is even")
...
>>> os.system('clear')
0
>>> True and False
False
>>> True and True
True
True
False
>>> os.system('clear')
0
>>> i = 10 >>> j = 15
^
IndentationError: expected an indented block
>>> if i%2==0 or j%2==0:
... print("atleast one of i and j are even")
...
atleast one of i and j are even
>>> i = 15
>>> j
14
>>> if i%2==0 or j%2==0:
... print("atleast one of i and j are even")
...
atleast one of i and j are even
>>> j = 23
>>> if i%2==0 or j%2==0:
... print("atleast one of i and j are even")
...
>>> i
15
>>> if(True ^ False)
File "<stdin>", line 1
if(True ^ False)
^
>>> if x=6:
File "<stdin>", line 1
if x=6:
^
1
>>> int(False)
0
>>> x = -6
>>> if x:
... print("something")
...
something
>>> bool(6)
True
>>> bool(-6)
True
>>> bool(0)
False
>>> os.system('clear')
0
>>> i = 2
>>> if i%2 == 0:
... print("i is even");
... else:
... print("i is odd");
...
i is even
>>> i = 3
>>> if i%2 == 0:
... print("i is even");
... else:
... else:
... print("i is not 1 or 2")
...
i is not 1 or 2
>>>
Text in Python
>>> "hello".capitalize()
'Hello'
>>> 'hello'.capitalize()
'Hello'
>>> 'hello'.islower()
True
>>> 'Hello'.islower()
False
>>> 'Hello'.istitle()
True
>>> 'hello'.istitle()
False
>>> 'hello'.isupper()
False
>>> 'Hello'.isupper()
False
>>> 'HELLO'.isupper()
True
>>> '123'.isdigit()
True
>>> 'A23'.isdigit()
False
>>> '2 3'.isdigit()
False
>>> '23'.isdigit()
True
>>> '23'.isalpha()
False
>>> '2A'.isalpha()
False
>>> 'ABC'.isalpha()
True
>>> 'ABC123'.isalnum()
True >>> 'ABC 123'.isalnum()
False
'True'
>>> bool('True')
True
>>> bool('true')
True
>>> bool('tru')
True
>>> bool('false')
True
>>> bool('False')
True
>>> bool('')
False
>>> str(123)
'123'
>>> str(12345)
'12345'
>>> str(12345.45678)
'12345.45678'
>>> int('45')
45
>>> int('45.56')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '45.56'
>>> int('45dfsafk')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10:
'45dfsafk'
>>> int('45abc',16)
285372
>>> int('a',16) 10
>>> int('b',16)
11
>>> int('c',16)
12
>>> int('f',16)
15
>>> int('g',16)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 16: 'g'
>>> float("34.43")
34.43
>>> float("34.43rer")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: '34.43rer'
>>> os.system('clear')
0
>>> message = "Hello"
>>> message.upper()
'HELLO'
>>> message
'Hello'
>>> message = message.upper()
>>> message
'HELLO'
>>> message = "Hello"
>>> message.upper()
'HELLO'
>>> message_upper = message.upper()
>>> message = "ABC"
>>> message = message.lowercase()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'lowercase'
>>> message = message.lower()
>>> os.system('clear')
0
>>> message = "Hello World"
>>> message[0]
'H'
>>> type(message[0])
<class 'str'>
>>> type(message)
<class 'str'>
>>> message[0]
'H'
>>> message[1]
'e'
>>> message[2]
'l'
>>> message[3]
'l'
>>> message[100]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> for ch in message:
... print(ch)
...
H
e
l
l
o
W o
r
l
d
>>> os.system('clear')
0
>>> import string
>>> string.
string.Formatter( string.ascii_uppercase
string.octdigits
string.Template( string.capwords(
string.printable
string.ascii_letters string.digits
string.punctuation
string.ascii_lowercase string.hexdigits
string.whitespace
>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.digits
'0123456789'
>>> string.hexdigits
'0123456789abcdefABCDEF'
>>> string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
>>> 'a' in string.ascii_letters
True
>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
False
>>> '4' in '13579'
False
>>> char = 'a'
>>> vowel_string = 'aeiouAEIOU'
>>> char in vowel_string
True
>>> char = 'b'
>>> char in vowel_string
False
>>> vowel_string = 'AEIOU'
>>> char.upper() in vowel_string
False
>>> char = 'a'
>>> char.upper() in vowel_string
True
>>> vowel_string = 'aeiou'
>>> char.lower() in vowel_string
True
>>> char = 'A'
>>> char.lower() in vowel_string
True
>>> import string
>>> string.
string.Formatter( string.ascii_uppercase
string.octdigits
string.Template( string.capwords(
string.printable string.ascii_letters
string.digits string.punctuation
string.ascii_lowercase string.hexdigits
string.whitespace
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> for char in string.ascii_uppercase:
... print(char)
...
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
>>> for char in string.
string.Formatter( string.ascii_uppercase
string.octdigits
string.Template( string.capwords(
string.printable
string.ascii_letters string.digits
string.punctuation
string.ascii_lowercase string.hexdigits
string.whitespace
>>> for char in string.digits:
... print(char)
...
0
1
2
3
4
5
6
7
8
9
>>> vowel_string = 'aeiou'
>>> char.lower() in vowel_string
False
>>> 'b'.lower() not in vowel_string
True
>>> 'a'.lower() not in vowel_string
False
>>> '1'.lower() not in vowel_string
True
>>> '1'.isalpha() and '1'.lower() not in vowel_string
False
>>> char.isalpha() and char.lower() not in vowel_string
True
>>> char
'b'
>>> char = '1'
>>> char.isalpha() and char.lower() not in vowel_string
False
>>> os.system('clear')
0
string_example.encode( string_example.maketrans(
string_example.endswith( string_example.partition(
string_example.expandtabs( string_example.replace(
string_example.find( string_example.rfind(
string_example.format( string_example.rindex(
string_example.format_map( string_example.rjust(
string_example.index( string_example.rpartition(
string_example.isalnum( string_example.rsplit(
string_example.isalpha( string_example.rstrip(
string_example.isdecimal( string_example.split(
string_example.isdigit( string_example.splitlines(
string_example.isidentifier( string_example.startswith(
string_example.islower( string_example.strip(
string_example.isnumeric( string_example.swapcase(
string_example.isprintable( string_example.title(
string_example.isspace( string_example.translate(
string_example.istitle( string_example.upper(
string_example.isupper( string_example.zfill(
>>> string_example.split()
['This', 'is', 'a', 'great', 'thing']
>>> for word in string_example.split():
... print(word)
...
This
is
a
great
thing
>>> string_example = "This\nis\n\ngreat\nthing"
>>> print(string_example)
This
is
great
thing
PyCharm Code
/01-first-python-project/while_exercises.py
# print_squares_upto_limit(30)
# //For limit = 30, output would be 1 4 9 16 25
#
# print_cubes_upto_limit(30)
# //For limit = 30, output would be 1 8 27
def print_squares_upto_limit(limit):
i = 1
while i * i < limit:
print(i*i, end = " ")
i = i + 1
def
print_cubes_upto_limit(limit):
i = 1
while i * i * i < limit:
print(i*i*i, end = " ")
i = i + 1
print_cubes_upto_limit(80)
/01-first-python-project/number_menu_loop.py
print("\n\n1 - Add")
print("2 - Subtract")
print("3 - Divide")
print("4 - Multiply")
print("5 - Exit")
while(choice != 5):
# print(number1 + number2)
# print(choice)
if choice==1:
result = number1 + number2
elif choice==2:
result = number1 - number2
elif choice==3:
result = number1 / number2
elif choice==4:
result = number1 * number2
else:
result = "Invalid
Choice"
print(result)
print("Thank You")
/01-first-python-project/loop_puzzles.py
# for i in range(1,11,2):
# print(i, end=' ')
# for i in range(11,0,-1):
# print(i, end=' ')
#
# i = 5
# while i*i < 10:
# print(i)
# print("done")
#
#
# i = 2
# while i*i < 10:
# print(i, end=' ')
# i = i + 1
# print("done")
# for i in range(1,11):
# if i==5:
# break
# print(i, end=' ')
# print("done")
# for i in range(2,11):
# if i%2:
# break
# print(i , end=' ')
#
# print("done")
# for i in range(1,11):
# if i%2:
# continue
# print(i , end=' ')
#
# print("done")
for i in range(1,11):
if i%2!=0:
continue
print(i , end=' ')
print("done")
/01-first-python-project/for_exercises.py
return True
# print(is_prime(15));
# sum_upto_n(6)
# Sum of numbers upto n?
# 1 + 2 + 3 + 4 + 5 + 6
def
sum_upto_n(number):
sum =
return sum
# print(sum_upto_n(6))
# print(sum_upto_n(10))
def calculate_sum_of_divisors(number):
sum = 0
return sum
# print(calculate_sum_of_divisors(6))
# print(calculate_sum_of_divisors(15))
def
print_a_number_triangle(number):
for j in range(1, number + 1):
for i in range(1, j + 1):
print(i, end=' ')
print()
print_a_number_triangle(6)
W
o
r
l
d
>>> for word in "Hello World".split():
... print(word)
...
Hello
World
>>> for item in (3, 6, 9):
... print(item)
...
3
6
9
>>> os.system('clear')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>> import os
>>> os.system('clear')
0
>>> i = 5
>>> if i == 5:
... print("i is 5")
...
i is 5
>>> i = 0
>>> while i < 5:
... print(i)
...
0
0
0
0
0
0
0
0
^CTraceback (most recent call last):
File "<stdin>", line 2, in <module>
KeyboardInterrupt
>>>
KeyboardInterrupt
>>> while i < 5:
... print(i)
... i = i + 1
...
0
1
2
3
4
>>> i = 0
>>> while i < 5:
... print(i, end=" ")
... i = i + 1
...
0 1 2 3 4 >>> for i in range(0,5): print(i)
...
0
1
2
3
4
>>> os.system('clear')
0
>>>
Introduction To Object
Oriented Programming
PyCharm Code
/02-oops/book.py
class
Book:
def __init__(self, name, copies=0):
self.name = name
self.copies = copies
# print(the_art_of_computer_programming.name)
# print(learning_python.name)
# print(learning_restful_services.name)
learning_python.increase_copies(25)
learning_python.decrease_copies(10)
learning_python.copies = 50
print(learning_python.copies)
/02-oops/country.py
class Country:
def __repr__(self):
return repr((self.name,self.population,self.area))
countries = [Country('India',1200,100),
Country('China', 1400, 200),
Country('USA', 120, 300)]
countries.append(Country('Russia',80,900))
countries.sort(key=attrgetter('population'), reverse=True)
print(max(countries, key=attrgetter('population')))
print(min(countries, key=attrgetter('population')))
print(min(countries, key=attrgetter('area')))
print(max(countries, key=attrgetter('area')))
print(countries)
/02-oops/motor_bike.py
# Class
class MotorBike:
def __init__(self, speed):
self.speed = speed #State
# Behavior
def increase_speed(self, how_much):
self.speed += how_much
# Behavior
def decrease_speed(self, how_much):
if(self.speed-how_much>0):
self.speed -= how_much
else:
print("Get a life")
# instance 1 or object 1
honda = MotorBike(50)
# instance 2 or object 2
ducati = MotorBike(250)
# print(honda)
# print(ducati) #
honda.decrease_speed(350)
print(honda.speed)
print(ducati.speed)
#
# honda.speed = 150
# print(honda.speed)
# print(ducati.speed)
/02-oops/planet.py
class Planet(object):
def rotate(self):
print("rotate")
def
revolve(self):
print("revolve")
def rotate_and_revolve(self):
self.rotate()
self.revolve()
earth = Planet() earth.rotate_and_revolve()
0
>>> class Planet:
... def __init__(): pass
...
>>> Planet()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() takes 0 positional arguments but 1
was given
>>> class Planet:
... def __init__(self): pass
...
>>> Planet() <__main__.Planet object at 0x10426bc88>
>>> class Planet:
... def __init__(self): pass
... def __init__(self, name): pass
... >>> Planet()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() missing 1 required positional
argument: 'name'
>>> class Planet:
... def __init__(self, name): pass
... def __init__(self): pass
...
>>> Planet()
<__main__.Planet object at 0x10426bdd8>
>>> Planet("Jupiter")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() takes 1 positional argument but 2
were given
>>> class Planet:
... def __init__(self, name="Earth"): pass
...
>>> Planet()
<__main__.Planet object at 0x10426beb8>
>>> Planet("Jupiter")
<__main__.Planet object at 0x10426bef0>
>>> class Planet:
... def __init__(self, name="Earth"):
... self.speed = 10
0
>>> class Planet:
... def revolve(): pass
...
>>> earth = Planet()
>>> earth.revolve()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: revolve() takes 0 positional arguments but 1 was
given
>>> class Planet:
... def revolve(self): pass
...
>>> earth = Planet()
>>> earth.revolve()
Revolve
>>> os.system('clear')
0
>>> 5
5
>>> type(5)
<class 'int'>
>>> type(True)
<class 'bool'>
>>> type('Hello')
<class 'str'> >>> 'Hello'.upper()
'HELLO'
>>> type(5.5)
<class 'float'>
>>> def do_something(): pass
...
>>> do_something <function do_something at 0x104275488>
>>> def do_something():
... print("something")
...
>>> do_something
<function do_something at 0x104275510>
>>> do_something()
something
>>> test = do_something
>>> test
<function do_something at 0x104275510>
>>> test()
something
>>>
Python Data Structures
PyCharm Code
/02-oops/Student.py
class Student:
def get_number_of_marks(self):
return
len(self.marks)
def get_total_sum_of_marks(self):
return sum(self.marks)
def
determine_maximum_mark(self):
return max(self.marks)
def determine_minimum_mark(self):
return min(self.marks)
def determine_average(self):
return
self.get_total_sum_of_marks()/self.get_number_of_marks()
sum_of_marks = student.get_total_sum_of_marks()
maximum_mark = student.determine_maximum_mark()
minimum_mark = student.determine_minimum_mark()
average = student.determine_average()
student.add_new_mark(35)
student.remove_mark_at_index(2)
print(student.marks)
print(f"""Student[
number_of_marks-{number}
sum_of_marks-{sum_of_marks}
max-{maximum_mark}
min-{minimum_mark}
avg-{average} ] """)
/02-oops/word_count.py
char_occurances = {} #[]
print(char_occurances)
word_occurances = {} #[]
print(word_occurances)
mark2 + mark3
179
>>> (mark1 + mark2 + mark3)/3
59.666666666666664
0
>>> marks = [23,56,67]
>>> sum(marks)
146
>>> max(marks)
67
>>> min(marks)
23
>>> len(marks)
3
>>> marks.append(76)
>>> marks [23, 56, 67, 76]
>>> marks.insert(2, 60) >>> marks
>>> marks.remove(60)
>>> 55 in marks False
>>> 56 in marks
True
>>> marks.index(67)
2
>>> marks
[23, 56, 67, 76]
>>> marks.index(69)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 69 is not in list
>>> for mark in marks:
... print(mark)
...
23
56
67
76
>>> os.system('clear')
0
>>> animals = ['Cat', 'Dog','Elephant']
>>> len(animals)
3
>>> sum(animals)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and
'str'
>>> animals.append('Fish') >>> animals
['Cat', 'Dog', 'Elephant', 'Fish']
>>> animals.remove('Dog')
>>> animals
['Cat', 'Elephant', 'Fish'] >>> animals[2] 'Fish' >>>
animals[1]
'Elephant'
>>> animals[0] 'Cat'
>>> animals[4]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> del animals[2]
>>> animals
['Cat', 'Elephant']
>>> animals.extend('Fish')
>>> animals
['Cat', 'Elephant', 'F', 'i', 's', 'h']
>>> animals.append('Fish')
>>> animals
['Cat', 'Elephant', 'F', 'i', 's', 'h', 'Fish']
>>> animals.extend(['Giraffe', 'Horse'])
>>> animals
['Cat', 'Elephant', 'F', 'i', 's', 'h', 'Fish', 'Giraffe',
'Horse']
>>> animals = animals + ['Jackal','Kangaroo']
>>> animals
['Cat', 'Elephant', 'F', 'i', 's', 'h', 'Fish', 'Giraffe',
'Horse', 'Jackal', 'Kangaroo']
>>> animals += ['Lion','Monkey']
>>> animals
['Cat', 'Elephant', 'F', 'i', 's', 'h', 'Fish', 'Giraffe',
'Horse', 'Jackal', 'Kangaroo', 'Lion', 'Monkey'] >>>
animals.append(10)
>>> animals
['Cat', 'Elephant', 'F', 'i', 's', 'h', 'Fish', 'Giraffe',
['Zero','One','Two','Three','Four','Five','Six','Seven','E
ight','Nine'] >>> len(numbers)
10
>>> number[2]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'number' is not defined
>>> numbers[2]
'Two'
>>> numbers[2:6]
['Two', 'Three', 'Four', 'Five']
>>> numbers[:6]
['Zero', 'One', 'Two', 'Three', 'Four', 'Five']
>>> numbers[3:]
['Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine']
>>> numbers[1:8:2]
['One', 'Three', 'Five', 'Seven']
>>> numbers[1:8:3]
['One', 'Four', 'Seven']
>>> numbers[::3]
['Zero', 'Three', 'Six', 'Nine']
>>> numbers[::-1]
['Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three',
'Two', 'One', 'Zero']
>>> numbers[::-3]
['Nine', 'Six', 'Three', 'Zero']
>>> del numbers[3:] >>> numbers
['Zero', 'One', 'Two']
>>> numbers =
['Zero','One','Two','Three','Four','Five','Six','Seven','E
ight','Nine']
>>> numbers =
['Zero','One','Two','Three','Four','Five','Six','Seven','Ei
ght','Nine']
>>> numbers.reverse()
>>> numbers
['Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three',
'Two', 'One', 'Zero']
>>> numbers =
['Zero','One','Two','Three','Four','Five','Six','Seven','Ei
ght','Nine']
>>> numbers
['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six',
'Seven', 'Eight', 'Nine']
>>> reversed(numbers)
<list_reverseiterator object at 0x109560ba8>
>>> for number in reversed(numbers):
... print(number)
...
Nine
Eight
Seven
Six
Five Four
Three
Two
One
Zero
>>> numbers
['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six',
'Seven', 'Eight', 'Nine'] >>> numbers.sort()
>>> numbers
['Eight', 'Five', 'Four', 'Nine', 'One', 'Seven', 'Six',
'Three', 'Two', 'Zero'] >>> numbers =
['Zero','One','Two','Three','Four','Five','Six','Seven','E
ight','Nine']
>>> for number in sorted(numbers):
... print(number)
...
Eight
Five
Four
Nine
One
Seven
Six
Three
Two
Zero
>>> numbers
['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six',
'Seven', 'Eight', 'Nine']
>>> for number in sorted(numbers, key=len):
... print(number)
...
One
Two Six
Zero
Four
Five
Nine
Three
Seven
Eight
>>> for number in sorted(numbers, key=len, reverse=True):
... print(number)
...
Three Seven
Eight Zero Four
Five
Nine
One
Two
Six
>>> numbers.sort(key=len)
>>> numbers
['One', 'Two', 'Six', 'Zero', 'Four', 'Five', 'Nine',
'Three', 'Seven', 'Eight']
>>> numbers.sort(key=len, reverse=True)
>>> numbers
['Three', 'Seven', 'Eight', 'Zero', 'Four', 'Five', 'Nine',
'One', 'Two', 'Six']
>>> os.system('clear')
0
>>> numbers = []
>>> numbers.append(1)
>>> numbers.append(2) >>> numbers.append(3)
>>> numbers.append(4)
>>> numbers.pop()
4
>>> numbers
[1, 2, 3]
>>> numbers.pop()
3
>>> numbers
[1, 2] >>> numbers.append(10)
>>> numbers.pop()
10
>>> numbers [1, 2]
>>> numbers = []
>>> numbers.append(1) >>> numbers.append(2)
>>> numbers.append(3) >>> numbers.append(4) >>>
numbers.pop(0)
1
>>> numbers
[2, 3, 4]
>>> numbers.pop(0)
2
>>> numbers
[3, 4]
>>> numbers.append(10)
>>> numbers.pop(0)
3
>>> numbers.pop(0)
4
>>> numbers.pop(0)
10 >>> numbers
[]
>>> os.system('clear')
0
>>> numbers = ['Zero',
'One','Two','Three','Four','Five','Six','Seven',
'Eight','Nine']
>>> numbers_length_four=[]
>>> numbers_length_four
['Zero', 'Four', 'Five', 'Nine']
>>> values = [3, 6, 9, 1, 4, 15, 6, 3]
>>> values_even = [ value for value in values if
value%2==0]
>>> values_even [6, 4, 6]
>>> values_odd = [ value for value in values if value%2==1]
>>> values_odd
[3, 9, 1, 15, 3]
>>> os.system('clear')
0
>>> numbers = [1,2,3,2,1]
>>> numbers
[1, 2, 3, 2, 1]
>>> numbers_set = set(numbers)
>>> numbers_set {1, 2, 3}
>>> numbers_set.add(3)
>>> numbers_set
{1, 2, 3}
>>> numbers_set.add(4)
>>> numbers_set {1, 2, 3, 4}
>>> numbers_set.add(0)
>>> numbers_set
{0, 1, 2, 3, 4}
>>> numbers_set.remove(0) >>> numbers_set
{1, 2, 3, 4}
>>> numbers_set[0] Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'set' object does not support indexing
>>> 1 in numbers_set
True
>>> 5 in numbers_set
False
>>> min(numbers_set)
1 >>> max(numbers_set)
4
>>> sum(numbers_set)
10
>>> len(numbers_set)
4
>>> numbers_1_to_5_set = set(range(1,6))
>>> numbers_1_to_5_set
{1, 2, 3, 4, 5}
>>> numbers_4_to_10_set = set(range(4,11))
>>> numbers_4_to_10_set
{4, 5, 6, 7, 8, 9, 10}
>>> numbers_1_to_5_set + numbers_4_to_10_set Traceback
(most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'set' and
'set'
>>> numbers_1_to_5_set | numbers_4_to_10_set
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} >>> numbers_1_to_5_set &
numbers_4_to_10_set
{4, 5}
>>> numbers_1_to_5_set - numbers_4_to_10_set
{1, 2, 3}
>>> numbers_4_to_10_set - numbers_1_to_5_set {6, 7, 8, 9,
10}
>>> os.system('clear')
0
>>> occurances = dict(a=5 b=6 c=8)
File "<stdin>", line 1
occurances = dict(a=5 b=6
c=8)
^
SyntaxError: invalid syntax
>>> occurances = dict(a=5,b=6,c=8)
>>> occurances['e']
Traceback (most recent call last):
File "<stdin>", line 1, in <module> KeyError: 'e'
>>> occurances.get('d')
10
>>> occurances.get('e')
>>> occurances.get('e', 10)
10
>>> occurances {'a': 5, 'b': 6, 'c': 8, 'd': 10}
>>> occurances.keys()
dict_keys(['a', 'b', 'c', 'd'])
>>> occurances.values()
dict_values([5, 6, 8, 10])
>>> occurances.items() dict_items([('a', 5), ('b', 6),
('c', 8), ('d', 10)]) >>> for (key,value) in
occurances.items():
... print(f"{key} {value}") ...
a 5
b 6
c 8
d 10 >>> occurances['a']=0
>>> occurances
{'a': 0, 'b': 6, 'c': 8, 'd': 10}
>>> del occurances['a']
>>> occurances
{'b': 6, 'c': 8, 'd': 10}
>>> os.system('clear'
... )
0
>>> str = "This is an awesome occasion. This has never
happened before."
>>> type(())
<class 'tuple'>
>>> type((1,2,3))
<class 'tuple'>
>>>
Object Oriented Programming
Again
PyCharm Code
/06-oops-advanced/amphibian.py
class LandAnimal:
def __init__(self):
super().__init__()
self.walking_speed = 5
class WaterAnimal:
def
__init__(self):
super().__init__()
self.swimming_speed =
10
class AbstractAnimal(ABC):
@abstractmethod
def bark(self): pass
class Dog(AbstractAnimal):
def bark(self):
print("Bow Bow")
print(Dog().bark())
/06-oops-advanced/book_reviews.py
class Book(object):
def __init__(self, id, name, author):
self.id = id
self.name =
name
self.author = author
self.reviews =
[]
def __repr__(self):
return
repr((self.id,self.name,self.author,self.reviews))
def __repr__(self):
return repr((self.id,self.description,self.rating))
book = Book(123, 'Object Oriented Programming with Python',
'Ranga')
# book.add_review()
book.add_review(Review(10, "Great Book", 5))
book.add_review(Review(101, "Awesome", 5))
print(book)
/06-oops-advanced/fan.py
# State
# make
# radius
# color
# speed
# is_on
# Behavior
# switch_on
# switch_off
# increase_speed # decrease_speed
class Fan:
def __init__(self, make, radius, color):
self.make = make
self.radius = radius
self.color = color
self.speed = 0
self.is_on = False
def __repr__(self):
return
repr((self.make,self.radius,self.color,self.speed,self.is_o
n))
def switch_on(self):
self.is_on = True
self.speed = 3
def switch_off(self):
self.is_on = False
self.speed = 0
# increase_speed
# decrease_speed
fan = Fan('Manufacturer 1', 5, 'Green')
fan.switch_on()
print(fan)
fan.switch_off()
print(fan)
/06-oops-advanced/person_inheritance.py
class Person:
def __init__(self,name,
email):
self.name = name
self.email = email
def __repr__(self):
return repr((self.name,self.email))
class Student(Person):
def __init__(self, name, email, college, cls):
super().__init__(name,email)
self.college = college
self.cls = cls
def __repr__(self):
return repr((super().__repr__(),
self.college,self.cls))
person = Person('Ranga','[email protected]')
print(person)
student = Student('Ranga','[email protected]',
'Stanford', 'Algorithms' )
print(student)
# Person
# name, email
# Student
# college, class
# Employee
# title, employer
/06-oops-advanced/recipe.py
AbstractRecipe(ABC):
def execute(self):
self.prepare()
self.recipe()
self.cleanup()
@abstractmethod
def prepare(self): pass
@abstractmethod
def recipe(self): pass
@abstractmethod
def cleanup(self): pass
class Recipe1(AbstractRecipe):
def prepare(self):
print('do the dishes')
print('get raw materials')
def recipe(self):
print('execute the steps')
def prepare(self):
print('do the dishes')
print('get raw materials')
print('switch on
microwave')
def
recipe(self):
print('execute the steps')
def cleanup(self):
print('switch off microwave')
MicrowaveRecipe().execute()
... print("bark")
... def groom(self):
... print("groom")
...
>>> pet = Pet() >>> pet.bark()
bark
>>> pet.groom()
groom
>>> class Pet(Animal):
... def groom(self):
... print("groom")
...
>>> dog = Pet()
>>> os.system('clear')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>> import os
>>> os.system('clear')
0
>>>
Error Handling with Python
PyCharm Code
/04-exception-handling/currency.py
#USD 20
#USD 30
#USD 50
#INR 500
class CurrenciesDoNotMatchError(Exception):
def __init__(self,message):
super().__init__(message)
class Currency:
def __init__(self, currency, amount):
self.currency = currency
self.amount =
amount
def __repr__(self):
return repr((self.currency,self.amount))
# Open File/Resource
try:
# Business Logic to read
i = 0 # Not hardcoded, getting a input from user
j = 10/i
values = [1,2]
sum(values)
except TypeError:
print("TypeError")
j = 10
except ZeroDivisionError:
print("ZeroDivisionError")
j = 0
except:
print("OtherError")
j = 5
else:
print("Else")
finally:
# Close
print("Finally")
print(j)
print("End")
/04-exception-handling/exception_handling_puzzles.py
# try:
# 10/0
# except TypeError:
# print("TypeError")
# except ZeroDivisionError:
# print("ZeroDivisionError")
#
# print("End")
# try:
# 10/0
# except object:
# print("ZeroDivisionError")
# # catching classes that do not inherit from BaseException
is not allowed
# print("End")
# try:
# 10/0
# except BaseException:
# print("BaseException")
#
# print("End")
# try:
# 10/0 # except Exception:
# print("Exception")
try:
sum([1,'1'])
except TypeError as error:
print(error)
print("End")
last):
File "<stdin>", line 1, in <module> ZeroDivisionError:
division by zero
>>> 2 + '2'
Traceback (most recent call last):
File "<stdin>", line 1, in <module> TypeError:
unsupported operand type(s) for +: 'int' and 'str'
>>> values = [1,'2']
>>> sum(values)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and
'str'
>>> value
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'value' is not defined
>>> values.non_existing
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute
'non_existing'
>>> values.non_existing()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute
'non_existing'
>>> import builtins
>>> help(builtins)
>>> help(builtins)
>>> k = 10/non_existing_variable
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'non_existing_variable' is not defined >>>
10/0
>>>
Python Tips
More Tips
PyCharm Code
/05-tips/all_about_methods.py
def example_method(mandatory_parameter,
default_parameter="Default"
, *args, **kwargs):
print(f"""
mandatory_parameter = {mandatory_parameter}
{type(mandatory_parameter)}
default_parameter = {default_parameter}
{type(default_parameter)}
args = {args} {type(args)}
kwargs = {kwargs}
{type(kwargs)}
""")
def method_1():
print("method 1")
class ClassA:
def class_method_1(self):
print("class_method_1 method 1")
# print(__name__)
if __name__ == '__main__':
method_1()
ClassA().class_method_1()
/05-tips/module_2.py
import
module_1
module_1.method_1()
module_1.ClassA().class_method_1()
math.pow(
>>> help(math.ceil)
>>> math.ceil(5.5)
6
>>> math.ceil(-5.5)
-5
>>> import os
>>> os.system('clear')
0
>>> import statistics
>>> statistics.
statistics.Decimal( statistics.mean(
statistics.Fraction( statistics.median(
statistics.StatisticsError( statistics.median_grouped(
statistics.bisect_left( statistics.median_high(
statistics.bisect_right( statistics.median_low(
statistics.chain( statistics.mode(
statistics.collections statistics.numbers
statistics.decimal statistics.pstdev(
statistics.groupby( statistics.pvariance(
statistics.harmonic_mean( statistics.stdev(
statistics.math statistics.variance(
>>> marks = [1, 6, 9, 23, 2] >>> statistics.mean(marks) 8.2
>>> statistics.median(marks)
6
>>> marks = [1, 6, 9, 23, 2, 7]
>>> statistics.median(marks)
6.5
>>> statistics.median_high(marks)
7
>>> statistics.median_low(marks)
6
>>> statistics.variance(marks)
63.2
>>> os.system('clear')
0
>>> from collections import deque
>>> queue = deque(['Zero','One','Two'])
>>> queue.pop()
'Two'
>>> queue.append('Three')
>>> queue
deque(['Zero', 'One', 'Three'])
>>> queue.append('Four')
>>> queue.append('Five')
0
>>> import datetime
>>> datetime.datetime.today()
datetime.datetime(2018, 5, 21, 9, 59, 57, 450683)
>>> today_date = datetime.datetime.today()
>>> today_date
datetime.datetime(2018, 5, 21, 10, 0, 39, 732463)
>>> today_date.year
2018
>>> today_date.month
5
>>> today_date.day
21
>>> today_date.hour
10
>>> today_date.minute
0
>>> today_date.second
39
>>> some_date = datetime.datetime(2019, 5, 27)
>>> some_date
datetime.datetime(2019, 5, 27, 0, 0)
>>> some_date = datetime.datetime(2019, 5, 27, 9, 5,25)
>>> some_date
>>> math.
math.acos( math.erf( math.inf math.pi
>>> help(math)
>>>
>>> from math import *
>>> floor(5)
5
>>> gcd(34,56)
2
>>> from math import gcd
>>> gcd(56,68)
4
>>> os.system('clear')
0
4 - u
>>> import os
>>> os.system('clear')
0
0
>>> a = 1
>>> len(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object of type 'int' has no len()
>>> type(a)
<class 'int'>
>>> str = "Value"
>>> str.upper()
'VALUE'
>>> a.upper()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
<class 'float'>
>>> type("1.5") <class 'str'>
0
>>> def create_ranga():
... return 'Ranga',1981,'India'
...
>>> ranga = create_ranga()
>>> type(ranga)
<class 'tuple'>
>>> name, year, country = ranga
>>> ranga
('Ranga', 1981, 'India')
>>> name
'Ranga'
>>> year
1981 >>> country
'India' >>> len(ranga)
3 >>> ranga[0]
'Ranga'
>>> ranga[1]
>>> sum
<built-in function sum>
>>> sum([12,34,56])
102 >>> number1 = 10 >>> number2 = 20
30
>>> sum([12,34,56])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
>>> sum_ = number1 + number2
>>> del sum
>>> sum
<built-in function sum>
>>> sum([12,34,56])
102
>>> os.system('clear')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>> import os
>>> os.system('clear')
0
>>> None
>>> type(None)
<class 'NoneType'>
>>> def email(subject, content, to , cc , bcc):
... print(f" {subject}, {content}, {to}, {cc}, "
... ) ...
>>> email("subject", "great work", [email protected])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'in28minutes' is not defined
...
>>> email("subject", "great work", "[email protected]")
subject, great work, [email protected], None, None
>>> email("subject", "great work", "[email protected]",
None, None)
subject, great work, [email protected], None, None
>>> email(None, "great work", "[email protected]",
None, None)
None, great work, [email protected], None, None
>>> var = "123"
>>> if var is None : print ("do something");
...
>>> var = None
>>> if var is None : print ("do something");
...
do something
>>> os.system('clear')
0
>>> class Student: pass
...
>>> student1 = Student()
>>> student2 = Student()
>>> id(student1) 4554811768
>>> id(student2) 4554811992
>>> student1 is student2
False
>>> student3 = student1
>>> id(student3)
4554811768 >>> student1 is student3
True >>> student1 == student2
False
>>> os.system('clear')
0
>>> i=1
File "<stdin>", line 1
i=1
^
IndentationError: unexpected indent
>>> i=3
File "<stdin>", line 1
i=3
^
IndentationError: unexpected indent
>>> i=1
>>> if(i==3):
... print('somethin')
File "<stdin>", line 2
print('somethin')
^
IndentationError: expected an indented block
>>> if(i==3):
... print('something') ... print('')
File "<stdin>", line 3
print('')
^
IndentationError: unindent does not match any outer
indentation level
>>> os.system('clear')
0
>>> import this