Friday, 12 February 2021

Tuples in Python

 '''import sys

print("Apne 3 frd ka name likho: ")

a=sys.stdin.readlines()

for i in a:

    print(i,end="")


print("**************")


L=[]

T=()

for i in range(5):

    a=int(input("Enter a number : "))

    L.append(a)

    T=T+(a,)

print(L)

print(T)


c="y"

while c=="y" or c=="Y":

    print("Available Choices: ")

    print("1. Temp Convert from F to C")

    print("2. Temp Convert from C to F")

    choice=int(input("Enter Your Choice: "))

    if choice==1:

        fahrenheit = float(input("Enter temperature in F: "))

        

        celsius=(fahrenheit-32)*(5/9)

        print('%.2f fahrenheit is: %0.2f celsius' %(fahrenheit, celsius))

    elif choice==2:

        celsius = float(input("Enter temperature in celsius: "))

        fahrenheit = (celsius * 9/5) + 32

        print('%.2f Celsius is: %0.2f Fahrenheit' %(celsius, fahrenheit))

    else:

        print("Wrong Choice")

    c=input("Do you want to continue(y/n): ")



n=int(input("Enter no of stars you want: "))

k = n - 1

for i in range(1, n+1):

    for s in range(1, k+1):

        print(end=" ")

    k = k - 1

    for j in range(0, i):

        print(j+1, end=" ")

        

    print("")

    



for i in range(5):

    num = 1

    for j in range(5, i, -1):

        print(num, end=" ")

        num = num + 1

    print()


'''


n=int(input("Enter no of stars you want: "))

k = n - 1

m=1

for i in range(1, n+1):

    for s in range(1, k+1):

        print(end=" ")

    k = k - 1

    for j in range(0, m):

        print("*", end="")

        

    print("")

    m=m+2












Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08) [MSC v.1926 32 bit (Intel)] on win32

Type "help", "copyright", "credits" or "license()" for more information.

>>> A=(1,3,5)

>>> A

(1, 3, 5)

>>> L=[5,7,9]

>>> L

[5, 7, 9]

>>> 

============ RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py ===========

Hello

>>> 

============ RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py ===========

Kuch To Likhohello

>>> 

============ RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py ===========

Apne 3 frd ka name likho: Ram

>>> 

============ RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py ===========

Apne 3 frd ka name likho: Ram Raj

>>> 

============ RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py ===========

Traceback (most recent call last):

  File "C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py", line 1, in <module>

    input("Apne 3 frd ka name likho: ",a,b,c)

NameError: name 'a' is not defined

>>> 

============ RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py ===========

Apne 3 frd ka name likho: 

Ram

Raj

Sam

>>> 

============ RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py ===========

Apne 3 frd ka name likho: 

Ram

Raj

Sam

['Ram\n', 'Raj\n', 'Sam\n']

>>> 

============ RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py ===========

Apne 3 frd ka name likho: 

Ram

Raj

Sam

['Ram\n', 'Raj\n', 'Sam\n']

['Ram\n', 'Raj\n', 'Sam\n']

['Ram\n', 'Raj\n', 'Sam\n']

>>> 

============ RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py ===========

Apne 3 frd ka name likho: 

Ram

Raj

Sam

Ram


Raj


Sam


>>> 

============ RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py ===========

Apne 3 frd ka name likho: 

Ram

Raj

Sam

Ram

Raj

Sam

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py

Enter a number : 3

Enter a number : 5

Enter a number : 7

Enter a number : 9

Enter a number : 2

[3, 5, 7, 9, 2]

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py

Enter a number : 1

Traceback (most recent call last):

  File "C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py", line 14, in <module>

    T.append(a)

AttributeError: 'tuple' object has no attribute 'append'

>>> T=(1,2)

>>> T=(1,2)+(3,4)

>>> T

(1, 2, 3, 4)

>>> T=T=5

>>> T

5

>>> T=(1,2,3)

>>> T=T+5

Traceback (most recent call last):

  File "<pyshell#10>", line 1, in <module>

    T=T+5

TypeError: can only concatenate tuple (not "int") to tuple

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py

Enter a number : 3

Enter a number : 5

Enter a number : 7

Enter a number : 9

Enter a number : 2

[3, 5, 7, 9, 2]

(3, 5, 7, 9, 2)

>>> range(4)

range(0, 4)

>>> list(range(4))

[0, 1, 2, 3]

>>> tuple(range(4))

(0, 1, 2, 3)

>>> t1=(1,2,3)

>>> t2=("a","b")

>>> t3=t1+t2

>>> t3

(1, 2, 3, 'a', 'b')

>>> t4=(t1,t2)

>>> t4

((1, 2, 3), ('a', 'b'))

>>> t3[2]

3

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py

Enter a number : 3

Enter a number : 4

Enter a number : 5

Enter a number : 6

Enter a number : 7

[3, 4, 5, 6, 7]

(3, 4, 5, 6, 7)

>>> t=(1,2,3)

>>> t=t+4

Traceback (most recent call last):

  File "<pyshell#22>", line 1, in <module>

    t=t+4

TypeError: can only concatenate tuple (not "int") to tuple

>>> t=t*2

>>> t

(1, 2, 3, 1, 2, 3)

>>> t[2:5]

(3, 1, 2)

>>> t[2:3]

(3,)

>>> t[2]

3

>>> 3 in t

True

>>> 4 in t

False

>>> 3 in (3,4,5,7)

True

>>> len(t)

6

>>> t.count(3)

2

>>> any(t)

True

>>> a=()

>>> any(a)

False

>>> b=(3,)

>>> any(b)

True

>>> a=(4,9,2,7)

>>> min(a)

2

>>> max(a)

9

>>> b=(4,"ram",2,"raj")

>>> b

(4, 'ram', 2, 'raj')

>>> min(b)

Traceback (most recent call last):

  File "<pyshell#43>", line 1, in <module>

    min(b)

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

>>> c=("ram","amit","sumit")

>>> min(c)

'amit'

>>> max(c)

'sumit'

>>> d=[4,9,2,7]

>>> min(d)

2

>>> max(d)

9

>>> d=[4,"ram"]

>>> min(d)

Traceback (most recent call last):

  File "<pyshell#51>", line 1, in <module>

    min(d)

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

>>> max(d)

Traceback (most recent call last):

  File "<pyshell#52>", line 1, in <module>

    max(d)

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

>>> e=(1,3.5)

>>> min(e)

1

>>> max(e)

3.5

>>> a=(4,9,2,7)

>>> sum(a)

22

>>> sorted(a)

[2, 4, 7, 9]

>>> a

(4, 9, 2, 7)

>>> a=(4,9,2,7,4,5,4,1)

>>> a

(4, 9, 2, 7, 4, 5, 4, 1)

>>> a.count(7)

1

>>> a.count(4)

3

>>> a.index(4)

0

>>> a.index(4,1)

4

>>> a.index(4,1,3)

Traceback (most recent call last):

  File "<pyshell#66>", line 1, in <module>

    a.index(4,1,3)

ValueError: tuple.index(x): x not in tuple

>>> a.index(4,1,4)

Traceback (most recent call last):

  File "<pyshell#67>", line 1, in <module>

    a.index(4,1,4)

ValueError: tuple.index(x): x not in tuple

>>> a.index(4,1,5)

4

>>> a

(4, 9, 2, 7, 4, 5, 4, 1)

>>> 9 in a

True

>>> b=(1,2,3)

>>> c=(1,2,3)

>>> d=(3,4,5)

>>> b==c

True

>>> b==d

False

>>> b>d

False

>>> d>b

True

>>> e=(0,4,5)

>>> e>b

False

>>> e

(0, 4, 5)

>>> del e

>>> e

Traceback (most recent call last):

  File "<pyshell#82>", line 1, in <module>

    e

NameError: name 'e' is not defined

>>> p=[1,2,3]

>>> del p

>>> p

Traceback (most recent call last):

  File "<pyshell#85>", line 1, in <module>

    p

NameError: name 'p' is not defined

>>> e=4,6,8

>>> a=4

>>> a

4

>>> del a

>>> a

Traceback (most recent call last):

  File "<pyshell#90>", line 1, in <module>

    a

NameError: name 'a' is not defined

>>> e=4,5,6

>>> e

(4, 5, 6)

>>> a,b,c=e

>>> a

4

>>> b

5

>>> c

6

>>> e

(4, 5, 6)

>>> a,b=e

Traceback (most recent call last):

  File "<pyshell#98>", line 1, in <module>

    a,b=e

ValueError: too many values to unpack (expected 2)

>>> a,b,c,d=e

Traceback (most recent call last):

  File "<pyshell#99>", line 1, in <module>

    a,b,c,d=e

ValueError: not enough values to unpack (expected 4, got 3)

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py

Enter temperature in celsius: 37

37.00 Celsius is: 98.60 Fahrenheit

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py

Available Choices: 

1. Temp Convert from F to C

2. Temp Convert from C to F

Enter Your Choice

Traceback (most recent call last):

  File "C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py", line 21, in <module>

    choice=int(input("Enter Your Choice"))

KeyboardInterrupt

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py

Available Choices: 

1. Temp Convert from F to C

2. Temp Convert from C to F

Enter Your Choice: 1

Enter temperature in F: 98.6

98.60 fahrenheit is: 37.00 celsius

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py

Available Choices: 

1. Temp Convert from F to C

2. Temp Convert from C to F

Enter Your Choice: 2

Enter temperature in celsius: 37

37.00 Celsius is: 98.60 Fahrenheit

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py

Available Choices: 

1. Temp Convert from F to C

2. Temp Convert from C to F

Enter Your Choice: 1

Enter temperature in F: 98.6

98.60 fahrenheit is: 37.00 celsius

Do you want to continue(y/n)y

Available Choices: 

1. Temp Convert from F to C

2. Temp Convert from C to F

Enter Your Choice: 1

Enter temperature in F: 100

100.00 fahrenheit is: 37.78 celsius

Do you want to continue(y/n)y

Available Choices: 

1. Temp Convert from F to C

2. Temp Convert from C to F

Enter Your Choice: 2

Enter temperature in celsius: 37

37.00 Celsius is: 98.60 Fahrenheit

Do you want to continue(y/n)y

Available Choices: 

1. Temp Convert from F to C

2. Temp Convert from C to F

Enter Your Choice: 2

Enter temperature in celsius: 80

80.00 Celsius is: 176.00 Fahrenheit

Do you want to continue(y/n)n

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py

Enter no of stars you want: 5

    


   1 


  1 2 


 1 2 3 


1 2 3 4 


>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py

Enter no of stars you want: 5

    


   1 


  1 2 


 1 2 3 


1 2 3 4 


1 2 3 4 5 


>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py

Enter no of stars you want: 4

   0 


  0 1 


 0 1 2 


0 1 2 3 


>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py

Enter no of stars you want: 4

   1 


  1 2 


 1 2 3 


1 2 3 4 


>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py

Enter no of stars you want: 5

    1 


   1 2 


  1 2 3 


 1 2 3 4 


1 2 3 4 5 


>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py

Enter no of stars you want: 5

    1 


   1 2 


  1 2 3 


 1 2 3 4 


1 2 3 4 5 


>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py

Enter no of stars you want: 5

    1 

   1 2 

  1 2 3 

 1 2 3 4 

1 2 3 4 5 

>>> 

= RESTART: C:/Users/S P SHARMA/AppData/Local/Programs/Python/Python38-32/12feb.py

1 2 3 4 5 

1 2 3 4 

1 2 3 

1 2 

>>> d={}

>>> d

{}

>>> d=dict()

>>> d

{}

>>> d={1:"Ram",2:"Raj"}

>>> d

{1: 'Ram', 2: 'Raj'}

>>> d1={1:1,2:4,3:9,1:3}

>>> d1

{1: 3, 2: 4, 3: 9}

>>> 

>>> 

>>> 



>>> 


>>> 


>>> 


>>> 

>>> 

>>> 



>>> 

>>> 

>>> 

>>> 

No comments:

Post a Comment