0% found this document useful (0 votes)
96 views2 pages

Untitled8 Ipynb

The document contains Python code examples demonstrating: 1) List comprehensions to calculate cumulative sums and print the results. 2) Formatting a string with dictionary values and raising a TypeError. 3) Using map to raise each element of a list to the power of 2 and print the results. 4) Defining a class with an __init__ method and say_hello method to print a greeting. 5) Demonstrating default arguments in a function definition and how the default persists between calls.

Uploaded by

Nitesh Tiwari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views2 pages

Untitled8 Ipynb

The document contains Python code examples demonstrating: 1) List comprehensions to calculate cumulative sums and print the results. 2) Formatting a string with dictionary values and raising a TypeError. 3) Using map to raise each element of a list to the power of 2 and print the results. 4) Defining a class with an __init__ method and say_hello method to print a greeting. 5) Demonstrating default arguments in a function definition and how the default persists between calls.

Uploaded by

Nitesh Tiwari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

05/06/2022, 02:07 Untitled8.

ipynb - Colaboratory

a = [1,2,3,4]
b = [sum(a[0:x+1]) for x in range (0, len(a))]
print(b)

[1, 3, 6, 10]

t = '%(a)s%(b)s %(c)s'
print(t % dict(a='Welcome', b='to', c='Turing'))

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

TypeError Traceback (most recent call last)

<ipython-input-3-472a7fec4f3c> in <module>()

1 t = '%(a)s% (b)s% (c)s'

----> 2 print(t % dict(a='Welcome', b='to', c='Turing'))

TypeError: not enough arguments for format string

SEARCH STACK OVERFLOW

l = [1,2,3,4,5]
m = map (lambda x: 2**x, l)
print(list(m))

[2, 4, 8, 16, 32]

class Welcome:
  def __init__(self, name):
    self.name = name
  def say_hello(self):
    print('Welcome to', self.name)

cw = Welcome ('Turing')
cw.say_hello()

Welcome to Turing

def f(x,l=[]):

  for i in range(x):

    l.append(i*i)

  print(l)

f(2)

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

f(3)

[0, 1]

[3, 2, 1, 0, 1, 4]

[0, 1, 0, 1, 4]

https://colab.research.google.com/drive/1b_L4pbta3svUhHfuRYpogz7KOTQ-8A5x#printMode=true 1/2
05/06/2022, 02:07 Untitled8.ipynb - Colaboratory

y=

https://colab.research.google.com/drive/1b_L4pbta3svUhHfuRYpogz7KOTQ-8A5x#printMode=true 2/2

You might also like