Docsity

Tuples and Lists in Python, Summaries of Physics

Toggle sidebar
bg1
James Ryan L. Villapaña
IT1R13
1. Lists and tuples are both sequence data types in
Python, but the main differences are mutability and
syntax. Lists are mutable, meaning their elements can be
changed after creation, whereas tuples are immutable. To
create a tuple from a list, you can use the tuple()
constructor, passing the list as an argument. To create a
list from a tuple, you can use the list() constructor,
passing the tuple as an argument.'
2. In the provided code:
• t.append(4) will raise an AttributeError because tuples
are immutable and do not have an append() method,
• t.remove(0) will raise a ValueError because the element
O does not exist in the tuple.
• t[0]=1 will raise a TypeError because tuples are
immutable and cannot be modified in place.
3. No, the code is incorrect. Python variables cannot start
with a number. So, tl and t2 should be valid variable
names. Additionally, assigning tl = t2 will not copy the
elements of t2 into tl. Instead, it will make tl reference
the same memory Location as t2, essentially making both
variables point to the same tuple object. If you want to
create a new tuple with the same elements as t2, you
should use the tuple() constructor: tl tuple(t2).
4.t=(1,2,3,7,9,0,5)
print(t) # Output: (1, 2, 3, 7, 9,0.5)
print(t[0]) # Output: 1
pf2

Partial preview of the text

Download Tuples and Lists in Python and more Summaries Physics in PDF only on Docsity!

James Ryan L. Villapaña IT1R

  1. Lists and tuples are both sequence data types in Python, but the main differences are mutability and syntax. Lists are mutable, meaning their elements can be changed after creation, whereas tuples are immutable. To create a tuple from a list, you can use the tuple() constructor, passing the list as an argument. To create a list from a tuple, you can use the list() constructor, passing the tuple as an argument.
  2. In the provided code:
  • t.append(4) will raise an AttributeError because tuples are immutable and do not have an append() method,
  • t.remove(0) will raise a ValueError because the element O does not exist in the tuple.
  • t[0]=1 will raise a TypeError because tuples are immutable and cannot be modified in place.
  1. No, the code is incorrect. Python variables cannot start with a number. So, tl and t2 should be valid variable names. Additionally, assigning tl = t2 will not copy the elements of t2 into tl. Instead, it will make tl reference the same memory Location as t2, essentially making both variables point to the same tuple object. If you want to create a new tuple with the same elements as t2, you should use the tuple() constructor: tl tuple(t2). 4.t=(1,2,3,7,9,0,5) print(t) # Output: (1, 2, 3, 7, 9,0.5) print(t[0]) # Output: 1

print(t[1:3]) # Output: (2, 3) print(t[-1]) # Output: 5 print(t[:-1]) # Output: (1, 2, 3, 7, 9, 0) print(t[1-1]) # Output: (2, 3, 7, 9, 0)

  1. t=(1,2,3,7,9,0,5) print(max(t)) # Output: 9 print(min(t)) #Output: 0 print(sum(t)) # Output: 27 print(len(t)) # Output: 7