Java & Python Learning Guide
Java & Python Learning Guide
Recommended Versions
Python Python 3
Java >8
Github Page :
[Link]
All Python Code
/oops/abstract_class_example.py
class AbstractRecipe(ABC):
def execute(self):
self.get_ready()
self.do_the_dish()
[Link]()
@abstractmethod
def get_ready(self):
pass
@abstractmethod
def do_the_dish(self):
pass
@abstractmethod
def cleanup(self):
pass
class Recipe1(AbstractRecipe):
def
get_ready(self):
print('Get raw materials')
print('Get utensils')
def do_the_dish(self):
print('do the dish')
def cleanup(self):
print('clean utensils')
recipe = Recipe1()
[Link]()
/oops/abstract_class_to_implement_interface_example.py
# class GamingConsole(ABC):
# @abstractmethod
# def up(self): pass
#
# @abstractmethod
# def down(self): pass
#
# @abstractmethod
# def left(self): pass
#
# @abstractmethod
# def right(self): pass
class MarioGame:
def up(self): print('jump')
class ChessGame:
def up(self): print('Move piece up')
class Test1:
def method(self): print("Test1")
class Test2:
def method(self): print("Test2")
tests = [Test1(),Test2()]
/oops/book_example.py
# MotorBike
# gear, speed
class Book:
def __repr__(self):
return repr(([Link], [Link]))
#set
#get
print(book1)
print(book2)
/oops/encapsulation_examples.py
class BookEnhanced:
def __init__(self, name, copies):
[Link] = name
self._copies = copies
@property
def copies(self):
print('getter called')
return self._copies
@[Link]
def copies(self, copies):
print('setter called')
if(copies>=0):
self._copies = copies
microservices = BookEnhanced('Microservices',5)
print([Link])
[Link] = 10
print([Link])
/oops/exception_handling_examples.py
# try:
# i = 0
# number = 10/i
# except ZeroDivisionError as error:
# print(error)
# number = 0
# else: # else is execute when exception is not thrown
# print('else')
# finally:
# print('finally')
#
# print(number)
#
class Amount:
def __init__(self, currency, amount):
[Link] = currency
[Link] = amount
def __repr__(self):
return repr(([Link],[Link]))
class CurrencyDoNotMatchException(Exception):
def __init__(self, message):
super().__init__(message)
amount1 = Amount('EUR',35)
amount2 = Amount('INR',70)
[Link](amount1)
print(amount2)
/oops/inheritance_examples.py
# Student(college,year,degree)
# IS A Person(name, address)
class Person:
def __init__(self, name):
[Link] = name
def __repr__(self):
return repr(([Link]))
class Student(Person):
def __repr__(self):
return repr((super().__repr__(),self.college_name))
person = Person('Ranga')
print(person)
print(student)
class Planet:
pass
earth = Planet()
earth.__repr__()
/oops/motor_bike_example.py
class MotorBike:
def __repr__(self):
return repr(([Link], [Link]))
# instance 1 or object 1
honda = MotorBike(3, 50)
# instance 2 or object 2
ducati = MotorBike(1, 10)
print(honda)
print(ducati)
/oops/multiple_inheritance_examples.py
class LandAnimal:
def walk(self):
print('walk')
class WaterAnimal:
def swim(self):
print('swim')
frog = Amphibian()
[Link]()
[Link]()
/oops/oops_in_depth.py
class Planet:
def __init__(self, name, distance_from_sun):
[Link] = name
self.distance_from_sun = distance_from_sun
[Link] = 10000
print([Link])
# [Link] = 'Mars'
print([Link])
/oops/oops_puzzles.py
class Country:
# def __init__(self):
# print('constuctor 1')
def instance_method(self):
print('instance method')
default_country = Country()
india = Country('India')
print(default_country.name)
print([Link])
#TypeError: __init__() missing 1 required positional
argument: 'name'
/oops/operator_overloading_examples.py
@total_ordering
class Money:
def __init__(self, currency, amount):
[Link] = currency
[Link] = amount
def __repr__(self):
return repr(([Link], [Link]))
# print(amount1 == amount2)
# print(amount1 != amount2)
# print(amount1 == amount3)
# print(amount1 != amount3)
# print(amount1 + amount2)
# print(amount2 - amount1)
# object.__add__(self, other)
# object.__sub__(self, other)
# object.__mul__(self, other) *
# object.__matmul__(self, other)
# object.__truediv__(self, other) \
# object.__floordiv__(self, other) \\
# object.__mod__(self, other) %
# object.__pow__(self, other[, modulo]) **
# object.__and__(self, other) and
# object.__xor__(self, other) ^
# object.__or__(self, other) or
#
# i methods
/oops/static_examples.py
class Player:
count =
0
@staticmethod
def get_count():
return [Link]
messi = Player('Messi')
ronaldo = Player('Ronaldo')
print(messi.get_count())
print(ronaldo.get_count())
print(Player.get_count())
# print([Link])
#
# print([Link])
# print([Link])
#
# [Link] = 100
#
# print([Link])
#
# print([Link])
# print([Link])
# [Link] = 100
#
# print([Link])//2
#
# print([Link])//100
# print([Link])//2
/python-hello-world/first_method.py
# print("Hello World")
def print_hello_world_twice():
print("Hello World 1")
print("Hello World 2")
def print_hello_world_multiple_times(times):
for i in range(1, times+1):
print("Hello World")
# print_hello_world_twice()
print_hello_world_multiple_times(4)
/python-hello-world/for_loop_examples.py
for i in range(1,10):
print(i)
print("Done")
for i in range(1,10):
print(i*i)
print("Test")
/python-hello-world/for_loop_exercises.py
def is_prime(number):
if number < 2:
return False
return True
# print(is_prime(15));
def sum_upto_n(number):
sum = 0
return sum
# print(sum_upto_n(6))
# print(sum_upto_n(10))
def calculate_sum_of_divisors(number):
sum_of_divisors = 0
return sum_of_divisors
# 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)
/python-hello-world/hello_world.py
print('Hello World')
print("Hello World2")
print("Hello World3")
/python-hello-world/if_examples.py
if x == 1:
print(f"{x} is 1")
print("this is part of if")
elif x == 2:
print(f"{x} is 2")
print("this is part of elif")
else:
print(f"{x} is NOT 1 or 2")
print("this is part of else")
/python-hello-world/math_basic.py
def print_squares_of_numbers_upto(n):
for i in range(1,n+1):
print(i*i)
def print_squares_of_even_numbers_upto(n):
for i in range(2,n+1,2):
print(i*i)
def sum_of_two_numbers(number1,number2):
sum = number1 + number2
return sum
def print_numbers_in_reverse(n):
for i in range(n,0,-1):
print(i)
print(sum_of_two_numbers(5,6))
# print_squares_of_even_numbers_upto(10)
# print_numbers_in_reverse(10)
/python-hello-world/menu_with_if.py
if choice == 1:
print(f"Result = {number1 + number2}")
elif choice == 2:
print(f"Result = {number1 - number2}")
elif choice == 3:
print(f"Result = {number1 / number2}")
elif choice == 4:
print(f"Result = {number1 * number2}")
else:
print("Invalid Operation")
/python-hello-world/multiplication_table.py
print_multiplication_table()
# print_multiplication_table(6)
/python-hello-world/oops_trials.py
class Book:
count = 0
def __init__(self,
name):
self._name = name
[Link] = [Link] + 1
@property
def name(self):
print("Getter For Name Called")
return self._name
@[Link]
def name(self, name):
print("Setter For Name Called")
self._name = name
@staticmethod
def static_method():
print("I'm static")
# book1 = Book('Microservices')
# print([Link])
# [Link] = 'ABC'
# print([Link])
# book2 = Book('Web Services')
# print([Link])
# print([Link])
# print([Link])
# Book.static_method()
# book1.static_method()
# print([Link])
def do_this_and_print(func,data):
print(func(data))
def double(data):
return data * 2
def triple(data):
return data * 3
do_this_and_print(double,5)
do_this_and_print(triple,5)
do_this_and_print(lambda x : x*2, 5)
do_this_and_print(lambda x : x*5, 5)
/python-hello-world/repeated_question.py
/python-hello-world/while_loop_examples.py
i = 0
while i<11:
print(i)
i += 1
# print all the squares of numbers < 100
# 1 4 9 .. 81
def print_squares_of_numbers_below(limit):
i = 1
while i*i < limit :
print(i*i, end=' ')
i += 1
print()
print_squares_of_numbers_below(100)
/[Link]
0
>>> i = 45
>>> if i: print("Something")
...
Something
>>> bool(45)
True
>>> bool(-45)
True
>>> bool(-1)
True
>>> bool(1)
True
>>> bool(0)
False
>>> i = 0
>>> if i: print("Something")
...
>>> bool(0.0)
False
>>> bool(-1.0)
True
>>> str = "Test"
>>> bool(str)
True
>>> bool("")
False
>>> bool('')
False
>>> i = 45
>>> if i%2 == 1: print("odd")
...
odd
>>> i = 44
>>> if i%2 == 1: print("odd")
...
>>> if i%2: print("odd")
...
>>> i = 45
>>> if i%2: print("odd")
...
odd
>>> for ch in 'Hello World':
... print(ch)
...
H
e
l
l
o
W
o
r
l
d
>>> for word in 'Hello World'.split():
... print(word)
...
Hello
World
>>> for item in (3, 8, 9):
... print(item)
...
3
8
9
>>> 1/0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>> i = 0
File "<stdin>", line 1
i = 0
^
IndentationError: unexpected indent
>>> j = 10/i
>>> 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
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>> values = [1,'1']
>>> sum(values)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and
'str'
0
>>> def multiply_by_2(data):
... return data*2
...
>>> multiply_by_2
<function multiply_by_2 at 0x103274f28>
>>> def do_this_and_print(func, data):
... print(func(data))
...
>>> do_this_and_print(multiply_by_2, 125)
250
>>> func_example_reference = multiply_by_2
>>> func_example_reference(23)
46
>>> def multiply_by_3(data):
... return data * 3
...
>>> do_this_and_print(multiply_by_3, 125)
375
>>> do_this_and_print(lambda data:data*3, 125)
375
>>> do_this_and_print(lambda data:data*5, 125)
625
>>> do_this_and_print(lambda data:data*data, 125)
15625
>>> do_this_and_print(lambda data:data*data*data, 125)
1953125
>>> do_this_and_print(lambda data:data ** 3, 125)
1953125
>>> do_this_and_print(lambda data:len(data), 'Test')
4
>>> numbers = [1,89,54,35]
>>> filter( lambda x : x%2==1 ,numbers)
<filter object at 0x103290278>
>>> list(filter( lambda x : x%2==1 ,numbers))
[1, 89, 35]
>>> list(filter( lambda x : x%2==0 ,numbers))
[54]
>>> list(filter( lambda x : x%2 ,numbers))
[1, 89, 35]
>>> words = ["Apple", "Ant", "Bat"]
>>> list(filter(lambda x: [Link]('at'), words))
['Bat']
>>> list( filter( lambda x: [Link]('at') , words ) )
['Bat']
>>> list( filter( lambda x: len(x)==3 , words ) )
['Ant', 'Bat']
>>> list( filter( lambda x: [Link]('A') , words ) )
['Apple', 'Ant']
>>> [Link]('clear')
0
>>> words = ["Apple", "Ant", "Bat"]
>>> "Apple".upper
<built-in method upper of str object at 0x103279e30>
>>> "Apple".upper()
'APPLE'
>>> map(lambda x: [Link]() ,words)
<map object at 0x103290278>
>>> list(map(lambda x: [Link]() ,words))
['APPLE', 'ANT', 'BAT']
>>> list(map(lambda x: len(x) ,words))
[5, 3, 3]
>>> number = [1, 5, 2 , 9]
>>> numbers = [1, 5, 2 , 9]
>>> list(map(lambda x: x*x ,numbers))
[1, 25, 4, 81]
>>> list(map(lambda x: x*x ,range(1,11)))
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> list(map(lambda x: x ** 3 ,range(1,11)))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
>>> [Link]('clear')
0
>>> numbers = [3, 15,12,10]
>>> sum(numbers)
40
>>> max(numbers)
15
>>> reduce(lambda x,y:x+y ,numbers )
40
>>> reduce(lambda x,y:x*y ,numbers )
5400
>>> reduce(lambda x,y:max(x,y) ,numbers )
15
>>> reduce(lambda x,y:min(x,y) ,numbers )
3
>>> words = ['Apple', 'Ant','Bat']
>>> reduce(lambda x,y: x if len(x)>len(y) else y ,words )
'Apple'
>>> [Link]('clear')
0
>>> numbers = [3, 7, 8, 15, 24, 35, 46]
>>> list(filter(lambda x: x%2==0, numbers))
[8, 24, 46]
>>> list(map( lambda x:x*x ,filter(lambda x: x%2==0,
numbers)) )
[64, 576, 2116]
>>> sum(map( lambda x:x*x ,filter(lambda x: x%2==0,
numbers)) )
2756
>>> reduce(lambda x,y:x+y, map( lambda x:x*x
,filter(lambda x: x%2==0, numbers)) )
2756
>>> [Link]('clear')
0
>>> (1,1) == (1,1)
True
>>> ('1',1) == ('1',1)
True
>>> ('1',1) == ('1',2)
False
>>> ('1',1) == ('2',1)
False
>>> (1,1) > (1,1)
False
>>> (1,1) > (0,1)
True
>>> (1,2) > (1,1)
True
>>> (1,2) > (1,3)
False
>>> (1,2) < (1,3)
True
>>> [Link]('clear')
0
>>> str(True)
'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'
>>> [Link]('clear')
0
>>> message = "Hello"
>>> [Link]()
'HELLO'
>>> message
'Hello'
>>> message = [Link]()
>>> message
'HELLO'
>>> message = "Hello"
>>> [Link]()
'HELLO'
>>> message_upper = [Link]()
>>> message = "ABC"
>>> message = [Link]()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'lowercase'
>>> message = [Link]()
>>> [Link]('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
>>> [Link]('clear')
0
>>> import string
>>> string.
[Link]( string.ascii_uppercase
[Link]
[Link]( [Link](
[Link]
string.ascii_letters [Link]
[Link]
string.ascii_lowercase [Link]
[Link]
>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> [Link]
'0123456789'
>>> [Link]
'0123456789abcdefABCDEF'
>>> [Link]
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
>>> 'a' in string.ascii_letters
True
>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> 'ab' in string.ascii_letters
True
>>> 'abc' in string.ascii_letters
True
>>> 'a' in string.ascii_letters
True
>>> '1' in '13579'
True
>>> '2' in '13579'
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'
>>> [Link]() in vowel_string
False
>>> char = 'a'
>>> [Link]() in vowel_string
True
>>> vowel_string = 'aeiou'
>>> [Link]() in vowel_string
True
>>> char = 'A'
>>> [Link]() in vowel_string
True
>>> import string
>>> string.
[Link]( string.ascii_uppercase
[Link]
[Link]( [Link](
[Link]
string.ascii_letters [Link]
[Link]
string.ascii_lowercase [Link]
[Link]
>>> 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
>>> for char in string.ascii_lowercase:
... 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
>>> for char in string.
[Link]( string.ascii_uppercase
[Link]
[Link]( [Link](
[Link]
string.ascii_letters [Link]
[Link]
string.ascii_lowercase [Link]
[Link]
>>> for char in [Link]:
... print(char)
...
0
1
2
3
4
5
6
7
8
9
>>> vowel_string = 'aeiou'
>>> [Link]() 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
>>> [Link]() and [Link]() not in vowel_string
True
>>> char
'b'
>>> char = '1'
>>> [Link]() and [Link]() not in vowel_string
False
>>> [Link]('clear')
0
>>> string_example = "This is a great thing"
>>> string_example.
string_example.capitalize( string_example.join(
string_example.casefold( string_example.ljust(
string_example.center( string_example.lower(
string_example.count( string_example.lstrip(
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
>>> string_example = "This\nis\na\ngreat\nthing"
>>> print(string_example)
This
is
a
great
thing
>>> string_example.split
string_example.split( string_example.splitlines(
>>> string_example.splitlines()
['This', 'is', 'a', 'great', 'thing']
>>> 1 + 2
3
>>> "1" + "2"
'12'
>>> "1" + 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be str, not int
>>> "ABC" + "DEF"
'ABCDEF'
>>> 1 * 20
20
>>> '1' * 20
'11111111111111111111'
>>> 'A' * 10
'AAAAAAAAAA'
>>> str = "test"
>>> str2 = "test1"
>>> str == str2
False
>>> str2 = "test"
>>> str == str2
True
>>>
0
>>> marks = [23,56,67]
>>> sum(marks)
146
>>> max(marks)
67
>>> min(marks)
23
>>> len(marks)
3
>>> [Link](76)
>>> marks
[23, 56, 67, 76]
>>> [Link](2, 60)
>>> marks
[23, 56, 60, 67, 76]
>>> [Link](60)
>>> 55 in marks
False
>>> 56 in marks
True
>>> [Link](67)
2
>>> marks
[23, 56, 67, 76]
>>> [Link](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
>>> [Link]('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'
>>> [Link]('Fish')
>>> animals
['Cat', 'Dog', 'Elephant', 'Fish']
>>> [Link]('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']
>>> [Link]('Fish')
>>> animals
['Cat', 'Elephant', 'F', 'i', 's', 'h']
>>> [Link]('Fish')
>>> animals
['Cat', 'Elephant', 'F', 'i', 's', 'h', 'Fish']
>>> [Link](['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']
>>> [Link](10)
>>> animals
['Cat', 'Elephant', 'F', 'i', 's', 'h', 'Fish', 'Giraffe',
'Horse', 'Jackal', 'Kangaroo', 'Lion', 'Monkey', 10]
>>> [Link]('clear')
0
>>> numbers =
['Zero','One','Two','Three','Four','Five','Six','Seven','Ei
ght','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[Link]
['One', 'Three', 'Five', 'Seven']
>>> numbers[Link]
['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','Ei
ght','Nine']
>>> del numbers[5:7]
>>> numbers =
['Zero','One','Two','Three','Four','Five','Six','Seven','Ei
ght','Nine']
>>> numbers[3:7] = [3,4,5,6]
>>> numbers
['Zero', 'One', 'Two', 3, 4, 5, 6, 'Seven', 'Eight',
'Nine']
>>> [Link]('clear')
0
>>> numbers =
['Zero','One','Two','Three','Four','Five','Six','Seven','Ei
ght','Nine']
>>> [Link]()
>>> 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']
>>> [Link]()
>>> numbers
['Eight', 'Five', 'Four', 'Nine', 'One', 'Seven', 'Six',
'Three', 'Two', 'Zero']
>>> numbers =
['Zero','One','Two','Three','Four','Five','Six','Seven','Ei
ght','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
>>> [Link](key=len)
>>> numbers
['One', 'Two', 'Six', 'Zero', 'Four', 'Five', 'Nine',
'Three', 'Seven', 'Eight']
>>> [Link](key=len, reverse=True)
>>> numbers
['Three', 'Seven', 'Eight', 'Zero', 'Four', 'Five', 'Nine',
'One', 'Two', 'Six']
>>> [Link]('clear')
0
>>> numbers = []
>>> [Link](1)
>>> [Link](2)
>>> [Link](3)
>>> [Link](4)
>>> [Link]()
4
>>> numbers
[1, 2, 3]
>>> [Link]()
3
>>> numbers
[1, 2]
>>> [Link](10)
>>> [Link]()
10
>>> numbers
[1, 2]
>>> numbers = []
>>> [Link](1)
>>> [Link](2)
>>> [Link](3)
>>> [Link](4)
>>> [Link](0)
1
>>> numbers
[2, 3, 4]
>>> [Link](0)
2
>>> numbers
[3, 4]
>>> [Link](10)
>>> [Link](0)
3
>>> [Link](0)
4
>>> [Link](0)
10
>>> numbers
[]
>>> [Link]('clear')
0
>>> numbers = ['Zero',
'One','Two','Three','Four','Five','Six','Seven',
'Eight','Nine']
>>> numbers_length_four=[]
>>> for number in numbers:
... if len(number)== 4:
... numbers_length_four.append(number)
...
>>> numbers_length_four
['Zero', 'Four', 'Five', 'Nine']
>>> numbers_length_four = [ number for number in numbers ]
>>> numbers_length_four
['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six',
'Seven', 'Eight', 'Nine']
>>> numbers_length_four = [ len(number) for number in
numbers ]
>>> numbers_length_four
[4, 3, 3, 5, 4, 4, 3, 5, 5, 4]
>>> numbers_length_four = [ [Link]() for number in
numbers ]
>>> numbers_length_four
['ZERO', 'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX',
'SEVEN', 'EIGHT', 'NINE']
>>> numbers_length_four = [ number for number in numbers if
len(number)==4 ]
>>> 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]
>>> [Link]('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}
>>> [Link]('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
{'a': 5, 'b': 6, 'c': 8}
>>> type(occurances)
<class 'dict'>
>>> occurances['d'] = 15
>>> occurances
{'a': 5, 'b': 6, 'c': 8, 'd': 15}
>>> occurances['d'] = 10
>>> occurances
{'a': 5, 'b': 6, 'c': 8, 'd': 10}
>>> occurances['d']
10
>>> occurances['e']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'e'
>>> [Link]('d')
10
>>> [Link]('e')
>>> [Link]('e', 10)
10
>>> occurances
{'a': 5, 'b': 6, 'c': 8, 'd': 10}
>>> [Link]()
dict_keys(['a', 'b', 'c', 'd'])
>>> [Link]()
dict_values([5, 6, 8, 10])
>>> [Link]()
dict_items([('a', 5), ('b', 6), ('c', 8), ('d', 10)])
>>> for (key,value) in [Link]():
... 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}
>>> [Link]('clear'
... )
0
>>> str = "This is an awesome occasion. This has never
happened before."
>>> squares_first_ten_numbers = [ i*i for i in range(1,11)
]
>>> type(squares_first_ten_numbers)
<class 'list'>
>>> squares_first_ten_numbers_set =
set(squares_of_first_10_numbers)
>>> squares_first_ten_numbers_set = { i*i for i in
range(1,11)}
>>> type(squares_first_ten_numbers_set)
<class 'set'>
>>> squares_first_ten_numbers_dict = { i:i*i for i in
range(1,11)}
>>> type(squares_first_ten_numbers_dict)
<class 'dict'>
>>> squares_first_ten_numbers_dict
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9:
81, 10: 100}
>>> type([])
<class 'list'>
>>> type({})
<class 'dict'>
>>> type(set())
<class 'set'>
>>> type({1})
<class 'set'>
>>> type({'A':5})
<class 'dict'>
>>> type(())
<class 'tuple'>
>>> type((1,2,3))
<class 'tuple'>
>>>
>>> help([Link])
>>> [Link](5.5)
6
>>> [Link](-5.5)
-5
>>> import os
>>> [Link]('clear')
0
>>> import statistics
>>> statistics.
[Link]( [Link](
[Link]( [Link](
[Link]( statistics.median_grouped(
statistics.bisect_left( statistics.median_high(
statistics.bisect_right( statistics.median_low(
[Link]( [Link](
[Link] [Link]
[Link] [Link](
[Link]( [Link](
statistics.harmonic_mean( [Link](
[Link] [Link](
>>> marks = [1, 6, 9, 23, 2]
>>> [Link](marks)
8.2
>>> [Link](marks)
6
>>> marks = [1, 6, 9, 23, 2, 7]
>>> [Link](marks)
6.5
>>> statistics.median_high(marks)
7
>>> statistics.median_low(marks)
6
>>> [Link](marks)
63.2
>>> [Link]('clear')
0
>>> from collections import deque
>>> queue = deque(['Zero','One','Two'])
>>> [Link]()
'Two'
>>> [Link]('Three')
>>> queue
deque(['Zero', 'One', 'Three'])
>>> [Link]('Four')
>>> [Link]('Five')
>>> [Link]('Minus One')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: '[Link]' object has no attribute
'appendLeft'
>>> [Link]
[Link]( [Link](
>>> [Link]('Minus One')
>>> queue
deque(['Minus One', 'Zero', 'One', 'Three', 'Four',
'Five'])
>>> [Link]()
'Five'
>>> [Link]()
'Minus One'
>>> [Link]('clear')
0
>>> import datetime
>>> [Link]()
[Link](2018, 5, 21, 9, 59, 57, 450683)
>>> today_date = [Link]()
>>> today_date
[Link](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 = [Link](2019, 5, 27)
>>> some_date
[Link](2019, 5, 27, 0, 0)
>>> some_date = [Link](2019, 5, 27, 9, 5,25)
>>> some_date
[Link](2019, 5, 27, 9, 5, 25)
>>> some_date = [Link](2019, 5, 27, 9, 5,25,
234567)
>>> some_date
[Link](2019, 5, 27, 9, 5, 25, 234567)
>>> some_date.date()
[Link](2019, 5, 27)
>>> some_date.time()
[Link](9, 5, 25, 234567)
>>> some_date
[Link](2019, 5, 27, 9, 5, 25, 234567)
>>> day = some_date
>>> day
[Link](2019, 5, 27, 9, 5, 25, 234567)
>>> day + [Link](day=90)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'time' is not defined
>>> day + [Link](day=90)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'day' is an invalid keyword argument for this
function
>>> day + [Link](days=90)
[Link](2019, 8, 25, 9, 5, 25, 234567)
>>> day
[Link](2019, 5, 27, 9, 5, 25, 234567)
>>> day + [Link](days=90)
[Link](2019, 8, 25, 9, 5, 25, 234567)
>>> day + [Link](weeks=3)
[Link](2019, 6, 17, 9, 5, 25, 234567)
>>> day + [Link](hours=48)
[Link](2019, 5, 29, 9, 5, 25, 234567)
>>> [Link]('clear')
0
>>> import math
>>> math.
[Link]( [Link]( [Link] [Link]
[Link]( [Link]( [Link](
[Link](
[Link]( [Link]( [Link](
[Link](
[Link]( math.expm1( [Link](
[Link](
[Link]( [Link]( [Link](
[Link](
math.atan2( [Link]( [Link](
[Link](
[Link]( [Link]( [Link](
[Link](
[Link]( [Link]( [Link](
[Link](
[Link]( [Link]( math.log10( [Link]
[Link]( [Link]( math.log1p(
[Link](
[Link]( [Link]( math.log2(
[Link]( [Link]( [Link](
math.e [Link]( [Link]
>>> [Link](4.5)
4
>>> help([Link])
>>> help(math)
>>>
>>> from math import *
>>> floor(5)
5
>>> gcd(34,56)
2
>>> from math import gcd
>>> gcd(56,68)
4
>>> [Link]('clear')
0
>>> numbers = [1,4,6,3,4]
>>> for number in numbers:
... print(number)
...
1
4
6
3
4
>>> for index,number in enumerate(numbers):
... print(f'{index} - {number}')
...
0 - 1
1 - 4
2 - 6
3 - 3
4 - 4
>>> values = list('aeiou')
>>> values
['a', 'e', 'i', 'o', 'u']
>>> for index, vowel in enumerate(values):
... printf(f'{index} - {vowel}')
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
NameError: name 'printf' is not defined
>>> for index, vowel in enumerate(values):
... print(f'{index} - {vowel}')
...
0 - a
1 - e
2 - i
3 - o
4 - u
>>> import os
>>> [Link]('clear')
0
>>> number = 5
>>> if(number%2==0):
... isEven = True
... else:
... isEven = False
...
>>> isEven = True if number%2==0 else False
>>> isEven
False
>>> number = 6
>>> isEven = True if number%2==0 else False
>>> isEven
True
>>> isEven = number%2==0
>>> isEven = "Yes" if number%2==0 else "No"
>>> isEven
'Yes'
>>> [Link]('clear')
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"
>>> [Link]()
'VALUE'
>>> [Link]()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'upper'
>>> type(1)
<class 'int'>
>>> type(1.5)
<class 'float'>
>>> type("1.5")
<class 'str'>
>>> type(True)
<class 'bool'>
>>> type(str)
<class 'str'>
>>> str = 1
>>> type(str)
<class 'int'>
>>> str = True
>>> type(str)
<class 'bool'>
>>> str = [1,2]
>>> type(str)
<class 'list'>
>>> [Link]('clear')
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]
1981
>>> ranga[2]
'India'
>>> ranga[1] = 1991
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> person = ('Ranga', 5, 'India')
>>> person = 'Ranga', 5, 'India'
>>> type(person)
<class 'tuple'>
>>> name, age, country = person
>>> name, age = person
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 2)
>>> x = 0
>>> y = 1
>>> x, y = 0, 1
>>> x, y = y, x
>>> x
1
>>> y
0
>>> x = (0)
>>> type(x)
<class 'int'>
>>> x = (0,)
>>> x = 1,
>>> type(x)
<class 'tuple'>
>>> [Link]('clear')
0
>>>
>>> sum
<built-in function sum>
>>> sum([12,34,56])
102
>>> number1 = 10
>>> number2 = 20
>>> sum = number1 + number2
>>> sum
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
>>> [Link]('clear')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>> import os
>>> [Link]('clear')
0
>>> None
>>> type(None)
<class 'NoneType'>
>>> def email(subject, content, to , cc , bcc):
... print(f" {subject}, {content}, {to}, {cc}, "
... )
...
>>> email("subject", "great work", in28minutes@[Link])
Traceback (most recent call
last):
File "<stdin>", line 1, in <module>
NameError: name 'in28minutes' is not defined
>>> email("subject", "great work", "in28minutes@[Link]")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: email() missing 2 required positional arguments:
'cc' and 'bcc'
>>> def email(subject, content, to , cc=None , bcc=None):
... print(f" {subject}, {content}, {to}, {cc}, {bcc}");
...
>>> email("subject", "great work", "in28minutes@[Link]")
subject, great work, in28minutes@[Link], None, None
>>> email("subject", "great work", "in28minutes@[Link]",
None, None)
subject, great work, in28minutes@[Link], None, None
>>> email(None, "great work", "in28minutes@[Link]",
None, None)
None, great work, in28minutes@[Link], None, None
>>> var = "123"
>>> if var is None : print ("do something");
...
>>> var = None
>>> if var is None : print ("do something");
...
do something
>>> [Link]('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
>>> student1 == student3
True
>>> class Student:
... def __init__(self, id):
... [Link] = id
...
>>> student1 = Student(1)
>>> student2 = Student(2)
>>> student3 = Student(1)
>>> student4 = student1
>>> id(student1)
4554812160
>>> id(student4)
4554812160
>>> student1 is student4
True
>>> student1 is student2
False
>>> student1 is student3
False
>>> student1 == student3
False
>>> class Student:
... def __init__(self, id):
... [Link] = id
... def __eq__(self, other):
... return [Link] == [Link]
...
>>> student1 = Student(1)
>>> student2 = Student(2)
>>> student3 = Student(1)
>>> student4 = student1
>>> student4 == student1
True
>>> student2 == student1
False
>>> student3 == student1
True
>>> [Link]('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
>>> [Link]('clear')
0
>>> import this
The Zen of Python, by Tim Peters
>>> 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
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>> values = [1,'1']
File "<stdin>", line 1
values = [1,'1']
^
IndentationError: unexpected indent
>>> sum(values)
File "<stdin>", line 1
sum(values)
^
IndentationError: unexpected indent
>>> values = [1,'1']
>>> sum(values)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and
'str'
>>> import builtins
>>> help(builtins)
>>>
/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)}
""")
/tips/enum_examples.py
class
Currency(Enum):
USD = 1
EUR = 2
INR = 3
print(Currency(1))
print(Currency(1).name)
print(Currency(1).value)
# print([Link])
# print([Link])
/tips/module_1.py
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()
/tips/module_2.py
import module_1
module_1.method_1()
module_1.ClassA().class_method_1()
/tips/switch_alternatives.py
week_days = {
0 : 'Sunday',
1 : 'Monday',
2 : 'Tuesday'
# You can fill rest of the stuff
}
print(week_days.get(7,'Invalid_day'))