Colab

x = [3,2,1]

result = x.sort()

print(x)

[1, 2, 3]
print(result)

None
result = sorted(x)

result

[1, 2, 3]
def multiply(a,b):
    result = a*b
    return result

ab = multiply(10,5)

ab

50
multiply(10,5)
multiply(10,5)
multiply(10,5)
multiply(10,5)
x= sorted([3,2,1])

def print_multiply(a,b):
    print("a*b=", a*b)

print_multiply(10,5)
print_multiply(10,5)
print_multiply(10,5)

a*b= 50
a*b= 50
a*b= 50
result = print_multiply(10,5)

a*b= 50
print(result)

None
def question():
    if False:
        return "hello"
    

x = question()

print(x)

None
def question2():
    return "hello"
    return 1

    

x = question2()

x

'hello'
def switch2(x):
    if x>0:
        return True
    else:
        return False
    

switch2(10)

True
switch2(-10)

False
switch2("hello")


    ---------------------------------------------------------------------------

    TypeError                                 Traceback (most recent call last)

    <ipython-input-27-537247db8cc1> in <module>
    ----> 1 switch2("hello")
    

    <ipython-input-24-9a18bb272b09> in switch2(x)
          1 def switch2(x):
    ----> 2     if x>0:
          3         return True
          4     else:
          5         return False


    TypeError: '>' not supported between instances of 'str' and 'int'


switch2(1,2,3)


    ---------------------------------------------------------------------------

    TypeError                                 Traceback (most recent call last)

    <ipython-input-29-c847d27deea5> in <module>
    ----> 1 switch2(1,2,3)
    

    TypeError: switch2() takes 1 positional argument but 3 were given


import sys
def myprint(x, f=sys.stdout):
    print(x, file=f)
    
myprint(10, sys.stderr)
myprint(20)

20
myprint(10)


    ---------------------------------------------------------------------------

    TypeError                                 Traceback (most recent call last)

    <ipython-input-32-aba4d1247425> in <module>
    ----> 1 myprint(10)
    

    TypeError: myprint() missing 1 required positional argument: 'f'


x=10
x="hello"

x

'hello'
def stam():
    print(10)

def stam(x):
    print(x)

stam

<function __main__.stam()>
stam(10)

10
stam

<function __main__.stam(x)>
type(stam)

function
stam = 10

stam

10
def stam():
    def inner():
        print("hello")
    inner()
    

stam()

hello
def many_format(x):
    return [int(x), str(x), float(x), [x]]

many_format(10)

[10, '10', 10.0, [10]]
sorted([1,2,3])

[1, 2, 3]
sorted( (1,2,3) ) 

[1, 2, 3]
def add2(x):
    return x+2

add2(10)

12
add2(10.0)

12.0
import decimal

decimal.Decimal(2)

Decimal('2')
add2(decimal.Decimal(2))

Decimal('4')
del x

def put_10_in_x():
    x= 10
    print("now x is", x)
    
put_10_in_x()
print("and now ...", x)

now x is 10

    ---------------------------------------------------------------------------

    NameError                                 Traceback (most recent call last)

    <ipython-input-21-ffeb80569fd4> in <module>
          4 
          5 put_10_in_x()
    ----> 6 print("and now ...", x)
    

    NameError: name 'x' is not defined


x=0
def put_10_in_x():
    x= 10
    print("now x is", x)
    
put_10_in_x()
print("and now ...", x)

now x is 10
and now ... 0