A very good indicator that an object is immutable is when all its methods return something
@Cmatrix_PY
@Cmatrix_PY
What is the output?
Anonymous Quiz
15%
[], [2], [3]
37%
[1], [2], [3]
20%
[1], [2], [1, 3]
28%
[1], [2], [2, 3]
Pythonic Dev
#quiz #easy
This is why, in general, mutable objects shouldn't be used as default arguments.
This is the standard practice
@Cmatrix_PY
This is the standard practice
@Cmatrix_PY
π2
Python uses a pass-by-assignment model, and understanding it requires you to realise all objects are characterised by an identity number, their type, and their contents.
@Cmatrix_PY
@Cmatrix_PY
Pass by value, pass by reference, and pass by assignment are all ways in which arguments can be passed to a function or method.
In pass by value, the actual value of an argument is passed to the function. This means that any changes made to the parameter inside the function have no effect on the original argument. In other words, a copy of the value is sent to the function, so any modifications made within the function will not affect the original variable.
In pass by reference, a reference or pointer to the memory location of the argument is passed to the function. This means that any changes made to the parameter inside the function will also affect the original argument. In other words, both variables point to the same memory address, so any modifications made within the function will also be reflected outside the function.
#python
#pass_by_value
#pass_by_reference
@Cmatrix_PY
In pass by value, the actual value of an argument is passed to the function. This means that any changes made to the parameter inside the function have no effect on the original argument. In other words, a copy of the value is sent to the function, so any modifications made within the function will not affect the original variable.
In pass by reference, a reference or pointer to the memory location of the argument is passed to the function. This means that any changes made to the parameter inside the function will also affect the original argument. In other words, both variables point to the same memory address, so any modifications made within the function will also be reflected outside the function.
#python
#pass_by_value
#pass_by_reference
@Cmatrix_PY
π2
In pass by assignment, a copy of the reference to the object is created and passed to the function. This means that any changes made to the parameter inside the function may or may not affect the original object depending on the programming language and the type of object being passed. For example, in Python, all objects are passed by assignment, which means that if you pass a mutable object like a list or dictionary to a function and modify it inside the function, those changes will be reflected outside the function as well. However, if you pass an immutable object like a number or string to a function and modify it inside the function, those changes will not be reflected outside the function because a new object is created when the value is changed.
#python
#pass_by_assignment
@Cmatrix_PY
#python
#pass_by_assignment
@Cmatrix_PY
π₯1
Generally, tuples are more efficient that lists, so, unless you need mutability of the container, prefer using a tuple over a list.
#python
@Cmatrix_PY
#python
@Cmatrix_PY
Constant folding is the process of recognizing and evaluating constant expressions at compile time rather than computing them at runtime.
In this example, we define a function called calculate() that multiplies the constants 10 and 20 together. Since this expression involves only constants, the Python interpreter can simplify it to 200 during compilation. Therefore, at runtime, the function simply returns the constant value 200, rather than performing the multiplication operation again.
This is a simple example, but constant folding can be very useful for optimizing performance in more complex programs. By simplifying expressions involving only constants, the compiler or interpreter can reduce the amount of work that needs to be done at runtime, leading to faster and more efficient code.
#python
@Cmatrix_PY
In this example, we define a function called calculate() that multiplies the constants 10 and 20 together. Since this expression involves only constants, the Python interpreter can simplify it to 200 during compilation. Therefore, at runtime, the function simply returns the constant value 200, rather than performing the multiplication operation again.
This is a simple example, but constant folding can be very useful for optimizing performance in more complex programs. By simplifying expressions involving only constants, the compiler or interpreter can reduce the amount of work that needs to be done at runtime, leading to faster and more efficient code.
#python
@Cmatrix_PY
What is a dynamic array?
A dynamic array is similar to an array, but with the difference that its size can be dynamically modified at runtime. Donβt need to specify how much large an array beforehand. The elements of an array occupy a contiguous block of memory, and once created, its size cannot be changed. A dynamic array can, once the array is filled, allocate a bigger chunk of memory, copy the contents from the original array to this new space, and continue to fill the available slots.
#data_structure
@Cmatrix_PY
A dynamic array is similar to an array, but with the difference that its size can be dynamically modified at runtime. Donβt need to specify how much large an array beforehand. The elements of an array occupy a contiguous block of memory, and once created, its size cannot be changed. A dynamic array can, once the array is filled, allocate a bigger chunk of memory, copy the contents from the original array to this new space, and continue to fill the available slots.
#data_structure
@Cmatrix_PY
π1
Dynamic Array Logic Implementation:
The key is to provide means to grows an array A that stores the elements of a list. We canβt actually grow the array, its capacity is fixed. If an element is appended to a list at a time, when the underlying array is full, we need to perform following steps.
1.Allocate a new array B with larger capacity (A commonly used rule for the new array is to have twice the capacity of the existing array )
2.Set B[i]=A[i], for i=0 to n-1 where n denotes the current no of items.
3.Set A=B that is, we hence forth use B as the array of supporting list.
4.Insert new element in the new array.
#data_structure
@Cmatrix_PY
The key is to provide means to grows an array A that stores the elements of a list. We canβt actually grow the array, its capacity is fixed. If an element is appended to a list at a time, when the underlying array is full, we need to perform following steps.
1.Allocate a new array B with larger capacity (A commonly used rule for the new array is to have twice the capacity of the existing array )
2.Set B[i]=A[i], for i=0 to n-1 where n denotes the current no of items.
3.Set A=B that is, we hence forth use B as the array of supporting list.
4.Insert new element in the new array.
#data_structure
@Cmatrix_PY
π1
Why tuple Load faster than list?
Tuples can load faster than lists when they contain constants (such as integers and strings) because tuples are immutable, meaning their values cannot be changed once they are created. This immutability allows the interpreter to make certain optimizations during the loading process.
When a tuple is created with constant values, the compiler or interpreter can treat it as a single constant value in memory. This means that the entire tuple can be loaded in one step without any intermediate steps or memory allocations. Since tuples are immutable, there is no need for additional memory allocation or resizing because the values cannot be modified later.
On the other hand, lists are mutable, so they need to support dynamic resizing and element addition. When building a list, each element is added individually, which involves multiple steps of memory allocation, resizing, and assignment. The list needs to allocate memory for each new element, potentially resize the underlying data structure, and copy the existing elements to the new memory location.
Because of these differences, tuples can load faster than lists when they contain constant values because the loading process for tuples is more streamlined and doesn't require the same level of memory management and resizing operations performed by lists.
It's important to note that this performance difference between tuples and lists is generally only noticeable in specific scenarios where large collections or intensive operations are involved. For most general use cases, the speed difference between tuples and lists is unlikely to have a significant impact on overall performance.
#data_structure
#python
@Cmatrix_PY
Tuples can load faster than lists when they contain constants (such as integers and strings) because tuples are immutable, meaning their values cannot be changed once they are created. This immutability allows the interpreter to make certain optimizations during the loading process.
When a tuple is created with constant values, the compiler or interpreter can treat it as a single constant value in memory. This means that the entire tuple can be loaded in one step without any intermediate steps or memory allocations. Since tuples are immutable, there is no need for additional memory allocation or resizing because the values cannot be modified later.
On the other hand, lists are mutable, so they need to support dynamic resizing and element addition. When building a list, each element is added individually, which involves multiple steps of memory allocation, resizing, and assignment. The list needs to allocate memory for each new element, potentially resize the underlying data structure, and copy the existing elements to the new memory location.
Because of these differences, tuples can load faster than lists when they contain constant values because the loading process for tuples is more streamlined and doesn't require the same level of memory management and resizing operations performed by lists.
It's important to note that this performance difference between tuples and lists is generally only noticeable in specific scenarios where large collections or intensive operations are involved. For most general use cases, the speed difference between tuples and lists is unlikely to have a significant impact on overall performance.
#data_structure
#python
@Cmatrix_PY
π2π₯1
π’ New Article Alert: Test Driven Development of a Django RESTful API ! ππ¬
π Article Link: https://realpython.com/test-driven-development-of-a-django-restful-api/
In this article, you'll learn about test-driven development (TDD) using Django REST Framework. Discover how to build robust APIs with comprehensive test suites. The article covers setting up your project, writing unit tests for views and serializers and ...
Don't miss out on this opportunity to level up your API testing skills!
Happy coding and testing! π»π§ͺ
#Django
#API
#restframework
@Cmatrix_PY
π Article Link: https://realpython.com/test-driven-development-of-a-django-restful-api/
In this article, you'll learn about test-driven development (TDD) using Django REST Framework. Discover how to build robust APIs with comprehensive test suites. The article covers setting up your project, writing unit tests for views and serializers and ...
Don't miss out on this opportunity to level up your API testing skills!
Happy coding and testing! π»π§ͺ
#Django
#API
#restframework
@Cmatrix_PY
β€3
How can we represent the range of numbers "1, 2, ...,15" without using an ellipsis (β¦) in python?
Anonymous Quiz
43%
0 < n < 16
13%
0 < n <= 15
22%
1 <= n < 16
22%
1 <= n <= 15
π€2π1π1
π Why Python Indexes Start from 0 π
Hey fellow Pythonistas! Today, let's dive into the fascinating world of indexing in Python and explore why it all begins from zero. π
1οΈβ£ Zero-based indexing simplifies calculations:
One of the main reasons Python uses zero-based indexing is to simplify various calculations and operations. When an element is at index
2οΈβ£ Consistency with other programming languages:
Python draws inspiration from several programming languages, including C and Java, which also use zero-based indexing. Adopting the same convention allows for greater consistency when transitioning between different languages. It facilitates code readability and reduces confusion when collaborating with developers familiar with other languages.
3οΈβ£ Array offset and pointer arithmetic:
In many low-level programming languages, arrays are represented as contiguous blocks of memory. Using zero-based indexing aligns with the underlying concept of array offsets and pointer arithmetic. It simplifies the conversion between indices and memory addresses, making it more efficient and intuitive to work with arrays.
4οΈβ£ Mathematical elegance and simplicity:
By starting indexing at zero, Python adheres to a clean and elegant mathematical convention. When using ranges, the length of a range from
5οΈβ£ Avoiding off-by-one errors:
Using zero-based indexing helps in avoiding off-by-one errors, which frequently occur when working with one-based indexing. By starting at zero, developers can directly access the first element using index 0, avoiding any confusion or edge cases that may arise from starting at index 1.
6οΈβ£ Compatibility with slicing notation:
Python's slice notation, such as
These are some of the key reasons why Python embraces zero-based indexing. If you have any additional thoughts or reasons, please feel free to share them! Let's keep the discussion going in the comments below. Happy coding! ππ»
#python
@Pythonic_Dev
Hey fellow Pythonistas! Today, let's dive into the fascinating world of indexing in Python and explore why it all begins from zero. π
1οΈβ£ Zero-based indexing simplifies calculations:
One of the main reasons Python uses zero-based indexing is to simplify various calculations and operations. When an element is at index
n, the number of elements that precede it will indeed be n. This correlation between the index and the number of preceding elements makes it easier to perform computations and manipulate data structures.2οΈβ£ Consistency with other programming languages:
Python draws inspiration from several programming languages, including C and Java, which also use zero-based indexing. Adopting the same convention allows for greater consistency when transitioning between different languages. It facilitates code readability and reduces confusion when collaborating with developers familiar with other languages.
3οΈβ£ Array offset and pointer arithmetic:
In many low-level programming languages, arrays are represented as contiguous blocks of memory. Using zero-based indexing aligns with the underlying concept of array offsets and pointer arithmetic. It simplifies the conversion between indices and memory addresses, making it more efficient and intuitive to work with arrays.
4οΈβ£ Mathematical elegance and simplicity:
By starting indexing at zero, Python adheres to a clean and elegant mathematical convention. When using ranges, the length of a range from
l to u is given by u - l. This simple formula preserves an intuitive relationship between indices and lengths, making it easier to reason about code and manipulate sequences.5οΈβ£ Avoiding off-by-one errors:
Using zero-based indexing helps in avoiding off-by-one errors, which frequently occur when working with one-based indexing. By starting at zero, developers can directly access the first element using index 0, avoiding any confusion or edge cases that may arise from starting at index 1.
6οΈβ£ Compatibility with slicing notation:
Python's slice notation, such as
list[start:end], is widely used for accessing subsequences. By aligning with zero-based indexing, the slices become more intuitive and consistent. For example, list[0:3] retrieves the first three elements of a list, making it clear that the range is inclusive of the start index.These are some of the key reasons why Python embraces zero-based indexing. If you have any additional thoughts or reasons, please feel free to share them! Let's keep the discussion going in the comments below. Happy coding! ππ»
#python
@Pythonic_Dev
β€2π2π1