Python Classroom notes 30/Aug/2024

Project Euler Problem 3

  • Refer Here
  • since they have asked for largest prime factor iterating from higher to lower is sensible.
  • Break this problem into
    • largest factor
    • if factor is prime or not
  • Flora
1. Hello flora
2. number = 13195
3. max = number / 2
4. index = max
6. while index >= 2
    6.1 if index is factor and is prime
        6.1.1 print prime
        6.1.2 exit out of this loop (break)
    6.2 index = index - 1

Datatypes contd

  • Lets discuss about
    • lists
    • tuples
    • sets
    • dictionaries

Lists

  • Refer Here for samples
  • list is an ordered collection of any type

for loop in python

for <item> in <sequence>:
   <block>
  • It is called as for each loop in other languages

Mutable and Immutable Types

In Python, data types are categorized into two main types based on their mutability: mutable and immutable. Understanding these concepts is crucial for effective programming as they influence how data is stored, modified, and accessed.

Mutable Types

Mutable types are those whose values can be changed after their creation. This means that the object can be modified without creating a new object. Common examples of mutable types in Python include:

  • Lists: Ordered collections that can be modified by adding, removing, or changing elements. For example, you can append items or change existing ones directly.
  • Dictionaries: Key-value pairs that allow for dynamic updates. You can modify values associated with keys or add new key-value pairs.
  • Sets: Unordered collections of unique elements. You can add or remove elements, but duplicates are not allowed.

Mutable objects are useful when you need to frequently change the contents or size of your data structures. However, they are not thread-safe, meaning that simultaneous modifications from different threads can lead to unexpected behavior[1][2][4].

Immutable Types

Immutable types, on the other hand, cannot be changed once they are created. Any attempt to modify an immutable object results in the creation of a new object. Common examples of immutable types include:

  • Integers: Numeric types that cannot be altered. For example, when you increment an integer, a new integer object is created.
  • Strings: Text data that cannot be modified in place. Any modification results in a new string being created.
  • Tuples: Ordered collections similar to lists, but their contents cannot be changed after creation.

Immutable objects are beneficial for maintaining data integrity and consistency, especially when used as keys in dictionaries or elements in sets. They are generally faster to access than mutable types and are considered thread-safe[1][2][3][4].

Summary

To summarize:

  • Mutable Types: Lists, Dictionaries, Sets. They can be changed after creation and are useful for dynamic data.
  • Immutable Types: Integers, Strings, Tuples. They cannot be modified once created, ensuring data integrity and consistency.

Understanding these distinctions helps in writing more efficient and reliable Python code, as it dictates how data can be manipulated and stored in memory[1][2][4][5].

Citations:
[1] https://ioflood.com/blog/mutable-vs-immutable-in-python-object-data-types-explained/
[2] https://www.javatpoint.com/python-mutable-vs-immutable-data-types
[3] https://www.scaler.com/topics/mutable-data-types-in-python/
[4] https://www.linkedin.com/pulse/understanding-mutable-immutable-data-types-python-rasmi-ranjan-swain
[5] https://imarticus.org/blog/mutable-and-immutable-data-types-in-python/
[6] https://realpython.com/python-mutable-vs-immutable-types/
[7] https://www.geeksforgeeks.org/mutable-vs-immutable-objects-in-python/

Tuples

Sets and Frozensets

Published
Categorized as Uncategorized Tagged

By continuous learner

devops & cloud enthusiastic learner

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Animated Social Media Icons by Acurax Responsive Web Designing Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube