print("hello")
a = [1,
2,
3]
a
a = [1, # x-axis
2, # y-axis
3 # z-axis
]
a
a = (1, # x-axis
2, # y-axis
3 # z-axis
)
a
a = {1, # first element
2 # second element
}
a
a = {'key1': 'value', #comment
'key2': #comment on the key
'value2' #comment for the value
}
a
def my_func(a, # some comment on a
b, # comment on b
c # comment on c
):
print(a, b, c)
a = 10
b = 20
c = 30
if a > 5 \
and b > 10 \
and c > 20:
print("yes!!")
yes!!
a = 10
b = 20
c = 30
if a > 5 \
and b > 10 \
and c > 20:
print("yes!!")
yes!!
a = '''this is
a multi line string'''
print(a)
this is
a multi line string
a = """some items:\n
1. item 1
2. item 2"""
print(a)
some items:
1. item 1
2. item 2
def my_func():
a = """a multi-line string
that is actually indented in the second line"""
return a
print(my_func())
a multi-line string
that is actually indented in the second line
def my_func():
a = """a multi-line string
that is actually indented in the second line"""
return a
print(my_func())
a multi-line string
that is actually indented in the second line
def my_func():
"""a multi-line string that is actually indented in the second line
a multi-line string that is actually indented in the second line
a multi-line string that is actually indented in the second line
a multi-line string that is actually indented in the second line
a multi-line string that is actually indented in the second line
a multi-line string that is actually indented in the second line
a multi-line string that is actually indented in the second line
a multi-line string that is actually indented in the second line
a multi-line string that is actually indented in the second line
a multi-line string that is actually indented in the second line
"""
return a
# this is
# a multi-line
# string
a = 2
if a < 3:
print("a < 3")
else:
print("a >=3")
a < 3
a = 15
if a < 5:
print("a < 5")
else:
if a < 10:
print("5 <= a < 10")
else:
print("a >=10")
a >=10
a = 15
if a < 5:
print("a < 5")
elif a < 10:
print("5 <= a < 10")
else:
print("a >=10")
a >=10
# X if (condition) else Y
a = 5
res = 'a < 10' if a < 10 else 'a >=10'
print(res)
a < 10
a = 15
res = 'a < 10' if a < 10 else 'a >=10'
print(res)
a >=10
def say_hello():
print("Hello")
def say_world():
print("World")
a = 15
say_hello() if a < 10 else say_world()
World
@check_country
@IP_correct
@authorize
def calculate_your_savings():
pass
Execution error
NameError: name 'check_country' is not defined
s = [1, 2, 3]
len(s)
from math import sqrt
sqrt(4)
import math
math.exp(1)
def func_1():
print("running func1")
func_1
# invoke or call
func_1()
running func1
def func_2(a, b):
return a * b
func_2(3, 2)
func_2(3.3453453453, 9.123121)
func_2("rohan", 5)
func_2("rohan", "shravan")
Execution error
TypeError: can't multiply sequence by non-int of type 'str'
def func_3(a: int, b: int):
return a * b
func_3('a', 5)
my_func = func_3
my_func(3, 4)
def func_4():
# we are going to be doing something here later on
a = 2
# return None
res = func_4()
print(res)
None
def func_5():
100/0
100/0
Execution error
ZeroDivisionError: division by zero
def fn_1():
fn_2()
def fn_2():
print("hello")
fn_1()
hello
def fn_3():
fn_4()
fn_3()
def fn_4():
print("hello")
Execution error
NameError: name 'fn_4' is not defined
def func_6(x):
return x**2
func_6 = lambda x: x**2
def func_7(fn, a, b):
return fn(a, b)
def sum(a, b):
return a + b
func_7(sum, 3, 4)
func_7(lambda x, y: x + y, 3, 4)
func_6 = lambda x: x**2
func_6, func_7
i = 0
while i < 5:
print(i)
i += 1
0
1
2
3
4
i = 5
while i < 5:
print(i)
i += 1
i = 5
while True:
print(i)
if i >=5:
break
5
min_length = 2
name = input('Please enter your name:')
while not(len(name) >= min_length and name.isprintable() and name.isalpha()):
name = input('Please enter your name:')
print(f'Hello {name}')
Hello Rohan
min_length = 2
while True:
name = input('Please enter your name:')
if len(name) >= min_length and name.isprintable() and name.isalpha():
break
print(f'Hello {name}')
Hello Rohan
a = 0
while a < 10:
a += 1
if a%2:
continue
print(a)
2
4
6
8
10
l = [1, 2, 3]
val = 10
found = False
idx = 0
while idx < len(l):
if l[idx] == val:
found = True
break
idx += 1
if not found:
l.append(val)
print(l)
[1, 2, 3, 10]
l = [1, 2, 3, 10]
val = 10
idx = 0
while idx < len(l):
if l[idx] == val:
break
idx += 1
else:
l.append(val)
print("I was called")
print(l)
[1, 2, 3, 10]
a = 10
b = 1
try:
a / b
except ZeroDivisionError:
print('division by 0')
finally:
print("Finally ALWAYS runs!")
Finally ALWAYS runs!
a = 10
b = 0
try:
a / b
except ZeroDivisionError:
print('division by 0')
finally:
print("Finally ALWAYS runs!")
division by 0
Finally ALWAYS runs!
a = 0
b = 2
while a < 3:
print("_________________")
a += 1
b -= 1
try:
res = a / b
except ZeroDivisionError:
print(f'{a}, {b} - division by 0')
res = 0
continue
finally:
print(f'{a}, {b} - I always execute')
print(f'{a} {b} - main loop')
_________________
1, 1 - I always execute
1 1 - main loop
_________________
2, 0 - division by 0
2, 0 - I always execute
_________________
3, -1 - I always execute
3 -1 - main loop
a = 0
b = 2
while a < 3:
print("_________________")
a += 1
b -= 1
try:
res = a / b
except ZeroDivisionError:
print(f'{a}, {b} - division by 0')
res = 0
break
finally:
print(f'{a}, {b} - I always execute')
print(f'{a} {b} - main loop')
_________________
1, 1 - I always execute
1 1 - main loop
_________________
2, 0 - division by 0
2, 0 - I always execute
a = 0
b = 2
while a < 3:
print("_________________")
a += 1
b -= 1
try:
res = a / b
except ZeroDivisionError:
print(f'{a}, {b} - division by 0')
res = 0
break
finally:
print(f'{a}, {b} - I always execute')
print(f'{a} {b} - main loop')
else:
print('No errors were encountered')
_________________
1, 1 - I always execute
1 1 - main loop
_________________
2, 0 - division by 0
2, 0 - I always execute
a = 0
b = 5
while a < 3:
print("_________________")
a += 1
b -= 1
try:
res = a / b
except ZeroDivisionError:
print(f'{a}, {b} - division by 0')
res = 0
break
finally:
print(f'{a}, {b} - I always execute')
print(f'{a} {b} - main loop')
else:
print('No errors were encountered')
_________________
1, 4 - I always execute
1 4 - main loop
_________________
2, 3 - I always execute
2 3 - main loop
_________________
3, 2 - I always execute
3 2 - main loop
No errors were encountered
i = 0
while i < 5:
# some code
print(i)
i += 1
i = None
# for(int i =0; i<5; i++)
0
1
2
3
4
for i in "rohan":
print(i)
r
o
h
a
n
for i in range(5):
print(i)
0
1
2
3
4
for i in [1, 2, 3, 'rohan']:
print(i)
1
2
3
rohan
for x in [(1,2), (2, 3), (3, 4)]:
print(x)
(1, 2)
(2, 3)
(3, 4)
for x, y in [(1,2), (2, 3), (3, 4)]:
print(x, y)
1 2
2 3
3 4
for i in range(5):
if i == 3:
continue
print(i)
0
1
2
4
for i in range(5):
if i == 3:
break
print(i)
0
1
2
for i in range(1, 5):
print(i)
if i % 7 == 0:
print('multiple of 7 found')
break
else:
print('No multiples of 7 found')
1
2
3
4
No multiples of 7 found
for i in range(1, 8):
print(i)
if i % 7 == 0:
print('multiple of 7 found')
break
else:
print('No multiples of 7 found')
1
2
3
4
5
6
7
multiple of 7 found
for i in range(5):
print("_____________")
try:
10 / (i - 3)
except ZeroDivisionError:
print("divided by 0")
continue
finally:
print("always runs")
print(i)
_____________
always runs
0
_____________
always runs
1
_____________
always runs
2
_____________
divided by 0
always runs
_____________
always runs
4
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
r1 = Rectangle(10, 20)
r2 = Rectangle(3, 5)
r1.width
class Rectangle:
def __init__(rohan, width, height):
rohan.width = width
rohan.height = height
r1 = Rectangle(10, 20)
r2 = Rectangle(3, 5)
r1.width