Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Tuples and Lists in Python, Summaries of Physics

The differences between tuples and lists in python, a popular programming language. It covers the key distinctions in terms of mutability, syntax, and common operations. Examples to illustrate the behavior of tuples and lists, highlighting the importance of understanding these fundamental data structures in python. It covers topics such as creating tuples from lists, creating lists from tuples, and common operations like appending, removing, and modifying elements. The document also addresses variable naming conventions and the concept of referencing the same object in python. Overall, this document serves as a valuable resource for students and learners seeking to deepen their understanding of tuples and lists, which are essential building blocks in python programming.

Typology: Summaries

2023/2024

Uploaded on 03/04/2024

james-ryan-villapana
james-ryan-villapana 🇵🇭

1 document

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

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