0% found this document useful (0 votes)
49 views

Hidden Gems of Python

The function func takes an input x, calculates an intermediate variable as a quadratic function of x, and returns either the cube of the intermediate variable or the cube plus one depending on whether the intermediate variable is even or odd. The function sets two attributes, one storing the intermediate variable and the other a string, and returns the final value. The code then calls the function, prints the return value and attributes.

Uploaded by

ante mitar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Hidden Gems of Python

The function func takes an input x, calculates an intermediate variable as a quadratic function of x, and returns either the cube of the intermediate variable or the cube plus one depending on whether the intermediate variable is even or odd. The function sets two attributes, one storing the intermediate variable and the other a string, and returns the final value. The code then calls the function, prints the return value and attributes.

Uploaded by

ante mitar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

def func(x):

intermediate_var = x**2 + x + 1

if intermediate_var % 2:
y = intermediate_var ** 3
else:
y = intermediate_var **3 + 1

# setting attributes here


func.optional_return = intermediate_var
func.is_awesome = 'Yes, my function is awesome.'

return y

y = func(3)
print('Final answer is', y)

# Accessing function attributes


print('Show calculations -->', func.optional_return)
print('Is my function awesome? -->', func.is_awesome)

function_attribute.py hosted with by GitHub view raw

Final answer is 2197


Show calculations --> 13
Is my function awesome? --> Yes, my function is awesome.
my_list = ['some', 'list', 'containing', 'five', 'elements']

min_len = 3

for element in my_list:


if len(element) < min_len:
print(f'Caught an element shorter than {min_len} letters')
break
else:
print(f'All elements at least {min_len} letters long')

for_else_demo.py hosted with by GitHub view raw

All elements at least 3 letters long


my_list = ['some', 'list', 'containing', 'five', 'elements']

min_len = 3

no_break = True
for element in my_list:
if len(element) < min_len:
print(f'Caught an element shorter than {min_len} letters')
no_break = False
break

if no_break:
print(f'All elements at least {min_len} letters long')

for_else_alt.py hosted with by GitHub view raw

a = 3250
b = 67_543_423_778

print(type(a))
print(type(b))
print(type(a)==type(b))

int_separators.py hosted with by GitHub view raw

<class 'int'>
<class 'int'>
True
a = 3

b = eval('a + 2')
print('b =', b)

exec('c = a ** 2')
print('c is', c)

eval_exec.py hosted with by GitHub view raw

b = 5
c is 9
This function needs some input
Non integer input provided to nth_odd() function
9 is 5th odd number
16 is not an odd number

[ 0 2 4 6 8 10 12 14]
[ 0 2 4 6 8 10 12 14]

You might also like